AI Personal Learning
and practical guidance
Ali-painted frog

Using the Ollama API in JavaScript

This article describes how to use the Ollama API . This documentation is intended to help developers get up to speed quickly and take full advantage of Ollama's capabilities. You can use it in a Node.js environment or import the module directly in your browser. By studying this document, you can easily integrate Ollama into your projects.

 

Install Ollama

npm i ollama

 

Usage

import ollama from 'ollama'
const response = await ollama.chat({
model: 'ollama3.1', message: [{ role: 'user', content: 'Why is the sky blue?
messages: [{ role: 'user', content: 'Why is the sky blue?' }]
})
console.log(response.message.content)

Browser Use

To use this library without Node.js, import the browser module.

import ollama from 'ollama/browser'

 

Streaming Response

This can be done by setting the stream: true Enable response streaming so that a function call returns a AsyncGenerator , where each part is an object in the stream.

import ollama from 'ollama'
const message = { role: 'user', content: 'Why is the sky blue?' }
const response = await ollama.chat({ model: 'ollama3.1', messages: [message], stream: true })
for await (const part of response) {
process.stdout.write(part.message.content)
}

 

Structured Output

Using the Ollama JavaScript library, the architecture as a JSON object is passed to the format parameter, you can optionally use the object format, or you can use Zod (recommended) to pass the zodToJsonSchema() Method Serialization Architecture.

import ollama from 'ollama';
import { z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema';
const Country = z.object({
name: z.string(), capital: z.string(), {

languages: z.array(z.string()),
}); const response = await ollama.
const response = await ollama.chat({

messages: [{ role: 'user', content: 'Tell me about Canada.' }], format: zodToJson(z.string()), }
format: zodToJsonSchema(Country), }); }
}).
const country = Country.parse(JSON.parse(response.message.content)); console.log(country.log(response.message);)
console.log(country);

 

Creating Models

import ollama from 'ollama'
const modelfile = `
FROM llama3.1
SYSTEM "You are Mario from Super Mario Bros."
`
await ollama.create({ model: 'example', modelfile: modelfile })

 

API

The Ollama JavaScript library API is designed around the Ollama REST API. If you want to learn more about the underlying implementation and full API endpoint information, we recommend referring to the Ollama API User's Guide

chats

ollama.chat(request)