fix: 修复流式工具调用解析并更新标签格式

主要变更:
- 将工具调用标签从 {} 改为 <invoke></invoke>,避免与 JSON 括号冲突
- 修复流式请求未解析工具调用的问题,现在返回 OpenAI 格式的 tool_calls
- 从 SSE 响应中正确提取 content 并解析工具调用
- 更新提示词格式以使用新标签
- 更新所有相关测试用例

问题修复:
- 流式请求现在正确返回 OpenAI 格式的 tool_calls
- 标签冲突导致的解析失败问题已解决
- 所有单元测试通过 (20/20)
- API 完全兼容 OpenAI REST API tools 字段行为

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Vertex-AI-Step-Builder
2025-12-31 09:20:08 +00:00
parent 3f9dbb5448
commit 42548108ba
4 changed files with 122 additions and 37 deletions

View File

@@ -47,17 +47,16 @@ def inject_tools_into_prompt(messages: List[ChatMessage], tools: List[Tool]) ->
tool_defs = json.dumps([tool.model_dump() for tool in tools], indent=2)
# Build the format example separately to avoid f-string escaping issues
# We need to show double braces: outer {{ }} are tags, inner { } is JSON
# Build the format example
json_example = '{"name": "search", "arguments": {"query": "example"}}'
full_example = f'{{{json_example}}}'
full_example = f'{TOOL_CALL_START_TAG}{json_example}{TOOL_CALL_END_TAG}'
tool_prompt = f"""
You are a helpful assistant with access to a set of tools.
You can call them by emitting a JSON object inside tool call tags.
IMPORTANT: Use double braces for tool calls - the outer braces are the tags ({TOOL_CALL_START_TAG} and {TOOL_CALL_END_TAG}), the inner braces are the JSON.
Format: {TOOL_CALL_START_TAG}{{\"name\": \"tool_name\", \"arguments\": {{...}}}}{TOOL_CALL_END_TAG}
IMPORTANT: Use the following format for tool calls:
Format: {TOOL_CALL_START_TAG}{{"name": "tool_name", "arguments": {{...}}}}{TOOL_CALL_END_TAG}
Example: {full_example}