API Reference
发布时间:2026-10-11 | 浏览:1
Generate a completion
Generate a chat completion
List Local Models
Show Model Information
Generate Embeddings
List Running Models
Model names follow a model:tag format, where model can have an optional namespace such as example/model . Some examples are orca-mini:3b-q4_1 and llama3:70b . The tag is optional and, if not provided, will default to latest . The tag is used to identify a specific version.
All durations are returned in nanoseconds.
Streaming responses
Certain endpoints stream responses as JSON objects. Streaming can be disabled by providing {"stream": false} for these endpoints.
Generate a completion
Generate a response for a given prompt with a provided model. This is a streaming endpoint, so there will be a series of responses. The final response object will include statistics and additional data from the request.
model : (required) the model name
prompt : the prompt to generate a response for
suffix : the text after the model response
images : (optional) a list of base64-encoded images (for multimodal models such as llava )
Advanced parameters (optional):
format : the format to return a response in. Currently the only accepted value is json
options : additional model parameters listed in the documentation for the Modelfile such as temperature
system : system message to (overrides what is defined in the Modelfile )
template : the prompt template to use (overrides what is defined in the Modelfile )
context : the context parameter returned from a previous request to /generate , this can be used to keep a short conversational memory
stream : if false the response will be returned as a single response object, rather than a stream of objects
raw : if true no formatting will be applied to the prompt. You may choose to use the raw parameter if you are specifying a full templated prompt in your request to the API
keep_alive : controls how long the model will stay loaded into memory following the request (default: 5m )
Enable JSON mode by setting the format parameter to json . This will structure the response as a valid JSON object. See the JSON mode example below.
[!IMPORTANT] It's important to instruct the model to use JSON in the prompt . Otherwise, the model may generate large amounts whitespace.
Generate request (Streaming)
A stream of JSON objects is returned:
The final response in the stream also includes additional data about the generation:
total_duration : time spent generating the response
load_duration : time spent in nanoseconds loading the model
prompt_eval_count : number of tokens in the prompt
prompt_eval_duration : time spent in nanoseconds evaluating the prompt
eval_count : number of tokens in the response
eval_duration : time in nanoseconds spent generating the response
context : an encoding of the conversation used in this response, this can be sent in the next request to keep a conversational memory
response : empty if the response was streamed, if not streamed, this will contain the full response
To calculate how fast the response is generated in tokens per second (token/s), divide eval_count / eval_duration * 10^9 .
Request (No streaming)
A response can be received in one reply when streaming is off.
If stream is set to false , the response will be a single JSON object:
Request (with suffix)
Request (JSON mode)
[!IMPORTANT] When format is set to json , the output will always be a well-formed JSON object. It's important to also instruct the model to respond in JSON.
The value of response will be a string containing JSON similar to:
Request (with images)
To submit images to multimodal models such as llava or bakllava , provide a list of base64-encoded images :
Request (Raw Mode)
In some cases, you may wish to bypass the templating system and provide a full prompt. In this case, you can use the raw parameter to disable templating. Also note that raw mode will not return a context.
Request (Reproducible outputs)
For reproducible outputs, set seed to a number:
Generate request (With options)
If you want to set custom options for the model at runtime rather than in the Modelfile, you can do so with the options parameter. This example sets every available option, but you can set any of them individually and omit the ones you do not want to override.
If an empty prompt is provided, the model will be loaded into memory.
A single JSON object is returned:
If an empty prompt is provided and the keep_alive parameter is set to 0 , a model will be unloaded from memory.
A single JSON object is returned:
Generate a chat completion
Generate the next message in a chat with a provided model. This is a streaming endpoint, so there will be a series of responses. Streaming can be disabled using "stream": false . The final response object will include statistics and additional data from the request.
model : (required) the model name
messages : the messages of the chat, this can be used to keep a chat memory
tools : tools for the model to use if supported. Requires stream to be set to false
The message object has the following fields:
role : the role of the message, either system , user , assistant , or tool
content : the content of the message
images (optional): a list of images to include in the message (for multimodal models such as llava )
tool_calls (optional): a list of tools the model wants to use
Advanced parameters (optional):
format : the format to return a response in. Currently the only accepted value is json
options : additional model parameters listed in the documentation for the Modelfile such as temperature
stream : if false the response will be returned as a single response object, rather than a stream of objects
keep_alive : controls how long the model will stay loaded into memory following the request (default: 5m )
Chat Request (Streaming)
Send a chat message with a streaming response.
A stream of JSON objects is returned:
Final response:
Chat request (No streaming)
Chat request (With History)
Send a chat message with a conversation history. You can use this same approach to start the conversation using multi-shot or chain-of-thought prompting.
A stream of JSON objects is returned:
Final response:
Chat request (with images)
Send a chat message with images. The images should be provided as an array, with the individual images encoded in Base64.
Chat request (Reproducible outputs)
Chat request (with tools)
If the messages array is empty, the model will be loaded into memory.
If the messages array is empty and the keep_alive parameter is set to 0 , a model will be unloaded from memory.
A single JSON object is returned:
Create a model from a Modelfile . It is recommended to set modelfile to the content of the Modelfile rather than just set path . This is a requirement for remote create. Remote model creation must also create any file blobs, fields such as FROM and ADAPTER , explicitly with the server using Create a Blob and the value to the path indicated in the response.
model : name of the model to create
modelfile (optional): contents of the Modelfile
stream : (optional) if false the response will be returned as a single response object, rather than a stream of objects
path (optional): path to the Modelfile
quantize (optional): quantize a non-quantized (e.g. float16) model
Quantization types
Create a new model
Create a new model from a Modelfile .
A stream of JSON objects is returned:
Quantize a model
Quantize a non-quantized model.
A stream of JSON objects is returned:
Check if a Blob Exists
Ensures that the file blob used for a FROM or ADAPTER field exists on the server. This is checking your Ollama server and not ollama.com.
Query Parameters
digest : the SHA256 digest of the blob
Return 200 OK if the blob exists, 404 Not Found if it does not.
Create a blob from a file on the server. Returns the server file path.
Query Parameters
digest : the expected SHA256 digest of the file
Return 201 Created if the blob was successfully created, 400 Bad Request if the digest used is not expected.
List Local Models
List models that are available locally.
A single JSON object will be returned.
Show Model Information
Show information about a model including details, modelfile, template, parameters, license, system prompt.
model : name of the model to show
verbose : (optional) if set to true , returns full data for verbose response fields
Copy a model. Creates a model with another name from an existing model.
Returns a 200 OK if successful, or a 404 Not Found if the source model doesn't exist.
Delete a model and its data.
model : model name to delete
Returns a 200 OK if successful, 404 Not Found if the model to be deleted doesn't exist.
Download a model from the ollama library. Cancelled pulls are resumed from where they left off, and multiple calls will share the same download progress.
model : name of the model to pull
insecure : (optional) allow insecure connections to the library. Only use this if you are pulling from your own library during development.
stream : (optional) if false the response will be returned as a single response object, rather than a stream of objects
If stream is not specified, or set to true , a stream of JSON objects is returned:
The first object is the manifest:
Then there is a series of downloading responses. Until any of the download is completed, the completed key may not be included. The number of files to be downloaded depends on the number of layers specified in the manifest.
After all the files are downloaded, the final responses are:
if stream is set to false, then the response is a single JSON object:
Upload a model to a model library. Requires registering for ollama.ai and adding a public key first.
model : name of the model to push in the form of <namespace>/<model>:<tag>
insecure : (optional) allow insecure connections to the library. Only use this if you are pushing to your library during development.
stream : (optional) if false the response will be returned as a single response object, rather than a stream of objects
If stream is not specified, or set to true , a stream of JSON objects is returned:
Then there is a series of uploading responses:
Finally, when the upload is complete:
If stream is set to false , then the response is a single JSON object:
Generate Embeddings
Generate embeddings from a model
model : name of model to generate embeddings from
input : text or list of text to generate embeddings for
Advanced parameters:
truncate : truncates the end of each input to fit within context length. Returns error if false and context length is exceeded. Defaults to true
options : additional model parameters listed in the documentation for the Modelfile such as temperature
keep_alive : controls how long the model will stay loaded into memory following the request (default: 5m )
Request (Multiple input)
List Running Models
List models that are currently loaded into memory.
A single JSON object will be returned.
Generate Embedding
Note: this endpoint has been superseded by /api/embed
Generate embeddings from a model
model : name of model to generate embeddings from
prompt : text to generate embeddings for
Advanced parameters:
options : additional model parameters listed in the documentation for the Modelfile such as temperature
keep_alive : controls how long the model will stay loaded into memory following the request (default: 5m )