| 130 | } |
| 131 | |
| 132 | async fn chat_stream(&self, request: ChatRequest) -> Result<StreamResult, ProviderError> { |
| 133 | let mut body = serde_json::json!({ |
| 134 | "model": request.model, |
| 135 | "messages": request.messages, |
| 136 | "stream": true, |
| 137 | }); |
| 138 | |
| 139 | if let Some(temp) = request.temperature { |
| 140 | body["temperature"] = serde_json::json!(temp); |
| 141 | } |
| 142 | if let Some(max_tokens) = request.max_tokens { |
| 143 | body["max_tokens"] = serde_json::json!(max_tokens); |
| 144 | } |
| 145 | if let Some(tools) = &request.tools { |
| 146 | if !tools.is_empty() { |
| 147 | body["tools"] = serde_json::json!(tools); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | let response = self |
| 152 | .client |
| 153 | .post(DEEPINFRA_API_URL) |
| 154 | .header("Authorization", format!("Bearer {}", self.api_key)) |
| 155 | .header("Content-Type", "application/json") |
| 156 | .json(&body) |
| 157 | .send() |
| 158 | .await |
| 159 | .map_err(|e| ProviderError::NetworkError(e.to_string()))?; |
| 160 | |
| 161 | if !response.status().is_success() { |
| 162 | let error_text = response.text().await.unwrap_or_default(); |
| 163 | return Err(ProviderError::ApiError(error_text)); |
| 164 | } |
| 165 | |
| 166 | let stream = response |
| 167 | .bytes_stream() |
| 168 | .then(|result| async move { |
| 169 | match result { |
| 170 | Ok(bytes) => { |
| 171 | let text = String::from_utf8_lossy(&bytes); |
| 172 | let mut events: Vec<Result<StreamEvent, ProviderError>> = Vec::new(); |
| 173 | |
| 174 | for line in text.lines() { |
| 175 | if let Some(data) = line.strip_prefix("data: ") { |
| 176 | if data == "[DONE]" { |
| 177 | events.push(Ok(StreamEvent::Done)); |
| 178 | continue; |
| 179 | } |
| 180 | |
| 181 | if let Some(event) = crate::stream::parse_openai_sse(data) { |
| 182 | events.push(Ok(event)); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | events |
| 188 | } |
| 189 | Err(e) => vec![Err(ProviderError::StreamError(e.to_string()))], |