feat: simplify tool format to TOOL:/ARGUMENTS: pattern
Replace complex OpenAI-style JSON format with simple format:
TOOL: tool_name
ARGUMENTS: {param: value}
This matches what the tool server expects and is much easier
for smaller models to generate correctly. Also add parser for
this format with priority over other formats.
This commit is contained in:
+31
-4
@@ -68,13 +68,18 @@ def format_messages_with_tools(messages: list, tools: Optional[list] = None) ->
|
||||
tool_instructions = """You have access to these tools:
|
||||
|
||||
read: Read a file (filePath)
|
||||
write: Write to a file (filePath, content)
|
||||
write: Write to a file (filePath, content)
|
||||
bash: Run a shell command (command)
|
||||
|
||||
When you need to use a tool, respond with ONLY this JSON format:
|
||||
{"tool_calls": [{"id": "call_1", "type": "function", "function": {"name": "TOOL_NAME", "arguments": "{\\"param\\": \\"value\\"}"}}]}
|
||||
When you need to use a tool, respond with ONLY this format:
|
||||
TOOL: tool_name
|
||||
ARGUMENTS: {"param": "value"}
|
||||
|
||||
Do not explain - just output the JSON when using tools."""
|
||||
Example:
|
||||
TOOL: read
|
||||
ARGUMENTS: {"filePath": "hello.txt"}
|
||||
|
||||
Do not explain - just output TOOL: and ARGUMENTS: when using tools."""
|
||||
|
||||
# Add to system message or create one
|
||||
has_system = False
|
||||
@@ -264,6 +269,28 @@ def parse_tool_calls(text: str) -> tuple:
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
# Try new simple format: TOOL: name\nARGUMENTS: {...}
|
||||
tool_pattern = r'TOOL:\s*(\w+)\s*\nARGUMENTS:\s*(\{[^}]*\})'
|
||||
tool_match = re.search(tool_pattern, text, re.IGNORECASE)
|
||||
if tool_match:
|
||||
tool_name = tool_match.group(1)
|
||||
args_str = tool_match.group(2)
|
||||
try:
|
||||
args_dict = json.loads(args_str)
|
||||
tool_calls = [{
|
||||
"id": "call_1",
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": tool_name,
|
||||
"arguments": json.dumps(args_dict)
|
||||
}
|
||||
}]
|
||||
# Extract content before the tool call
|
||||
content = text[:tool_match.start()].strip()
|
||||
return content, tool_calls
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
|
||||
# Try alternative format: look for function call patterns
|
||||
# Pattern: function_name(arg1=value1, arg2=value2)
|
||||
func_pattern = r'(\w+)\s*\(([^)]*)\)'
|
||||
|
||||
Reference in New Issue
Block a user