AngusHuang
commited on
Add an example for basic usage
Browse files
README.md
CHANGED
@@ -28,6 +28,110 @@ More details can be found in our paper on arxiv: [*ToolACE: Winning the Points
|
|
28 |
![image/jpeg](https://cdn-uploads.huggingface.co/production/uploads/66bf01f45bdd611f9a602087/WmyWOYtg_dbTgwQmvlqcz.jpeg)
|
29 |
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
### Citation
|
32 |
|
33 |
If you think ToolACE is useful in your work, please cite our paper:
|
|
|
28 |
![image/jpeg](https://cdn-uploads.huggingface.co/production/uploads/66bf01f45bdd611f9a602087/WmyWOYtg_dbTgwQmvlqcz.jpeg)
|
29 |
|
30 |
|
31 |
+
### Usage
|
32 |
+
Here we provide a code snippet with `apply_chat_template` to show you how to load the tokenizer and model and how to generate function calling with given functions.
|
33 |
+
|
34 |
+
```python
|
35 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
36 |
+
|
37 |
+
model_name = "/home/huangxu/work/OpenLLMs/ToolACE-8B-zh-v2.2_1ep"
|
38 |
+
|
39 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
40 |
+
model = AutoModelForCausalLM.from_pretrained(
|
41 |
+
model_name,
|
42 |
+
torch_dtype='auto',
|
43 |
+
device_map='auto'
|
44 |
+
)
|
45 |
+
|
46 |
+
|
47 |
+
# You can modify the prompt for your task
|
48 |
+
system_prompt = """You are an expert in composing functions. You are given a question and a set of possible functions. Based on the question, you will need to make one or more function/tool calls to achieve the purpose.
|
49 |
+
If none of the function can be used, point it out. If the given question lacks the parameters required by the function, also point it out.
|
50 |
+
You should only return the function call in tools call sections.
|
51 |
+
|
52 |
+
If you decide to invoke any of the function(s), you MUST put it in the format of [func_name1(params_name1=params_value1, params_name2=params_value2...), func_name2(params)]
|
53 |
+
You SHOULD NOT include any other text in the response.
|
54 |
+
Here is a list of functions in JSON format that you can invoke.\n{functions}\n
|
55 |
+
"""
|
56 |
+
|
57 |
+
# User query
|
58 |
+
query = "Find me the sales growth rate for company XYZ for the last 3 years and also the interest coverage ratio for the same duration."
|
59 |
+
|
60 |
+
# Availabel tools in JSON format (OpenAI-format)
|
61 |
+
tools = [
|
62 |
+
{
|
63 |
+
"name": "financial_ratios.interest_coverage", "description": "Calculate a company's interest coverage ratio given the company name and duration",
|
64 |
+
"arguments": {
|
65 |
+
"type": "dict",
|
66 |
+
"properties": {
|
67 |
+
"company_name": {
|
68 |
+
"type": "string",
|
69 |
+
"description": "The name of the company."
|
70 |
+
},
|
71 |
+
"years": {
|
72 |
+
"type": "integer",
|
73 |
+
"description": "Number of past years to calculate the ratio."
|
74 |
+
}
|
75 |
+
},
|
76 |
+
"required": ["company_name", "years"]
|
77 |
+
}
|
78 |
+
},
|
79 |
+
{
|
80 |
+
"name": "sales_growth.calculate",
|
81 |
+
"description": "Calculate a company's sales growth rate given the company name and duration",
|
82 |
+
"arguments": {
|
83 |
+
"type": "dict",
|
84 |
+
"properties": {
|
85 |
+
"company": {
|
86 |
+
"type": "string",
|
87 |
+
"description": "The company that you want to get the sales growth rate for."
|
88 |
+
},
|
89 |
+
"years": {
|
90 |
+
"type": "integer",
|
91 |
+
"description": "Number of past years for which to calculate the sales growth rate."
|
92 |
+
}
|
93 |
+
},
|
94 |
+
"required": ["company", "years"]
|
95 |
+
}
|
96 |
+
},
|
97 |
+
{
|
98 |
+
"name": "weather_forecast",
|
99 |
+
"description": "Retrieve a weather forecast for a specific location and time frame.",
|
100 |
+
"arguments": {
|
101 |
+
"type": "dict",
|
102 |
+
"properties": {
|
103 |
+
"location": {
|
104 |
+
"type": "string",
|
105 |
+
"description": "The city that you want to get the weather for."
|
106 |
+
},
|
107 |
+
"days": {
|
108 |
+
"type": "integer",
|
109 |
+
"description": "Number of days for the forecast."
|
110 |
+
}
|
111 |
+
},
|
112 |
+
"required": ["location", "days"]
|
113 |
+
}
|
114 |
+
}
|
115 |
+
]
|
116 |
+
|
117 |
+
messages = [
|
118 |
+
{'role': 'system', 'content': system_prompt.format(functions=tools)},
|
119 |
+
{'role': 'user', 'content': query}
|
120 |
+
]
|
121 |
+
|
122 |
+
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
123 |
+
|
124 |
+
outputs = model.generate(inputs, max_new_tokens=512, do_sample=False, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id)
|
125 |
+
print(tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True))
|
126 |
+
```
|
127 |
+
|
128 |
+
Then you should be able to see the following output functional calls:
|
129 |
+
```
|
130 |
+
[sales_growth.calculate(company="XYZ", years=3), financial_ratios.interest_coverage(company_name="XYZ", years=3)]
|
131 |
+
```
|
132 |
+
|
133 |
+
|
134 |
+
|
135 |
### Citation
|
136 |
|
137 |
If you think ToolACE is useful in your work, please cite our paper:
|