Skip to content

Commit

Permalink
Merge pull request #270 from MeetKai/fix-jinja
Browse files Browse the repository at this point in the history
Fix jinja templates
  • Loading branch information
jeffreymeetkai authored Sep 20, 2024
2 parents ac42b58 + 0c694f1 commit b368a11
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 49 deletions.
28 changes: 16 additions & 12 deletions functionary/prompt_template/jinja_templates/v2.llama3.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
{# version=v2.llama3 #}{%- if not tools -%}
{%- set tools = [] -%}
{%- endif -%}
{{ bos_token + '<|start_header_id|>system<|end_header_id|>\n\n' + generate_schema_from_functions(tools) + '<|eot_id|>' -}}
{{- bos_token + '<|start_header_id|>system<|end_header_id|>\n\n' + generate_schema_from_functions(tools) + '<|eot_id|>' -}}
{%- if tools|length > 0 and tools|selectattr("type", "equalto", "code_interpreter")|list|length > 0 -%}
{{ '<|start_header_id|>system<|end_header_id|>\n\nWhen you send a message containing Python code to python, it will be executed in a stateful Jupyter notebook environment. python will respond with the output of the execution or time out after 60.0 seconds. The drive at \'/mnt/data\' can be used to save and persist user files.<|eot_id|>' }}
{{- '<|start_header_id|>system<|end_header_id|>\n\nWhen you send a message containing Python code to python, it will be executed in a stateful Jupyter notebook environment. python will respond with the output of the execution or time out after 60.0 seconds. The drive at \'/mnt/data\' can be used to save and persist user files.<|eot_id|>' -}}
{%- else -%}
{{ "<|start_header_id|>system<|end_header_id|>\n\nA chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions. The assistant calls functions with appropriate input when necessary<|eot_id|>" }}
{{- "<|start_header_id|>system<|end_header_id|>\n\nA chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions. The assistant calls functions with appropriate input when necessary<|eot_id|>" -}}
{%- endif -%}
{%- for message in messages -%}
{%- if message['role'] == 'user' or message['role'] == 'system' -%}
{{ '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n' + message['content'] + '<|eot_id|>' }}
{{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n' + message['content'] + '<|eot_id|>' -}}
{%- elif message['role'] == 'tool' -%}
{{ '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n' + 'name=' + message['name'] + '\n' + message['content'] + '<|eot_id|>' }}
{{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n' + 'name=' + message['name'] + '\n' + message['content'] + '<|eot_id|>' -}}
{%- else -%}
{{ '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'}}
{%- if message['content'] -%}
{{ message['content'] }}
{%- if (message['content'] and message['content']|length > 0) or ('tool_calls' in message and message['tool_calls'] and message['tool_calls']|length > 0) -%}
{{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'-}}
{%- endif -%}
{%- if 'tool_calls' in message and message['tool_calls'] -%}
{%- if message['content'] and message['content']|length > 0 -%}
{{- message['content'] -}}
{%- endif -%}
{%- if 'tool_calls' in message and message['tool_calls'] and message['tool_calls']|length > 0 -%}
{%- for tool_call in message['tool_calls'] -%}
{{ '<|reserved_special_token_249|>' + tool_call['function']['name'] + '\n' + tool_call['function']['arguments'] }}
{{- '<|reserved_special_token_249|>' + tool_call['function']['name'] + '\n' + tool_call['function']['arguments'] -}}
{%- endfor -%}
{%- endif -%}
{{ '<|eot_id|>' }}
{%- if (message['content'] and message['content']|length > 0) or ('tool_calls' in message and message['tool_calls'] and message['tool_calls']|length > 0) -%}
{{- '<|eot_id|>' -}}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{% if add_generation_prompt %}{{ '<|start_header_id|>assistant<|end_header_id|>\n\n' }}{% endif %}
{% if add_generation_prompt %}{{- '<|start_header_id|>assistant<|end_header_id|>\n\n' -}}{% endif %}
27 changes: 16 additions & 11 deletions functionary/prompt_template/jinja_templates/v2.txt
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
{# version=v2 #}{%- if not tools -%}
{%- set tools = [] -%}
{%- endif -%}
{{ bos_token + '<|from|>system\n<|recipient|>all\n<|content|>' + generate_schema_from_functions(tools) -}}
{{- bos_token + '<|from|>system\n<|recipient|>all\n<|content|>' + generate_schema_from_functions(tools) -}}
{%- if tools|length > 0 and tools|selectattr("type", "equalto", "code_interpreter")|list|length > 0 -%}
{{ '\n<|from|>system\n<|recipient|>all\n<|content|>When you send a message containing Python code to python, it will be executed in a stateful Jupyter notebook environment. python will respond with the output of the execution or time out after 60.0 seconds. The drive at \'/mnt/data\' can be used to save and persist user files.' }}
{{- '\n<|from|>system\n<|recipient|>all\n<|content|>When you send a message containing Python code to python, it will be executed in a stateful Jupyter notebook environment. python will respond with the output of the execution or time out after 60.0 seconds. The drive at \'/mnt/data\' can be used to save and persist user files.' -}}
{%- else -%}
{{ "\n<|from|>system\n<|recipient|>all\n<|content|>A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions. The assistant calls functions with appropriate input when necessary" }}
{{- "\n<|from|>system\n<|recipient|>all\n<|content|>A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions. The assistant calls functions with appropriate input when necessary" -}}
{%- endif -%}
{%- for message in messages -%}
{%- if message['role'] == 'user' or message['role'] == 'system' -%}
{{ '\n<|from|>' + message['role'] + '\n<|recipient|>all\n<|content|>' + message['content'] }}
{{- '\n<|from|>' + message['role'] + '\n<|recipient|>all\n<|content|>' + message['content'] -}}
{%- elif message['role'] == 'tool' -%}
{{ '\n<|from|>' + message['name'] + '\n<|recipient|>all\n<|content|>' + message['content'] }}
{{- '\n<|from|>' + message['name'] + '\n<|recipient|>all\n<|content|>' + message['content'] -}}
{%- else -%}
{%- if message['content'] -%}
{{ "\n<|from|>" + message['role'] + "\n<|recipient|>all\n<|content|>" + message['content'] }}
{% set print_stop = false %}
{%- if message['content'] and message['content']|length > 0 -%}
{{- "\n<|from|>" + message['role'] + "\n<|recipient|>all\n<|content|>" + message['content'] -}}
{% set print_stop = true %}
{%- endif -%}
{%- if 'tool_calls' in message and message['tool_calls'] -%}
{%- if 'tool_calls' in message and message['tool_calls'] and message['tool_calls']|length > 0 -%}
{%- for tool_call in message['tool_calls'] -%}
{{ '\n<|from|>' + message['role'] + '\n<|recipient|>' + tool_call['function']['name'] + '\n<|content|>' + tool_call['function']['arguments'] }}
{{- '\n<|from|>' + message['role'] + '\n<|recipient|>' + tool_call['function']['name'] + '\n<|content|>' + tool_call['function']['arguments'] -}}
{%- endfor -%}
{% set print_stop = true %}
{%- endif -%}
{%- if print_stop -%}
{{- "<|stop|>" -}}
{%- endif -%}
{{ "<|stop|>" }}
{%- endif -%}
{%- endfor -%}
{% if add_generation_prompt %}{{ '\n<|from|>assistant\n<|recipient|>' }}{% endif %}
{% if add_generation_prompt %}{{- '\n<|from|>assistant\n<|recipient|>' -}}{% endif %}
14 changes: 8 additions & 6 deletions functionary/prompt_template/jinja_templates/v3-llama3.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
{{- "\nYou have access to the following functions:\n\n" }}
{%- for t in tools %}
{%- if "type" in t -%}
{{ "Use the function '"|safe + t["function"]["name"] + "' to '"|safe + t["function"]["description"] + "'\n"|safe + t["function"] | tojson() }}
{{ "Use the function '"|safe + t["function"]["name"] + "' to '"|safe + t["function"]["description"] + "'\n"|safe + t["function"] | tojson() | safe }}
{%- else -%}
{{ "Use the function '"|safe + t["name"] + "' to '"|safe + t["description"] + "'\n"|safe + t | tojson() }}
{{ "Use the function '"|safe + t["name"] + "' to '"|safe + t["description"] + "'\n"|safe + t | tojson() | safe }}
{%- endif -%}
{{- "\n\n" }}
{%- endfor %}
Expand All @@ -35,11 +35,13 @@
{%- elif message['role'] == 'tool' -%}
{{ '<|start_header_id|>ipython<|end_header_id|>\n\n' + message['content'] + '<|eot_id|>' }}
{%- else -%}
{{ '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'}}
{%- if message['content'] -%}
{%- if (message['content'] and message['content']|length > 0) or ('tool_calls' in message and message['tool_calls'] and message['tool_calls']|length > 0) -%}
{{ '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'}}
{%- endif -%}
{%- if message['content'] and message['content']|length > 0 -%}
{{ message['content'] }}
{%- endif -%}
{%- if 'tool_calls' in message and message['tool_calls'] -%}
{%- if 'tool_calls' in message and message['tool_calls'] and message['tool_calls']|length > 0 -%}
{%- for tool_call in message['tool_calls'] -%}
{%- if tool_call["function"]["name"] == "python" -%}
{{ '<|python_tag|>' + tool_call['function']['arguments'] }}
Expand All @@ -48,7 +50,7 @@
{%- endif -%}
{%- endfor -%}
{{ '<|eom_id|>' }}
{%- else -%}
{%- elif message['content'] and message['content']|length > 0 -%}
{{ '<|eot_id|>' }}
{%- endif -%}
{%- endif -%}
Expand Down
26 changes: 15 additions & 11 deletions functionary/prompt_template/jinja_templates/v3.llama3.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
{# version=v3.llama3 #}{%- if not tools -%}
{%- set tools = [] -%}
{%- endif -%}
{{ bos_token + '<|start_header_id|>system<|end_header_id|>\n\nYou are capable of executing available function(s) if required.\nOnly execute function(s) when absolutely necessary.\nAsk for the required input to:recipient==all\nUse JSON for function arguments.\nRespond in this format:\n>>>${recipient}\n${content}\nAvailable functions:\n' + generate_schema_from_functions(tools) + '<|eot_id|>' -}}
{{- bos_token + '<|start_header_id|>system<|end_header_id|>\n\nYou are capable of executing available function(s) if required.\nOnly execute function(s) when absolutely necessary.\nAsk for the required input to:recipient==all\nUse JSON for function arguments.\nRespond in this format:\n>>>${recipient}\n${content}\nAvailable functions:\n' + generate_schema_from_functions(tools) + '<|eot_id|>' -}}
{%- if tools|length > 0 and tools|selectattr("type", "equalto", "code_interpreter")|list|length > 0 -%}
{{ '<|start_header_id|>system<|end_header_id|>\n\nWhen you send a message containing Python code to python, it will be executed in a stateful Jupyter notebook environment. python will respond with the output of the execution or time out after 60.0 seconds. The drive at \'/mnt/data\' can be used to save and persist user files.<|eot_id|>' }}
{{- '<|start_header_id|>system<|end_header_id|>\n\nWhen you send a message containing Python code to python, it will be executed in a stateful Jupyter notebook environment. python will respond with the output of the execution or time out after 60.0 seconds. The drive at \'/mnt/data\' can be used to save and persist user files.<|eot_id|>' -}}
{%- endif -%}
{%- for message in messages -%}
{%- if message['role'] == 'user' or message['role'] == 'system' -%}
{{ '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n' + message['content'] + '<|eot_id|>' }}
{{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n' + message['content'] + '<|eot_id|>' -}}
{%- elif message['role'] == 'tool' -%}
{{ '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n' + message['content'] + '<|eot_id|>' }}
{{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n' + message['content'] + '<|eot_id|>' -}}
{%- else -%}
{{ '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'}}
{%- if message['content'] -%}
{{ '>>>all\n' + message['content'] }}
{%- if (message['content'] and message['content']|length > 0) or ('tool_calls' in message and message['tool_calls'] and message['tool_calls']|length > 0) -%}
{{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'-}}
{%- endif -%}
{%- if 'tool_calls' in message and message['tool_calls'] -%}
{%- if message['content'] and message['content']|length > 0 -%}
{{- '>>>all\n' + message['content'] -}}
{%- endif -%}
{%- if 'tool_calls' in message and message['tool_calls'] and message['tool_calls']|length > 0 -%}
{%- for tool_call in message['tool_calls'] -%}
{{ '>>>' + tool_call['function']['name'] + '\n' + tool_call['function']['arguments'] }}
{{- '>>>' + tool_call['function']['name'] + '\n' + tool_call['function']['arguments'] -}}
{%- endfor -%}
{%- endif -%}
{{ '<|eot_id|>' }}
{%- if (message['content'] and message['content']|length > 0) or ('tool_calls' in message and message['tool_calls'] and message['tool_calls']|length > 0) -%}
{{- '<|eot_id|>' -}}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{% if add_generation_prompt %}{{ '<|start_header_id|>assistant<|end_header_id|>\n\n>>>' }}{% endif %}
{% if add_generation_prompt %}{{- '<|start_header_id|>assistant<|end_header_id|>\n\n>>>' -}}{% endif %}
12 changes: 8 additions & 4 deletions functionary/prompt_template/jinja_templates/v3.llava_llama.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@
{%- elif message['role'] == 'tool' -%}
{{ '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n' + message['content'] + '<|eot_id|>' }}
{%- else -%}
{{ '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'}}
{%- if message['content'] -%}
{%- if (message['content'] and message['content']|length > 0) or ('tool_calls' in message and message['tool_calls'] and message['tool_calls']|length > 0) -%}
{{ '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'}}
{%- endif -%}
{%- if message['content'] and message['content']|length > 0 -%}
{{ '>>>all\n' + message['content'] }}
{%- endif -%}
{%- if 'tool_calls' in message and message['tool_calls'] -%}
{%- if 'tool_calls' in message and message['tool_calls'] and message['tool_calls']|length > 0 -%}
{%- for tool_call in message['tool_calls'] -%}
{{ '>>>' + tool_call['function']['name'] + '\n' + tool_call['function']['arguments'] }}
{%- endfor -%}
{%- endif -%}
{{ '<|eot_id|>' }}
{%- if (message['content'] and message['content']|length > 0) or ('tool_calls' in message and message['tool_calls'] and message['tool_calls']|length > 0) -%}
{{ '<|eot_id|>' }}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{% if add_generation_prompt %}{{ '<|start_header_id|>assistant<|end_header_id|>\n\n>>>' }}{% endif %}
12 changes: 11 additions & 1 deletion tests/prompt_test_v2.llama3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,14 @@ l<|eot_id|><|start_header_id|>tool<|end_header_id|>
name=python
[0,1,2,3,5,]<|eot_id|><|start_header_id|>assistant<|end_header_id|>

The final list is: 0,1,2,3,5<|eot_id|>
The final list is: 0,1,2,3,5<|eot_id|><|start_header_id|>user<|end_header_id|>

Thanks! What is the weather in San Francisco?<|eot_id|><|start_header_id|>assistant<|end_header_id|>

<|reserved_special_token_249|>get_weather
{"location": "San Francisco, CA"}<|eot_id|><|start_header_id|>tool<|end_header_id|>

name=get_weather
{"result": {"temperature": 20}}<|eot_id|><|start_header_id|>assistant<|end_header_id|>

The temperature in San Francisco is: 20 degree Celcious<|eot_id|>
14 changes: 13 additions & 1 deletion tests/prompt_test_v2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,16 @@ l<|stop|>
<|content|>[0,1,2,3,5,]
<|from|>assistant
<|recipient|>all
<|content|>The final list is: 0,1,2,3,5<|stop|>
<|content|>The final list is: 0,1,2,3,5<|stop|>
<|from|>user
<|recipient|>all
<|content|>Thanks! What is the weather in San Francisco?
<|from|>assistant
<|recipient|>get_weather
<|content|>{"location": "San Francisco, CA"}<|stop|>
<|from|>get_weather
<|recipient|>all
<|content|>{"result": {"temperature": 20}}
<|from|>assistant
<|recipient|>all
<|content|>The temperature in San Francisco is: 20 degree Celcious<|stop|>
10 changes: 9 additions & 1 deletion tests/prompt_test_v3-llama3.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,12 @@ l<|eom_id|><|start_header_id|>ipython<|end_header_id|>

[0,1,2,3,5,]<|eot_id|><|start_header_id|>assistant<|end_header_id|>

The final list is: 0,1,2,3,5<|eot_id|>
The final list is: 0,1,2,3,5<|eot_id|><|start_header_id|>user<|end_header_id|>

Thanks! What is the weather in San Francisco?<|eot_id|><|start_header_id|>assistant<|end_header_id|>

<function=get_weather>{"location": "San Francisco, CA"}</function><|eom_id|><|start_header_id|>ipython<|end_header_id|>

{"result": {"temperature": 20}}<|eot_id|><|start_header_id|>assistant<|end_header_id|>

The temperature in San Francisco is: 20 degree Celcious<|eot_id|>
12 changes: 11 additions & 1 deletion tests/prompt_test_v3.llama3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,14 @@ l<|eot_id|><|start_header_id|>tool<|end_header_id|>
[0,1,2,3,5,]<|eot_id|><|start_header_id|>assistant<|end_header_id|>

>>>all
The final list is: 0,1,2,3,5<|eot_id|>
The final list is: 0,1,2,3,5<|eot_id|><|start_header_id|>user<|end_header_id|>

Thanks! What is the weather in San Francisco?<|eot_id|><|start_header_id|>assistant<|end_header_id|>

>>>get_weather
{"location": "San Francisco, CA"}<|eot_id|><|start_header_id|>tool<|end_header_id|>

{"result": {"temperature": 20}}<|eot_id|><|start_header_id|>assistant<|end_header_id|>

>>>all
The temperature in San Francisco is: 20 degree Celcious<|eot_id|>
12 changes: 11 additions & 1 deletion tests/prompt_test_v3.llava_llama.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,14 @@ l<|eot_id|><|start_header_id|>tool<|end_header_id|>
[0,1,2,3,5,]<|eot_id|><|start_header_id|>assistant<|end_header_id|>

>>>all
The final list is: 0,1,2,3,5<|eot_id|>
The final list is: 0,1,2,3,5<|eot_id|><|start_header_id|>user<|end_header_id|>

Thanks! What is the weather in San Francisco?<|eot_id|><|start_header_id|>assistant<|end_header_id|>

>>>get_weather
{"location": "San Francisco, CA"}<|eot_id|><|start_header_id|>tool<|end_header_id|>

{"result": {"temperature": 20}}<|eot_id|><|start_header_id|>assistant<|end_header_id|>

>>>all
The temperature in San Francisco is: 20 degree Celcious<|eot_id|>
25 changes: 25 additions & 0 deletions tests/test_case.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,31 @@
{
"role": "assistant",
"content": "The final list is: 0,1,2,3,5"
},
{
"role": "user",
"content": "Thanks! What is the weather in San Francisco?"
},
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"function": {
"name": "get_weather",
"arguments": "{\"location\": \"San Francisco, CA\"}"
}
}
]
},
{
"role": "tool",
"content": "{\"result\": {\"temperature\": 20}}",
"name": "get_weather"
},
{
"role": "assistant",
"content": "The temperature in San Francisco is: 20 degree Celcious"
}
]
}
Loading

0 comments on commit b368a11

Please sign in to comment.