Ollama cheatsheet

Ollama: REST API

7 common Ollama tasks.

Generate text

Send a prompt to the generate endpoint. Setting stream to false returns one JSON response.

curl http://localhost:11434/api/generate -d '{"model": "llama3.2", "prompt": "Why is the sky blue?", "stream": false}'

Chat with message history

Send a list of messages, including earlier turns.

curl http://localhost:11434/api/chat -d '{"model": "llama3.2", "messages": [{"role": "user", "content": "Hello"}], "stream": false}'

Create embeddings

Turn text into a vector, using an embedding model such as nomic-embed-text.

curl http://localhost:11434/api/embed -d '{"model": "nomic-embed-text", "input": "hello world"}'

List models over the API

Return the locally available models as JSON.

curl http://localhost:11434/api/tags

Use the OpenAI-compatible endpoint

Call the chat completions endpoint so tools built for the OpenAI API work with Ollama.

curl http://localhost:11434/v1/chat/completions -H "Content-Type: application/json" -d '{"model": "llama3.2", "messages": [{"role": "user", "content": "Hello"}]}'

Use the Python library

Install the client and chat with a model from Python.

pip install ollama

import ollama

response = ollama.chat(model="llama3.2", messages=[{"role": "user", "content": "Why is the sky blue?"}])
print(response.message.content)

Use the OpenAI Python client

Point the OpenAI client at your local server. The API key is required but ignored.

from openai import OpenAI

client = OpenAI(base_url="http://localhost:11434/v1", api_key="ollama")
reply = client.chat.completions.create(model="llama3.2", messages=[{"role": "user", "content": "Hello"}])

Was this page helpful?