| 89 | } |
| 90 | |
| 91 | async fn chat(&self, request: ChatRequest) -> Result<ChatResponse, ProviderError> { |
| 92 | let mut body = serde_json::json!({ |
| 93 | "model": request.model, |
| 94 | "messages": request.messages, |
| 95 | }); |
| 96 | |
| 97 | if let Some(temp) = request.temperature { |
| 98 | body["temperature"] = serde_json::json!(temp); |
| 99 | } |
| 100 | if let Some(max_tokens) = request.max_tokens { |
| 101 | body["max_tokens"] = serde_json::json!(max_tokens); |
| 102 | } |
| 103 | if let Some(tools) = &request.tools { |
| 104 | if !tools.is_empty() { |
| 105 | body["tools"] = serde_json::json!(tools); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | let response = self |
| 110 | .client |
| 111 | .post(DEEPINFRA_API_URL) |
| 112 | .header("Authorization", format!("Bearer {}", self.api_key)) |
| 113 | .header("Content-Type", "application/json") |
| 114 | .json(&body) |
| 115 | .send() |
| 116 | .await |
| 117 | .map_err(|e| ProviderError::NetworkError(e.to_string()))?; |
| 118 | |
| 119 | if !response.status().is_success() { |
| 120 | let error_text = response.text().await.unwrap_or_default(); |
| 121 | return Err(ProviderError::ApiError(error_text)); |
| 122 | } |
| 123 | |
| 124 | let chat_response: ChatResponse = response |
| 125 | .json() |
| 126 | .await |
| 127 | .map_err(|e| ProviderError::ApiError(e.to_string()))?; |
| 128 | |
| 129 | Ok(chat_response) |
| 130 | } |
| 131 | |
| 132 | async fn chat_stream(&self, request: ChatRequest) -> Result<StreamResult, ProviderError> { |
| 133 | let mut body = serde_json::json!({ |