openai

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package openai adapts OpenAI's Chat Completions API to the provider-agnostic llm.Model port. It speaks the HTTP API directly (no vendor SDK), so the agent keeps its single-binary shape and the adapter stays a thin, fully-testable mapping. Chat Completions is stateless - the full conversation is sent on every call - which matches the port, and it is the format every OpenAI-compatible endpoint (local models, gateways) speaks, so the same adapter reaches all of them by changing the base URL. The default model is GPT-5.5.

Index

Constants

View Source
const (
	// DefaultModel is the model used when none is configured.
	DefaultModel = "gpt-5.5"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is an llm.Model backed by the OpenAI Chat Completions API.

func New

func New(apiKey secret.Text, opts ...Option) *Client

New builds a Client authenticating with apiKey. The key is held as a secret.Text, so it cannot leak through logging or formatting of the Client. With no HTTP client injected the shared core picks the governed default (netguard for a hosted endpoint, plain for loopback).

func (*Client) Generate

func (c *Client) Generate(ctx context.Context, req llm.Request) (llm.Response, error)

Generate implements llm.Model.

type Option

type Option func(*Client)

Option configures a Client.

func WithBaseURL

func WithBaseURL(u string) Option

WithBaseURL overrides the API base URL, so any OpenAI-compatible endpoint (a local server, a gateway) can be targeted. An unsafe URL (plaintext http to a non-loopback host, where the API key could be sniffed in transit) is rejected and the secure default is kept, so the override can never downgrade the transport. See llm.SafeBaseURL.

func WithHTTPClient

func WithHTTPClient(h *http.Client) Option

WithHTTPClient injects the HTTP client (tests supply a mock transport).

func WithMaxTokens

func WithMaxTokens(n int) Option

WithMaxTokens sets the per-turn output ceiling (a request's own MaxTokens wins; 0 leaves it to the model's default).

func WithModel

func WithModel(m string) Option

WithModel sets the model id (default DefaultModel).

func WithToolGrammar

func WithToolGrammar() Option

WithToolGrammar makes the client constrain a tool-using request to a grammar compiled from the offered tools, so the backend can only sample a structurally valid tool call: a real tool name bound to arguments that satisfy that tool's schema. It targets a local runtime that honors the grammar request field (a local model server), which is where a weaker model needs the structural guarantee most; a hosted endpoint that does not recognize the field simply ignores it. The constraint is attached only when every offered tool's schema can be compiled, so a request never advertises a tool the grammar would forbid. Off by default.

func WithVision

func WithVision() Option

WithVision marks the served model as able to accept image input, so a user message that carries an image is encoded as OpenAI vision content (a content array with image_url parts) instead of being refused. It is off by default: an arbitrary OpenAI-compatible endpoint may serve a text-only model, and silently dropping an image would let a picture vanish from a turn. Enable it only when the configured model can see; the hosted GPT and Gemini models do.

Jump to

Keyboard shortcuts

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