| 795 | } |
| 796 | |
| 797 | func (s *openAIServer) writeResponsesAPINonStreaming(w http.ResponseWriter, resp *OpenAICompletion) { |
| 798 | // Convert all choices to output format |
| 799 | outputs := make([]map[string]interface{}, len(resp.Choices)) |
| 800 | for i, choice := range resp.Choices { |
| 801 | outputs[i] = map[string]interface{}{ |
| 802 | "id": uuid.New().String(), |
| 803 | "type": "message", |
| 804 | "role": "assistant", |
| 805 | "content": []map[string]interface{}{ |
| 806 | { |
| 807 | "type": "output_text", |
| 808 | "text": choice.Message.Content, |
| 809 | }, |
| 810 | }, |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | response := map[string]interface{}{ |
| 815 | "id": resp.ID, |
| 816 | "object": "response", |
| 817 | "created": resp.Created, |
| 818 | "model": resp.Model, |
| 819 | "output": outputs, |
| 820 | "usage": map[string]interface{}{ |
| 821 | "input_tokens": resp.Usage.PromptTokens, |
| 822 | "output_tokens": resp.Usage.CompletionTokens, |
| 823 | "total_tokens": resp.Usage.TotalTokens, |
| 824 | }, |
| 825 | } |
| 826 | w.Header().Set("Content-Type", "application/json") |
| 827 | if err := json.NewEncoder(w).Encode(response); err != nil { |
| 828 | s.t.Errorf("writeResponsesAPINonStreaming: failed to encode response: %v", err) |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | // OpenAIStreamingResponse creates a streaming response from chunks. |
| 833 | func OpenAIStreamingResponse(chunks ...OpenAIChunk) OpenAIResponse { |