IIC
/

gonzalo-santamaria-iic commited on
Commit
4068d41
·
verified ·
1 Parent(s): cf25684

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +67 -7
README.md CHANGED
@@ -137,28 +137,29 @@ For a better experience, we recommend to use [the following generation parameter
137
  ### Tool Use
138
 
139
  ```python
140
- def get_current_weather(location: str, date: str) -> float:
141
  """
142
- Obtener datos del tiempo de una localización.
143
 
144
  Args:
145
  location: La locaización, con el siguiente formato: "Ciudad, País."
146
- date: La fecha, en el formato AAAA-MM-DD.
147
  Returns:
148
- El tiempo en dicha localización.
149
  """
150
- return {"temperatura": 22, "cielo": "nublado", "probabilidad de lluvias": "60%"}
 
151
 
152
  messages = [
153
- {"role": "user", "content": "Este fin de semana quiero visitar Madrid, y no se qué ropa llevarme. ¿Podrías decirme qué tal va a hacer? Es el puente del 6 de diciembre de 2024."}
154
  ]
155
 
156
  text = tokenizer.apply_chat_template(
157
  messages,
158
  tokenize=False,
159
- tools=[get_current_weather],
160
  add_generation_prompt=True
161
  )
 
162
  model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
163
 
164
  generated_ids = model.generate(
@@ -170,10 +171,69 @@ generated_ids = [
170
  ]
171
 
172
  response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
 
173
  ```
174
 
175
  Check the [tool use documentation](https://huggingface.co/docs/transformers/main/chat_templating#advanced-tool-use--function-calling) from HuggingFace for more information.
176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  ## Training Details
178
 
179
  ### Training Data
 
137
  ### Tool Use
138
 
139
  ```python
140
+ def obtener_temperatura_actual(location: str) -> float:
141
  """
142
+ Obtener la temperatura actual de una localización.
143
 
144
  Args:
145
  location: La locaización, con el siguiente formato: "Ciudad, País."
 
146
  Returns:
147
+ El tiempo en dicha localización, en grados Celsius.
148
  """
149
+ return 22.
150
+
151
 
152
  messages = [
153
+ {"role": "user", "content": "¿Cuál es el tiempo en Madrid ahora mismo?"}
154
  ]
155
 
156
  text = tokenizer.apply_chat_template(
157
  messages,
158
  tokenize=False,
159
+ tools=[obtener_temperatura_actual],
160
  add_generation_prompt=True
161
  )
162
+
163
  model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
164
 
165
  generated_ids = model.generate(
 
171
  ]
172
 
173
  response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
174
+ print(response)
175
  ```
176
 
177
  Check the [tool use documentation](https://huggingface.co/docs/transformers/main/chat_templating#advanced-tool-use--function-calling) from HuggingFace for more information.
178
 
179
+ If the model generates a tool call, you should add it to the chat like so:
180
+
181
+ ```python
182
+ import re
183
+ import json
184
+
185
+ tools = {
186
+ "obtener_temperatura_actual" : obtener_temperatura_actual,
187
+ }
188
+
189
+ tool_call = re.search(
190
+ r"<tool_call>\s*(\{.*?\})\s*</tool_call>",
191
+ response,
192
+ )
193
+ tool_call = json.loads(tool_call.group(1))
194
+
195
+ # Add tool metadata to messages
196
+ messages.append(
197
+ {
198
+ "role": "assistant",
199
+ "tool_calls": [{"type": "function", "function": tool_call}],
200
+ },
201
+ )
202
+
203
+ # Add tool result to messages
204
+ messages.append(
205
+ {
206
+ "role": "tool",
207
+ "name": tool_call["name"],
208
+ "content": tools[tool_call["name"]](**tool_call["arguments"]),
209
+ },
210
+ )
211
+ ```
212
+
213
+ The above code is intended only for when the model generates a function call, but the same logic can be used if several functions are called at the same time. After that, you can continue to generate messages as normal:
214
+
215
+ ```python
216
+ text = tokenizer.apply_chat_template(
217
+ messages,
218
+ tokenize=False,
219
+ tools=[obtener_temperatura_actual],
220
+ add_generation_prompt=True
221
+ )
222
+
223
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
224
+
225
+ generated_ids = model.generate(
226
+ **model_inputs,
227
+ max_new_tokens=1024
228
+ )
229
+ generated_ids = [
230
+ output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
231
+ ]
232
+
233
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
234
+ print(response)
235
+ ```
236
+
237
  ## Training Details
238
 
239
  ### Training Data