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
- func Factory(opts Options) ai.ProviderFactory
- func OllamaFactory(opts Options) ai.ProviderFactory
- type Options
- type Provider
- func (p *Provider) BaseURL() string
- func (p *Provider) Check(ctx context.Context) (ai.CheckResult, error)
- func (p *Provider) ListModels(ctx context.Context) ([]ai.ModelInfo, error)
- func (p *Provider) Model() string
- func (p *Provider) Name() string
- func (p *Provider) ToolResultMessage(results []ai.ToolResult) ai.Message
- func (p *Provider) Turn(ctx context.Context, in ai.TurnInput, emit func(ai.Event)) (*ai.Turn, error)
- func (p *Provider) UserMessage(text string) ai.Message
Constants ¶
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) Check ¶
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 ¶
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) 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.