main
jinja 120 lines 5.13 KB
Raw
1 {{- bos_token }}
2 {%- if custom_tools is defined %}
3 {%- set tools = custom_tools %}
4 {%- endif %}
5 {%- if not tools_in_user_message is defined %}
6 {#- Llama 3.1 doesn't pass all tests if the tools are in the system prompt #}
7 {%- set tools_in_user_message = true %}
8 {%- endif %}
9 {%- if not date_string is defined %}
10 {%- if strftime_now is defined %}
11 {%- set date_string = strftime_now("%d %b %Y") %}
12 {%- else %}
13 {%- set date_string = "26 Jul 2024" %}
14 {%- endif %}
15 {%- endif %}
16 {%- if not tools is defined %}
17 {%- set tools = none %}
18 {%- endif %}
19
20 {#- This block extracts the system message, so we can slot it into the right place. #}
21 {%- if messages[0]['role'] == 'system' %}
22 {%- if messages[0]['content'] is string %}
23 {%- set system_message = messages[0]['content']|trim %}
24 {%- else %}
25 {%- set system_message = messages[0]['content'][0]['text']|trim %}
26 {%- endif %}
27 {%- set messages = messages[1:] %}
28 {%- else %}
29 {%- if tools is not none %}
30 {%- set system_message = "You are a helpful assistant with tool calling capabilities. Only reply with a tool call if the function exists in the library provided by the user. If it doesn't exist, just reply directly in natural language. When you receive a tool call response, use the output to format an answer to the original user question." %}
31 {%- else %}
32 {%- set system_message = "" %}
33 {%- endif %}
34 {%- endif %}
35
36 {#- System message #}
37 {{- "<|start_header_id|>system<|end_header_id|>\n\n" }}
38 {%- if tools is not none %}
39 {{- "Environment: ipython\n" }}
40 {%- endif %}
41 {{- "Cutting Knowledge Date: December 2023\n" }}
42 {{- "Today Date: " + date_string + "\n\n" }}
43 {%- if tools is not none and not tools_in_user_message %}
44 {{- "You have access to the following functions. To call a function, please respond with JSON for a function call. " }}
45 {{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}. ' }}
46 {{- "Do not use variables.\n\n" }}
47 {%- for t in tools %}
48 {{- t | tojson(indent=4) }}
49 {{- "\n\n" }}
50 {%- endfor %}
51 {%- endif %}
52 {{- system_message }}
53 {{- "<|eot_id|>" }}
54
55 {#- Custom tools are passed in a user message with some extra guidance #}
56 {%- if tools_in_user_message and not tools is none %}
57 {#- Extract the first user message so we can plug it in here #}
58 {%- if messages | length != 0 %}
59 {%- if messages[0]['content'] is string %}
60 {%- set first_user_message = messages[0]['content']|trim %}
61 {%- else %}
62 {%- set first_user_message = messages[0]['content'] | selectattr('type', 'equalto', 'text') | map(attribute='text') | map('trim') | join('\n') %}
63 {%- endif %}
64 {%- set messages = messages[1:] %}
65 {%- else %}
66 {{- raise_exception("Cannot put tools in the first user message when there's no first user message!") }}
67 {%- endif %}
68 {{- '<|start_header_id|>user<|end_header_id|>\n\n' -}}
69 {{- "Given the following functions, please respond with a JSON for a function call " }}
70 {{- "with its proper arguments that best answers the given prompt.\n\n" }}
71 {{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}. ' }}
72 {{- "Do not use variables.\n\n" }}
73 {%- for t in tools %}
74 {{- t | tojson(indent=4) }}
75 {{- "\n\n" }}
76 {%- endfor %}
77 {{- first_user_message + "<|eot_id|>"}}
78 {%- endif %}
79
80 {%- for message in messages %}
81 {%- if not (message.role == 'ipython' or message.role == 'tool' or 'tool_calls' in message) %}
82 {{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n' }}
83 {%- if message['content'] is string %}
84 {{- message['content'] | trim}}
85 {%- else %}
86 {%- for content in message['content'] %}
87 {%- if content['type'] == 'text' %}
88 {{- content['text'] | trim }}
89 {%- endif %}
90 {%- endfor %}
91 {%- endif %}
92 {{- '<|eot_id|>' }}
93 {%- elif 'tool_calls' in message %}
94 {%- if not message.tool_calls|length == 1 %}
95 {{- raise_exception("This model only supports single tool-calls at once!") }}
96 {%- endif %}
97 {%- set tool_call = message.tool_calls[0].function %}
98 {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' -}}
99 {{- '{"name": "' + tool_call.name + '", ' }}
100 {{- '"parameters": ' }}
101 {{- tool_call.arguments | tojson }}
102 {{- "}" }}
103 {{- "<|eot_id|>" }}
104 {%- elif message.role == "tool" or message.role == "ipython" %}
105 {{- "<|start_header_id|>ipython<|end_header_id|>\n\n" }}
106 {%- if message.content is string %}
107 {{- { "output": message.content } | tojson }}
108 {%- else %}
109 {%- for content in message['content'] %}
110 {%- if content['type'] == 'text' %}
111 {{- { "output": content['text'] } | tojson }}
112 {%- endif %}
113 {%- endfor %}
114 {%- endif %}
115 {{- "<|eot_id|>" }}
116 {%- endif %}
117 {%- endfor %}
118 {%- if add_generation_prompt %}
119 {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' }}
120 {%- endif %}