(w http.ResponseWriter, youReq *http.Request)
| 221 | } |
| 222 | |
| 223 | func handleNonStreamingResponse(w http.ResponseWriter, youReq *http.Request) { |
| 224 | client := &http.Client{ |
| 225 | Timeout: 60 * time.Second, |
| 226 | } |
| 227 | resp, err := client.Do(youReq) |
| 228 | if err != nil { |
| 229 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 230 | return |
| 231 | } |
| 232 | defer resp.Body.Close() |
| 233 | |
| 234 | var fullResponse strings.Builder |
| 235 | scanner := bufio.NewScanner(resp.Body) |
| 236 | |
| 237 | buf := make([]byte, 0, 64*1024) |
| 238 | scanner.Buffer(buf, 1024*1024) |
| 239 | |
| 240 | for scanner.Scan() { |
| 241 | line := scanner.Text() |
| 242 | if strings.HasPrefix(line, "event: youChatToken") { |
| 243 | scanner.Scan() |
| 244 | data := scanner.Text() |
| 245 | if !strings.HasPrefix(data, "data: ") { |
| 246 | continue |
| 247 | } |
| 248 | var token YouChatResponse |
| 249 | if err := json.Unmarshal([]byte(strings.TrimPrefix(data, "data: ")), &token); err != nil { |
| 250 | continue |
| 251 | } |
| 252 | fullResponse.WriteString(token.YouChatToken) |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | if scanner.Err() != nil { |
| 257 | http.Error(w, "Error reading response", http.StatusInternalServerError) |
| 258 | return |
| 259 | } |
| 260 | |
| 261 | openAIResp := OpenAIResponse{ |
| 262 | ID: "chatcmpl-" + fmt.Sprintf("%d", time.Now().Unix()), |
| 263 | Object: "chat.completion", |
| 264 | Created: time.Now().Unix(), |
| 265 | Model: reverseMapModelName(mapModelName(originalModel)), |
| 266 | Choices: []OpenAIChoice{ |
| 267 | { |
| 268 | Message: Message{ |
| 269 | Role: "assistant", |
| 270 | Content: fullResponse.String(), |
| 271 | }, |
| 272 | Index: 0, |
| 273 | FinishReason: "stop", |
| 274 | }, |
| 275 | }, |
| 276 | } |
| 277 | |
| 278 | w.Header().Set("Content-Type", "application/json") |
| 279 | if err := json.NewEncoder(w).Encode(openAIResp); err != nil { |
| 280 | http.Error(w, "Error encoding response", http.StatusInternalServerError) |
no test coverage detected