plaguss commited on
Commit
eff893b
·
verified ·
1 Parent(s): b20edc1

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +143 -5
README.md CHANGED
@@ -17,13 +17,151 @@ It has been trained using [TRL](https://github.com/huggingface/trl).
17
 
18
  ## Quick start
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  ```python
21
- from transformers import pipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- question = "If you had a time machine, but could only go to the past or the future once and never return, which would you choose and why?"
24
- generator = pipeline("text-generation", model="plaguss/Llama-3.2-1B-Instruct-APIGen-FC-v0.1-notoken", device="cuda")
25
- output = generator([{"role": "user", "content": question}], max_new_tokens=128, return_full_text=False)[0]
26
- print(output["generated_text"])
27
  ```
28
 
29
  ## Training procedure
 
17
 
18
  ## Quick start
19
 
20
+ Example query with prompt:
21
+
22
+ ````python
23
+ import json
24
+
25
+ import torch
26
+ from transformers import AutoModelForCausalLM, AutoTokenizer
27
+ from jinja2 import Template
28
+
29
+ model_name = "plaguss/Llama-3.2-1B-Instruct-APIGen-FC-v0.1-notoken"
30
+ model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype="auto", trust_remote_code=True)
31
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
32
+
33
+ SYSTEM_PROMPT = """
34
+ You are an expert in composing functions. You are given a question and a set of possible functions.
35
+ Based on the question, you will need to make one or more function/tool calls to achieve the purpose.
36
+ If none of the functions can be used, point it out and refuse to answer.
37
+ If the given question lacks the parameters required by the function, also point it out.
38
+
39
+ The output MUST strictly adhere to the following format, and NO other text MUST be included.
40
+ The example format is as follows. Please make sure the parameter type is correct. If no function call is needed, please make the tool calls an empty list '[]'.
41
+ ```
42
+ <tool_call>[
43
+ {"name": "func_name1", "arguments": {"argument1": "value1", "argument2": "value2"}},
44
+ ... (more tool calls as required)
45
+ ]</tool_call>
46
+ ```
47
+ """.strip()
48
+
49
+ prompt = Template("""
50
+ You have access to the following tools:
51
+ <tools>{{ tools }}</tools>
52
+
53
+ Please answer the following query:
54
+ {{ query }}
55
+ """.lstrip())
56
+
57
+ get_weather_api = {
58
+ "name": "get_weather",
59
+ "description": "Get the current weather for a location",
60
+ "parameters": {
61
+ "type": "object",
62
+ "properties": {
63
+ "location": {
64
+ "type": "string",
65
+ "description": "The city and state, e.g. San Francisco, New York"
66
+ },
67
+ "unit": {
68
+ "type": "string",
69
+ "enum": ["celsius", "fahrenheit"],
70
+ "description": "The unit of temperature to return"
71
+ }
72
+ },
73
+ "required": ["location"]
74
+ }
75
+ }
76
+
77
+ search_api = {
78
+ "name": "search",
79
+ "description": "Search for information on the internet",
80
+ "parameters": {
81
+ "type": "object",
82
+ "properties": {
83
+ "query": {
84
+ "type": "string",
85
+ "description": "The search query, e.g. 'latest news on AI'"
86
+ }
87
+ },
88
+ "required": ["query"]
89
+ }
90
+ }
91
+
92
+ tools = [get_weather_api, search_api]
93
+
94
+ query = "What's the weather like in New York in fahrenheit?"
95
+
96
+ user_prompt = prompt.render(tools=json.dumps(tools), query=query)
97
+
98
+ messages=[
99
+ {"role": "system", "content": SYSTEM_PROMPT},
100
+ { 'role': 'user', 'content': user_prompt}
101
+ ]
102
+ inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
103
+
104
+ outputs = model.generate(inputs, max_new_tokens=512, do_sample=False, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id)
105
+ result = tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=False)
106
+
107
+ pattern = r'<tool_call>(.*?)</tool_call>'
108
+ import re
109
+ matches = re.findall(pattern, result, re.DOTALL)
110
+ response = json.loads(matches[0])
111
+ # [{'name': 'get_weather', 'arguments': {'location': 'New York', 'unit': 'fahrenheit'}}]
112
+ ````
113
+
114
+ Example response with no tools available
115
+
116
  ```python
117
+ tools = "[]"
118
+
119
+ query = "What's the weather like in New York in fahrenheit?"
120
+
121
+ user_prompt = prompt.render(tools=json.dumps(tools), query=query)
122
+
123
+ messages=[
124
+ {"role": "system", "content": SYSTEM_PROMPT},
125
+ { 'role': 'user', 'content': user_prompt}
126
+ ]
127
+ inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
128
+
129
+ outputs = model.generate(inputs, max_new_tokens=512, do_sample=False, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id)
130
+ result = tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True)
131
+ # 'The query cannot be answered, no tools were provided.'
132
+ ```
133
+
134
+ Example when a wrong tool is informed:
135
+
136
+ ```python
137
+ cut_number = {
138
+ 'type': 'function',
139
+ 'function': {
140
+ 'name': 'cut_number',
141
+ 'description': 'Returns the value `number` if it is greater than or equal to `threshold`, otherwise returns the value `threshold`.',
142
+ 'parameters': {
143
+ 'type': 'object',
144
+ 'properties': {'number': {'type': 'number', 'description': 'The number to compare.'}},
145
+ 'required': ['number']
146
+ }
147
+ }
148
+ }
149
+
150
+ tools = [cut_number]
151
+
152
+ query = "What's the weather like in New York in fahrenheit?"
153
+
154
+ user_prompt = prompt.render(tools=json.dumps(tools), query=query)
155
+
156
+ messages=[
157
+ {"role": "system", "content": SYSTEM_PROMPT},
158
+ { 'role': 'user', 'content': user_prompt}
159
+ ]
160
+ inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
161
 
162
+ outputs = model.generate(inputs, max_new_tokens=512, do_sample=False, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id)
163
+ result = tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True)
164
+ # "The query cannot be answered with the provided tools. The query lacks the parameters required by the function. Please provide the parameters, and I'll be happy to assist."
 
165
  ```
166
 
167
  ## Training procedure