|
| 1 | +use std::time::Duration; |
| 2 | + |
| 3 | +use super::request_schemas::{AnthropicPrompt, OpenAiPrompt, PromptFormat}; |
| 4 | +use super::response_schemas::{AnthropicResponse, OllamaResponse, OpenAiResponse}; |
| 5 | +use crate::config::api::{ApiClient, ApiConfig, ApiError}; |
| 6 | +use crate::config::prompt::{Message, Prompt}; |
| 7 | +use crate::utils::handle_api_response; |
| 8 | +use crate::Api; |
| 9 | + |
| 10 | +pub struct ReqwestClient { |
| 11 | + api_config: ApiConfig, |
| 12 | + client: reqwest::blocking::Client, |
| 13 | + prompt: Prompt, |
| 14 | +} |
| 15 | + |
| 16 | +impl ReqwestClient { |
| 17 | + pub fn new(api_config: ApiConfig, prompt: Prompt) -> Self { |
| 18 | + let client = reqwest::blocking::Client::builder() |
| 19 | + .timeout( |
| 20 | + api_config |
| 21 | + .timeout_seconds |
| 22 | + .map(|t| Duration::from_secs(t.into())), |
| 23 | + ) |
| 24 | + .build() |
| 25 | + .expect("Unable to initialize reqwest HTTP client"); |
| 26 | + |
| 27 | + ReqwestClient { |
| 28 | + api_config, |
| 29 | + client, |
| 30 | + prompt, |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl ApiClient for ReqwestClient { |
| 36 | + fn do_request(&self) -> Result<Message, ApiError> { |
| 37 | + let prompt_format = match self.prompt.api { |
| 38 | + Api::Ollama |
| 39 | + | Api::Openai |
| 40 | + | Api::AzureOpenai |
| 41 | + | Api::Mistral |
| 42 | + | Api::Groq |
| 43 | + | Api::Cerebras => PromptFormat::OpenAi(OpenAiPrompt::from(self.prompt.clone())), |
| 44 | + Api::Anthropic => PromptFormat::Anthropic(AnthropicPrompt::from(self.prompt.clone())), |
| 45 | + Api::AWSBedrock => PromptFormat::AWSBedrock(AnthropicPrompt::from(self.prompt.clone())), |
| 46 | + Api::AnotherApiForTests => panic!("This api is not made for actual use."), |
| 47 | + }; |
| 48 | + |
| 49 | + let request = self |
| 50 | + .client |
| 51 | + .post(&self.api_config.url) |
| 52 | + .header("Content-Type", "application/json") |
| 53 | + .json(&prompt_format); |
| 54 | + |
| 55 | + // https://stackoverflow.com/questions/77862683/rust-reqwest-cant-make-a-request |
| 56 | + let request = match self.prompt.api { |
| 57 | + Api::Cerebras => request.header("User-Agent", "CUSTOM_NAME/1.0"), |
| 58 | + _ => request, |
| 59 | + }; |
| 60 | + |
| 61 | + // Add auth if necessary |
| 62 | + let request = match self.prompt.api { |
| 63 | + Api::Openai | Api::Mistral | Api::Groq | Api::Cerebras => request.header( |
| 64 | + "Authorization", |
| 65 | + &format!("Bearer {}", &self.api_config.get_api_key()), |
| 66 | + ), |
| 67 | + Api::AzureOpenai => request.header("api-key", &self.api_config.get_api_key()), |
| 68 | + Api::Anthropic => request |
| 69 | + .header("x-api-key", &self.api_config.get_api_key()) |
| 70 | + .header( |
| 71 | + "anthropic-version", |
| 72 | + self.api_config.version.as_ref().expect( |
| 73 | + "version required for Anthropic, please add version key to your api config", |
| 74 | + ), |
| 75 | + ), |
| 76 | + _ => request, |
| 77 | + }; |
| 78 | + |
| 79 | + let response_text: String = match self.prompt.api { |
| 80 | + Api::Ollama => handle_api_response::<OllamaResponse>( |
| 81 | + request |
| 82 | + .send() |
| 83 | + .map_err(|e| ApiError::new(self.prompt.model.clone(), e.to_string()))?, |
| 84 | + ), |
| 85 | + Api::Openai | Api::AzureOpenai | Api::Mistral | Api::Groq | Api::Cerebras => { |
| 86 | + handle_api_response::<OpenAiResponse>( |
| 87 | + request |
| 88 | + .send() |
| 89 | + .map_err(|e| ApiError::new(self.prompt.model.clone(), e.to_string()))?, |
| 90 | + ) |
| 91 | + } |
| 92 | + Api::Anthropic => handle_api_response::<AnthropicResponse>( |
| 93 | + request |
| 94 | + .send() |
| 95 | + .map_err(|e| ApiError::new(self.prompt.model.clone(), e.to_string()))?, |
| 96 | + ), |
| 97 | + Api::AWSBedrock | Api::AnotherApiForTests => unreachable!(), |
| 98 | + }; |
| 99 | + |
| 100 | + Ok(Message::assistant(&response_text)) |
| 101 | + } |
| 102 | +} |
0 commit comments