Back to roadmaps ollama Course

Integrating Ollama with the JavaScript SDK

Let us use the official JavaScript library to program Ollama model queries inside our Node.js backends.


1. Installation

Install the official client SDK package:

# Install the Ollama JS/TS SDK
npm install @ollama/ollama

2. Basic Chat Completions (Non-Streaming)

Import ollama and query a model:

// src/services/ollamaService.ts
import ollama from "@ollama/ollama";

export async function askLocalAI(userPrompt: string) {
  try {
    const response = await ollama.chat({
      model: "qwen2.5",
      messages: [{ role: "user", content: userPrompt }],
      // Set options to customize generation parameters
      options: {
        temperature: 0.2,
      },
    });

    return response.message.content;
  } catch (err: any) {
    console.error("Local model query failed:", err.message);
    return "Local AI service error.";
  }
}

3. Streaming Chat responses

To receive response tokens incrementally, iterate over the async generator returned by setting stream: true:

export async function streamLocalAI(userPrompt: string, onTokenCallback: (token: string) => void) {
  const response = await ollama.chat({
    model: "qwen2.5",
    messages: [{ role: "user", content: userPrompt }],
    stream: true,
  });

  // Loop through token generator chunks
  for await (const part of response) {
    const token = part.message.content;
    onTokenCallback(token);
  }
}
Published on Last updated: