Views
No views yet

apply_chat_template to show you how to load the tokenizer and model and how to generate function calling with given functions.1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_name = "Team-ACE/ToolACE-2.5-Llama-3.1-8B"
4
5tokenizer = AutoTokenizer.from_pretrained(model_name)
6model = AutoModelForCausalLM.from_pretrained(
7 model_name,
8 torch_dtype='auto',
9 device_map='auto'
10)
11
12
13# You can modify the prompt for your task
14system_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.
15If 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.
16You should only return the function call in tools call sections.
17
18If 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)]
19You SHOULD NOT include any other text in the response.
20Here is a list of functions in JSON format that you can invoke.\n{functions}\n
21"""
22
23# User query
24query = "Find me the sales growth rate for company XYZ for the last 3 years and also the interest coverage ratio for the same duration."
25
26# Availabel tools in JSON format (OpenAI-format)
27tools = [
28 {
29 "name": "financial_ratios.interest_coverage", "description": "Calculate a company's interest coverage ratio given the company name and duration",
30 "arguments": {
31 "type": "dict",
32 "properties": {
33 "company_name": {
34 "type": "string",
35 "description": "The name of the company."
36 },
37 "years": {
38 "type": "integer",
39 "description": "Number of past years to calculate the ratio."
40 }
41 },
42 "required": ["company_name", "years"]
43 }
44 },
45 {
46 "name": "sales_growth.calculate",
47 "description": "Calculate a company's sales growth rate given the company name and duration",
48 "arguments": {
49 "type": "dict",
50 "properties": {
51 "company": {
52 "type": "string",
53 "description": "The company that you want to get the sales growth rate for."
54 },
55 "years": {
56 "type": "integer",
57 "description": "Number of past years for which to calculate the sales growth rate."
58 }
59 },
60 "required": ["company", "years"]
61 }
62 },
63 {
64 "name": "weather_forecast",
65 "description": "Retrieve a weather forecast for a specific location and time frame.",
66 "arguments": {
67 "type": "dict",
68 "properties": {
69 "location": {
70 "type": "string",
71 "description": "The city that you want to get the weather for."
72 },
73 "days": {
74 "type": "integer",
75 "description": "Number of days for the forecast."
76 }
77 },
78 "required": ["location", "days"]
79 }
80 }
81]
82
83messages = [
84 {'role': 'system', 'content': system_prompt.format(functions=tools)},
85 {'role': 'user', 'content': query}
86]
87
88inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
89
90outputs = model.generate(inputs, max_new_tokens=512, do_sample=False, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id)
91print(tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True))[sales_growth.calculate(company="XYZ", years=3), financial_ratios.interest_coverage(company_name="XYZ", years=3)]@inproceedings{
liu2025toolace,
title={Tool{ACE}: Winning the Points of {LLM} Function Calling},
author={Weiwen Liu and Xu Huang and Xingshan Zeng and xinlong hao and Shuai Yu and Dexun Li and Shuai Wang and Weinan Gan and Zhengying Liu and Yuanqing Yu and Zezhong WANG and Yuxian Wang and Wu Ning and Yutai Hou and Bin Wang and Chuhan Wu and Wang Xinzhi and Yong Liu and Yasheng Wang and Duyu Tang and Dandan Tu and Lifeng Shang and Xin Jiang and Ruiming Tang and Defu Lian and Qun Liu and Enhong Chen},
booktitle={The Thirteenth International Conference on Learning Representations},
year={2025},
url={https://openreview.net/forum?id=8EB8k6DdCU}
}@article{zeng2025toolacer,
title={ToolACE-R: Model-aware Iterative Training and Adaptive Refinement for Tool Learning},
author={Zeng, Xingshan and Liu, Weiwen and Huang, Xu and Wang, Zezhong and Wang, Lingzhi and Li, Liangyou and Wang, Yasheng and Shang, Lifeng and Jiang, Xin and Tang, Ruiming and Liu, Qun},
journal={arXiv preprint arXiv:2504.01400},
year={2025}
}@article{huang2025toolace,
title={ToolACE-DEV: Self-Improving Tool Learning via Decomposition and EVolution},
author={Huang, Xu and Liu, Weiwen and Zeng, Xingshan and Huang, Yuefeng and Hao, Xinlong and Wang, Yuxian and Zeng, Yirong and Wu, Chuhan and Wang, Yasheng and Tang, Ruiming and Lian, Defu},
journal={arXiv preprint arXiv:2505.07512},
year={2025}
}@article{zeng2025toolacemt,
title={ToolACE-MT: Non-Autoregressive Generation for Agentic Multi-Turn Interaction},
author={Zeng, Xingshan and Liu, Weiwen and Wang, Lingzhi and Li, Liangyou and Mi, Fei and Wang, Yasheng and Shang, Lifeng and Jiang, Xin and Liu, Qun},
journal={arXiv preprint arXiv:2508.12685},
year={2025}
}