run very good
%%writefile 4.js
import { pipeline, TextStreamer } from "@huggingface/transformers";
// Define the list of messages
const messages = [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Write me a poem about Machine Learning." },
];
// Create a text generation pipeline
// Switching to 'Xenova/Qwen1.5-0.5B-Chat' which is optimized for this library,
// and removing the explicit 'q4f16' dtype to use the default stable quantization.
const generator = await pipeline(
"text-generation",
"Xenova/Qwen1.5-0.5B-Chat" // <--- CHANGED MODEL
);
// Generate a response with streaming enabled
const output = await generator(messages, {
max_new_tokens: 512,
do_sample: false,
// The structure of the output changes slightly when using a Xenova chat model
// but the streamer logic remains the same for how it's generated.
streamer: new TextStreamer(generator.tokenizer, { skip_prompt: true, skip_special_tokens: true}),
});
// Access the content of the last generated text segment.
// The output structure for streaming is usually: [{ generated_text: [{ content: '...' }] }]
// When using streamer, the generated_text is an array of messages/segments.
console.log(output[0].generated_text.at(-1).content);
!node 4.js
(node:11906) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///content/4.js is not specified and it doesn't parse as CommonJS.
Reparsing as ES module because module syntax was detected. This incurs a performance overhead.
To eliminate this warning, add "type": "module" to /content/package.json.
(Use node --trace-warnings ... to show where the warning was created)
dtype not specified for "model". Using the default dtype (fp32) for this device (cpu).
Machine learning, oh how it's grown,
A tool that helps us make decisions,
With algorithms and models so fine,
It learns from data with ease.
From image recognition to natural language processing,
It can analyze and understand,
And use that knowledge to make sense,
Of complex situations we face.
It's like a friend who always knows,
When to help or when to be,
And helps us make decisions that're right,
For our future and our well-being.
So let's embrace this technology,
And see what it can do,
With machine learning by our side,
We'll make the world a better place.
Machine learning, oh how it's grown,
A tool that helps us make decisions,
With algorithms and models so fine,
It learns from data with ease.
From image recognition to natural language processing,
It can analyze and understand,
And use that knowledge to make sense,
Of complex situations we face.
It's like a friend who always knows,
When to help or when to be,
And helps us make decisions that're right,
For our future and our well-being.
So let's embrace this technology,
And see what it can do,
With machine learning by our side,
We'll make the world a better place.