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