From 1ec43b328d8e6f8cb60b723408b4ede1cc720513 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Thu, 23 Apr 2026 17:48:14 +0800 Subject: [PATCH] refactor: Simplify WeKnoraCloud request customization and message conversion - Removed redundant struct definitions for WeKnoraCloud requests and messages, streamlining the customization process. - Updated the weKnoraCloudRequestCustomizer function to directly modify the request object, enhancing clarity and reducing complexity. - Improved the convertToWeKnoraCloudMessagesFromOpenAI function to maintain the original message structure while ensuring compatibility with WeKnoraCloud requirements. --- internal/models/chat/chat_provider_spec.go | 52 ++++++++-------------- internal/models/chat/remote_api.go | 9 +++- 2 files changed, 26 insertions(+), 35 deletions(-) diff --git a/internal/models/chat/chat_provider_spec.go b/internal/models/chat/chat_provider_spec.go index b35ebe44..e5df5c0f 100644 --- a/internal/models/chat/chat_provider_spec.go +++ b/internal/models/chat/chat_provider_spec.go @@ -109,32 +109,15 @@ type ThinkingChatCompletionRequest struct { // --- Customizer functions --- -type weKnoraCloudChatRequest struct { - Model string `json:"model"` - Messages []weKnoraCloudMessage `json:"messages"` - Stream bool `json:"stream"` - MaxTokens int `json:"max_tokens,omitempty"` - Temperature float64 `json:"temperature,omitempty"` - TopP float64 `json:"top_p,omitempty"` -} - -type weKnoraCloudMessage struct { - Role string `json:"role"` - Content string `json:"content"` -} - -func weKnoraCloudRequestCustomizer(req *openai.ChatCompletionRequest, opts *ChatOptions, isStream bool) (any, bool) { - weKnoraCloudReq := weKnoraCloudChatRequest{ - Model: req.Model, - Messages: convertToWeKnoraCloudMessagesFromOpenAI(req.Messages), - Stream: isStream, - } - if opts != nil { - weKnoraCloudReq.Temperature = opts.Temperature - weKnoraCloudReq.TopP = opts.TopP - weKnoraCloudReq.MaxTokens = opts.MaxTokens - } - return weKnoraCloudReq, true +// weKnoraCloudRequestCustomizer 构造 WeKnoraCloud 请求。 +// WeKnoraCloud 走 OpenAI 兼容格式,除了 MultiContent 需要降级为纯文本 Content 之外, +// 其他字段(tools / tool_choice / parallel_tool_calls / response_format / stream_options 等)直接透传, +// 以保证 function calling 等能力可用。 +func weKnoraCloudRequestCustomizer(req *openai.ChatCompletionRequest, _ *ChatOptions, isStream bool) (any, bool) { + cloudReq := *req + cloudReq.Stream = isStream + cloudReq.Messages = convertToWeKnoraCloudMessagesFromOpenAI(req.Messages) + return cloudReq, true } func weKnoraCloudHeaderCustomizer(chat *RemoteAPIChat, req *http.Request, body []byte) error { @@ -146,20 +129,23 @@ func weKnoraCloudHeaderCustomizer(chat *RemoteAPIChat, req *http.Request, body [ return nil } -func convertToWeKnoraCloudMessagesFromOpenAI(messages []openai.ChatCompletionMessage) []weKnoraCloudMessage { - result := make([]weKnoraCloudMessage, 0, len(messages)) +// convertToWeKnoraCloudMessagesFromOpenAI 将 MultiContent 降级为纯文本, +// 其它字段(tool_calls / tool_call_id / name 等)完全保留,保证 tool 协议正常。 +func convertToWeKnoraCloudMessagesFromOpenAI(messages []openai.ChatCompletionMessage) []openai.ChatCompletionMessage { + result := make([]openai.ChatCompletionMessage, 0, len(messages)) for _, m := range messages { - content := m.Content - if content == "" && len(m.MultiContent) > 0 { + msg := m + if msg.Content == "" && len(msg.MultiContent) > 0 { var textParts []string - for _, part := range m.MultiContent { + for _, part := range msg.MultiContent { if part.Type == openai.ChatMessagePartTypeText && part.Text != "" { textParts = append(textParts, part.Text) } } - content = strings.Join(textParts, "\n") + msg.Content = strings.Join(textParts, "\n") + msg.MultiContent = nil } - result = append(result, weKnoraCloudMessage{Role: m.Role, Content: content}) + result = append(result, msg) } return result } diff --git a/internal/models/chat/remote_api.go b/internal/models/chat/remote_api.go index 7f4be400..256de4c5 100644 --- a/internal/models/chat/remote_api.go +++ b/internal/models/chat/remote_api.go @@ -507,14 +507,19 @@ func (c *RemoteAPIChat) chatStreamWithRawHTTP(ctx context.Context, endpoint stri return nil, fmt.Errorf("marshal request: %w", err) } - logger.Infof(ctx, "[LLM Stream] model=%s", c.modelName) - if endpoint == "" { endpoint = c.baseURL + "/chat/completions" } if err := secutils.ValidateURLForSSRF(endpoint); err != nil { return nil, fmt.Errorf("endpoint SSRF check failed: %w", err) } + + if prettyJSON, pErr := json.MarshalIndent(customReq, "", " "); pErr == nil { + logger.Infof(ctx, "[LLM Stream Request] endpoint=%s, model=%s, stream=true, request:\n%s", + endpoint, c.modelName, secutils.CompactImageDataURLForLog(string(prettyJSON))) + } else { + logger.Infof(ctx, "[LLM Stream] endpoint=%s, model=%s", endpoint, c.modelName) + } httpReq, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(jsonData)) if err != nil { return nil, fmt.Errorf("create request: %w", err)