| |
| |
|
|
|
|
| import {HumanMessage, SystemMessage, LlamaCppLLM} from '../../../../src/index.js';
|
|
|
| async function exercise2() {
|
| console.log('=== Exercise 2: Batch Processing ===\n');
|
|
|
| const llm = new LlamaCppLLM({
|
| modelPath: './models/Meta-Llama-3.1-8B-Instruct-Q5_K_S.gguf',
|
| temperature: 0.3,
|
| maxTokens: 50
|
| });
|
|
|
| try {
|
|
|
| console.log('Part 1: Batch processing simple questions');
|
|
|
| const mathQuestions = [
|
| "What is 10 + 15?",
|
| "What is 20 * 3?",
|
| "What is 100 / 4?",
|
| "What is 50 - 18?",
|
| "What is 7 squared?"
|
| ];
|
|
|
| const mathAnswers = await llm.batch(mathQuestions);
|
|
|
| mathQuestions.forEach((question, i) => {
|
| console.log(`Q${i + 1}: ${question}`);
|
| console.log(`A${i + 1}: ${mathAnswers[i].content}`);
|
| console.log();
|
| });
|
|
|
|
|
| console.log('Part 2: Batch processing with message arrays');
|
|
|
| const conversationBatch = [
|
| [
|
| new SystemMessage("You are a professional chef"),
|
| new HumanMessage("Describe an apple in one sentence")
|
| ],
|
| [
|
| new SystemMessage("You are a research scientist"),
|
| new HumanMessage("Describe an apple in one sentence")
|
| ],
|
| [
|
| new SystemMessage("You are a romantic poet"),
|
| new HumanMessage("Describe an apple in one sentence")
|
| ]
|
| ];
|
|
|
| const perspectives = await llm.batch(conversationBatch);
|
|
|
| const roles = ["Chef", "Scientist", "Poet"];
|
| perspectives.forEach((response, i) => {
|
| console.log(`${roles[i]}: ${response.content}`);
|
| });
|
|
|
| console.log();
|
|
|
|
|
| console.log('Part 3: Sequential vs Batch performance');
|
|
|
| const testQuestions = [
|
| "What is AI?",
|
| "What is ML?",
|
| "What is DL?",
|
| "What is NLP?",
|
| "What is CV?"
|
| ];
|
|
|
|
|
| console.log('Sequential processing...');
|
| const startSeq = Date.now();
|
| const seqResults = [];
|
| for (const question of testQuestions) {
|
| const response = await llm.invoke(question);
|
| seqResults.push(response);
|
| }
|
| const seqTime = Date.now() - startSeq;
|
| console.log(`Sequential: ${seqTime}ms for ${testQuestions.length} questions`);
|
|
|
|
|
| console.log('\nBatch processing...');
|
| const startBatch = Date.now();
|
| const batchResults = await llm.batch(testQuestions);
|
| const batchTime = Date.now() - startBatch;
|
| console.log(`Batch: ${batchTime}ms for ${testQuestions.length} questions`);
|
|
|
| console.log(`\nSpeedup: ${(seqTime / batchTime).toFixed(2)}x faster`);
|
| console.log(`Time saved: ${seqTime - batchTime}ms`);
|
|
|
| } finally {
|
| await llm.dispose();
|
| }
|
|
|
| console.log('\n✓ Exercise 2 complete!');
|
| }
|
|
|
|
|
| exercise2().catch(console.error);
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |