Views
No views yet
1npm install @huggingface/transformers
21import { AutoModelForCausalLM, AutoTokenizer } from '@huggingface/transformers';
2
3const model_id = "LemOneLabs/Granite-4.1-8b-Onnx";
4const tokenizer = await AutoTokenizer.from_pretrained(model_id);
5
6// Load model. Uses WASM by default. For hardware acceleration, specify { device: 'webgpu' }
7const model = await AutoModelForCausalLM.from_pretrained(model_id, {
8 device: 'webgpu' // Drop this options object if WebGPU is not supported in your environment
9});
10
11// change input text as desired
12const chat = [
13 { role: "user", content: "Please list one IBM Research laboratory located in the United States. You should only output its name and location." }
14];
15
16const input_text = tokenizer.apply_chat_template(chat, {
17 tokenize: false,
18 add_generation_prompt: true
19});
20
21// tokenize the text
22const input_tokens = tokenizer(input_text);
23
24// generate output tokens
25const output = await model.generate({
26 ...input_tokens,
27 max_new_tokens: 100
28});
29
30// decode output tokens into text
31const decoded = tokenizer.batch_decode(output, { skip_special_tokens: false });
32
33// print output
34console.log(decoded[0]);
351<|start_of_role|>user<|end_of_role|>Please list one IBM Research laboratory located in the United States. You should only output its name and location.<|end_of_text|>
2<|start_of_role|>assistant<|end_of_role|>IBM Almaden Research Laboratory, San Jose, California, United States.<|end_of_text|>
31import { AutoModelForCausalLM, AutoTokenizer } from '@huggingface/transformers';
2
3const model_id = "ibm-granite/granite-4.1-8b";
4const tokenizer = await AutoTokenizer.from_pretrained(model_id);
5
6// Load model. Uses WASM by default. For hardware acceleration, specify { device: 'webgpu' }
7const model = await AutoModelForCausalLM.from_pretrained(model_id, {
8 device: 'webgpu' // Drop this options object if WebGPU is not supported in your environment
9});
10
11const tools = [
12 {
13 type: "function",
14 function: {
15 name: "get_current_weather",
16 description: "Get the current weather for a specified city.",
17 parameters: {
18 type: "object",
19 properties: {
20 city: {
21 type: "string",
22 description: "Name of the city"
23 }
24 },
25 required: ["city"]
26 }
27 }
28 }
29];
30
31// change input text as desired
32const chat = [
33 { role: "user", content: "What's the weather like in Boston right now?" },
34];
35
36const input_text = tokenizer.apply_chat_template(chat, {
37 tokenize: false,
38 add_generation_prompt: true,
39 tools: tools
40});
41
42// tokenize the text
43const input_tokens = tokenizer(input_text);
44
45// generate output tokens
46const output = await model.generate({
47 ...input_tokens,
48 max_new_tokens: 100
49});
50
51// decode output tokens into text
52const decoded = tokenizer.batch_decode(output, { skip_special_tokens: false });
53
54// print output
55console.log(decoded[0]);
561<|start_of_role|>system<|end_of_role|>You are a helpful assistant with access to the following tools. You may call one or more tools to assist with the user query.
2You are provided with function signatures within <tools></tools> XML tags:
3<tools>
4{"type": "function", "function": {"name": "get_current_weather", "description": "Get the current weather for a specified city.", "parameters": {"type": "object", "properties": {"city": {"type": "string", "description": "Name of the city"}}, "required": ["city"]}}}
5</tools>
6For each tool call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
7<tool_call>
8{"name": <function-name>, "arguments": <args-json-object>}
9</tool_call>. If a tool does not exist in the provided list of tools, notify the user that you do not have the ability to fulfill the request.<|end_of_text|>
10<|start_of_role|>user<|end_of_role|>What's the weather like in Boston right now?<|end_of_text|>
11<|start_of_role|>assistant<|end_of_role|><tool_call>
12{"name": "get_current_weather", "arguments": {"city": "Boston"}}
13</tool_call><|end_of_text|>
14