mishig HF Staff commited on
Commit
69f4342
·
1 Parent(s): fd1d061

tool usage example

Browse files
src/lib/JsonEditor/JsonEditor.svelte CHANGED
@@ -9,14 +9,16 @@
9
  import type { FormattedChatTemplate } from '../ChatTemplateViewer/types';
10
  import { getExampleHelloWorld } from '$lib/example-inputs/helloWorld';
11
  import { onMount } from 'svelte';
 
12
 
13
  export let content: Record<string, unknown> = {};
14
  export let error = '';
15
  export let selectedTemplate: FormattedChatTemplate | undefined = undefined;
16
 
17
  let value = JSON5.stringify(content, null, 2);
18
-
19
  let wrapLines = true;
 
 
20
 
21
  async function handleUpdateEditor(e: CustomEvent<string>) {
22
  const currentCode = e.detail;
@@ -103,14 +105,31 @@
103
  };
104
  }
105
 
 
 
 
 
 
 
 
 
 
 
106
  onMount(() => {
107
  if (selectedTemplate) {
108
- console.log('or here');
109
  const exampleHelloWorld = getExampleHelloWorld(selectedTemplate.template);
110
  if (exampleHelloWorld) {
111
- content = exampleHelloWorld;
112
- value = JSON5.stringify(content, null, 2);
113
  }
 
 
 
 
 
 
 
 
 
114
  }
115
  });
116
  </script>
@@ -122,10 +141,10 @@
122
  >
123
  JSON Input
124
 
125
- <select class="ml-auto rounded border px-1 py-0.5 text-sm">
126
- <option value="hello_world">hello world example</option>
127
- <option value="tool_usage" disabled>tool usage example</option>
128
- <option value="reasoning" disabled>reasoning example</option>
129
  </select>
130
  </div>
131
  <div class="flex items-center border-b px-3 py-2">
 
9
  import type { FormattedChatTemplate } from '../ChatTemplateViewer/types';
10
  import { getExampleHelloWorld } from '$lib/example-inputs/helloWorld';
11
  import { onMount } from 'svelte';
12
+ import { getExampleToolUsage } from '$lib/example-inputs/toolUsage';
13
 
14
  export let content: Record<string, unknown> = {};
15
  export let error = '';
16
  export let selectedTemplate: FormattedChatTemplate | undefined = undefined;
17
 
18
  let value = JSON5.stringify(content, null, 2);
 
19
  let wrapLines = true;
20
+ let exampleInputs: { id: string; label: string; content: Record<string, unknown> }[] = [];
21
+ let selectedExampleInputId = '';
22
 
23
  async function handleUpdateEditor(e: CustomEvent<string>) {
24
  const currentCode = e.detail;
 
105
  };
106
  }
107
 
108
+ function handleExampleInputChange(e: Event) {
109
+ const target = e.target as HTMLSelectElement;
110
+ const selectedId = target.value;
111
+ const selectedExampleInput = exampleInputs.find((exampleInput) => exampleInput.id === selectedId);
112
+ if (selectedExampleInput) {
113
+ content = selectedExampleInput.content;
114
+ value = JSON5.stringify(content, null, 2);
115
+ }
116
+ }
117
+
118
  onMount(() => {
119
  if (selectedTemplate) {
 
120
  const exampleHelloWorld = getExampleHelloWorld(selectedTemplate.template);
121
  if (exampleHelloWorld) {
122
+ exampleInputs = [...exampleInputs, {id: 'hello-world', label: 'hello world example', content: exampleHelloWorld}]
 
123
  }
124
+
125
+ const exampleToolUsage = getExampleToolUsage(selectedTemplate.template);
126
+ if (exampleToolUsage) {
127
+ exampleInputs = [...exampleInputs, {id: 'tool-usage', label: 'tool usage example', content: exampleToolUsage}]
128
+ }
129
+
130
+ content = exampleInputs.at(-1)?.content ?? {};
131
+ selectedExampleInputId = exampleInputs.at(-1)?.id ?? '';
132
+ value = JSON5.stringify(content, null, 2);
133
  }
134
  });
135
  </script>
 
141
  >
142
  JSON Input
143
 
