Documentation
¶
Overview ¶
Package openai implements agent.LLMProvider for the OpenAI Chat Completions API and any OpenAI-compatible endpoint (Ollama, Groq, Together AI, vLLM, ...), plus the OpenAI-backed embedder, vision analyzer, and history summary provider.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ClientOption ¶ added in v0.40.0
type ClientOption interface {
Option
// contains filtered or unexported methods
}
ClientOption configures transport for any client in this package — Provider, Embedder, VisionAnalyzer, SummaryProvider.
It is a strict subset of Option. Sampling has no meaning for an embedder, so NewEmbedder and friends accept only these: passing WithTemperature to one is a compile error rather than an option that silently does nothing.
func WithBaseURL ¶ added in v0.40.0
func WithBaseURL(url string) ClientOption
WithBaseURL points the client at an OpenAI-compatible endpoint — OpenRouter, Ollama, vLLM, Groq, Together, or any gateway speaking the same API.
This is the only way to keep an Embedder, VisionAnalyzer, or SummaryProvider off api.openai.com. Without it they call OpenAI directly, which on a deliberately local or gateway-only deployment means an unexpected egress and a key the operator may not hold.
url must be absolute, HTTP(S), and free of embedded credentials, a query, or a fragment; it is validated at construction. Prefer HTTPS except for local development endpoints.
func WithHTTPHeader ¶ added in v0.40.0
func WithHTTPHeader(name, value string) ClientOption
WithHTTPHeader adds a header to every request the client makes. It is useful for compatible gateways that accept attribution or routing headers.
Headers are applied after the SDK has built the request, so a name the SDK already sets is overwritten: passing "Authorization" replaces the bearer token derived from apiKey. Repeated calls with the same name keep the last value.
type Embedder ¶
type Embedder struct {
// contains filtered or unexported fields
}
Embedder implements tools.Embedder using OpenAI's embeddings API.
text-embedding-3-small is the default — 1536 dimensions, ~$0.02/1M tokens, and more than enough lexical/semantic signal for tool-description ranking. Pass model = "" to use the default, or a specific model id (e.g. openai.LargeEmbedding3 for higher fidelity at ~6x the cost).
func NewEmbedder ¶
func NewEmbedder(apiKey string, model string, opts ...ClientOption) (*Embedder, error)
NewEmbedder constructs an embedder. apiKey falls back to OPENAI_API_KEY. model defaults to text-embedding-3-small. Pass WithBaseURL to embed against an OpenAI-compatible endpoint (Ollama, vLLM, Together, a gateway) instead of api.openai.com.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures a chat Provider. Both the sampling options (WithTemperature, WithTopP, WithSeed) and the transport options (WithBaseURL, WithHTTPHeader) satisfy it, so one call can mix them.
func WithSeed ¶
WithSeed requests best-effort deterministic sampling. Determinism is not guaranteed across model versions or backend changes — pair with a pinned temperature for the strongest reproducibility OpenAI offers.
func WithTemperature ¶
WithTemperature pins the sampling temperature (0.0–2.0 for OpenAI). Use 0 for maximally reproducible classification/extraction turns; unset keeps the provider default. An explicit 0 is honored (the SDK's omitempty would otherwise drop it and silently revert to the provider default).
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider implements agent.LLMProvider using the OpenAI Chat Completions API.
func New ¶
New creates a wrapper over OpenAI's API. Auto-discovers OPENAI_API_KEY from environment if apiKey is empty.
Pass WithBaseURL to target an OpenAI-compatible endpoint instead; NewCompat is the same thing with baseURL required rather than optional.
func NewCompat ¶
NewCompat creates a provider for any OpenAI-compatible API endpoint. This covers: OpenRouter, Google Gemini (via OpenAI compat), Ollama, Groq, Together AI, vLLM, etc. baseURL must be an absolute HTTP(S) URL without embedded credentials, a query, or a fragment. Prefer HTTPS except for local development endpoints.
It is New plus WithBaseURL, with baseURL required rather than optional and model required rather than defaulted — a compatible endpoint has no sensible default model. Note that this configures the chat provider only: point NewEmbedder, NewVisionAnalyzer, and NewSummaryProvider at the same endpoint with WithBaseURL, or they will call api.openai.com.
Examples:
Gemini: NewCompat("GEMINI_API_KEY", "gemini-2.0-flash", "https://generativelanguage.googleapis.com/v1beta/openai")
OpenRouter: NewCompat("OPENROUTER_API_KEY", "openai/gpt-4o", "https://openrouter.ai/api/v1")
Ollama: NewCompat("ollama", "llama3", "http://localhost:11434/v1")
Groq: NewCompat("GROQ_KEY", "llama-3.3-70b-versatile", "https://api.groq.com/openai/v1")
func (*Provider) Capabilities ¶ added in v0.40.0
func (*Provider) Capabilities() agent.LLMCapabilities
Capabilities reports features implemented by this adapter. Compatible endpoints still need model-specific discovery when their catalog varies.
func (*Provider) GenerateStream ¶
func (p *Provider) GenerateStream(ctx context.Context, memory []history.Message, availableTools *tools.Registry, streamChan chan<- agent.StreamEvent) (agent.LLMResult, error)
GenerateStream maps GopherAgent history to OpenAI API format and triggers streaming generation.
type SummaryProvider ¶
type SummaryProvider struct {
// contains filtered or unexported fields
}
SummaryProvider implements history.SummaryProvider using OpenAI. It uses a cheap, fast model (default: gpt-4o-mini) to extract long-term behavioral patterns from conversation history, injected back into the system prompt on the next turn.
Usage:
sp, _ := openai.NewSummaryProvider("", "gpt-4o-mini")
sm := history.NewInMemSessionManager("You are an assistant.")
sm.SummaryProvider = sp
func NewSummaryProvider ¶
func NewSummaryProvider(apiKey, model string, opts ...ClientOption) (*SummaryProvider, error)
NewSummaryProvider creates an OpenAI-backed SummaryProvider. Auto-discovers OPENAI_API_KEY from environment if apiKey is empty. model defaults to "gpt-4o-mini" (cheap + fast — ideal for background summarization). Pass WithBaseURL to summarize against an OpenAI-compatible endpoint instead of api.openai.com.
func (*SummaryProvider) SummarizeBehaviors ¶
func (s *SummaryProvider) SummarizeBehaviors(ctx context.Context, newMsgs []history.Message, previousSummary string) (string, error)
SummarizeBehaviors analyzes recent messages and merges new evidence into the existing previousSummary, returning an updated behavioral profile paragraph. Only newMessages (since last summarization) are analyzed — not the full history.
type VisionAnalyzer ¶
type VisionAnalyzer struct {
// contains filtered or unexported fields
}
VisionAnalyzer calls a vision-capable OpenAI model (default gpt-4o) and returns a natural-language answer about the supplied image.
It satisfies pkg/tools/builtin.MediaAnalyzer by structural compatibility — no import of that package is needed.
media may be:
- an https:// URL publicly reachable by the OpenAI API
- a data URI ("data:image/png;base64,...") for locally hosted images
func NewVisionAnalyzer ¶
func NewVisionAnalyzer(apiKey, model string, opts ...ClientOption) (*VisionAnalyzer, error)
NewVisionAnalyzer builds an analyzer. apiKey defaults to OPENAI_API_KEY; model defaults to "gpt-4o" (smallest model with reliable vision support across all image tasks). Pass WithBaseURL to analyze against an OpenAI-compatible endpoint instead of api.openai.com.