Update README.md
Browse files
README.md
CHANGED
@@ -112,7 +112,10 @@ If you haven't already, you can install the [Transformers.js](https://huggingfac
|
|
112 |
npm i @huggingface/transformers
|
113 |
```
|
114 |
|
115 |
-
|
|
|
|
|
|
|
116 |
```js
|
117 |
import { pipeline, TextStreamer } from "@huggingface/transformers";
|
118 |
|
@@ -133,12 +136,77 @@ const messages = [
|
|
133 |
const output = await generator(messages, {
|
134 |
max_new_tokens: 512,
|
135 |
do_sample: false,
|
136 |
-
streamer: new TextStreamer(generator.tokenizer, { skip_prompt: true, skip_special_tokens: true}),
|
137 |
});
|
138 |
console.log(output[0].generated_text.at(-1).content);
|
139 |
// The capital of France is Paris.
|
140 |
```
|
141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
### ONNXRuntime
|
143 |
|
144 |
```py
|
|
|
112 |
npm i @huggingface/transformers
|
113 |
```
|
114 |
|
115 |
+
|
116 |
+
|
117 |
+
**Example**: Basic example
|
118 |
+
|
119 |
```js
|
120 |
import { pipeline, TextStreamer } from "@huggingface/transformers";
|
121 |
|
|
|
136 |
const output = await generator(messages, {
|
137 |
max_new_tokens: 512,
|
138 |
do_sample: false,
|
139 |
+
streamer: new TextStreamer(generator.tokenizer, { skip_prompt: true, skip_special_tokens: true }),
|
140 |
});
|
141 |
console.log(output[0].generated_text.at(-1).content);
|
142 |
// The capital of France is Paris.
|
143 |
```
|
144 |
|
145 |
+
|
146 |
+
**Example**: Tool calling
|
147 |
+
|
148 |
+
```js
|
149 |
+
import { AutoModelForCausalLM, AutoTokenizer, TextStreamer } from "@huggingface/transformers";
|
150 |
+
|
151 |
+
// Load tokenizer and model
|
152 |
+
const model_id = "onnx-community/LFM2-700M-ONNX";
|
153 |
+
const tokenizer = await AutoTokenizer.from_pretrained(model_id);
|
154 |
+
const model = await AutoModelForCausalLM.from_pretrained(
|
155 |
+
model_id, { dtype: "q4", device: "webgpu" },
|
156 |
+
);
|
157 |
+
|
158 |
+
// Define tools and messages
|
159 |
+
const tools = [
|
160 |
+
{
|
161 |
+
name: "get_weather",
|
162 |
+
description: "Get current weather information for a location",
|
163 |
+
parameters: {
|
164 |
+
type: "object",
|
165 |
+
properties: {
|
166 |
+
location: {
|
167 |
+
type: "string",
|
168 |
+
description: "The city and state, e.g. San Francisco, CA",
|
169 |
+
},
|
170 |
+
unit: {
|
171 |
+
type: "string",
|
172 |
+
enum: ["celsius", "fahrenheit"],
|
173 |
+
description: "The unit of temperature to use",
|
174 |
+
},
|
175 |
+
},
|
176 |
+
required: ["location"],
|
177 |
+
},
|
178 |
+
},
|
179 |
+
];
|
180 |
+
const messages = [
|
181 |
+
{
|
182 |
+
role: "user",
|
183 |
+
content: "What's the weather like in New York?"
|
184 |
+
},
|
185 |
+
];
|
186 |
+
|
187 |
+
// Prepare inputs
|
188 |
+
const input = tokenizer.apply_chat_template(messages, {
|
189 |
+
tools,
|
190 |
+
add_generation_prompt: true,
|
191 |
+
return_dict: true,
|
192 |
+
});
|
193 |
+
|
194 |
+
// Generate output
|
195 |
+
const sequences = await model.generate({
|
196 |
+
...input,
|
197 |
+
max_new_tokens: 512,
|
198 |
+
do_sample: false,
|
199 |
+
streamer: new TextStreamer(tokenizer, { skip_prompt: true, skip_special_tokens: false }),
|
200 |
+
});
|
201 |
+
|
202 |
+
// Decode and print the generated text
|
203 |
+
const response = tokenizer.batch_decode(
|
204 |
+
sequences.slice(null, [input.input_ids.dims[1], null]),
|
205 |
+
{ skip_special_tokens: true },
|
206 |
+
);
|
207 |
+
console.log(response[0]); // [get_weather(location="New York", unit="fahrenheit")]
|
208 |
+
```
|
209 |
+
|
210 |
### ONNXRuntime
|
211 |
|
212 |
```py
|