openai

package
v0.56.12 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package openaiwire is the OpenAI Responses API adapter — the ONE implementation of the Responses dialect (request encoding, streaming decode, reasoning-item round-trip, tool calls, usage parsing), shared by the hosted gateway (which injects its own or the user's BYOK key) and the CLI's direct endpoint mode (api.openai.com / api.x.ai). Extracted verbatim from the gateway's internal provider package; transport encoding only — no routing policy, no metering.

Index

Constants

View Source
const EnvGrokKey = "XAI_API_KEY"

EnvGrokKey is the environment variable holding the xAI API key.

View Source
const EnvOpenAIKey = "OPENAI_API_KEY"

EnvOpenAIKey is the environment variable holding the OpenAI API key.

Variables

This section is empty.

Functions

This section is empty.

Types

type Grok

type Grok struct {
	*OpenAI
}

Grok is the ModelProvider for xAI's Grok API — the OpenAI adapter pointed at api.x.ai. xAI's /v1/responses endpoint is OpenAI-Responses-compatible and its documented Go path is exactly this (xAI ships Python/TS SDKs, no Go SDK), so Grok embeds the OpenAI adapter with vendor fields overridden instead of duplicating the dialect. That puts the Agent Tools web_search built-in INSIDE agentic serving turns (xAI retired Live Search — search_parameters → 410, 2026-07-18 — and Agent Tools on /v1/responses is its replacement), plus the same for the WebSearch/WebFetch side channel.

Vendor differences carried by the embedded adapter's fields:

  • backend "grok" — its own wire/ledger tag, distinct from "openai".
  • reasoning.effort clamped to low|high (xAI's vocabulary; xhigh 400s).
  • no encrypted-reasoning includable (OpenAI-only round-trip).

Search-fee metering rides the embedded adapter too: Agent Tools bills web_search PER INVOCATION ($5/1k, docs.x.ai/developers/pricing), and each invocation surfaces as a web_search_call output item — so the shared SearchCount counter prices grok turns via models.json search_fees["grok"]. (Live Search's per-source num_sources_used card is dead alongside search_parameters; there is nothing per-source left to read.)

func NewGrok

func NewGrok(apiKey string) *Grok

NewGrok returns a provider for the xAI Grok API. apiKey is the xAI API key (XAI_API_KEY); the default served model is grok-4.6 (the single model for all three strong-tier roles).

type OpenAI

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

OpenAI is the strong-tier ModelProvider backed by the OpenAI Responses API (GPT-5.6 family). It implements ModelProvider + Streamer + WebSearcher + WebFetcher — the same capability surface as the Anthropic provider. It maps wire.Request onto the Responses API (instructions + input items + flat function tools + reasoning.effort + prompt-cache breakpoints) and Responses output back to wire.Response.

The Responses API is item-structured (not message-structured): the conversation is a flat list of input items (messages, function calls, function outputs, reasoning), and the response is a flat list of output items. Reasoning items carry an encrypted_content blob that must be round-tripped on the next turn for stateless multi-turn tool use (store=false; we manage conversation state ourselves).

func NewOpenAI

func NewOpenAI(apiKey string) *OpenAI

NewOpenAI returns a client using the given API key. baseURL is left empty so the SDK defaults; tests override it via o.baseURL = srv.URL before use (same pattern as Anthropic).

func (*OpenAI) BaseURL

func (o *OpenAI) BaseURL() string

BaseURL reports the configured override ("" = the SDK default).

func (*OpenAI) Complete

func (o *OpenAI) Complete(ctx context.Context, r wire.Request) (wire.Response, error)

Complete satisfies the non-streamed contract by streaming under the hood and assembling the full Response — same rationale as Anthropic.Complete (the Responses API has the same long-operation streaming requirement for large max_tokens).

func (*OpenAI) Model

func (o *OpenAI) Model() string

Model returns the default model id (for display). The OpenAI provider serves whatever model resolve.go chose; this is the strong-tier default.

func (*OpenAI) SetBaseURL

func (o *OpenAI) SetBaseURL(u string)

SetBaseURL points the adapter at a different Responses-API host (tests, proxies, enterprise gateways). "" restores the SDK default.

func (*OpenAI) SetExtraHeaders added in v0.10.0

func (o *OpenAI) SetExtraHeaders(h map[string]string)

SetExtraHeaders sets identity headers sent on every request — the shape a subscription backend (ChatGPT/Codex) requires to accept the call.

func (*OpenAI) Speak added in v0.15.0

func (o *OpenAI) Speak(ctx context.Context, text string) ([]byte, error)

Speak synthesizes speech for text and returns OGG/Opus bytes — the container chat platforms accept as a voice note directly, so no transcoding step (and no ffmpeg) is needed anywhere downstream.

func (*OpenAI) Stream

Stream sends a streaming Responses request, forwarding text/usage to h as they arrive, and returns the fully assembled Response (so the agent loop is identical to the non-streaming path).

func (*OpenAI) Transcribe added in v0.15.0

func (o *OpenAI) Transcribe(ctx context.Context, path, mime string) (string, error)

Transcribe converts an audio file (ogg/mp3/m4a/wav/…) to text via the audio transcriptions endpoint. Exported from the provider home so the OpenAI SDK stays contained here (TestVendorSDKsOnlyInTheirAdapters); the gateway calls it for inbound voice notes. Tries the 4o mini transcribe model first and falls back to whisper-1 once on any API error.

func (*OpenAI) WebFetch

func (o *OpenAI) WebFetch(ctx context.Context, url string) (string, wire.Response, error)

WebFetch retrieves a URL using the web_search tool with the URL in the prompt (the Responses API has no dedicated url_fetch tool; the model retrieves and summarizes the URL content). Returns the readable content as text.

func (*OpenAI) WebSearch

func (o *OpenAI) WebSearch(ctx context.Context, query string) (string, wire.Response, error)

WebSearch answers a query using the Responses API's built-in web_search tool, returning the synthesized text (replaces Anthropic's server-side web_search).

Jump to

Keyboard shortcuts

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