fix(tools): adapt tool prompt framing to ensure reliable tool calls in persona spaces

This commit is contained in:
Luxferre
2026-09-07 10:36:18 +03:00
parent b599038881
commit 4de3ab3646
2 changed files with 89 additions and 33 deletions
+37 -5
View File
@@ -534,7 +534,7 @@ func TestUniversalToolCallingTransformMessages(t *testing.T) {
t.Fatalf("expected 4 processed messages, got %d", len(processed))
}
if processed[0].Role != "system" || !strings.Contains(processed[0].GetContentString(), "Tool Calling Instructions") {
if processed[0].Role != "system" || !strings.Contains(processed[0].GetContentString(), "API router") {
t.Errorf("unexpected message 0: %+v", processed[0])
}
@@ -701,7 +701,7 @@ func TestBuildGradioPayloadGenericSpaces(t *testing.T) {
t.Fatalf("failed to build payload 1: %v", err)
}
sysStr, ok := data1[0].(string)
if !ok || !strings.Contains(sysStr, "Tool Calling Instructions") {
if !ok || !strings.Contains(sysStr, "API router") {
t.Errorf("expected system prompt at index 0, got %v", data1[0])
}
msgStr, ok := data1[1].(string)
@@ -733,7 +733,7 @@ func TestBuildGradioPayloadGenericSpaces(t *testing.T) {
t.Fatalf("expected 1 history pair at index 1, got %T (%v)", data2[1], data2[1])
}
// Instructions prepended to the first user turn:
if !strings.Contains(pairs2[0][0], "Tool Calling Instructions") || !strings.Contains(pairs2[0][0], "What is 10+10?") {
if !strings.Contains(pairs2[0][0], "API router") || !strings.Contains(pairs2[0][0], "What is 10+10?") {
t.Errorf("expected system instructions prepended to first pair user message, got: %q", pairs2[0][0])
}
if !strings.Contains(pairs2[0][1], "lookup") {
@@ -1249,7 +1249,7 @@ func TestSystemPromptAugmentationWithoutNativeToolCalling(t *testing.T) {
if !strings.Contains(sysStr, "You are a specialized documentation bot.") {
t.Errorf("expected default system prompt to be retained, got: %s", sysStr)
}
if !strings.Contains(sysStr, "Tool Calling Instructions") || !strings.Contains(sysStr, "search_docs") {
if !strings.Contains(sysStr, "API router") || !strings.Contains(sysStr, "search_docs") {
t.Errorf("expected tool calling instructions and function name in system prompt, got: %s", sysStr)
}
@@ -1291,10 +1291,42 @@ func TestSystemPromptAugmentationWithoutNativeToolCalling(t *testing.T) {
if !ok {
t.Fatalf("expected string at index 0, got %T", payload3[0])
}
if !strings.Contains(singleMsg, "Tool Calling Instructions") || !strings.Contains(singleMsg, "How to configure SSL?") {
if !strings.Contains(singleMsg, "API router") || !strings.Contains(singleMsg, "How to configure SSL?") {
t.Errorf("expected single message to contain augmented instructions and user query, got: %s", singleMsg)
}
}
func TestToolChoiceHandling(t *testing.T) {
tools := []Tool{
{
Type: "function",
Function: map[string]interface{}{
"name": "calculator",
"description": "Evaluate math expression",
},
},
}
// 1. ToolChoice: "none" -> no tool instruction generated
instrNone := BuildToolInstruction(tools, "none")
if instrNone != "" {
t.Errorf("expected empty instruction for tool_choice: none, got: %s", instrNone)
}
// 2. ToolChoice: "required" -> mandatory directive
instrReq := BuildToolInstruction(tools, "required")
if !strings.Contains(instrReq, "You MUST call one of the available tools") {
t.Errorf("expected required directive in instruction, got: %s", instrReq)
}
// 3. ToolChoice: specific function
instrFn := BuildToolInstruction(tools, map[string]interface{}{
"type": "function",
"function": map[string]interface{}{
"name": "calculator",
},
})
if !strings.Contains(instrFn, "You MUST call the calculator tool") {
t.Errorf("expected specific function directive in instruction, got: %s", instrFn)
}
}