Documentation
¶
Overview ¶
Package openai is the weft adapter for the OpenAI Chat Completions API and OpenAI-compatible servers (gateways, local models), wrapping the official openai-go SDK. weft never owns the HTTP: every request goes through the SDK's client, and the SDK's stream type is the contract.
Mapping highlights (ADR 0013 has the full tables):
- Text deltas pass through as ModelTextDelta; tool-call argument fragments are buffered by delta index and emitted as whole ModelToolCall values before ModelFinish — the core's uniform-streaming rent.
- Stop reasons map to weft's three; anything unmapped keeps its raw value on ModelFinish.Raw.
- A RoleTool message fans out to one OpenAI tool message per result; assistant ReasoningParts are dropped (Chat Completions has no reasoning input).
- SequentialTools sets parallel_tool_calls: false.
Retry stance: transport retries (429, 5xx, net.Error) belong to the SDK via MaxRetries; the weft loop never retries a model call, and logic retries are model-seam middleware.
The Responses API (reasoning items, built-in tools) is not wrapped here; it would be an openai/responses sub-package.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Model ¶
Model returns a weft.Model backed by the OpenAI Chat Completions API (or any compatible server, via BaseURL). A Model is immutable and safe for concurrent runs; tool definitions are converted once per *ToolDef and cached by pointer.
Example ¶
Constructing a model performs no I/O; a key ($OPENAI_API_KEY) is read only when a run starts. Swap providers by swapping this one line.
package main
import (
"context"
"github.com/weftgo/weft"
"github.com/weftgo/weft/openai"
)
func main() {
m := openai.Model("gpt-4o-mini")
echo := weft.Tool("echo", "Echo a message.", func(ctx context.Context, in struct {
Message string `json:"message" jsonschema:"the text to repeat"`
}) (string, error) {
return in.Message, nil
})
agt := weft.New(m, echo)
_ = agt // a run is agt.Generate(ctx, weft.Prompt("Echo: hello"))
}
Output:
Types ¶
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures the adapter at construction, the same functional style as the core. The zero configuration reads $OPENAI_API_KEY (and $OPENAI_BASE_URL) and does not retry.
func BaseURL ¶
BaseURL points the adapter at an OpenAI-compatible server — gateways, local models. Also read from $OPENAI_BASE_URL when the option is not given. The provider name stays "openai": a base URL host is not a provider identity and would leak into telemetry cardinality.
func Client ¶
Client uses an already-configured SDK client (Azure, custom transports); it overrides BaseURL, APIKey, and MaxRetries.
func IdleTimeout ¶
IdleTimeout is the maximum gap between two stream chunks before the call fails wrapping weft.ErrStreamIdle (default 60s; zero disables it). The ctx deadline stays the hard limit on the whole call — a slow but actively streaming response is never killed.
func MaxRetries ¶
MaxRetries forwards to the SDK's transport retry configuration (429/5xx/connection errors only). The weft loop never retries a model call; logic retries are model-seam middleware (TODO §4.1).
func MaxTokens ¶
MaxTokens caps a step's output tokens (max_completion_tokens). Zero keeps the provider default.
func Temperature ¶
Temperature sets the sampling temperature; it is not sent unless the option is given.