Add tool use template
Browse filesThis PR is a work in progress, please don't merge it yet! It is intended to be used with the [PR branch here](https://github.com/huggingface/transformers/pull/30621). It adds proper tool use support to Hermes-2-Pro's chat template. You can test it out with the following code block, after `pip install --upgrade https://github.com/huggingface/transformers.git@new_chat_template_args`
```python
from transformers import AutoTokenizer
from transformers.utils import add_json_schema
from typing import Dict
@add_json_schema
def get_stock_fundamentals(symbol: str) -> Dict:
"""
Get fundamental data for a given stock symbol using yfinance API.
Args:
symbol: The stock symbol.
Returns:
A dictionary containing fundamental data.
Keys:
- 'symbol': The stock symbol.
- 'company_name': The long name of the company.
- 'sector': The sector to which the company belongs.
- 'industry': The industry to which the company belongs.
- 'market_cap': The market capitalization of the company.
- 'pe_ratio': The forward price-to-earnings ratio.
- 'pb_ratio': The price-to-book ratio.
- 'dividend_yield': The dividend yield.
- 'eps': The trailing earnings per share.
- 'beta': The beta value of the stock.
- '52_week_high': The 52-week high price of the stock.
- '52_week_low': The 52-week low price of the stock.
"""
pass
tokenizer = AutoTokenizer.from_pretrained("NousResearch/Hermes-2-Pro-Llama-3-8B")
test_chat = [{"role": "user", "content": "Fetch the stock fundamentals data for Tesla (TSLA)"}]
tools = [get_stock_fundamentals]
inputs = tokenizer.apply_chat_template(test_chat, tools=tools, chat_template="tool_use", tokenize=False, add_generation_prompt=True)
```
- tokenizer_config.json +10 -1
@@ -2306,7 +2306,16 @@
|
|
2306 |
}
|
2307 |
},
|
2308 |
"bos_token": "<|begin_of_text|>",
|
2309 |
-
"chat_template":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2310 |
"eos_token": "<|im_end|>",
|
2311 |
"model_input_names": [
|
2312 |
"input_ids",
|
|
|
2306 |
}
|
2307 |
},
|
2308 |
"bos_token": "<|begin_of_text|>",
|
2309 |
+
"chat_template": [
|
2310 |
+
{
|
2311 |
+
"name": "default",
|
2312 |
+
"template": "{{bos_token}}{% for message in messages %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}"
|
2313 |
+
},
|
2314 |
+
{
|
2315 |
+
"name": "tool_use",
|
2316 |
+
"template": "{%- macro json_to_python_type(json_spec) %}\n{%- set basic_type_map = {\n \"string\": \"str\",\n \"number\": \"float\",\n \"integer\": \"int\",\n \"boolean\": \"bool\"\n} %}\n\n{%- if basic_type_map[json_spec.type] is defined %}\n {{- basic_type_map[json_spec.type] }}\n{%- elif json_spec.type == \"array\" %}\n {{- \"list[\" + json_to_python_type(json_spec.items) + \"]\"}}\n{%- elif json_spec.type == \"object\" %}\n {%- if json_spec.additionalProperties is defined %}\n {{- \"dict[str, \" + json_to_python_type(json_spec.additionalProperties) + ']'}}\n {%- else %}\n {{- \"dict\" }}\n {%- endif %}\n{%- elif json_spec.type is iterable %}\n {{- \"Union[\" }}\n {%- for t in json_spec.type %}\n {{- json_to_python_type({\"type\": t}) }}\n {%- if not loop.last %}\n {{- \",\" }} \n {%- endif %}\n {%- endfor %}\n {{- \"]\" }}\n{%- else %}\n {{- \"Any\" }}\n{%- endif %}\n{%- endmacro %}\n\n\n{{- bos_token }}\n{{- \"You are a function calling AI model. You are provided with function signatures within <tools></tools> XML tags. You may call one or more functions to assist with the user query. Don't make assumptions about what values to plug into functions. Here are the available tools: <tools> \" }}\n{%- for tool in tools %}\n {{- '{\"type\": \"function\", \"function\": ' }}\n {{- '{\"name\": ' + tool.name + '\",' }}\n {{- '\"description\": \"' + tool.name + '(' }}\n {%- for param_name, param_fields in tool.parameters.properties.items() %}\n {{- param_name + \": \" + json_to_python_type(param_fields) }}\n {%- if not loop.last %}\n {{- \", \" }}\n {%- endif %}\n {%- endfor %}\n {{- \")\" }}\n {%- if tool.return is defined %}\n {{- \" -> \" + json_to_python_type(tool.return) }}\n {%- endif %}\n {{- \" - \" + tool.description + \"\n\n\" }}\n {%- for param_name, param_fields in tool.parameters.properties.items() %}\n {%- if loop.first %}\n {{- \" Args:\n\" }}\n {%- endif %}\n {{- \" \" + param_name + \"(\" + json_to_python_type(param_fields) + \"): \" + param_fields.description }}\n {%- endfor %}\n {%- if tool.return is defined and tool.return.description is defined %}\n {{- \" Returns:\n \" + tool.return.description }}\n {%- if not loop.last %}\n {{- '\n' }}\n {%- else %}\n {{- '\"' }}\n {%- endif %}\n {%- endif %}\n {{- '\"parameters\": ' }}\n {%- if tool.parameters.properties | length == 0 %}\n {{- \"{}\" }}\n {%- else %}\n {{- tool.parameters }}\n {%- endif %}\n{%- endfor %}\n{{- \" </tools>\" }}\n{{- 'Use the following pydantic model json schema for each tool call you will make: {\"properties\": {\"arguments\": {\"title\": \"Arguments\", \"type\": \"object\"}, \"name\": {\"title\": \"Name\", \"type\": \"string\"}}, \"required\": [\"arguments\", \"name\"], \"title\": \"FunctionCall\", \"type\": \"object\"}\n' }}\n{{- \"For each function call return a json object with function name and arguments within <tool_call></tool_call> XML tags as follows:\n\" }}\n{{- \"<tool_call>\n\" }}\n{{- '{\"arguments\": <args-dict>, \"name\": <function-name>}\n' }}\n{{- '</tool_call><|im_end|>' }}\n{%- for message in messages %}\n {{- '<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n' }}\n{%- endfor %}\n{%- if add_generation_prompt %}\n {{- '<|im_start|>assistant\n' }}\n{%- endif %}"
|
2317 |
+
}
|
2318 |
+
],
|
2319 |
"eos_token": "<|im_end|>",
|
2320 |
"model_input_names": [
|
2321 |
"input_ids",
|