To use the library without node, import the browser module.
`javascript 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.
`javascript 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.
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
$3
`javascript ollama.chat(request) `
- request
- model The name of the model to use for the chat. - messages: Array of message objects representing the chat history. - role: The role of the message sender ('user', 'system', or 'assistant'). - content: The content of the message. - images: (Optional) Images to be included in the message, either as Uint8Array or base64 encoded strings. - tool_name: (Optional) Add the name of the tool that was executed to inform the model of the result - format: (Optional) Set the expected format of the response (json). - stream: (Optional) When true an AsyncGenerator is returned. - think: (Optional) Enable model thinking. Use true/false or specify a level. Requires model support. - logprobs: (Optional) Return log probabilities for tokens. Requires model support. - top_logprobs: (Optional) Number of top log probabilities to return per token when logprobs is enabled. - keep_alive: (Optional) How long to keep the model loaded. A number (seconds) or a string with a duration unit suffix ("300ms", "1.5h", "2h45m", etc.) - tools: (Optional) A list of tool calls the model may make. - options: (Optional) Options to configure the runtime.
- Returns:
$3
`javascript ollama.generate(request) `
- request
$3
`javascript ollama.pull(request) `
- request
$3
`javascript ollama.push(request) `
- request
$3
`javascript ollama.create(request) `
- request
Note: The files parameter is not currently supported in ollama-js.
$3
`javascript ollama.delete(request) `
- request
$3
`javascript ollama.copy(request) `
- request
$3
`javascript ollama.list() `
- Returns:
$3
`javascript ollama.show(request) `
- request
$3
`javascript ollama.embed(request) `
- request
$3
- Web search capability requires an Ollama account. Sign up on ollama.com - Create an API key by visiting https://ollama.com/settings/keys `javascript ollama.webSearch(request) `