feat(tools): augment system prompt when no native tool calling is detected

This commit is contained in:
Luxferre
2026-09-07 10:23:10 +03:00
parent cfb26602b5
commit b599038881
2 changed files with 163 additions and 13 deletions
+93
View File
@@ -1204,4 +1204,97 @@ func TestToolUseFailedImmediateCallRecovery(t *testing.T) {
}
}
func TestSystemPromptAugmentationWithoutNativeToolCalling(t *testing.T) {
gw := NewGradioGateway("https://generic-space.hf.space", "", 10*time.Second)
tools := []Tool{
{
Type: "function",
Function: map[string]interface{}{
"name": "search_docs",
"description": "Search local documentation",
"parameters": map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"query": map[string]interface{}{"type": "string"},
},
"required": []string{"query"},
},
},
},
}
// Case 1: Space with SystemIndex and space default prompt, client sends no system message
discWithSys := NewDefaultSpaceDiscovery("https://generic-space.hf.space")
discWithSys.TotalInputs = 2
discWithSys.MessageIndex = 0
discWithSys.SystemIndex = 1
discWithSys.DefaultSystemPrompt = "You are a specialized documentation bot."
req1 := ChatCompletionRequest{
Tools: tools,
Messages: []ChatMessage{
{Role: "user", Content: "How to configure SSL?"},
},
}
payload1, err := gw.BuildGradioPayload(discWithSys, req1)
if err != nil {
t.Fatalf("BuildGradioPayload failed: %v", err)
}
sysStr, ok := payload1[1].(string)
if !ok {
t.Fatalf("expected string at SystemIndex 1, got %T", payload1[1])
}
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") {
t.Errorf("expected tool calling instructions and function name in system prompt, got: %s", sysStr)
}
// Case 2: Space with SystemIndex, client sends their own system message
req2 := ChatCompletionRequest{
Tools: tools,
Messages: []ChatMessage{
{Role: "system", Content: "You are an expert developer assistant."},
{Role: "user", Content: "How to configure SSL?"},
},
}
payload2, err := gw.BuildGradioPayload(discWithSys, req2)
if err != nil {
t.Fatalf("BuildGradioPayload failed: %v", err)
}
sysStr2, ok := payload2[1].(string)
if !ok {
t.Fatalf("expected string at SystemIndex 1, got %T", payload2[1])
}
if !strings.Contains(sysStr2, "You are an expert developer assistant.") {
t.Errorf("expected client system message, got: %s", sysStr2)
}
if !strings.Contains(sysStr2, "search_docs") {
t.Errorf("expected search_docs tool instruction, got: %s", sysStr2)
}
// Case 3: Space without SystemIndex, single input textbox
discSingle := NewDefaultSpaceDiscovery("https://single-input.hf.space")
discSingle.TotalInputs = 1
discSingle.MessageIndex = 0
discSingle.SystemIndex = -1
discSingle.HistoryIndex = -1
payload3, err := gw.BuildGradioPayload(discSingle, req1)
if err != nil {
t.Fatalf("BuildGradioPayload failed: %v", err)
}
singleMsg, ok := payload3[0].(string)
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?") {
t.Errorf("expected single message to contain augmented instructions and user query, got: %s", singleMsg)
}
}