npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

llama-service-snt

v2.0.9

Published

Client Node.js modulare per LLM locali (Ollama/Llava)

Readme

🧠 llama-service-snt

Libreria Node.js modulare per interfacciarsi con modelli LLM locali (es. llava, llama3) tramite Ollama API.
Supporta autenticazione, streaming real-time, immagini base64 e conversazioni multi-turno.


✨ Caratteristiche v2.0.0

  • Architettura modulare - Componenti separati per responsabilità
  • Prompt standard - Invio messaggi semplici
  • Streaming real-time - Callback progressivi
  • Supporto immagini - Invio immagini base64
  • Conversazioni multi-turno - Context management
  • System prompts - Personalizzazione comportamento modello
  • Validazione input - Controlli automatici
  • Gestione errori - Error handling chiaro

📦 Installazione

npm install llama-service-snt

⚙️ Configurazione

1. Crea config/configuration.json

{
  "api": {
    "host": "http://localhost:11434",
    "key": "sk-your-api-key",
    "timeout": 30000
  },
  "model": {
    "default": "llava"
  },
  "endpoints": {
    "chat": "/api/chat"
  }
}

2. (Opzionale) Crea .env per override

LLM_API_KEY=sk-your-api-key
LLM_API_HOST=http://localhost:11434
LLM_MODEL=llava

Nota: I valori in .env sovrascrivono quelli in configuration.json


🧪 Utilizzo Base

Importazione

import { LLMClient } from 'llama-service-snt';

const client = new LLMClient('sk-your-api-key', 'llava');

📚 Esempi

1️⃣ Prompt Semplice

const risposta = await client.send("Ciao, chi sei?");
console.log(risposta);

2️⃣ Con System Prompt

const risposta = await client.send("Qual è la capitale d'Italia?", {
  systemPrompt: "Rispondi in modo conciso, massimo 10 parole"
});
console.log(risposta); // "Roma"

3️⃣ Streaming Real-Time

await client.sendStream("Conta da 1 a 5", {
  systemPrompt: "Conta lentamente",
  onChunk: (chunk) => {
    if (!chunk.isComplete) {
      process.stdout.write(chunk.content); // Output progressivo
    } else {
      console.log("\n✅ Completato!");
    }
  }
});

4️⃣ Streaming Senza Callback

const rispostaCompleta = await client.sendStream("Raccontami una storia breve");
console.log(rispostaCompleta); // Ritorna tutta la risposta alla fine

5️⃣ Invio Immagine Base64

import { readFileSync } from 'fs';

// Leggi immagine e converti in base64
const imageBuffer = readFileSync('./image.jpg');
const base64Image = imageBuffer.toString('base64');

const risposta = await client.sendWithImage(
  "Cosa vedi in questa immagine?",
  base64Image,
  {
    systemPrompt: "Descrivi l'immagine in dettaglio"
  }
);
console.log(risposta);

6️⃣ Conversazione Multi-Turno

const messages = [];

// Turno 1
const turno1 = await client.send("Mi chiamo Marco", {
  systemPrompt: "Sei un assistente che ricorda le informazioni",
  messages: messages
});
messages.push({ role: "user", content: "Mi chiamo Marco" });
messages.push({ role: "assistant", content: turno1 });

// Turno 2
const turno2 = await client.send("Come mi chiamo?", {
  systemPrompt: "Sei un assistente che ricorda le informazioni",
  messages: messages
});
console.log(turno2); // "Ti chiami Marco"

🔧 API Reference

new LLMClient(apiKey, model?)

Crea una nuova istanza del client.

Parametri:

  • apiKey (string) - Chiave API per autenticazione
  • model (string, opzionale) - Modello da utilizzare (default: da config)

client.send(message, options?)

Invia un prompt standard (non-streaming).

Parametri:

  • message (string) - Il messaggio da inviare
  • options (object, opzionale):
    • systemPrompt (string) - Prompt di sistema
    • messages (array) - Messaggi precedenti per context

Ritorna: Promise<string> - Risposta del modello


client.sendStream(message, options?)

Invia un prompt con streaming.

Parametri:

  • message (string) - Il messaggio da inviare
  • options (object, opzionale):
    • systemPrompt (string) - Prompt di sistema
    • messages (array) - Messaggi precedenti per context
    • onChunk (function) - Callback per ogni chunk ricevuto
      • Riceve: { content, fullResponse, isComplete }

Ritorna: Promise<string> - Risposta completa del modello


client.sendWithImage(message, base64Image, options?)

Invia un prompt con immagine base64.

Parametri:

  • message (string) - Il messaggio da inviare
  • base64Image (string) - Immagine in formato base64
  • options (object, opzionale):
    • systemPrompt (string) - Prompt di sistema
    • messages (array) - Messaggi precedenti per context

Ritorna: Promise<string> - Risposta del modello


🏗️ Architettura

src/
├── core/
│   ├── Connection.js       # Gestione connessione
│   └── RequestBuilder.js   # Costruzione payload API
│
├── handlers/
│   ├── PromptHandler.js    # Invio prompt standard
│   ├── StreamHandler.js    # Gestione streaming
│   └── ImageHandler.js     # Gestione immagini
│
├── utils/
│   ├── ConfigLoader.js     # Caricamento configurazione
│   └── Validator.js        # Validazione input
│
└── LLMClient.js            # Facade principale

Principi:

  • KISS - Keep It Simple, Stupid
  • Single Responsibility - Un componente = una responsabilità
  • Separation of Concerns - Logica separata per layer

🧪 Testing

# Esegui test suite
npm test

🔐 Sicurezza

  • ✅ Validazione API key
  • ✅ Validazione input utente
  • ✅ Gestione errori di rete
  • ⚠️ Non committare .env e config/ (già in .gitignore)

📝 Changelog

v2.0.0 (2025-11-10)

  • 🎉 Refactoring completo - Architettura modulare
  • ✨ Supporto immagini base64
  • ✨ Streaming con callback real-time
  • ✨ Conversazioni multi-turno
  • ✨ System prompts personalizzabili
  • 🐛 Fix gestione configurazione

v1.2.4

  • Versione monolitica legacy

👤 Autore

Marco Paglicci