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-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)]