Samples
Basic chat completion
Have a conversation with a local model:Multi-turn conversation
Continue a conversation with message history:Use with specific host
Connect to a custom Ollama server:Configure chat options
Customize the chat parameters:Structured output with JSON
Request JSON responses:Use tools (function calling)
Enable the model to call tools:Arguments
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
model | TEXT | - | ✔ | The Ollama model to use (e.g., llama2, mistral, codellama) |
messages | JSONB | - | ✔ | Array of message objects with role and content |
host | TEXT | NULL | ✖ | Ollama server URL (defaults to http://localhost:11434) |
keep_alive | TEXT | NULL | ✖ | How long to keep the model loaded (e.g., 5m, 1h) |
chat_options | JSONB | NULL | ✖ | Model-specific options like temperature, top_p |
tools | JSONB | NULL | ✖ | Function definitions for tool calling |
response_format | JSONB | NULL | ✖ | Format specification (e.g., {"type": "json"}) |
verbose | BOOLEAN | FALSE | ✖ | Enable verbose logging for debugging |
Returns
JSONB: The complete API response including:
model: Model used for the chatmessage: The assistant’s response withroleandcontentcreated_at: Response timestampdone: Whether generation is completetotal_duration: Total time takenprompt_eval_count: Number of tokens in prompteval_count: Number of tokens generated
Related functions
ollama_generate(): single-turn text completionollama_embed(): generate embeddingsollama_list_models(): see available models