Repository navigation
feat(go-models): expand providers (SiliconFlow, Qiniu, Huawei Cloud, Jina) tool call support - #17241
Conversation
📝 WalkthroughWalkthroughThe change adds shared tool-call request and response handling across Aliyun, Huawei Cloud, Jina, Qiniu, and Siliconflow, declares tool support for configured models, adds streaming accumulation and termination handling, and introduces provider integration tests. ChangesTool-calling support
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant ChatClient
participant ModelDriver
participant ProviderAPI
participant ChatConfig
ChatClient->>ModelDriver: submit messages, tools, and tool_choice
ModelDriver->>ProviderAPI: send normalized chat request
ProviderAPI-->>ModelDriver: return content or tool-call response
ModelDriver->>ChatConfig: populate ToolCalls or ToolCallsResult
ModelDriver-->>ChatClient: return ChatResponse or stream completion
Possibly related issues
Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
internal/entity/models/aliyun.go (1)
167-178: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick winReasoning-content check doesn't account for tool-only responses.
toolCallsis now extracted at Line 168, but theThinking-gated block at Lines 175-177 still requiresreasoning_contentunconditionally, erroring out even when the response is a valid tool-call-only message with noreasoning_content.qiniu.go's equivalent path (ChatWithMessages, lines 244-245) was updated to allow this via!ok && len(toolCalls) == 0, but this file wasn't. As a result, any Aliyun model invoked withThinking=truethat returns tool calls withoutreasoning_contentwill incorrectly fail with"invalid content format", defeating the new tool-calling support for that combination.🐛 Proposed fix
var reasonContent string if chatModelConfig != nil && chatModelConfig.Thinking != nil && *chatModelConfig.Thinking { reasonContent, ok = messageMap["reasoning_content"].(string) - if !ok { + if !ok && len(toolCalls) == 0 { return nil, fmt.Errorf("invalid content format") }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/entity/models/aliyun.go` around lines 167 - 178, Update the Thinking-gated reasoning_content validation in the Aliyun response parsing flow to allow missing reasoning_content when toolCalls contains at least one call. Preserve the existing “invalid content format” error when reasoning_content is absent or invalid and no tool calls are present, matching the equivalent ChatWithMessages behavior in qiniu.go.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/entity/models/tool_call_test.go`:
- Around line 69-104: Strengthen the ChatStreamlyWithSender test by decoding and
validating the streaming request in the httptest server, including the
configured tools and tool_choice values. Expand result assertions beyond
arguments to verify the preserved tool-call id, type, function name, and fully
merged arguments. Keep the existing SSE fragments and success-path behavior
intact.
---
Outside diff comments:
In `@internal/entity/models/aliyun.go`:
- Around line 167-178: Update the Thinking-gated reasoning_content validation in
the Aliyun response parsing flow to allow missing reasoning_content when
toolCalls contains at least one call. Preserve the existing “invalid content
format” error when reasoning_content is absent or invalid and no tool calls are
present, matching the equivalent ChatWithMessages behavior in qiniu.go.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: ba4769b1-2bc0-4e44-9950-fe78bd9acb7b
📒 Files selected for processing (14)
conf/models/huaweicloud.jsonconf/models/qiniu.jsonconf/models/siliconflow.jsoninternal/entity/models/aliyun.gointernal/entity/models/base_model.gointernal/entity/models/huaweicloud.gointernal/entity/models/huaweicloud_test.gointernal/entity/models/jina.gointernal/entity/models/jina_test.gointernal/entity/models/qiniu.gointernal/entity/models/qiniu_test.gointernal/entity/models/siliconflow.gointernal/entity/models/siliconflow_test.gointernal/entity/models/tool_call_test.go
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if r.URL.Path != path { | ||
| t.Errorf("path = %q, want %q", r.URL.Path, path) | ||
| } | ||
| w.Header().Set("Content-Type", "text/event-stream") | ||
| _, _ = w.Write([]byte(`data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call-1","type":"function","function":{"name":"retrieval","arguments":"{\"query\":\""}}]}}]} | ||
|
|
||
| data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"ragflow\"}"}}]},"finish_reason":"tool_calls"}]} | ||
|
|
||
| `)) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| apiKey, toolChoice := "test-key", "auto" | ||
| config := &ChatConfig{ | ||
| Tools: []map[string]interface{}{{"type": "function"}}, | ||
| ToolChoice: &toolChoice, | ||
| ToolCallsResult: &[]map[string]interface{}{{"id": "stale"}}, | ||
| } | ||
| err := newDriver(server.URL).ChatStreamlyWithSender( | ||
| modelName, | ||
| []Message{{Role: "user", Content: "find ragflow"}}, | ||
| &APIConfig{ApiKey: &apiKey}, | ||
| config, | ||
| nil, | ||
| func(_, _ *string) error { return nil }, | ||
| ) | ||
| if err != nil { | ||
| t.Fatalf("ChatStreamlyWithSender: %v", err) | ||
| } | ||
| if config.ToolCallsResult == nil || len(*config.ToolCallsResult) != 1 { | ||
| t.Fatalf("tool calls = %#v", config.ToolCallsResult) | ||
| } | ||
| function, _ := (*config.ToolCallsResult)[0]["function"].(map[string]interface{}) | ||
| if function["arguments"] != `{"query":"ragflow"}` { | ||
| t.Errorf("function = %#v", function) |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Assert the complete streaming tool-call contract.
This helper never inspects the streaming request and only checks arguments; a provider may omit tools/tool_choice or lose the call id, type, or function name while every streaming test still passes.
Proposed test strengthening
func testStreamingToolCall(t *testing.T, modelName, path string, newDriver func(string) ModelDriver) {
t.Helper()
+ var requestBody map[string]interface{}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != path {
t.Errorf("path = %q, want %q", r.URL.Path, path)
}
+ if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
+ t.Errorf("decode request: %v", err)
+ }
w.Header().Set("Content-Type", "text/event-stream")
_, _ = w.Write([]byte(`data: ...`))
}))
@@
if err != nil {
t.Fatalf("ChatStreamlyWithSender: %v", err)
}
+ if requestBody["tool_choice"] != toolChoice || requestBody["tools"] == nil {
+ t.Errorf("tool config = %#v", requestBody)
+ }
if config.ToolCallsResult == nil || len(*config.ToolCallsResult) != 1 {
t.Fatalf("tool calls = %#v", config.ToolCallsResult)
}
- function, _ := (*config.ToolCallsResult)[0]["function"].(map[string]interface{})
+ toolCall := (*config.ToolCallsResult)[0]
+ if toolCall["id"] != "call-1" || toolCall["type"] != "function" {
+ t.Errorf("tool call = %#v", toolCall)
+ }
+ function, _ := toolCall["function"].(map[string]interface{})
+ if function["name"] != "retrieval" {
+ t.Errorf("function = %#v", function)
+ }
if function["arguments"] != `{"query":"ragflow"}` {
t.Errorf("function = %#v", function)
}
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| if r.URL.Path != path { | |
| t.Errorf("path = %q, want %q", r.URL.Path, path) | |
| } | |
| w.Header().Set("Content-Type", "text/event-stream") | |
| _, _ = w.Write([]byte(`data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call-1","type":"function","function":{"name":"retrieval","arguments":"{\"query\":\""}}]}}]} | |
| data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"ragflow\"}"}}]},"finish_reason":"tool_calls"}]} | |
| `)) | |
| })) | |
| defer server.Close() | |
| apiKey, toolChoice := "test-key", "auto" | |
| config := &ChatConfig{ | |
| Tools: []map[string]interface{}{{"type": "function"}}, | |
| ToolChoice: &toolChoice, | |
| ToolCallsResult: &[]map[string]interface{}{{"id": "stale"}}, | |
| } | |
| err := newDriver(server.URL).ChatStreamlyWithSender( | |
| modelName, | |
| []Message{{Role: "user", Content: "find ragflow"}}, | |
| &APIConfig{ApiKey: &apiKey}, | |
| config, | |
| nil, | |
| func(_, _ *string) error { return nil }, | |
| ) | |
| if err != nil { | |
| t.Fatalf("ChatStreamlyWithSender: %v", err) | |
| } | |
| if config.ToolCallsResult == nil || len(*config.ToolCallsResult) != 1 { | |
| t.Fatalf("tool calls = %#v", config.ToolCallsResult) | |
| } | |
| function, _ := (*config.ToolCallsResult)[0]["function"].(map[string]interface{}) | |
| if function["arguments"] != `{"query":"ragflow"}` { | |
| t.Errorf("function = %#v", function) | |
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| if r.URL.Path != path { | |
| t.Errorf("path = %q, want %q", r.URL.Path, path) | |
| } | |
| if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil { | |
| t.Errorf("decode request: %v", err) | |
| } | |
| w.Header().Set("Content-Type", "text/event-stream") | |
| _, _ = w.Write([]byte(`data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call-1","type":"function","function":{"name":"retrieval","arguments":"{\"query\":\""}}]}}]} | |
| data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"ragflow\"}"}}]},"finish_reason":"tool_calls"}]} | |
| `)) | |
| })) | |
| defer server.Close() | |
| apiKey, toolChoice := "test-key", "auto" | |
| config := &ChatConfig{ | |
| Tools: []map[string]interface{}{{"type": "function"}}, | |
| ToolChoice: &toolChoice, | |
| ToolCallsResult: &[]map[string]interface{}{{"id": "stale"}}, | |
| } | |
| err := newDriver(server.URL).ChatStreamlyWithSender( | |
| modelName, | |
| []Message{{Role: "user", Content: "find ragflow"}}, | |
| &APIConfig{ApiKey: &apiKey}, | |
| config, | |
| nil, | |
| func(_, _ *string) error { return nil }, | |
| ) | |
| if err != nil { | |
| t.Fatalf("ChatStreamlyWithSender: %v", err) | |
| } | |
| if requestBody["tool_choice"] != toolChoice || requestBody["tools"] == nil { | |
| t.Errorf("tool config = %#v", requestBody) | |
| } | |
| if config.ToolCallsResult == nil || len(*config.ToolCallsResult) != 1 { | |
| t.Fatalf("tool calls = %#v", config.ToolCallsResult) | |
| } | |
| toolCall := (*config.ToolCallsResult)[0] | |
| if toolCall["id"] != "call-1" || toolCall["type"] != "function" { | |
| t.Errorf("tool call = %#v", toolCall) | |
| } | |
| function, _ := toolCall["function"].(map[string]interface{}) | |
| if function["name"] != "retrieval" { | |
| t.Errorf("function = %#v", function) | |
| } | |
| if function["arguments"] != `{"query":"ragflow"}` { | |
| t.Errorf("function = %#v", function) | |
| } |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/entity/models/tool_call_test.go` around lines 69 - 104, Strengthen
the ChatStreamlyWithSender test by decoding and validating the streaming request
in the httptest server, including the configured tools and tool_choice values.
Expand result assertions beyond arguments to verify the preserved tool-call id,
type, function name, and fully merged arguments. Keep the existing SSE fragments
and success-path behavior intact.
Summary
Testing
bash build.sh --test -c -o /tmp/ragflow-models.test ./internal/entity/models#16990