Documentation
¶
Index ¶
- Constants
- func New(ctx context.Context, settings chat.Settings, opts ...Option) (chat.ChatClient, error)
- type OpenAI
- func (a *OpenAI) Add(_ context.Context, prompt string, media ...chat.Media) error
- func (a *OpenAI) AddCached(ctx context.Context, prompt string, media ...chat.Media) error
- func (a *OpenAI) ApplyPolicyNow(ctx context.Context, policy chat.HistoryPolicy) (int, error)
- func (a *OpenAI) Ask(ctx context.Context, question string, target any, media ...chat.Media) error
- func (a *OpenAI) Chat(ctx context.Context, prompt string, media ...chat.Media) (string, error)
- func (a *OpenAI) History() chat.History
- func (o *OpenAI) Model() string
- func (o *OpenAI) Provider() chat.Provider
- func (a *OpenAI) Restore(snapshot *chat.Snapshot) error
- func (a *OpenAI) Save() (*chat.Snapshot, error)
- func (a *OpenAI) SetTools(tools []chat.Tool) error
- func (a *OpenAI) StreamChat(ctx context.Context, prompt string, callback chat.StreamCallback, ...) (string, error)
- func (a *OpenAI) SupportsEffort()
- func (a *OpenAI) SupportsSampling()
- func (a *OpenAI) SupportsStateless()
- type Option
Constants ¶
const EnvOpenAIKey = "OPENAI_API_KEY"
EnvOpenAIKey is the well-known unprefixed environment variable used as the ecosystem fallback when no explicit credential is configured.
Variables ¶
This section is empty.
Functions ¶
func New ¶ added in v0.9.0
New builds an OpenAI (or OpenAI-compatible) client, accepting options this provider owns. Reach for it when you need one; otherwise chat.New with the provider name does the same job through the registry.
It goes through chat.NewWithFactory rather than calling build directly, so it gets every guard chat.New applies: the endpoint is validated before credentials reach it, the logger this package logs through is resolved, provider defaults are applied, unapplicable settings are dropped and reported, and the built client is asserted able to carry what survived.
Calling build directly is what this used to do. An http:// endpoint was accepted with a nil error and the API key then went over the wire in cleartext, and a call with no Logger panicked. See https://gitlab.com/phpboyscout/go/chat-openai/-/issues/2 and spec 0015.
Types ¶
type OpenAI ¶
type OpenAI struct {
chat.UsageTracker
// contains filtered or unexported fields
}
func (*OpenAI) AddCached ¶ added in v0.5.0
AddCached appends a user turn. OpenAI caches eligible prefixes automatically and reliably — measured caching on every call after the first, covering 88% of a ~4,400-token prefix on gpt-5.4 and 99.7% on gpt-5.6 — so there is no annotation to send and this is Add with a documented intent.
The intent is not wasted: it tells the client a stable prefix exists, which is what prompt_cache_key is derived from.
func (*OpenAI) ApplyPolicyNow ¶ added in v0.11.0
ApplyPolicyNow bounds the retained conversation immediately, rather than waiting for the next request to do it.
It is the same sequence this client runs before every request — describe the turns, ask the policy, rewrite from the result — with two differences. There is no pending turn to hold out of the budget, because nothing is about to be answered. And the system turn is held out as it always is: it lives in the message list on this API but it is configuration, and a command changes what is in a conversation rather than how the client is configured.
A nil policy means the configured one. With none configured there is nothing to apply and it returns zero rather than an error.
func (*OpenAI) Ask ¶
Ask sends a question to the OpenAI chat client and expects a structured response which is unmarshalled into the target interface.
func (*OpenAI) Chat ¶
Chat sends a message and returns the response content. It handles tool calls internally.
func (*OpenAI) History ¶ added in v0.9.0
History reports the conversation this client will re-send, counting the committed turns and anything buffered by Add and not yet sent.
The system turn is included when there is one: it is re-sent on every call and costs input tokens like any other turn, so a caller watching for growth should see it.
Known is true — this provider owns its transcript, so the count is authoritative.
func (*OpenAI) Provider ¶ added in v0.6.0
Provider implements chat.ModelIdentifier, reporting which of the two registered providers this client was built for.
func (*OpenAI) Restore ¶
Restore replaces the current conversation state with a previously saved snapshot.
func (*OpenAI) Save ¶
Save captures the current OpenAI conversation state as a snapshot.
A stateless client retains no conversation, so its snapshot carries only the system prefix it starts every call from.
func (*OpenAI) StreamChat ¶
func (a *OpenAI) StreamChat(ctx context.Context, prompt string, callback chat.StreamCallback, media ...chat.Media) (string, error)
StreamChat implements StreamingChatClient.
func (*OpenAI) SupportsEffort ¶ added in v0.3.0
func (a *OpenAI) SupportsEffort()
SupportsEffort marks OpenAI as able to carry Config.Effort, via reasoning_effort. OpenAI's ladder is a superset of the neutral one, so all five rungs map directly.
func (*OpenAI) SupportsSampling ¶ added in v0.3.0
func (a *OpenAI) SupportsSampling()
SupportsSampling marks OpenAI as able to carry Config.Temperature and TopP.
Structural only: gpt-5.4 accepts both, but reasoning models in general are where vendors have been withdrawing sampling controls, so a model that refuses yields chat.ErrModelRejectedParameter at request time.
func (*OpenAI) SupportsStateless ¶ added in v0.2.0
func (a *OpenAI) SupportsStateless()
SupportsStateless marks OpenAI as honouring chat.Config.Stateless.
type Option ¶ added in v0.9.0
type Option func(*options)
Option configures an OpenAI client at construction.
These are knobs only this provider understands. The core's Config carries what every provider means the same way; anything one provider owns belongs to that provider's own constructor, where the compiler can see it. See https://gitlab.com/phpboyscout/go/chat/-/wikis/specs/0011-chat-provider-conformance D10.
func WithSeed ¶ added in v0.9.0
WithSeed pins the sampling seed, asking the backend for reproducible-ish completions. Unset, no seed is sent and the backend samples normally.
It replaces chat.Config.Seed, removed from the core in v0.10.0: it was an OpenAI-only field that every other provider carried and none could read.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
detectmodels
command
Command detectmodels reports when OpenAI has shipped something the module's default model does not reflect.
|
Command detectmodels reports when OpenAI has shipped something the module's default model does not reflect. |
|
genmodels
command
Command genmodels regenerates the per-model capability table.
|
Command genmodels regenerates the per-model capability table. |
|
modelfilter
Package modelfilter selects the general-purpose chat models from OpenAI's model listing.
|
Package modelfilter selects the general-purpose chat models from OpenAI's model listing. |