ollama/ollama-js

★ 4,370⑂ 477

Ollama JavaScript library

About ollama/ollama-js

ollama/ollama-js is an open-source project on GitHub, mainly written in TypeScript. Ollama JavaScript library It currently holds 4,370 stars and 477 forks with 0 open issues, and was last pushed on an unknown date (repository created unknown).

Project Overview

AI Homed tracks it on the Local & On-Device AI board.

GitHub Repository Details

Repository ollama/ollama-js · default branch - · size 0 KB · watchers 0 · source: GitHub REST API and repository README

README

Ollama JavaScript Library

The Ollama JavaScript library provides the easiest way to integrate your JavaScript project with Ollama.

Getting Started

npm i ollama

Usage

import ollama from 'ollama'

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

Browser Usage

To use the library without node, import the browser module.

import ollama from 'ollama/browser'

Streaming responses

Response streaming can be enabled by setting stream: true, modifying function calls to return an 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: 'llama3.1', messages: [message], stream: true, }) for await (const part of response) { process.stdout.write(part.message.content) }

Cloud Models

Run larger models by offloading to Ollama’s cloud while keeping your local workflow.

You can see models currently available on Ollama's cloud here.

Run via local Ollama

1) Sign in (one-time):

ollama signin

2) Pull a cloud model:

ollama pull gpt-oss:120b-cloud

3) Use as usual (offloads automatically):

import { Ollama } from 'ollama'

const ollama = new Ollama() const response = await ollama.chat({ model: 'gpt-oss:120b-cloud', messages: [{ role: 'user', content: 'Explain quantum computing' }], stream: true, }) for await (const part of response) { process.stdout.write(part.message.content) }

Cloud API (ollama.com)

Access cloud models directly by pointing the client at https://ollama.com.

1) Create an API key, then set the OLLAMA_API_KEY environment variable:

export OLLAMA_API_KEY=your_api_key

2) Generate a response via the cloud API:

import { Ollama } from 'ollama'

const ollama = new Ollama({ host: 'https://ollama.com', headers: { Authorization: 'Bearer ' + process.env.OLLAMA_API_KEY }, })

const response = await ollama.chat({ model: 'gpt-oss:120b', messages: [{ role: 'user', content: 'Explain quantum computing' }], stream: true, })

for await (const part of response) { process.stdout.write(part.message.content) }

API

The Ollama JavaScript library's API is designed around the Ollama REST API

chat

ollama.chat(request)

generate

ollama.generate(request)

pull

ollama.pull(request)

push

ollama.push(request)

create

ollama.create(request)
Note: The files parameter is not currently supported in ollama-js.

delete

ollama.delete(request)

copy

ollama.copy(request)

list

ollama.list()

show

ollama.show(request)

embed

ollama.embed(request)

web search

ollama.webSearch(request)

web fetch

ollama.webFetch(request)

ps

ollama.ps()

version

ollama.version()

abort

ollama.abort()

This method will abort all streamed generations currently running with the client instance. If there is a need to manage streams with timeouts, it is recommended to have one Ollama client per stream.

All asynchronous threads listening to streams (typically the for await (const part of response)) will throw an AbortError exception. See examples/abort/abort-all-requests.ts for an example.

Custom client

A custom client can be created with the following fields:

import { Ollama } from 'ollama'

const ollama = new Ollama({ host: 'http://127.0.0.1:11434' }) const response = await ollama.chat({ model: 'llama3.1', messages: [{ role: 'user', content: 'Why is the sky blue?' }], })

Custom Headers

You can set custom headers that will be included with every request:

import { Ollama } from 'ollama'

const ollama = new Ollama({ host: 'http://127.0.0.1:11434', headers: { Authorization: 'Bearer ', 'X-Custom-Header': 'custom-value', 'User-Agent': 'MyApp/1.0', }, })

Building

To build the project files run:

npm run build

GitHub Stars & Activity

4,370Stars
477Forks
0Open issues
TypeScriptLanguage

GitHub Popularity

GitHub stars4,370
Forks477
Open issues0
Primary languageTypeScript
License-
Stars gained today0
Created-
Last pushed-

Trending History

Trending statusnot on today's boards

Related AI Projects

1

ChatGPTNextWeb / NextChat

TypeScript★ 88,790⑂ 59,030
2

chatboxai / chatbox

TypeScript★ 41,812⑂ 4,252
3

dyad-sh / dyad

TypeScript★ 21,587⑂ 2,639
4

lidge-jun / opencodex

TypeScript★ 15,561⑂ 1,180
5

browseros-ai / BrowserOS

TypeScript★ 13,720⑂ 1,460
6

getumbrel / llama-gpt

TypeScript★ 10,937⑂ 704
7

miurla / morphic

TypeScript★ 9,133⑂ 2,348
8

n4ze3m / page-assist

TypeScript★ 8,220⑂ 784

More AI Rankings