( ctx context.Context, url string, headers map[string]string, body []byte, )
| 678 | MaxTokens: opts.MaxTokens, RequiredTool: opts.RequiredTool, DisableThinking: opts.DisableThinking, |
| 679 | Temperature: opts.Temperature, |
| 680 | })) |
| 681 | } |
| 682 | |
| 683 | func (c *OpenAICompatibleClient) StreamChat( |
| 684 | ctx context.Context, |
| 685 | systemPrompt string, |
| 686 | messages []map[string]any, |
| 687 | tools []map[string]any, |
| 688 | options *LLMRequestOptions, |
| 689 | ) <-chan EventResult { |
| 690 | url := c.chatCompletionsURL() |
| 691 | headers := c.buildHeaders() |
| 692 | openaiMessages := c.buildMessages(systemPrompt, messages) |
| 693 | |
| 694 | bodyMap := map[string]any{ |
| 695 | "model": c.Model, |
| 696 | "messages": openaiMessages, |
| 697 | "stream": true, |
| 698 | "stream_options": map[string]any{"include_usage": true}, |
| 699 | } |
| 700 | if options != nil && options.MaxTokens > 0 { |
| 701 | bodyMap["max_completion_tokens"] = options.MaxTokens |
| 702 | } |
| 703 | if options != nil && options.Temperature != nil { |
| 704 | bodyMap["temperature"] = *options.Temperature |
| 705 | } |
| 706 | |
| 707 | if len(tools) > 0 { |
| 708 | bodyMap["tools"] = AnthropicToolsToOpenAI(tools) |
| 709 | } |
| 710 | if options != nil && options.RequiredTool != "" { |
| 711 | bodyMap["tool_choice"] = map[string]any{ |
| 712 | "type": "function", "function": map[string]any{"name": options.RequiredTool}, |
| 713 | } |
| 714 | } |
| 715 | if options != nil && options.DisableThinking && supportsThinkingControl(c.Model) { |
| 716 | bodyMap["thinking"] = map[string]any{"type": "disabled"} |
| 717 | } |
| 718 | |
| 719 | bodyBytes, err := json.Marshal(bodyMap) |
| 720 | if err != nil { |
| 721 | return singleErrorEvent(err) |
| 722 | } |
| 723 | |
| 724 | return RetryWithBackoff(ctx, c.streamOpenAICompatible, func() (RequestParts, error) { |
| 725 | return RequestParts{ |
| 726 | URL: url, |
| 727 | Headers: headers, |
| 728 | Body: bodyBytes, |
| 729 | }, nil |
| 730 | }, c.RetryConfig) |
| 731 | } |
| 732 | |
| 733 | func (c *OpenAICompatibleClient) streamOpenAICompatible( |
| 734 | ctx context.Context, |
| 735 | url string, |
| 736 | headers map[string]string, |
| 737 | body []byte, |
nothing calls this directly
no test coverage detected