ollama

package module
v0.18.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 10, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package ollama exposes Ollama's native chat and embedding adapters. NewChatCompletions targets Ollama's OpenAI-compatible chat endpoint. Native chat maps a natural stop with tool calls to Core tool_calls while preserving interrupted outcomes and the native done reason.

Stream termination. A chunk with done true is the only claim that the generation finished, and the non-streaming path already refuses a response without it. A stream whose body ends before that chunk fails with [chat.ErrInvalidResponse], because to a delta consumer a truncated body looks exactly like a completed answer. Reasoning. A reasoning effort names a thinking level, which is what /api/chat's think parameter accepts (low, medium, high, max), so the portable option reaches the daemon. An empty effort leaves think untouched, including a boolean set through the request extension — no portable effort can express "do not think", so that stays the extension's job.

Embedding input is truncated by default. /api/embed's truncate parameter "defaults to true", so a text past the model's context window is embedded from a prefix and nothing reports it; setting it false makes Ollama return an error instead. The knob reaches the wire through the native request extension.

See https://github.com/ollama/ollama/blob/main/docs/api.md for the API reference.

Index

Constants

View Source
const (
	OpenAIRequestExtensionKey     = "ollama/openai_request"
	OpenAIResponseExtensionKey    = "ollama/openai_response"
	OpenAIStreamChunkExtensionKey = "ollama/openai_stream_chunk"
)

Namespacing preserves provider-specific data without promoting it into the shared Core protocol or colliding with another provider.

View Source
const (
	EmbeddingRequestExtensionKey = "ollama/embedding_request"

	// DefaultBaseURL is Ollama's default local listen address.
	DefaultBaseURL = "http://127.0.0.1:11434"

	// OpenAICompatPath is the suffix Ollama serves the OpenAI-compatible
	// API under. [NewChatCompletions] joins it with the configured host.
	OpenAICompatPath = "/v1"
)

Exported identifiers keep provider-owned names and defaults out of caller literals.

View Source
const (
	Provider = "Ollama"
)

Provider is the stable backend name for host-side attribution.

View Source
const (
	// RequestExtensionKey stores official Ollama /api/chat request fields that
	// have no provider-neutral Core equivalent. Core model, messages, tools,
	// streaming, and common options take precedence.
	RequestExtensionKey = "ollama/request"
)
View Source
const (
	// ResponseExtensionKey preserves the complete official Ollama response (or
	// the current official stream chunk), including log probabilities and image
	// output that Core does not normalize.
	ResponseExtensionKey = "ollama/response"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Chat

type Chat struct {
	// contains filtered or unexported fields
}

Chat implements Core chat over Ollama's native /api/chat endpoint.

func NewChat

func NewChat(_ context.Context, config ChatConfig) (*Chat, error)

NewChat rejects an invalid provider binding before the first chat call.

func (*Chat) Call

func (c *Chat) Call(ctx context.Context, req *corechat.Request) (*corechat.Response, error)

func (*Chat) Stream

Stream bridges Ollama's callback stream into Core's pull sequence. Returning false from yield aborts the HTTP stream without surfacing a cancellation error to the caller.

type ChatCompletions added in v0.13.0

type ChatCompletions = openai.ChatCompletions

ChatCompletions implements Ollama's OpenAI-compatible endpoint.

func NewChatCompletions added in v0.13.0

func NewChatCompletions(ctx context.Context, config ChatCompletionsConfig) (*ChatCompletions, error)

NewChatCompletions rejects an invalid provider binding before the first Chat Completions call.

type ChatCompletionsConfig added in v0.13.0

type ChatCompletionsConfig struct {
	APIKey         string
	DefaultOptions corechat.Options
	BaseURL        string
	HTTPClient     *http.Client
}

ChatCompletionsConfig binds provider access and defaults shared by every Chat Completions call.

func (ChatCompletionsConfig) Validate added in v0.13.0

func (c ChatCompletionsConfig) Validate() error

type ChatConfig

type ChatConfig struct {
	DefaultOptions corechat.Options
	BaseURL        string
	HTTPClient     *http.Client
}

ChatConfig binds provider access and defaults shared by every chat call.

func (ChatConfig) Validate

func (c ChatConfig) Validate() error

type EmbeddingModel

type EmbeddingModel struct {
	// contains filtered or unexported fields
}

EmbeddingModel wraps Ollama's /api/embed endpoint. Works with any embedding model the daemon has pulled: nomic-embed-text, mxbai-embed-large, snowflake-arctic-embed, etc. Use `ollama pull <model>` ahead of time.

func NewEmbeddingModel

func NewEmbeddingModel(_ context.Context, config EmbeddingModelConfig) (*EmbeddingModel, error)

NewEmbeddingModel rejects an invalid provider binding before the first embedding call.

func (*EmbeddingModel) Call

func (e *EmbeddingModel) Call(ctx context.Context, req *embedding.Request) (response *embedding.Response, err error)

type EmbeddingModelConfig

type EmbeddingModelConfig struct {
	DefaultOptions embedding.Options
	BaseURL        string
	HTTPClient     *http.Client
}

EmbeddingModelConfig binds provider access and defaults shared by every embedding call.

func (EmbeddingModelConfig) Validate

func (e EmbeddingModelConfig) Validate() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL