openai

package
v0.22.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package openai is the ai.Provider for servers that speak the OpenAI chat completions API: Ollama's /v1 endpoints, llama.cpp's llama-server, vLLM, LM Studio, and gateways in front of them. It serves two credential providers, ai.ProviderOllama (NewOllama, OllamaFactory: the endpoint is the Ollama server root, http://localhost:11434 by default) and ai.ProviderOpenAICompat (New, Factory: the endpoint is the API base URL ending in /v1 and is required).

One request per turn, streamed. Tools are sent as function tools, reasoning deltas (reasoning_content, or reasoning as Ollama names it) are emitted as thinking, and the assistant message is stored in the wire format, so a session created here stays on this provider.

Message.Content for this provider is a JSON array of chat messages: a user prompt is one user message, the results of a turn are one tool message per result, and an assistant turn is one assistant message carrying content, reasoning_content, and tool_calls. The history sent to the server is the concatenation.

Index

Constants

View Source
const (
	// DefaultMaxTokens is the output cap when neither the options nor the
	// credential set one. Local models have smaller windows than hosted
	// ones, so it is lower than the Anthropic default.
	DefaultMaxTokens = 8192
)

Variables

This section is empty.

Functions

func Factory

func Factory(opts Options) ai.ProviderFactory

Factory returns an ai.ProviderFactory for openai_compat credentials.

func OllamaFactory

func OllamaFactory(opts Options) ai.ProviderFactory

OllamaFactory returns an ai.ProviderFactory for ollama credentials.

Types

type Options

type Options struct {
	// Effort is the reasoning effort sent as reasoning_effort. It
	// overrides the credential's ai_effort tag; empty means that tag, and
	// no tag means the field is not sent. xhigh and max are sent as high,
	// the highest value the OpenAI dialect defines.
	Effort string
	// MaxTokens caps output tokens per turn; zero means the credential's
	// ai_max_output tag or DefaultMaxTokens.
	MaxTokens int
	// HTTPClient replaces http.DefaultClient, for tests and proxies. A
	// turn can take minutes on a local model, so it should carry no
	// overall timeout; the request context bounds it.
	HTTPClient *http.Client
	// Logger receives debug lines; nil is silent.
	Logger *logger.Logger
}

Options tunes the provider. The zero value sends no reasoning effort (the server's default) and caps output at DefaultMaxTokens.

type Provider

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

Provider is an ai.Provider for one credential.

func New

func New(cred ai.Credential, opts Options) (*Provider, error)

New builds a provider for an openai_compat credential: Endpoint is the API base URL (for example http://llm.example/v1) and is required, Model is the model id, and Bearer, when set, is sent as the Authorization bearer.

func NewOllama

func NewOllama(cred ai.Credential, opts Options) (*Provider, error)

NewOllama builds a provider for an ollama credential: Endpoint is the Ollama server root (ai.OllamaDefaultEndpoint when empty; a trailing /api or /v1 is accepted), Model is the model name as `ollama list` shows it, and Bearer is optional. It also works against any server that mounts the OpenAI API under /v1 of its root, such as llama-server.

func (*Provider) BaseURL

func (p *Provider) BaseURL() string

BaseURL returns the API base URL requests go to.

func (*Provider) Check

func (p *Provider) Check(ctx context.Context) (ai.CheckResult, error)

Check verifies the endpoint, the key, and the model by listing the server's models (GET /models), which spends no tokens. The model must be listed; Ollama's ":latest" suffix may be left off. An Ollama server is also asked for the model's details (POST /api/show) so the answer carries the context length and refuses a model that cannot call tools; a server without that route (llama-server) is checked by the listing alone.

func (*Provider) ListModels

func (p *Provider) ListModels(ctx context.Context) ([]ai.ModelInfo, error)

ListModels returns the models the endpoint serves (GET /models), sorted by id. llama-server's listing carries the loaded context size (n_ctx) and the trained one (n_ctx_train); the first is what a session gets, so it wins. Ollama's listing carries neither.

func (*Provider) Model

func (p *Provider) Model() string

Model returns the model id in use; "" when the credential names none.

func (*Provider) Name

func (p *Provider) Name() string

Name returns "ollama" or "openai_compat".

func (*Provider) ToolResultMessage

func (p *Provider) ToolResultMessage(results []ai.ToolResult) ai.Message

ToolResultMessage builds the message carrying one tool message per result, in order. The dialect has no error flag on a tool message, so a denied or failed call is prefixed so the model reads it as a failure.

func (*Provider) Turn

func (p *Provider) Turn(ctx context.Context, in ai.TurnInput, emit func(ai.Event)) (*ai.Turn, error)

Turn streams one chat completion, emitting text and thinking deltas as they arrive, and returns the assistant message in the wire format.

func (*Provider) UserMessage

func (p *Provider) UserMessage(text string) ai.Message

UserMessage builds a user message.

Jump to

Keyboard shortcuts

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