openai

package
v0.33.1 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// GPT-5.4 series (flagship, latest).
	ModelGPT54     = "gpt-5.4"
	ModelGPT54Mini = "gpt-5.4-mini"
	ModelGPT54Nano = "gpt-5.4-nano"
	ModelGPT54Pro  = "gpt-5.4-pro"

	// GPT-5.3 series.
	ModelGPT53Codex = "gpt-5.3-codex"

	// GPT-5.2 series.
	ModelGPT52      = "gpt-5.2"
	ModelGPT52Pro   = "gpt-5.2-pro"
	ModelGPT52Codex = "gpt-5.2-codex"

	// GPT-5.1 series.
	ModelGPT51          = "gpt-5.1"
	ModelGPT51Codex     = "gpt-5.1-codex"
	ModelGPT51CodexMax  = "gpt-5.1-codex-max"
	ModelGPT51CodexMini = "gpt-5.1-codex-mini"

	// GPT-5 series.
	ModelGPT5      = "gpt-5"
	ModelGPT5Mini  = "gpt-5-mini"
	ModelGPT5Nano  = "gpt-5-nano"
	ModelGPT5Pro   = "gpt-5-pro"
	ModelGPT5Codex = "gpt-5-codex"

	// GPT-4o series.
	ModelGPT4o     = "gpt-4o"
	ModelGPT4oMini = "gpt-4o-mini"

	// GPT-4.1 series.
	ModelGPT41     = "gpt-4.1"
	ModelGPT41Mini = "gpt-4.1-mini"
	ModelGPT41Nano = "gpt-4.1-nano"

	// Legacy models.
	ModelGPT4Turbo = "gpt-4-turbo"
	ModelGPT4      = "gpt-4"
	ModelGPT35     = "gpt-3.5-turbo"

	// O-series reasoning models.
	ModelO4Mini = "o4-mini"
	ModelO3     = "o3"
	ModelO3Mini = "o3-mini"
	ModelO3Pro  = "o3-pro"
	ModelO1     = "o1"
	ModelO1Mini = "o1-mini"
	ModelO1Pro  = "o1-pro"
)

Model ID constants for programmatic use.

View Source
const (

	// DefaultModel is the recommended default model (fast and capable).
	DefaultModel = "gpt-4o-mini"
)

Variables

View Source
var ErrUnknownModel = fmt.Errorf("unknown model")

ErrUnknownModel is returned when a model ID is not in the registry.

View Source
var ModelAliases = map[string]string{

	"flagship": ModelGPT54,
	"mini":     ModelGPT54Mini,
	"nano":     ModelGPT54Nano,
	"pro":      ModelGPT54Pro,

	"codex": ModelGPT53Codex,

	"o4": ModelO4Mini,
	"o3": ModelO3,
}

ModelAliases maps short alias names to full model IDs. These are used by the auto package for provider-prefixed resolution (e.g., "openai/mini").

Functions

func CodexLocalAvailable added in v0.32.0

func CodexLocalAvailable() bool

CodexLocalAvailable reports whether ~/.codex/auth.json exists and contains usable credentials. Does not validate the token against the server.

func DefaultOptions added in v0.12.0

func DefaultOptions() []llm.Option

DefaultOptions returns the default options for the OpenAI provider. The API key is read from OPENAI_API_KEY or OPENAI_KEY environment variables.

Types

type CodexAuth added in v0.32.0

type CodexAuth struct {
	// contains filtered or unexported fields
}

CodexAuth wraps Codex CLI OAuth credentials and handles transparent token refresh. It is safe for concurrent use.

Obtain one via LoadCodexAuth, then pass CodexAuth.Token to llm.WithAPIKeyFunc when constructing a Provider:

auth, err := openai.LoadCodexAuth()
if err != nil { ... }
p := openai.New(llm.WithAPIKeyFunc(auth.Token))

func LoadCodexAuth added in v0.32.0

func LoadCodexAuth() (*CodexAuth, error)

LoadCodexAuth reads ~/.codex/auth.json and returns a CodexAuth ready for use. Returns an error if the file does not exist or contains no tokens.

func (*CodexAuth) AccountID added in v0.32.0

func (c *CodexAuth) AccountID() string

AccountID returns the ChatGPT account UUID from the stored tokens. It is required as the chatgpt-account-id HTTP header on every Codex request.

func (*CodexAuth) NewProvider added in v0.32.0

func (c *CodexAuth) NewProvider(baseTransport ...http.RoundTripper) *Provider

NewProvider creates an OpenAI *Provider pre-configured to route requests to the ChatGPT Codex backend (https://chatgpt.com/backend-api).

The standard api.openai.com endpoints require an api.responses.write scope that the ChatGPT Plus OAuth token does not carry. The ChatGPT backend accepts the same token but requires three additional headers and store:false in the request body. codexTransport handles all of this transparently.

baseTransport is the underlying HTTP transport that codexTransport wraps. Pass nil (or omit) to use http.DefaultTransport. Callers that need custom proxy settings or timeouts should pass their own http.RoundTripper here.

The returned provider supports Codex models (gpt-5.3-codex, etc.) via the Responses API SSE format, which is identical between the two backends.

func (*CodexAuth) Token added in v0.32.0

func (c *CodexAuth) Token(ctx context.Context) (string, error)

Token returns a valid OAuth access token, refreshing it transparently when it is within [codexTokenExpiryBuffer] of expiry. It satisfies the signature expected by llm.WithAPIKeyFunc.

type Provider

type Provider struct {
	// contains filtered or unexported fields
}

Provider implements the OpenAI LLM backend. It dispatches to the Responses API for Codex models and to Chat Completions for everything else.

func New

func New(opts ...llm.Option) *Provider

New creates a new OpenAI provider with the given options applied on top of DefaultOptions.

func (*Provider) CountTokens added in v0.24.0

CountTokens estimates the number of input tokens for the given request using the tiktoken tokenizer. The encoding is selected per-model (o200k_base for GPT-4o and o-series; cl100k_base for GPT-4 and GPT-3.5).

The formula follows the OpenAI cookbook:

  • +4 tokens per message (role/framing overhead)
  • +3 tokens reply priming ("assistant" token prepended by the API)

Counts are exact for text-only conversations. Tool-heavy requests may vary by a small margin due to serialisation format differences.

func (*Provider) CreateStream

func (p *Provider) CreateStream(ctx context.Context, opts llm.Request) (llm.Stream, error)

Stream dispatches to the Responses API for Codex models and gpt-5.4-series models, and to Chat Completions for everything else.

Thought effort is validated and mapped before the request is forwarded. Unknown models (not in the registry) default to Chat Completions so that newly released models work without a registry update.

func (*Provider) FetchModels added in v0.19.0

func (p *Provider) FetchModels(ctx context.Context) ([]llm.Model, error)

FetchModels retrieves the live list of models from the OpenAI API.

func (*Provider) GetDefaultModel added in v0.19.0

func (p *Provider) GetDefaultModel() string

GetDefaultModel returns the configured default model ID.

func (*Provider) Models

func (p *Provider) Models() llm.Models

Models returns a curated list of well-known OpenAI models.

func (*Provider) Name

func (p *Provider) Name() string

func (*Provider) Resolve added in v0.29.0

func (p *Provider) Resolve(modelID string) (llm.Model, error)

func (*Provider) WithDefaultModel

func (p *Provider) WithDefaultModel(modelID string) *Provider

WithDefaultModel returns a copy of the provider using the given default model.

Jump to

Keyboard shortcuts

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