Documentation
¶
Overview ¶
Package openai adapts the official OpenAI Go SDK to elelem.Driver. OpenAI-compatible endpoints are supported through WithBaseURL.
Index ¶
- Constants
- Variables
- func KnownModels() []elelem.Model
- func LookupModel(id string) elelem.Model
- type AssistantMessageExtra
- type Driver
- func (d *Driver) Capabilities(model elelem.Model) elelem.Capabilities
- func (d *Driver) Complete(ctx context.Context, req elelem.DriverRequest, ...) (elelem.Usage, error)
- func (d *Driver) ListModels(ctx context.Context) ([]string, error)
- func (d *Driver) Stream(ctx context.Context, req elelem.DriverRequest, ...) (elelem.Usage, error)
- func (d *Driver) TokenCounter() elelem.TokenCounter
- type DriverOption
- func WithAPIKey(key string) DriverOption
- func WithAssistantMessageExtra(extra AssistantMessageExtra) DriverOption
- func WithBaseURL(baseURL string) DriverOption
- func WithFinishReasonMapper(mapper FinishReasonMapper) DriverOption
- func WithHTTPClient(client *http.Client) DriverOption
- func WithSDKOptions(opts ...option.RequestOption) DriverOption
- func WithoutEnvironmentDefaults() DriverOption
- type FinishReasonMapper
Constants ¶
const Name = "openai"
Name is the provider identifier for this driver — usable by callers selecting a driver by name. OpenAI-compatible endpoints reuse it.
Variables ¶
var ErrUnsupportedParameter = errors.New("unsupported OpenAI parameter")
ErrUnsupportedParameter indicates an elelem option the target OpenAI model rejects. Returned locally so the caller sees the offending parameter instead of a provider 400.
Functions ¶
func KnownModels ¶
KnownModels returns SDK-substantiated IDs and reasoning-family metadata. Context windows and pricing remain zero because they are volatile and are not supplied by the models endpoint or the vendored SDK.
func LookupModel ¶
LookupModel returns known metadata for id. An id that is not listed falls back to its FAMILY's metadata rather than to a bare Model.
The fallback is load-bearing, not politeness: capabilities() matches by family prefix, so an exact-match-only lookup left dated snapshots and unlisted siblings (o1-mini, o3-mini-<date>, …) reasoning-capable but with an empty ReasoningLevels — and the zero value resolves Min to "minimal", a gpt-5-only level the o-series rejects. The two must agree on family.
Types ¶
type AssistantMessageExtra ¶ added in v0.8.0
type AssistantMessageExtra func( context.Context, elelem.Model, elelem.Message, ) (map[string]any, error)
AssistantMessageExtra adds trusted provider-specific fields to translated assistant messages. It is intended for specialized drivers built over this transport, not for caller-provided request fields.
type Driver ¶
type Driver struct {
// contains filtered or unexported fields
}
Driver implements elelem.Driver over the official OpenAI Go SDK.
func NewDriver ¶
func NewDriver(opts ...DriverOption) *Driver
NewDriver builds an OpenAI-compatible driver.
func (*Driver) Capabilities ¶
func (d *Driver) Capabilities(model elelem.Model) elelem.Capabilities
Capabilities reports OpenAI chat-completion capabilities for a model.
func (*Driver) Complete ¶ added in v0.4.0
func (d *Driver) Complete( ctx context.Context, req elelem.DriverRequest, onDelta func(elelem.Delta) error, ) (elelem.Usage, error)
Complete issues the same chat completion with streaming off.
Chat.Completions.New omits the `stream` field entirely rather than sending `stream: false` — NewStreaming is the one that appends option.WithJSONSet("stream", true) — which is what a strict compat backend needs, since such a backend can reject an unexpected `stream: false` as readily as `stream: true`.
The response is adapted into ONE ChatCompletionChunk and pushed through the exact same publishChunk path the stream uses. That is deliberate: the refusal promotion, the tool-call index narrowing and the finish-reason mapping all live in that path and are each load-bearing. A second translation here would be the obvious way to write this and the reliable way to make the two paths disagree.
func (*Driver) ListModels ¶
ListModels returns the IDs exposed by the upstream models endpoint.
func (*Driver) Stream ¶
func (d *Driver) Stream( ctx context.Context, req elelem.DriverRequest, onDelta func(elelem.Delta) error, ) (elelem.Usage, error)
Stream issues one streaming chat completion.
func (*Driver) TokenCounter ¶
func (d *Driver) TokenCounter() elelem.TokenCounter
TokenCounter counts only reasoning fields this transport replays.
type DriverOption ¶
type DriverOption func(*driverConfig)
DriverOption configures an OpenAI driver.
func WithAPIKey ¶
func WithAPIKey(key string) DriverOption
WithAPIKey supplies the bearer token used by the upstream. When omitted, the SDK reads OPENAI_API_KEY.
func WithAssistantMessageExtra ¶ added in v0.8.0
func WithAssistantMessageExtra(extra AssistantMessageExtra) DriverOption
WithAssistantMessageExtra appends trusted fields to assistant messages. Specialized drivers use it to replay provider-owned response state on a later request, such as reasoning content that accompanies a tool call.
func WithBaseURL ¶
func WithBaseURL(baseURL string) DriverOption
WithBaseURL points the driver at an OpenAI-compatible endpoint.
func WithFinishReasonMapper ¶ added in v0.11.0
func WithFinishReasonMapper(mapper FinishReasonMapper) DriverOption
WithFinishReasonMapper consults mapper for any finish reason the transport does not recognize, after the response completes. The mapped value becomes Usage.FinishReason, the reason the engine records.
func WithHTTPClient ¶
func WithHTTPClient(client *http.Client) DriverOption
WithHTTPClient supplies the HTTP client used for upstream requests.
func WithSDKOptions ¶
func WithSDKOptions(opts ...option.RequestOption) DriverOption
WithSDKOptions applies official SDK request options at driver construction.
func WithoutEnvironmentDefaults ¶ added in v0.6.0
func WithoutEnvironmentDefaults() DriverOption
WithoutEnvironmentDefaults prevents the SDK from borrowing credentials or endpoint settings from OPENAI_* variables. Use it when the embedding app resolves each upstream's configuration itself, especially when one process holds clients for both authenticated and keyless OpenAI-compatible endpoints.
type FinishReasonMapper ¶ added in v0.11.0
type FinishReasonMapper func(raw string) (elelem.FinishReason, error)
FinishReasonMapper maps a provider finish reason this transport does not recognize. It returns the normalized reason, or an error when the provider reports a failure through the finish reason instead of an error event; the error ends the call. Returning FinishReasonUnset leaves the value unmapped. It is intended for specialized drivers over providers that extend the OpenAI finish-reason set.