This JavaScript code implements a large language model using the node-llama-cpp
library, providing functions to initialize and interact with the model. It includes functions for initializing chat sessions, loading LLM models, and analyzing prompts, with optional error handling.
npm run import -- "llama vision"
import path from "path"
import {getLlama, LlamaChatSession} from "node-llama-cpp"
import process from "process"
const HOMEPATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE
//const __dirname = path.dirname(fileURLToPath(import.meta.url));
let llama
let model
let context
let session
let initialChatHistory
const DEFAULT_PROMPT = '```markdown\n# You are a\n##large language model\nnamed ' + (process.env.MODEL_NAME || 'Llama') + ' that provides clear and concise answers in beautifully crafted `markdown` unless otherwise instructed.\n</think>\n...```\n'
const DEFAULT_MODEL = process.env.DEFAULT_GGUF || 'Qwen2-VL-7B-Instruct-Q6_K.gguf'
async function initSession(prompt, context2) {
if(!context2) {
context = await model.createContext()
} else {
context = context2
}
session = new LlamaChatSession({
contextSequence: context.getSequence(),
systemPrompt: prompt ? prompt : DEFAULT_PROMPT
})
// initialize the model
//console.log(await session.prompt())
initialChatHistory = session.getChatHistory();
// Reset the chat history
session.setChatHistory(initialChatHistory);
return session
}
async function createSession(modelName, prompt) {
if(!llama) {
llama = await getLlama();
}
if(!model) {
model = await llama.loadModel({
modelPath: path.join(HOMEPATH, "llama.cpp", "models", modelName ? modelName : DEFAULT_MODEL ),
//contextSize: 2048
});
}
await initSession(prompt, await model.createContext())
return session
}
async function getSession(model, prompt) {
if(!session) {
await createSession(model, prompt)
}
return session
}
async function llmAnalyze(prompt, session2) {
if(!session2) {
session2 = await getSession()
}
let result = await session2.prompt(prompt, {
//maxTokens: context.contextSize,
onTextChunk: function (text) {
process.stdout.write(text)
}
})
if(session == session2)
session2.setChatHistory(initialChatHistory);
return result
}
export default {
llmAnalyze,
createSession,
getSession,
}
import { createRequire } from'module';
import path from 'path';
import { getLlama, LlamaChatSession } from 'node-llama-cpp';
const require = createRequire(import.meta.url);
const process = require('process');
const HOMEPATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
const __dirname = path.dirname(import.meta.url);
let llama;
let model;
let context;
let session;
let initialChatHistory;
const DEFAULT_MODEL = process.env.DEFAULT_GGUF || 'Qwen2-VL-7B-Instruct-Q6_K.gguf';
const DEFAULT_PROMPT = (text) => ````markdown\n# You are a\n##large language model\nnamed ${process.env.MODEL_NAME || 'Llama'} that provides clear and concise answers in beautifully crafted ` + (text? `markdown unless otherwise instructed.\n\n${text}\n` :'markdown\n\n...```\n');
class LLM {
async createSession(modelName, prompt) {
if (!llama) {
llama = await getLlama();
}
if (!model) {
model = await llama.loadModel({
modelPath: path.join(HOMEPATH, "llama.cpp", "models", modelName? modelName : DEFAULT_MODEL),
//contextSize: 2048
});
}
const context2 = await model.createContext();
const session = new LlamaChatSession({
contextSequence: context2.getSequence(),
systemPrompt: prompt? prompt : DEFAULT_PROMPT(''),
});
initialChatHistory = session.getChatHistory();
session.setChatHistory(initialChatHistory);
this.model = model;
this.context = context2;
this.session = session;
return session;
}
async getSession(session) {
if (!session) {
await this.createSession();
}
return this.session;
}
async llmAnalyze(prompt, session2) {
if (!session2) {
session2 = await this.getSession();
}
let result = await session2.prompt(prompt, {
//maxTokens: context.contextSize,
onTextChunk: (text) => process.stdout.write(text),
});
if (session2 === this.session) {
session2.setChatHistory(initialChatHistory);
}
return result;
}
async llmAnalyzeWithSession(prompt, session) {
try {
return await session.prompt(prompt, {
//maxTokens: context.contextSize,
onTextChunk: (text) => process.stdout.write(text),
});
} catch (err) {
console.error('Error analyzing prompt:', err);
}
}
}
const llm = new LLM();
export default {
llm: llm,
createSession: llm.createSession.bind(llm),
getSession: llm.getSession.bind(llm),
llmAnalyze: llm.llmAnalyze.bind(llm),
llmAnalyzeWithSession: llm.llmAnalyzeWithSession.bind(llm),
};
Breakdown of the Code
This JavaScript code is for a large language model (LLM) implementation using the node-llama-cpp
library. It establishes a conversation interface with the LLM and provides functions to initialize and interact with the model.
Key Components:
HOMEPATH
: resolves to the user's home directoryMODEL_NAME
: optional model name for the LLMDEFAULT_GGUF
: default model name for the LLM (Qwen2-VL-7B-Instruct-Q6_K.gguf)DEFAULT_PROMPT
: default prompt for the LLMDEFAULT_MODEL
: default model name for the LLMinitSession(prompt, context2)
: initializes a new chat session with a given prompt and context (or generates a new context if none is provided)createSession(modelName, prompt)
: loads the LLM model and initializes a new chat session with a given model name and promptgetSession(model, prompt)
: gets an existing session or creates a new one if none is availablellmAnalyze(prompt, session2)
: interacts with the LLM to analyze a given prompt and prints the response to the consoleNotes:
node-llama-cpp
library to interact with the LLM.createSession
function loads the LLM model and initializes a new chat session, while the getSession
function gets an existing session or creates a new one if none is available.llmAnalyze
function interacts with the LLM to analyze a given prompt and prints the response to the console.