(ctx context.Context, w http.ResponseWriter, resp openAIResponse)
| 435 | } |
| 436 | |
| 437 | func (s *Server) sendOpenAIStream(ctx context.Context, w http.ResponseWriter, resp openAIResponse) { |
| 438 | w.Header().Set("Content-Type", "text/event-stream") |
| 439 | w.Header().Set("Cache-Control", "no-cache") |
| 440 | w.Header().Set("Connection", "keep-alive") |
| 441 | w.WriteHeader(http.StatusOK) |
| 442 | |
| 443 | flusher, ok := w.(http.Flusher) |
| 444 | if !ok { |
| 445 | s.logger.Error(ctx, "responseWriter does not support flushing", |
| 446 | slog.F("response_id", resp.ID), |
| 447 | ) |
| 448 | return |
| 449 | } |
| 450 | |
| 451 | writeChunk := func(data string) bool { |
| 452 | if _, err := fmt.Fprintf(w, "%s", data); err != nil { |
| 453 | s.logger.Error(ctx, "failed to write OpenAI stream chunk", |
| 454 | slog.F("response_id", resp.ID), |
| 455 | slog.Error(err), |
| 456 | slog.F("error_type", "write_error"), |
| 457 | slog.F("likely_cause", "network_error"), |
| 458 | ) |
| 459 | return false |
| 460 | } |
| 461 | flusher.Flush() |
| 462 | return true |
| 463 | } |
| 464 | |
| 465 | // Send initial chunk |
| 466 | chunk := map[string]interface{}{ |
| 467 | "id": resp.ID, |
| 468 | "object": "chat.completion.chunk", |
| 469 | "created": resp.Created, |
| 470 | "model": resp.Model, |
| 471 | "choices": []map[string]interface{}{ |
| 472 | { |
| 473 | "index": 0, |
| 474 | "delta": map[string]interface{}{ |
| 475 | "role": "assistant", |
| 476 | "content": resp.Choices[0].Message.Content, |
| 477 | }, |
| 478 | "finish_reason": nil, |
| 479 | }, |
| 480 | }, |
| 481 | } |
| 482 | chunkBytes, _ := json.Marshal(chunk) |
| 483 | if !writeChunk(fmt.Sprintf("data: %s\n\n", chunkBytes)) { |
| 484 | return |
| 485 | } |
| 486 | |
| 487 | // Send final chunk |
| 488 | finalChunk := map[string]interface{}{ |
| 489 | "id": resp.ID, |
| 490 | "object": "chat.completion.chunk", |
| 491 | "created": resp.Created, |
| 492 | "model": resp.Model, |
| 493 | "choices": []map[string]interface{}{ |
| 494 | { |
no test coverage detected