feat(chat): add ParallelToolCalls option to ChatOptions and update related functionality
- Introduced a new ParallelToolCalls field in ChatOptions to control parallel execution of tool calls. - Updated the BuildChatCompletionRequest method to handle the new ParallelToolCalls option, ensuring it is correctly propagated in requests. - Enhanced the streamThinkingToEventBus function to utilize the ParallelToolCalls setting. - Added comprehensive tests to validate the behavior of ParallelToolCalls in various scenarios, ensuring correct propagation and default handling.
This commit is contained in:
@@ -95,10 +95,12 @@ func (e *AgentEngine) streamThinkingToEventBus(
|
||||
logger.Debugf(ctx, "[Agent][Thinking] Iteration-%d: temp=%.2f, tools=%d, thinking=%v",
|
||||
iteration+1, e.config.Temperature, len(tools), e.config.Thinking)
|
||||
|
||||
parallelToolCalls := true
|
||||
opts := &chat.ChatOptions{
|
||||
Temperature: e.config.Temperature,
|
||||
Tools: tools,
|
||||
Thinking: e.config.Thinking,
|
||||
Temperature: e.config.Temperature,
|
||||
Tools: tools,
|
||||
Thinking: e.config.Thinking,
|
||||
ParallelToolCalls: ¶llelToolCalls,
|
||||
}
|
||||
|
||||
pendingToolCalls := make(map[string]bool)
|
||||
|
||||
@@ -26,17 +26,18 @@ type FunctionDef struct {
|
||||
|
||||
// ChatOptions 聊天选项
|
||||
type ChatOptions struct {
|
||||
Temperature float64 `json:"temperature"` // 温度参数
|
||||
TopP float64 `json:"top_p"` // Top P 参数
|
||||
Seed int `json:"seed"` // 随机种子
|
||||
MaxTokens int `json:"max_tokens"` // 最大 token 数
|
||||
MaxCompletionTokens int `json:"max_completion_tokens"` // 最大完成 token 数
|
||||
FrequencyPenalty float64 `json:"frequency_penalty"` // 频率惩罚
|
||||
PresencePenalty float64 `json:"presence_penalty"` // 存在惩罚
|
||||
Thinking *bool `json:"thinking"` // 是否启用思考
|
||||
Tools []Tool `json:"tools,omitempty"` // 可用工具列表
|
||||
ToolChoice string `json:"tool_choice,omitempty"` // "auto", "required", "none", or specific tool
|
||||
Format json.RawMessage `json:"format,omitempty"` // 响应格式定义
|
||||
Temperature float64 `json:"temperature"` // 温度参数
|
||||
TopP float64 `json:"top_p"` // Top P 参数
|
||||
Seed int `json:"seed"` // 随机种子
|
||||
MaxTokens int `json:"max_tokens"` // 最大 token 数
|
||||
MaxCompletionTokens int `json:"max_completion_tokens"` // 最大完成 token 数
|
||||
FrequencyPenalty float64 `json:"frequency_penalty"` // 频率惩罚
|
||||
PresencePenalty float64 `json:"presence_penalty"` // 存在惩罚
|
||||
Thinking *bool `json:"thinking"` // 是否启用思考
|
||||
Tools []Tool `json:"tools,omitempty"` // 可用工具列表
|
||||
ToolChoice string `json:"tool_choice,omitempty"` // "auto", "required", "none", or specific tool
|
||||
ParallelToolCalls *bool `json:"parallel_tool_calls,omitempty"` // 是否允许并行工具调用(默认 nil 表示由模型决定)
|
||||
Format json.RawMessage `json:"format,omitempty"` // 响应格式定义
|
||||
}
|
||||
|
||||
// MessageContentPart represents a part of multi-content message
|
||||
|
||||
@@ -208,6 +208,11 @@ func (c *RemoteAPIChat) BuildChatCompletionRequest(messages []Message, opts *Cha
|
||||
}
|
||||
}
|
||||
|
||||
// 处理 ParallelToolCalls
|
||||
if opts.ParallelToolCalls != nil {
|
||||
req.ParallelToolCalls = *opts.ParallelToolCalls
|
||||
}
|
||||
|
||||
// 处理 ToolChoice(标准实现)
|
||||
if opts.ToolChoice != "" {
|
||||
switch opts.ToolChoice {
|
||||
|
||||
@@ -2,6 +2,7 @@ package chat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -11,6 +12,121 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func newTestRemoteChat() *RemoteAPIChat {
|
||||
chat, _ := NewRemoteAPIChat(&ChatConfig{
|
||||
Source: types.ModelSourceRemote,
|
||||
BaseURL: "https://api.example.com/v1",
|
||||
ModelName: "test-model",
|
||||
APIKey: "test-key",
|
||||
ModelID: "test-model",
|
||||
})
|
||||
return chat
|
||||
}
|
||||
|
||||
func TestBuildChatCompletionRequest_ParallelToolCalls(t *testing.T) {
|
||||
chat := newTestRemoteChat()
|
||||
messages := []Message{{Role: "user", Content: "hello"}}
|
||||
|
||||
t.Run("nil ParallelToolCalls leaves default", func(t *testing.T) {
|
||||
opts := &ChatOptions{Temperature: 0.7}
|
||||
req := chat.BuildChatCompletionRequest(messages, opts, false)
|
||||
assert.Nil(t, req.ParallelToolCalls, "should be nil when not set")
|
||||
})
|
||||
|
||||
t.Run("ParallelToolCalls true is propagated", func(t *testing.T) {
|
||||
ptc := true
|
||||
opts := &ChatOptions{
|
||||
Temperature: 0.7,
|
||||
ParallelToolCalls: &ptc,
|
||||
Tools: []Tool{{
|
||||
Type: "function",
|
||||
Function: FunctionDef{
|
||||
Name: "mcp_weather_getforecast",
|
||||
Description: "Get weather",
|
||||
Parameters: json.RawMessage(`{"type":"object"}`),
|
||||
},
|
||||
}},
|
||||
}
|
||||
req := chat.BuildChatCompletionRequest(messages, opts, true)
|
||||
assert.Equal(t, true, req.ParallelToolCalls)
|
||||
assert.Len(t, req.Tools, 1)
|
||||
assert.Equal(t, "mcp_weather_getforecast", req.Tools[0].Function.Name)
|
||||
})
|
||||
|
||||
t.Run("ParallelToolCalls false is propagated", func(t *testing.T) {
|
||||
ptc := false
|
||||
opts := &ChatOptions{
|
||||
Temperature: 0.7,
|
||||
ParallelToolCalls: &ptc,
|
||||
}
|
||||
req := chat.BuildChatCompletionRequest(messages, opts, false)
|
||||
assert.Equal(t, false, req.ParallelToolCalls)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBuildChatCompletionRequest_MCPToolsFormat(t *testing.T) {
|
||||
chat := newTestRemoteChat()
|
||||
messages := []Message{{Role: "user", Content: "查询乙醇的理化性质"}}
|
||||
|
||||
mcpTools := []Tool{
|
||||
{
|
||||
Type: "function",
|
||||
Function: FunctionDef{
|
||||
Name: "mcp_hazardous_chemicals_gethazardouschemicals",
|
||||
Description: "[MCP Service: hazardous_chemicals (external)] Get hazardous chemicals list",
|
||||
Parameters: json.RawMessage(`{"type":"object","properties":{}}`),
|
||||
},
|
||||
},
|
||||
{
|
||||
Type: "function",
|
||||
Function: FunctionDef{
|
||||
Name: "mcp_hazardous_chemicals_gethazardouschemicalbybizid",
|
||||
Description: "[MCP Service: hazardous_chemicals (external)] Get hazardous chemical by biz ID",
|
||||
Parameters: json.RawMessage(`{"type":"object","properties":{"bizId":{"type":"string"}},"required":["bizId"]}`),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
ptc := true
|
||||
opts := &ChatOptions{
|
||||
Temperature: 0.7,
|
||||
Tools: mcpTools,
|
||||
ParallelToolCalls: &ptc,
|
||||
}
|
||||
|
||||
req := chat.BuildChatCompletionRequest(messages, opts, true)
|
||||
|
||||
assert.Len(t, req.Tools, 2)
|
||||
assert.Equal(t, "mcp_hazardous_chemicals_gethazardouschemicals", req.Tools[0].Function.Name)
|
||||
assert.Equal(t, "mcp_hazardous_chemicals_gethazardouschemicalbybizid", req.Tools[1].Function.Name)
|
||||
assert.Equal(t, true, req.ParallelToolCalls)
|
||||
assert.True(t, req.Stream)
|
||||
|
||||
for _, tool := range req.Tools {
|
||||
name := tool.Function.Name
|
||||
assert.NotContains(t, name, "ed606721", "tool name must use service name, not UUID")
|
||||
assert.Regexp(t, `^[a-zA-Z0-9_-]+$`, name, "tool name must match OpenAI pattern")
|
||||
assert.LessOrEqual(t, len(name), 64, "tool name must be <= 64 chars")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildChatCompletionRequest_ToolChoice(t *testing.T) {
|
||||
chat := newTestRemoteChat()
|
||||
messages := []Message{{Role: "user", Content: "test"}}
|
||||
|
||||
t.Run("auto tool choice", func(t *testing.T) {
|
||||
opts := &ChatOptions{ToolChoice: "auto"}
|
||||
req := chat.BuildChatCompletionRequest(messages, opts, false)
|
||||
assert.Equal(t, "auto", req.ToolChoice)
|
||||
})
|
||||
|
||||
t.Run("specific tool choice", func(t *testing.T) {
|
||||
opts := &ChatOptions{ToolChoice: "mcp_svc_tool"}
|
||||
req := chat.BuildChatCompletionRequest(messages, opts, false)
|
||||
assert.NotNil(t, req.ToolChoice)
|
||||
})
|
||||
}
|
||||
|
||||
// TestRemoteAPIChat 综合测试 Remote API Chat 的所有功能
|
||||
func TestRemoteAPIChat(t *testing.T) {
|
||||
// 获取环境变量
|
||||
|
||||
Reference in New Issue
Block a user