|
|
|
|
|
|
|
|
|
|
|
|
|
|
import {HumanMessage, SystemMessage, LlamaCppLLM} from '../../../../src/index.js';
|
|
|
|
|
|
async function exercise3() {
|
|
|
console.log('=== Exercise 3: Streaming Responses ===\n');
|
|
|
|
|
|
const llm = new LlamaCppLLM({
|
|
|
modelPath: './models/Meta-Llama-3.1-8B-Instruct-Q5_K_S.gguf',
|
|
|
temperature: 0.7,
|
|
|
maxTokens: 200
|
|
|
});
|
|
|
|
|
|
try {
|
|
|
|
|
|
console.log('Part 1: Basic streaming');
|
|
|
console.log('Question: Tell me a long fun fact about space.\n');
|
|
|
console.log('Response: ');
|
|
|
|
|
|
for await (const chunk of llm.stream("Tell me a long fun fact about space.")) {
|
|
|
process.stdout.write(chunk.content);
|
|
|
}
|
|
|
|
|
|
console.log('\n');
|
|
|
|
|
|
|
|
|
console.log('Part 2: Streaming with progress indicator');
|
|
|
console.log('Question: Explain what a black hole is in 2-3 sentences.\n');
|
|
|
|
|
|
let charCount = 0;
|
|
|
console.log('Progress: ');
|
|
|
console.log('Response: ');
|
|
|
|
|
|
for await (const chunk of llm.stream("Explain what a black hole is in 2-3 sentences.")) {
|
|
|
process.stdout.write(chunk.content);
|
|
|
charCount += chunk.content.length;
|
|
|
}
|
|
|
|
|
|
console.log(`\n\nTotal characters streamed: ${charCount}`);
|
|
|
console.log();
|
|
|
|
|
|
|
|
|
console.log('Part 3: Collecting full response from stream');
|
|
|
|
|
|
const messages = [
|
|
|
new SystemMessage("You are a helpful assistant"),
|
|
|
new HumanMessage("What are the three primary colors? Answer briefly.")
|
|
|
];
|
|
|
|
|
|
let fullResponse = '';
|
|
|
for await (const chunk of llm.stream(messages)) {
|
|
|
fullResponse += chunk.content;
|
|
|
}
|
|
|
|
|
|
console.log('Full response:', fullResponse);
|
|
|
console.log();
|
|
|
|
|
|
|
|
|
console.log('Part 4: Streaming vs Regular invoke');
|
|
|
const question = "What is JavaScript? Answer in one sentence.";
|
|
|
|
|
|
|
|
|
console.log('Streaming:');
|
|
|
const streamStart = Date.now();
|
|
|
let streamedText = '';
|
|
|
for await (const chunk of llm.stream(question)) {
|
|
|
streamedText += chunk.content;
|
|
|
}
|
|
|
const streamTime = Date.now() - streamStart;
|
|
|
console.log(`Time: ${streamTime}ms`);
|
|
|
console.log(`Response: ${streamedText}`);
|
|
|
console.log();
|
|
|
|
|
|
|
|
|
console.log('Regular invoke:');
|
|
|
const invokeStart = Date.now();
|
|
|
const invokeResponse = await llm.invoke(question);
|
|
|
const invokeTime = Date.now() - invokeStart;
|
|
|
console.log(`Time: ${invokeTime}ms`);
|
|
|
console.log(`Response: ${invokeResponse.content}`);
|
|
|
|
|
|
console.log(`\nTime difference: ${Math.abs(streamTime - invokeTime)}ms`);
|
|
|
console.log('Note: Streaming feels faster because you see results immediately!');
|
|
|
|
|
|
} finally {
|
|
|
await llm.dispose();
|
|
|
}
|
|
|
|
|
|
console.log('\n✓ Exercise 3 complete!');
|
|
|
}
|
|
|
|
|
|
|
|
|
exercise3().catch(console.error);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|