openai

package
v0.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package openai adapts the official OpenAI Go SDK to elelem.Driver. OpenAI-compatible endpoints are supported through WithBaseURL.

Index

Constants

View Source
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

View Source
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

func KnownModels() []elelem.Model

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

func LookupModel(id string) elelem.Model

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 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

func (d *Driver) ListModels(ctx context.Context) ([]string, error)

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 uses elelem's provider-neutral estimator.

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 WithBaseURL

func WithBaseURL(baseURL string) DriverOption

WithBaseURL points the driver at an OpenAI-compatible endpoint.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL