llama vision | | analyze image with llm | Search

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.

Run example

npm run import -- "llama vision"

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,
}

What the code could have been:

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:

  1. Environment Variables:
  2. Constants:
  3. Functions:

Notes: