Documentation
¶
Overview ¶
Package openai is a client for the OpenAI API, built on the goloop/ai interface.
The Client implements ai.Client, so Generate and Stream work the same as with any other goloop AI provider. On top of that it exposes OpenAI's native endpoints and their full options: chat completions, the responses API (synchronous and streaming), embeddings, images, audio (transcription, translation and speech), moderations, models, files and batches.
c := openai.New(os.Getenv("OPENAI_API_KEY"))
resp, err := c.Generate(ctx, &ai.Request{
Model: openai.ModelGPT4oMini,
Messages: []ai.Message{ai.UserText("Say hello in one word.")},
})
It depends only on goloop/ai and the standard library. The chat wire format it speaks is the one most other providers copied, so this package doubles as the reference for the OpenAI-compatible drivers.
Index ¶
- Constants
- type Batch
- type BatchCounts
- type ChatChoice
- type ChatFunctionCall
- type ChatFunctionDef
- type ChatMessage
- type ChatRequest
- type ChatResponse
- type ChatStreamChunk
- type ChatTool
- type ChatToolCall
- type ChatUsage
- type Client
- func (c *Client) CancelBatch(ctx context.Context, id string) (*Batch, error)
- func (c *Client) ChatCompletion(ctx context.Context, req *ChatRequest) (*ChatResponse, error)
- func (c *Client) ChatCompletionStream(ctx context.Context, req *ChatRequest) iter.Seq2[ChatStreamChunk, error]
- func (c *Client) CreateBatch(ctx context.Context, inputFileID, endpoint, completionWindow string) (*Batch, error)
- func (c *Client) CreateResponse(ctx context.Context, req *ResponsesRequest) (*ResponsesResponse, error)
- func (c *Client) DeleteFile(ctx context.Context, id string) error
- func (c *Client) Embed(ctx context.Context, model string, input ...string) ([][]float64, error)
- func (c *Client) Embeddings(ctx context.Context, req *EmbeddingRequest) (*EmbeddingResponse, error)
- func (c *Client) FileContent(ctx context.Context, id string) ([]byte, error)
- func (c *Client) Files(ctx context.Context) ([]File, error)
- func (c *Client) Generate(ctx context.Context, req *ai.Request) (*ai.Response, error)
- func (c *Client) GenerateImage(ctx context.Context, req *ImageRequest) (*ImageResponse, error)
- func (c *Client) GetBatch(ctx context.Context, id string) (*Batch, error)
- func (c *Client) GetFile(ctx context.Context, id string) (*File, error)
- func (c *Client) GetModel(ctx context.Context, id string) (*Model, error)
- func (c *Client) ListBatches(ctx context.Context) ([]Batch, error)
- func (c *Client) Models(ctx context.Context) ([]Model, error)
- func (c *Client) Moderate(ctx context.Context, input string) (*ModerationResult, error)
- func (c *Client) ResponsesStream(ctx context.Context, req *ResponsesRequest) iter.Seq2[ResponseStreamEvent, error]
- func (c *Client) Speech(ctx context.Context, req *SpeechRequest) ([]byte, error)
- func (c *Client) Stream(ctx context.Context, req *ai.Request) iter.Seq2[ai.Chunk, error]
- func (c *Client) Transcribe(ctx context.Context, req *TranscriptionRequest) (string, error)
- func (c *Client) Translate(ctx context.Context, req *TranscriptionRequest) (string, error)
- func (c *Client) UploadFile(ctx context.Context, filename string, data []byte, purpose string) (*File, error)
- type Embedding
- type EmbeddingRequest
- type EmbeddingResponse
- type File
- type ImageData
- type ImageRequest
- type ImageResponse
- type Model
- type ModerationResponse
- type ModerationResult
- type Option
- type ResponseItem
- type ResponseOutput
- type ResponseStreamEvent
- type ResponsesRequest
- type ResponsesResponse
- type SpeechRequest
- type TranscriptionRequest
Examples ¶
Constants ¶
const ( ModelGPT4o = "gpt-4o" ModelGPT4oMini = "gpt-4o-mini" ModelGPT4Turbo = "gpt-4-turbo" ModelO3Mini = "o3-mini" )
Convenience model identifiers. Any model string is accepted; use Models to discover what the account can call.
const DefaultBaseURL = "https://api.openai.com/v1"
DefaultBaseURL is the OpenAI API base URL, including the version segment.
const DefaultModerationModel = "omni-moderation-latest"
DefaultModerationModel is used by Moderate when no model is given.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Batch ¶
type Batch struct {
ID string `json:"id"`
Object string `json:"object"`
Endpoint string `json:"endpoint"`
Status string `json:"status"`
InputFileID string `json:"input_file_id"`
OutputFileID string `json:"output_file_id"`
ErrorFileID string `json:"error_file_id"`
CreatedAt int64 `json:"created_at"`
RequestCounts BatchCounts `json:"request_counts"`
}
Batch is the state of a batch job.
type BatchCounts ¶
type BatchCounts struct {
Total int `json:"total"`
Completed int `json:"completed"`
Failed int `json:"failed"`
}
BatchCounts breaks down how many requests are in each state.
type ChatChoice ¶
type ChatChoice struct {
Index int `json:"index"`
Message ChatMessage `json:"message"`
FinishReason string `json:"finish_reason"`
}
ChatChoice is one completion choice.
type ChatFunctionCall ¶
ChatFunctionCall is the function name and JSON-encoded arguments of a call.
type ChatFunctionDef ¶
type ChatFunctionDef struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Parameters json.RawMessage `json:"parameters,omitempty"`
}
ChatFunctionDef is a function's name, description and JSON Schema parameters.
type ChatMessage ¶
type ChatMessage struct {
Role string `json:"role"`
Content any `json:"content,omitempty"`
Name string `json:"name,omitempty"`
ToolCalls []ChatToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
}
ChatMessage is one message in a chat request or response. Content is a string or a slice of content parts (for images).
type ChatRequest ¶
type ChatRequest struct {
Model string `json:"model"`
Messages []ChatMessage `json:"messages"`
Tools []ChatTool `json:"tools,omitempty"`
ToolChoice any `json:"tool_choice,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
MaxCompletionTokens int `json:"max_completion_tokens,omitempty"`
Stop []string `json:"stop,omitempty"`
N int `json:"n,omitempty"`
Seed *int `json:"seed,omitempty"`
ResponseFormat json.RawMessage `json:"response_format,omitempty"`
User string `json:"user,omitempty"`
Stream bool `json:"stream,omitempty"`
StreamOptions *streamOptions `json:"stream_options,omitempty"`
}
ChatRequest is the native chat completions request, exposing OpenAI's full option set (response_format, seed, n and so on). Build one directly for features the shared ai.Request does not model, or let Generate build it.
type ChatResponse ¶
type ChatResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Model string `json:"model"`
Choices []ChatChoice `json:"choices"`
Usage ChatUsage `json:"usage"`
}
ChatResponse is the native chat completions response.
type ChatStreamChunk ¶
type ChatStreamChunk struct {
ID string `json:"id"`
Model string `json:"model"`
Choices []struct {
Index int `json:"index"`
Delta struct {
Content string `json:"content"`
ToolCalls []struct {
Index int `json:"index"`
ID string `json:"id"`
Function struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
} `json:"function"`
} `json:"tool_calls"`
} `json:"delta"`
FinishReason string `json:"finish_reason"`
} `json:"choices"`
Usage *ChatUsage `json:"usage"`
}
ChatStreamChunk is one streamed chat completions event.
type ChatTool ¶
type ChatTool struct {
Type string `json:"type"`
Function ChatFunctionDef `json:"function"`
}
ChatTool declares a callable function.
type ChatToolCall ¶
type ChatToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Function ChatFunctionCall `json:"function"`
}
ChatToolCall is a tool call the model produced.
type ChatUsage ¶
type ChatUsage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
ChatUsage reports token usage for a chat completion.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is an OpenAI API client. It implements ai.Client and adds the provider's native endpoints.
func New ¶
New returns a Client for the given API key. Shared options (WithBaseURL, WithHTTPClient, WithTimeout, WithMaxRetries, WithHeader) and OpenAI options (WithOrg, WithProject) configure it.
Example ¶
package main
import (
"fmt"
"github.com/goloop/openai"
)
func main() {
c := openai.New("sk-...")
_ = c // use c.Generate, c.Stream, c.ChatCompletion, ...
fmt.Println(openai.ModelGPT4oMini)
}
Output: gpt-4o-mini
func (*Client) CancelBatch ¶
CancelBatch requests cancellation of a batch in progress.
func (*Client) ChatCompletion ¶
func (c *Client) ChatCompletion(ctx context.Context, req *ChatRequest) (*ChatResponse, error)
ChatCompletion sends a native chat completions request and returns the whole response. Use it for OpenAI-specific options; use Generate for the shared, provider-agnostic path.
Example ¶
ExampleClient_ChatCompletion shows a native request with structured output.
package main
import (
"encoding/json"
"fmt"
"github.com/goloop/openai"
)
func main() {
req := &openai.ChatRequest{
Model: openai.ModelGPT4oMini,
Messages: []openai.ChatMessage{{Role: "user", Content: "as JSON"}},
ResponseFormat: json.RawMessage(`{"type":"json_object"}`),
}
fmt.Println(req.Model)
}
Output: gpt-4o-mini
func (*Client) ChatCompletionStream ¶
func (c *Client) ChatCompletionStream(ctx context.Context, req *ChatRequest) iter.Seq2[ChatStreamChunk, error]
ChatCompletionStream sends a native streaming chat request and returns an iterator over the raw chunks.
func (*Client) CreateBatch ¶
func (c *Client) CreateBatch( ctx context.Context, inputFileID, endpoint, completionWindow string, ) (*Batch, error)
CreateBatch starts a batch that runs the requests in the uploaded input file against endpoint (for example "/v1/chat/completions"). completionWindow is usually "24h". Upload the JSONL input with UploadFile(purpose "batch") first.
func (*Client) CreateResponse ¶
func (c *Client) CreateResponse(ctx context.Context, req *ResponsesRequest) (*ResponsesResponse, error)
CreateResponse sends a request to the responses API.
func (*Client) DeleteFile ¶
DeleteFile deletes an uploaded file.
func (*Client) Embed ¶
Embed is a convenience that returns the embedding vectors for the given inputs, in order.
func (*Client) Embeddings ¶
func (c *Client) Embeddings(ctx context.Context, req *EmbeddingRequest) (*EmbeddingResponse, error)
Embeddings returns embedding vectors for the request's inputs.
func (*Client) FileContent ¶
FileContent downloads a file's contents.
func (*Client) Generate ¶
Generate implements ai.Client over chat completions. It returns the first choice; to request and read several choices (n > 1) use the native Client.ChatCompletion, which exposes every choice.
Example ¶
ExampleClient_Generate builds a request. Sending it needs a real API key, so this example only shows the shape.
package main
import (
"fmt"
"github.com/goloop/ai"
"github.com/goloop/openai"
)
func main() {
req := &ai.Request{
Model: openai.ModelGPT4oMini,
Messages: []ai.Message{
ai.UserText("Name the capital of France."),
},
}
fmt.Println(req.Model, len(req.Messages))
}
Output: gpt-4o-mini 1
func (*Client) GenerateImage ¶
func (c *Client) GenerateImage(ctx context.Context, req *ImageRequest) (*ImageResponse, error)
GenerateImage creates images from a text prompt.
func (*Client) ListBatches ¶
ListBatches lists batches, most recent first.
func (*Client) ResponsesStream ¶ added in v0.1.1
func (c *Client) ResponsesStream(ctx context.Context, req *ResponsesRequest) iter.Seq2[ResponseStreamEvent, error]
ResponsesStream sends a streaming responses request and yields each raw event as it arrives. Text deltas come as "response.output_text.delta" events; the final "response.completed" event carries the full result and token usage.
Example ¶
ExampleClient_ResponsesStream shows a streaming responses request. Ranging over ResponsesStream yields raw events; text arrives on "response.output_text.delta" and the final "response.completed" carries the whole result and token usage.
package main
import (
"fmt"
"github.com/goloop/openai"
)
func main() {
req := &openai.ResponsesRequest{
Model: openai.ModelGPT4oMini,
Input: "Tell me a joke.",
}
fmt.Println(req.Model)
}
Output: gpt-4o-mini
func (*Client) Speech ¶
Speech synthesizes audio for the given text and returns the raw audio bytes.
func (*Client) Transcribe ¶
Transcribe converts speech in the audio file to text in the same language.
type EmbeddingRequest ¶
type EmbeddingRequest struct {
Model string `json:"model"`
Input []string `json:"input"`
Dimensions int `json:"dimensions,omitempty"`
EncodingFormat string `json:"encoding_format,omitempty"`
User string `json:"user,omitempty"`
}
EmbeddingRequest is an embeddings request.
type EmbeddingResponse ¶
type EmbeddingResponse struct {
Object string `json:"object"`
Model string `json:"model"`
Data []Embedding `json:"data"`
Usage struct {
PromptTokens int `json:"prompt_tokens"`
TotalTokens int `json:"total_tokens"`
} `json:"usage"`
}
EmbeddingResponse is an embeddings response.
type File ¶
type File struct {
ID string `json:"id"`
Object string `json:"object"`
Bytes int64 `json:"bytes"`
CreatedAt int64 `json:"created_at"`
Filename string `json:"filename"`
Purpose string `json:"purpose"`
}
File describes an uploaded file.
type ImageData ¶
type ImageData struct {
URL string `json:"url"`
B64JSON string `json:"b64_json"`
RevisedPrompt string `json:"revised_prompt"`
}
ImageData is one generated image, as a URL or base64 JSON.
type ImageRequest ¶
type ImageRequest struct {
Model string `json:"model,omitempty"`
Prompt string `json:"prompt"`
N int `json:"n,omitempty"`
Size string `json:"size,omitempty"`
Quality string `json:"quality,omitempty"`
Style string `json:"style,omitempty"`
ResponseFormat string `json:"response_format,omitempty"` // "url" or "b64_json"
User string `json:"user,omitempty"`
}
ImageRequest is an image generation request.
type ImageResponse ¶
ImageResponse is an image generation response.
type Model ¶
type Model struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
OwnedBy string `json:"owned_by"`
}
Model describes a model returned by the models endpoint.
type ModerationResponse ¶
type ModerationResponse struct {
ID string `json:"id"`
Model string `json:"model"`
Results []ModerationResult `json:"results"`
}
ModerationResponse is a moderations response.
type ModerationResult ¶
type ModerationResult struct {
Flagged bool `json:"flagged"`
Categories map[string]bool `json:"categories"`
CategoryScores map[string]float64 `json:"category_scores"`
}
ModerationResult is the classification of one input.
type Option ¶
type Option func(*settings)
Option configures a Client in New.
func WithBaseURL ¶
WithBaseURL overrides the API base URL (proxies, gateways, mock servers, OpenAI-compatible endpoints).
func WithHTTPClient ¶
WithHTTPClient sets the HTTP client used for requests.
func WithHeader ¶
WithHeader adds a header sent with every request.
func WithMaxRetries ¶
WithMaxRetries sets how many times a request is retried on 429 and 5xx.
func WithTimeout ¶
WithTimeout sets the per-request timeout when no custom HTTP client is set.
type ResponseItem ¶ added in v0.1.2
type ResponseItem struct {
Type string `json:"type"`
ID string `json:"id"`
CallID string `json:"call_id"`
Name string `json:"name"`
Arguments string `json:"arguments"`
}
ResponseItem is an output item announced by a "response.output_item.added" event. For a function call it carries the call's ID, name and the arguments accumulated so far.
type ResponseOutput ¶
type ResponseOutput struct {
Type string `json:"type"`
Content []struct {
Type string `json:"type"`
Text string `json:"text"`
} `json:"content"`
}
ResponseOutput is one output item from the responses API.
type ResponseStreamEvent ¶ added in v0.1.1
type ResponseStreamEvent struct {
Type string `json:"type"`
Delta string `json:"delta"`
Arguments string `json:"arguments"`
ItemID string `json:"item_id"`
OutputIndex int `json:"output_index"`
Item *ResponseItem `json:"item"`
Response *ResponsesResponse `json:"response"`
Message string `json:"message"`
Code string `json:"code"`
}
ResponseStreamEvent is one server-sent event of a streaming responses request. Type names the event and selects which fields apply:
- text: "response.output_text.delta" (Delta);
- tool call: "response.output_item.added" announces the call (Item, with its name and call_id), "response.function_call_arguments.delta" streams the JSON arguments (Delta, keyed by ItemID), and "response.function_call_arguments.done" carries the full Arguments;
- result: "response.completed"/"response.incomplete" (Response);
- failure: "response.failed"/"error" (Message, Code).
type ResponsesRequest ¶
type ResponsesRequest struct {
Model string `json:"model"`
Input any `json:"input"`
Instructions string `json:"instructions,omitempty"`
MaxOutputTokens int `json:"max_output_tokens,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
Store *bool `json:"store,omitempty"`
Stream bool `json:"stream,omitempty"`
}
ResponsesRequest is a request to the responses API, OpenAI's newer stateful generation endpoint. Input is a plain string or a structured input array.
type ResponsesResponse ¶
type ResponsesResponse struct {
ID string `json:"id"`
Model string `json:"model"`
Status string `json:"status"`
Output []ResponseOutput `json:"output"`
Usage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
} `json:"usage"`
}
ResponsesResponse is a responses API result.
func (*ResponsesResponse) Text ¶
func (r *ResponsesResponse) Text() string
Text returns the concatenation of the output's text segments.
type SpeechRequest ¶
type SpeechRequest struct {
Model string `json:"model"`
Input string `json:"input"`
Voice string `json:"voice"`
Format string `json:"response_format,omitempty"`
Speed float64 `json:"speed,omitempty"`
}
SpeechRequest turns text into speech.
type TranscriptionRequest ¶
type TranscriptionRequest struct {
Model string
File []byte
Filename string
Language string // transcription only, optional
Prompt string
Format string // response_format, for example "json" or "text"
}
TranscriptionRequest transcribes or translates an audio file. File holds the audio bytes and Filename gives them an extension the API can recognize (for example "audio.mp3").