mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-17 22:55:10 +08:00
93 lines
2.9 KiB
Go
93 lines
2.9 KiB
Go
package openai
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestBuildOpenAIFinalPrompt_HandlerPathIncludesToolRoundtripSemantics(t *testing.T) {
|
|
messages := []any{
|
|
map[string]any{"role": "user", "content": "查北京天气"},
|
|
map[string]any{
|
|
"role": "assistant",
|
|
"tool_calls": []any{
|
|
map[string]any{
|
|
"id": "call_1",
|
|
"function": map[string]any{
|
|
"name": "get_weather",
|
|
"arguments": "{\"city\":\"beijing\"}",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
map[string]any{
|
|
"role": "tool",
|
|
"tool_call_id": "call_1",
|
|
"name": "get_weather",
|
|
"content": map[string]any{"temp": 18, "condition": "sunny"},
|
|
},
|
|
}
|
|
tools := []any{
|
|
map[string]any{
|
|
"type": "function",
|
|
"function": map[string]any{
|
|
"name": "get_weather",
|
|
"description": "Get weather",
|
|
"parameters": map[string]any{
|
|
"type": "object",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
finalPrompt, toolNames := buildOpenAIFinalPrompt(messages, tools, "")
|
|
if len(toolNames) != 1 || toolNames[0] != "get_weather" {
|
|
t.Fatalf("unexpected tool names: %#v", toolNames)
|
|
}
|
|
if !strings.Contains(finalPrompt, `"condition":"sunny"`) {
|
|
t.Fatalf("handler finalPrompt should preserve tool output content: %q", finalPrompt)
|
|
}
|
|
if !strings.Contains(finalPrompt, "<tool_calls>") {
|
|
t.Fatalf("handler finalPrompt should preserve assistant tool history: %q", finalPrompt)
|
|
}
|
|
if !strings.Contains(finalPrompt, "<tool_name>get_weather</tool_name>") {
|
|
t.Fatalf("handler finalPrompt should include tool name history: %q", finalPrompt)
|
|
}
|
|
}
|
|
|
|
func TestBuildOpenAIFinalPrompt_VercelPreparePathKeepsFinalAnswerInstruction(t *testing.T) {
|
|
messages := []any{
|
|
map[string]any{"role": "system", "content": "You are helpful"},
|
|
map[string]any{"role": "user", "content": "请调用工具"},
|
|
}
|
|
tools := []any{
|
|
map[string]any{
|
|
"type": "function",
|
|
"function": map[string]any{
|
|
"name": "search",
|
|
"description": "search docs",
|
|
"parameters": map[string]any{
|
|
"type": "object",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
finalPrompt, _ := buildOpenAIFinalPrompt(messages, tools, "")
|
|
if !strings.Contains(finalPrompt, "After receiving a tool result, use it directly.") {
|
|
t.Fatalf("vercel prepare finalPrompt missing final-answer instruction: %q", finalPrompt)
|
|
}
|
|
if !strings.Contains(finalPrompt, "Only call another tool if the result is insufficient.") {
|
|
t.Fatalf("vercel prepare finalPrompt missing retry guard instruction: %q", finalPrompt)
|
|
}
|
|
if !strings.Contains(finalPrompt, "TOOL CALL FORMAT") {
|
|
t.Fatalf("vercel prepare finalPrompt missing xml format instruction: %q", finalPrompt)
|
|
}
|
|
if !strings.Contains(finalPrompt, "Do NOT wrap the XML in markdown code fences") {
|
|
t.Fatalf("vercel prepare finalPrompt missing no-fence xml instruction: %q", finalPrompt)
|
|
}
|
|
if strings.Contains(finalPrompt, "```json") {
|
|
t.Fatalf("vercel prepare finalPrompt should not require fenced tool calls: %q", finalPrompt)
|
|
}
|
|
}
|