Skip to content

Commit 7df8cc1

Browse files
chaunceyjianglk-chen
authored andcommitted
[Misc] Add a Jinja template to support Mistral3 function calling (vllm-project#17195)
Signed-off-by: chaunceyjiang <[email protected]>
1 parent b4b20a2 commit 7df8cc1

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
{%- set today = strftime_now("%Y-%m-%d") %}
2+
{%- set default_system_message = "You are Mistral Small 3, a Large Language Model (LLM) created by Mistral AI, a French startup headquartered in Paris.\nYour knowledge base was last updated on 2023-10-01. The current date is " + today + ".\n\nWhen you're not sure about some information, you say that you don't have the information and don't make up anything.\nIf the user's question is not clear, ambiguous, or does not provide enough context for you to accurately answer the question, you do not try to answer it right away and you rather ask the user to clarify their request (e.g. \"What are some good restaurants around me?\" => \"Where are you?\" or \"When is the next flight to Tokyo\" => \"Where do you travel from?\")" %}
3+
4+
{{- bos_token }}
5+
6+
{%- if messages[0]['role'] == 'system' %}
7+
{%- if messages[0]['content'] is string %}
8+
{%- set system_message = messages[0]['content'] %}
9+
{%- set loop_messages = messages[1:] %}
10+
{%- else %}
11+
{%- set system_message = messages[0]['content'][0]['text'] %}
12+
{%- set loop_messages = messages[1:] %}
13+
{%- endif %}
14+
{%- else %}
15+
{%- set system_message = default_system_message %}
16+
{%- set loop_messages = messages %}
17+
{%- endif %}
18+
{%- if not tools is defined %}
19+
{%- set tools = none %}
20+
{%- elif tools is not none %}
21+
{%- set parallel_tool_prompt = "You are a helpful assistant that can call tools. If you call one or more tools, format them in a single JSON array or objects, where each object is a tool call, not as separate objects outside of an array or multiple arrays. Use the format [{\"name\": tool call name, \"arguments\": tool call arguments}, additional tool calls] if you call more than one tool. If you call tools, do not attempt to interpret them or otherwise provide a response until you receive a tool call result that you can interpret for the user." %}
22+
{%- if system_message is defined %}
23+
{%- set system_message = parallel_tool_prompt + "\n\n" + system_message %}
24+
{%- else %}
25+
{%- set system_message = parallel_tool_prompt %}
26+
{%- endif %}
27+
{%- endif %}
28+
{{- '[SYSTEM_PROMPT]' + system_message + '[/SYSTEM_PROMPT]' }}
29+
30+
{%- set user_messages = loop_messages | selectattr("role", "equalto", "user") | list %}
31+
32+
{%- for message in loop_messages | rejectattr("role", "equalto", "tool") | rejectattr("role", "equalto", "tool_results") | selectattr("tool_calls", "undefined") %}
33+
{%- if (message["role"] == "user") != (loop.index0 % 2 == 0) %}
34+
{{- raise_exception("After the optional system message, conversation roles must alternate user/assistant/user/assistant/...") }}
35+
{%- endif %}
36+
{%- endfor %}
37+
38+
{%- for message in loop_messages %}
39+
{%- if message["role"] == "user" %}
40+
{%- if tools is not none and (message == user_messages[-1]) %}
41+
{{- "[AVAILABLE_TOOLS] [" }}
42+
{%- for tool in tools %}
43+
{%- set tool = tool.function %}
44+
{{- '{"type": "function", "function": {' }}
45+
{%- for key, val in tool.items() if key != "return" %}
46+
{%- if val is string %}
47+
{{- '"' + key + '": "' + val + '"' }}
48+
{%- else %}
49+
{{- '"' + key + '": ' + val|tojson }}
50+
{%- endif %}
51+
{%- if not loop.last %}
52+
{{- ", " }}
53+
{%- endif %}
54+
{%- endfor %}
55+
{{- "}}" }}
56+
{%- if not loop.last %}
57+
{{- ", " }}
58+
{%- else %}
59+
{{- "]" }}
60+
{%- endif %}
61+
{%- endfor %}
62+
{{- "[/AVAILABLE_TOOLS]" }}
63+
{%- endif %}
64+
{%- if message['content'] is string %}
65+
{{- '[INST]' + message['content'] + '[/INST]' }}
66+
{%- else %}
67+
{{- '[INST]' }}
68+
{%- for block in message['content'] %}
69+
{%- if block['type'] == 'text' %}
70+
{{- block['text'] }}
71+
{%- elif block['type'] == 'image' or block['type'] == 'image_url' %}
72+
{{- '[IMG]' }}
73+
{%- else %}
74+
{{- raise_exception('Only text and image blocks are supported in message content!') }}
75+
{%- endif %}
76+
{%- endfor %}
77+
{{- '[/INST]' }}
78+
{%- endif %}
79+
{%- elif message["role"] == "tool_calls" or message.tool_calls is defined %}
80+
{%- if message.tool_calls is defined %}
81+
{%- set tool_calls = message.tool_calls %}
82+
{%- else %}
83+
{%- set tool_calls = message.content %}
84+
{%- endif %}
85+
{{- "[TOOL_CALLS] [" }}
86+
{%- for tool_call in tool_calls %}
87+
{%- set out = tool_call.function|tojson %}
88+
{{- out[:-1] }}
89+
{%- if not tool_call.id is defined or tool_call.id|length < 9 %}
90+
{{- raise_exception("Tool call IDs should be alphanumeric strings with length >= 9! (1)" + tool_call.id) }}
91+
{%- endif %}
92+
{{- ', "id": "' + tool_call.id[-9:] + '"}' }}
93+
{%- if not loop.last %}
94+
{{- ", " }}
95+
{%- else %}
96+
{{- "]" + eos_token }}
97+
{%- endif %}
98+
{%- endfor %}
99+
{%- elif message['role'] == 'assistant' %}
100+
{%- if message['content'] is string %}
101+
{{- message['content'] + eos_token }}
102+
{%- else %}
103+
{{- message['content'][0]['text'] + eos_token }}
104+
{%- endif %}
105+
{%- elif message["role"] == "tool_results" or message["role"] == "tool" %}
106+
{%- if message.content is defined and message.content.content is defined %}
107+
{%- set content = message.content.content %}
108+
{%- else %}
109+
{%- set content = message.content %}
110+
{%- endif %}
111+
{{- '[TOOL_RESULTS] {"content": ' + content|string + ", " }}
112+
{%- if not message.tool_call_id is defined or message.tool_call_id|length < 9 %}
113+
{{- raise_exception("Tool call IDs should be alphanumeric strings with length >= 9! (2)" + message.tool_call_id) }}
114+
{%- endif %}
115+
{{- '"call_id": "' + message.tool_call_id[-9:] + '"}[/TOOL_RESULTS]' }}
116+
{%- else %}
117+
{{- raise_exception("Only user and assistant roles are supported, with the exception of an initial optional system message!") }}
118+
{%- endif %}
119+
{%- endfor %}

0 commit comments

Comments
 (0)