Documentation
¶
Overview ¶
Package openai adapts the OpenAI Chat Completions API to agent.Model. Because that API is the de-facto standard, this one adapter — via WithBaseURL — runs on OpenAI, Ollama (local), DeepSeek, Groq, Together, OpenRouter, vLLM, LM Studio, Azure, Mistral, xAI, and any other OpenAI-compatible endpoint. That's how "run on any model" is one adapter, not N.
Zero external deps (net/http + stdlib). Tool schemas use schema.OpenAIStrict when WithStrictSchema is set (genuine OpenAI structured-output strict mode — which many SDKs, incl. agenticenv, leave unused); off by default for max compatibility.
Index ¶
- type Model
- type Option
- func WithBaseURL(u string) Option
- func WithHTTPClient(c *http.Client) Option
- func WithMaxCompletionTokens(use bool) Option
- func WithMaxResponseBytes(n int64) Option
- func WithMaxTokens(n int) Option
- func WithModel(id string) Option
- func WithStrictSchema() Option
- func WithToolResultCodec(c provider.ToolResultCodec) Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Model ¶
type Model struct {
// contains filtered or unexported fields
}
Model is an OpenAI Chat Completions API adapter implementing agent.Model. It also serves OpenAI-compatible endpoints, such as Ollama and vLLM, through WithBaseURL.
func New ¶
New constructs an OpenAI-compatible adapter. For non-OpenAI endpoints set WithBaseURL (e.g. "http://localhost:11434/v1" for Ollama) and WithModel.
func (*Model) Describe ¶ added in v0.8.0
Describe reports the adapter's identity: provider "openai" (the Chat Completions API, also when WithBaseURL points it at a compatible server), the configured model ID, and support for a JSON-schema response format.
func (Model) Format ¶ added in v0.7.0
Format writes String for every verb, so no fmt verb (%v, %+v, %#v, %s, %x, ...) prints the key.
func (*Model) RequiresToolsForRequired ¶ added in v0.10.0
RequiresToolsForRequired implements agent.ToolRules: OpenAI refuses tool choice "required" with no tools declared.
func (Model) String ¶ added in v0.7.0
String describes the adapter by its model id. It never includes the API key (or the base URL, which can carry credentials of its own), so a config that holds a *Model can be logged.
func (*Model) ToolNameRule ¶ added in v0.10.0
ToolNameRule implements agent.ToolRules: the tool names OpenAI accepts, `^[a-zA-Z0-9_-]{1,64}$`, so agent.New refuses any other name when the agent is built.
type Option ¶
type Option func(*Model)
Option configures a Model in New.
func WithBaseURL ¶
WithBaseURL sets the API host, for a proxy or a compatible endpoint; /chat/completions is appended to it. The default is "https://api.openai.com/v1".
func WithHTTPClient ¶
WithHTTPClient sets the HTTP client requests are sent with. The default is http.DefaultClient.
func WithMaxCompletionTokens ¶ added in v0.7.0
WithMaxCompletionTokens chooses the request field that carries the token limit (WithMaxTokens, or Sampling.MaxTokens): max_completion_tokens when use is true, max_tokens when false. Without it the adapter decides: max_completion_tokens on OpenAI's own endpoint (api.openai.com, where max_tokens is deprecated and the reasoning models reject it) and for an OpenAI reasoning model (an o-series or gpt-5 model id, optionally behind a "vendor/" prefix) on any endpoint, and max_tokens otherwise, which is what OpenAI-compatible servers such as Ollama and vLLM implement. Set it for an endpoint the default gets wrong, such as Azure OpenAI serving a reasoning model under a deployment name.
func WithMaxResponseBytes ¶ added in v0.8.0
WithMaxResponseBytes caps how many bytes of one streamed reply the adapter reads: a reply that runs longer fails with agent.ErrResponseTooLarge, which middleware.Retryable does not retry. The default is provider.DefaultMaxResponseBytes (32 MiB); n <= 0 keeps it. Raise it for replies that legitimately run longer, such as large inline images.
func WithMaxTokens ¶
WithMaxTokens sets the default limit on tokens in a reply; a request's Sampling.MaxTokens overrides it. The default is 0, which leaves the limit to the provider.
func WithStrictSchema ¶
func WithStrictSchema() Option
WithStrictSchema sends every tool's input schema in OpenAI strict mode, rewritten by schema.OpenAIStrict. A request with a tool schema strict mode cannot express fails with agent.ErrConfig rather than being sent without strict mode.
func WithToolResultCodec ¶ added in v0.2.0
func WithToolResultCodec(c provider.ToolResultCodec) Option
WithToolResultCodec encodes tool results sent to the model with c instead of raw JSON (for example GCF, to cut tokens on structured output). The journal keeps the JSON form; only what the model reads changes. Default is JSON.