144
+ <select class="ml-auto rounded border px-1 py-0.5 text-sm" on:change={handleExampleInputChange}>
145
+ {#each exampleInputs as exampleInput}
146
+ <option value={exampleInput.id} selected={exampleInput.id === selectedExampleInputId}>{exampleInput.label}</option>
147
+ {/each}
148
  </select>
149
  </div>
150
  <div class="flex items-center border-b px-3 py-2">
src/lib/example-inputs/toolUsage.ts ADDED
@@ -0,0 +1,396 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Template } from '@huggingface/jinja';
2
+
3
+ const variations = {
4
+ variation1_qwen_xml_style: {
5
+ description:
6
+ "This variation reflects how Qwen-like models might structure tool definitions in the system message using XML-like tags and how tool responses are often wrapped. The assistant's tool invocation uses a standard `tool_calls` array which the template would then format into the model's expected string.",
7
+ example: {
8
+ messages: [
9
+ {
10
+ role: 'system',
11
+ content:
12
+ 'You are a helpful assistant that can use tools to get information for the user.\n\n# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>\n{"name": "get_weather", "description": "Get current weather information for a location", "parameters": {"type": "object", "properties": {"location": {"type": "string", "description": "The city and state, e.g. San Francisco, CA"}, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"], "description": "The unit of temperature to use"}}, "required": ["location"]}}\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags.'
13
+ },
14
+ {
15
+ role: 'user',
16
+ content: "What's the weather like in New York?"
17
+ },
18
+ {
19
+ role: 'assistant',
20
+ content:
21
+ "<think>\nThe user is asking about the weather in New York. I should use the weather tool to get this information.\n</think>\nI'll check the current weather in New York for you.",
22
+ tool_calls: [
23
+ {
24
+ function: {
25
+ name: 'get_weather',
26
+ arguments: {
27
+ location: 'New York',
28
+ unit: 'celsius'
29
+ }
30
+ }
31
+ }
32
+ ]
33
+ },
34
+ {
35
+ role: 'user',
36
+ content:
37
+ '<tool_response>\n{"temperature": 22, "condition": "Sunny", "humidity": 45, "wind_speed": 10}\n</tool_response>'
38
+ },
39
+ {
40
+ role: 'assistant',
41
+ content:
42
+ "The weather in New York is currently sunny with a temperature of 22°C. The humidity is at 45% with a wind speed of 10 km/h. It's a great day to be outside!"
43
+ },
44
+ {
45
+ role: 'user',
46
+ content: 'Thanks! What about Boston?'
47
+ }
48
+ ],
49
+ tools: [
50
+ {
51
+ name: 'get_weather',
52
+ description: 'Get current weather information for a location',
53
+ parameters: {
54
+ type: 'object',
55
+ properties: {
56
+ location: {
57
+ type: 'string',
58
+ description: 'The city and state, e.g. San Francisco, CA'
59
+ },
60
+ unit: {
61
+ type: 'string',
62
+ enum: ['celsius', 'fahrenheit'],
63
+ description: 'The unit of temperature to use'
64
+ }
65
+ },
66
+ required: ['location']
67
+ }
68
+ }
69
+ ],
70
+ add_generation_prompt: true
71
+ }
72
+ },
73
+ variation3_deepseek_special_tags_style: {
74
+ description:
75
+ 'This variation reflects DeepSeek-like models using specialized tags for tool calls. The `tool_calls` array in the assistant message would contain arguments as a JSON string, which the template then formats with specific tags and markdown.',
76
+ example: {
77
+ messages: [
78
+ {
79
+ role: 'system',
80
+ content: 'You are a helpful assistant.'
81
+ },
82
+ {
83
+ role: 'user',
84
+ content: "What's the weather like in New York?"
85
+ },
86
+ {
87
+ role: 'assistant',
88
+ content:
89
+ "<think>\nThe user is asking about the weather in New York. I should use the weather tool to get this information.\n</think>\nI'll check the current weather in New York for you.",
90
+ tool_calls: [
91
+ {
92
+ type: 'function',
93
+ function: {
94
+ name: 'get_weather',
95
+ arguments: '{"location": "New York", "unit": "celsius"}'
96
+ }
97
+ }
98
+ ]
99
+ },
100
+ {
101
+ role: 'tool',
102
+ content: '{"temperature": 22, "condition": "Sunny", "humidity": 45, "wind_speed": 10}'
103
+ },
104
+ {
105
+ role: 'assistant',
106
+ content:
107
+ "The weather in New York is currently sunny with a temperature of 22°C. The humidity is at 45% with a wind speed of 10 km/h. It's a great day to be outside!"
108
+ },
109
+ {
110
+ role: 'user',
111
+ content: 'Thanks! What about Boston?'
112
+ }
113
+ ],
114
+ tools: [
115
+ {
116
+ name: 'get_weather',
117
+ description: 'Get current weather information for a location',
118
+ parameters: {
119
+ type: 'object',
120
+ properties: {
121
+ location: {
122
+ type: 'string',
123
+ description: 'The city and state, e.g. San Francisco, CA'
124
+ },
125
+ unit: {
126
+ type: 'string',
127
+ enum: ['celsius', 'fahrenheit'],
128
+ description: 'The unit of temperature to use'
129
+ }
130
+ },
131
+ required: ['location']
132
+ }
133
+ }
134
+ ],
135
+ add_generation_prompt: true
136
+ }
137
+ },
138
+ variation4_mistral_tags_style: {
139
+ description:
140
+ "This variation demonstrates the Mistral-like approach using `[AVAILABLE_TOOLS]` (implicitly handled by the template from the 'tools' array), `[TOOL_CALLS]` with IDs, and `[TOOL_RESULTS]`.",
141
+ example: {
142
+ messages: [
143
+ {
144
+ role: 'system',
145
+ content: 'You are a helpful assistant that can use tools to get information for the user.'
146
+ },
147
+ {
148
+ role: 'user',
149
+ content: "What's the weather like in New York?"
150
+ },
151
+ {
152
+ role: 'assistant',
153
+ content:
154
+ "<think>\nThe user is asking about the weather in New York. I should use the weather tool to get this information.\n</think>\nI'll check the current weather in New York for you.",
155
+ tool_calls: [
156
+ {
157
+ id: 'call_weather_nyc_001',
158
+ function: {
159
+ name: 'get_weather',
160
+ arguments: {
161
+ location: 'New York',
162
+ unit: 'celsius'
163
+ }
164
+ }
165
+ }
166
+ ]
167
+ },
168
+ {
169
+ role: 'tool',
170
+ tool_call_id: 'call_weather_nyc_001',
171
+ content: '{"temperature": 22, "condition": "Sunny", "humidity": 45, "wind_speed": 10}'
172
+ },
173
+ {
174
+ role: 'assistant',
175
+ content:
176
+ "The weather in New York is currently sunny with a temperature of 22°C. The humidity is at 45% with a wind speed of 10 km/h. It's a great day to be outside!"
177
+ },
178
+ {
179
+ role: 'user',
180
+ content: 'Thanks! What about Boston?'
181
+ }
182
+ ],
183
+ tools: [
184
+ {
185
+ name: 'get_weather',
186
+ description: 'Get current weather information for a location',
187
+ parameters: {
188
+ type: 'object',
189
+ properties: {
190
+ location: {
191
+ type: 'string',
192
+ description: 'The city and state, e.g. San Francisco, CA'
193
+ },
194
+ unit: {
195
+ type: 'string',
196
+ enum: ['celsius', 'fahrenheit'],
197
+ description: 'The unit of temperature to use'
198
+ }
199
+ },
200
+ required: ['location']
201
+ }
202
+ }
203
+ ],
204
+ add_generation_prompt: true
205
+ }
206
+ },
207
+ variation5_generic_openai_anthropic_style: {
208
+ description:
209
+ 'This is the generic style, often compatible with OpenAI and Anthropic models, similar to your provided example. It serves as a baseline.',
210
+ example: {
211
+ messages: [
212
+ {
213
+ role: 'system',
214
+ content: 'You are a helpful assistant that can use tools to get information for the user.'
215
+ },
216
+ {
217
+ role: 'user',
218
+ content: "What's the weather like in New York?"
219
+ },
220
+ {
221
+ role: 'assistant',
222
+ content:
223
+ "<think>\nThe user is asking about the weather in New York. I should use the weather tool to get this information.\n</think>\nI'll check the current weather in New York for you.",
224
+ tool_calls: [
225
+ {
226
+ function: {
227
+ name: 'get_weather',
228
+ arguments: {
229
+ location: 'New York',
230
+ unit: 'celsius'
231
+ }
232
+ }
233
+ }
234
+ ]
235
+ },
236
+ {
237
+ role: 'tool',
238
+ content: '{"temperature": 22, "condition": "Sunny", "humidity": 45, "wind_speed": 10}'
239
+ },
240
+ {
241
+ role: 'assistant',
242
+ content:
243
+ "The weather in New York is currently sunny with a temperature of 22°C. The humidity is at 45% with a wind speed of 10 km/h. It's a great day to be outside!"
244
+ },
245
+ {
246
+ role: 'user',
247
+ content: 'Thanks! What about Boston?'
248
+ }
249
+ ],
250
+ tools: [
251
+ {
252
+ name: 'get_weather',
253
+ description: 'Get current weather information for a location',
254
+ parameters: {
255
+ type: 'object',
256
+ properties: {
257
+ location: {
258
+ type: 'string',
259
+ description: 'The city and state, e.g. San Francisco, CA'
260
+ },
261
+ unit: {
262
+ type: 'string',
263
+ enum: ['celsius', 'fahrenheit'],
264
+ description: 'The unit of temperature to use'
265
+ }
266
+ },
267
+ required: ['location']
268
+ }
269
+ }
270
+ ],
271
+ add_generation_prompt: true
272
+ }
273
+ },
274
+ variation6_granite_style: {
275
+ description:
276
+ "This variation reflects Granite-like models where the tool call might be embedded directly in the assistant's content string, prefixed by a special tag like `<|tool_call|>`. The `available_tools` would be passed to the template engine.",
277
+ example: {
278
+ messages: [
279
+ {
280
+ role: 'system',
281
+ content:
282
+ "You are Granite, developed by IBM. You are a helpful assistant with access to the following tools. When a tool is required to answer the user's query, respond only with <|tool_call|> followed by a JSON list of tools used."
283
+ },
284
+ {
285
+ role: 'user',
286
+ content: "What's the weather like in New York?"
287
+ },
288
+ {
289
+ role: 'assistant',
290
+ content:
291
+ '<think>\nThe user is asking about the weather in New York. I should use the weather tool to get this information.\n</think>\nI\'ll check the current weather in New York for you.\n<|tool_call|>[{"name": "get_weather", "arguments": {"location": "New York", "unit": "celsius"}}]'
292
+ },
293
+ {
294
+ role: 'tool',
295
+ content: '{"temperature": 22, "condition": "Sunny", "humidity": 45, "wind_speed": 10}'
296
+ },
297
+ {
298
+ role: 'assistant',
299
+ content:
300
+ "The weather in New York is currently sunny with a temperature of 22°C. The humidity is at 45% with a wind speed of 10 km/h. It's a great day to be outside!"
301
+ },
302
+ {
303
+ role: 'user',
304
+ content: 'Thanks! What about Boston?'
305
+ }
306
+ ],
307
+ tools: [
308
+ {
309
+ name: 'get_weather',
310
+ description: 'Get current weather information for a location',
311
+ parameters: {
312
+ type: 'object',
313
+ properties: {
314
+ location: {
315
+ type: 'string',
316
+ description: 'The city and state, e.g. San Francisco, CA'
317
+ },
318
+ unit: {
319
+ type: 'string',
320
+ enum: ['celsius', 'fahrenheit'],
321
+ description: 'The unit of temperature to use'
322
+ }
323
+ },
324
+ required: ['location']
325
+ }
326
+ }
327
+ ],
328
+ add_generation_prompt: true
329
+ }
330
+ },
331
+ variation2_llama3_style: {
332
+ description:
333
+ "This variation shows how Llama-3.1-like models might handle tool definitions passed within the first user message. The assistant's invocation uses a standard `tool_calls` array.",
334
+ example: {
335
+ messages: [
336
+ {
337
+ role: 'system',
338
+ content:
339
+ 'Environment: ipython\nCutting Knowledge Date: December 2023\nToday Date: 2025-05-14\n\nYou are a helpful assistant.'
340
+ },
341
+ {
342
+ role: 'user',
343
+ content:
344
+ 'Given the following functions, please respond with a JSON for a function call with its proper arguments that best answers the given prompt.\n\nRespond in the format {"name": function name, "parameters": dictionary of argument name and its value}.\nDo not use variables.\n\n[\n {\n "name": "get_weather",\n "description": "Get current weather information for a location",\n "parameters": {\n "type": "object",\n "properties": {\n "location": {\n "type": "string",\n "description": "The city and state, e.g. San Francisco, CA"\n },\n "unit": {\n "type": "string",\n "enum": ["celsius", "fahrenheit"],\n "description": "The unit of temperature to use"\n }\n },\n "required": ["location"]\n }\n }\n]\n\nWhat\'s the weather like in New York?'
345
+ },
346
+ {
347
+ role: 'assistant',
348
+ content:
349
+ "<think>\nThe user is asking about the weather in New York. I should use the weather tool to get this information.\n</think>\nI'll check the current weather in New York for you.",
350
+ tool_calls: [
351
+ {
352
+ function: {
353
+ name: 'get_weather',
354
+ arguments: {
355
+ location: 'New York',
356
+ unit: 'celsius'
357
+ }
358
+ }
359
+ }
360
+ ]
361
+ },
362
+ {
363
+ role: 'tool',
364
+ content: '{"temperature": 22, "condition": "Sunny", "humidity": 45, "wind_speed": 10}'
365
+ },
366
+ {
367
+ role: 'assistant',
368
+ content:
369
+ "The weather in New York is currently sunny with a temperature of 22°C. The humidity is at 45% with a wind speed of 10 km/h. It's a great day to be outside!"
370
+ },
371
+ {
372
+ role: 'user',
373
+ content: 'Thanks! What about Boston?'
374
+ }
375
+ ],
376
+ tools: null,
377
+ add_generation_prompt: true
378
+ }
379
+ }
380
+ };
381
+
382
+ export function getExampleToolUsage(templateStr: string): Record<string, unknown> | undefined {
383
+ const template = new Template(templateStr);
384
+ for(const variation of Object.values(variations)) {
385
+ try{
386
+ const variationRendered = template.render(variation.example);
387
+ if (variationRendered.includes('get_weather')) {
388
+ return variation.example;
389
+ }
390
+ }
391
+ catch(e){
392
+ console.error(e);
393
+ }
394
+ }
395
+ return undefined;
396
+ }