Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_id = "watt-ai/watt-tool-70B"
4
5tokenizer = AutoTokenizer.from_pretrained(model_id)
6model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype='auto', device_map="auto")
7
8# Example usage (adapt as needed for your specific tool usage scenario)
9"""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.
10If 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.
11You should only return the function call in tools call sections.
12
13If 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)]
14You SHOULD NOT include any other text in the response.
15Here is a list of functions in JSON format that you can invoke.\n{functions}\n
16"""
17# User query
18query = "Find me the sales growth rate for company XYZ for the last 3 years and also the interest coverage ratio for the same duration."
19
20tools = [
21 {
22 "name": "financial_ratios.interest_coverage", "description": "Calculate a company's interest coverage ratio given the company name and duration",
23 "arguments": {
24 "type": "dict",
25 "properties": {
26 "company_name": {
27 "type": "string",
28 "description": "The name of the company."
29 },
30 "years": {
31 "type": "integer",
32 "description": "Number of past years to calculate the ratio."
33 }
34 },
35 "required": ["company_name", "years"]
36 }
37 },
38 {
39 "name": "sales_growth.calculate",
40 "description": "Calculate a company's sales growth rate given the company name and duration",
41 "arguments": {
42 "type": "dict",
43 "properties": {
44 "company": {
45 "type": "string",
46 "description": "The company that you want to get the sales growth rate for."
47 },
48 "years": {
49 "type": "integer",
50 "description": "Number of past years for which to calculate the sales growth rate."
51 }
52 },
53 "required": ["company", "years"]
54 }
55 },
56 {
57 "name": "weather_forecast",
58 "description": "Retrieve a weather forecast for a specific location and time frame.",
59 "arguments": {
60 "type": "dict",
61 "properties": {
62 "location": {
63 "type": "string",
64 "description": "The city that you want to get the weather for."
65 },
66 "days": {
67 "type": "integer",
68 "description": "Number of days for the forecast."
69 }
70 },
71 "required": ["location", "days"]
72 }
73 }
74]
75
76messages = [
77 {'role': 'system', 'content': system_prompt.format(functions=tools)},
78 {'role': 'user', 'content': query}
79]
80inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
81
82outputs = model.generate(inputs, max_new_tokens=512, do_sample=False, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id)
83print(tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True))