Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Qwen function calling #39

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions fastmlx/tools/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
"parallel_tool_calling": true,
"tool_role": "tool"
},
"qwen2_5": {
"prompt_template": "qwen2_5.j2",
"parallel_tool_calling": true,
"tool_role": "tool"
},
"default": {
"prompt_template": "llama-3_1.j2",
"parallel_tool_calling": false
Expand Down
23 changes: 23 additions & 0 deletions fastmlx/tools/qwen2_5.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<|im_start|>system
You are Qwen, created by Alibaba Cloud. You are a helpful assistant.

Current Date: {{ current_date }}

# Tools

You may call one or more functions to assist with the user query.

You are provided with function signatures within <tools></tools> XML tags:
<tools>
{% for tool in tools %}
{"type": "function", "function": {{ tool | tojson }}}
{% endfor %}
</tools>

For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
<tool_call>
{"name": <function-name>, "arguments": <args-json-object>}
</tool_call><|im_end|>
<|im_start|>user
{{ prompt }}<|im_end|>
<|im_start|>assistant
23 changes: 23 additions & 0 deletions fastmlx/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,29 @@ def handle_function_calls(
except Exception as e:
print(f"Error parsing functools call: {e}")

# New: Check for Qwen-style <tool_call> function calls
elif "<tool_call>" in output:
try:
# Find all <tool_call>...</tool_call> blocks
qwen_calls = re.findall(r"<tool_call>\s*(\{.*?\})\s*</tool_call>", output, re.DOTALL)
for call_str in qwen_calls:
call_data = json.loads(call_str)
tool_calls.append(
ToolCall(
id=f"call_{os.urandom(4).hex()}",
function=FunctionCall(
name=call_data["name"],
arguments=json.dumps(call_data["arguments"])
),
)
)
# Remove all <tool_call>...</tool_call> blocks from the output
output = re.sub(r"<tool_call>\s*\{.*?\}\s*</tool_call>", "", output, flags=re.DOTALL).strip()
except json.JSONDecodeError as e:
print(f"Error parsing Qwen tool calls: {e}")
except Exception as e:
print(f"Unexpected error parsing Qwen tool calls: {e}")

# Prepare the response
response = ChatCompletionResponse(
id=f"chatcmpl-{os.urandom(4).hex()}",
Expand Down
Loading