openai

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 21 Imported by: 1 module, 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

openai implements an API client for OpenAI, and for other providers with an OpenAI-compatible API, such as vLLM, llama.cpp and LM Studio. https://platform.openai.com/docs/api-reference

Index

Constants

View Source
const (
	// OpenAIEndpoint is the endpoint of OpenAI's API, which is the default
	OpenAIEndpoint = "https://api.openai.com/v1"

	// DeepSeekEndpoint is the endpoint of DeepSeek's OpenAI-compatible API
	DeepSeekEndpoint = "https://api.deepseek.com"

	// MistralEndpoint is the endpoint of Mistral's OpenAI-compatible API
	MistralEndpoint = "https://api.mistral.ai/v1"

	// PingInterval is how long the result of pinging the API is kept, before
	// it is pinged again, so that third-party APIs aren't pinged too often
	PingInterval = 5 * time.Minute
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	*client.Client
	// contains filtered or unexported fields
}

func New

func New(endPoint, apiKey string, opts ...client.ClientOpt) (*Client, error)

Create a new client, with an OpenAI-compatible endpoint, such as "https://api.openai.com/v1" (the default), and an API key, which is sent as a bearer token. The key may be empty, for servers which don't need one.

func (*Client) Embed added in v0.5.0

func (c *Client) Embed(ctx context.Context, model llm.Model, messages []llm.Message, opts ...llm.Opt) ([]llm.Vector, llm.Usage, error)

Embed returns a vector for each of the messages, which must be for a model of this client which embeds, in the same order, and the tokens in their texts, in one request to the Embeddings API, which OpenAI-compatible servers such as Mistral, Gemini and Ollama have, but DeepSeek hasn't. A message is embedded by its text, which can't be empty, as the API embeds only text. The request isn't timed out, but is cancelled with the context.

func (*Client) GetModel

func (c *Client) GetModel(ctx context.Context, name string) (llm.Model, error)

GetModel returns the model with the given name. The model is found in the list of models, rather than retrieved by name, as not every OpenAI-compatible server can retrieve a single model, and model names can contain "/".

func (*Client) ListModels

func (c *Client) ListModels(ctx context.Context) ([]llm.Model, error)

ListModels returns the models available from the provider.

func (*Client) Name

func (*Client) Name() string

Name returns the provider name

func (*Client) Ping

func (c *Client) Ping(ctx context.Context) error

Ping checks the connectivity of the client, and that the API key is accepted, by listing the models. OpenAI-compatible APIs have no separate health or version endpoint. The API is only pinged once every ping interval: otherwise, the result of the last ping is returned, unless it ended as its caller's context did.

func (*Client) Recognize added in v0.5.0

func (c *Client) Recognize(ctx context.Context, model llm.Model, resource llm.Resource, opts ...llm.Opt) (llm.Document, llm.Usage, error)

Recognize returns the text in the resource, which is an image, or a document such as a PDF, as a document with the text of each of its pages, in Markdown, with its tables in the table format, in order, and their regions, which are Mistral's blocks, when they're asked for, and the pages recognized, with Mistral's OCR API, which other servers haven't. The model must be one of this client's which recognizes text, such as mistral-ocr-latest. The images in a document aren't returned, but the text has links to them. The request isn't timed out, as a document can take a while, but is cancelled with the context.

func (*Client) Self

func (c *Client) Self() llm.Provider

Self returns the underlying client implementation.

func (*Client) Transcribe added in v0.5.0

func (c *Client) Transcribe(ctx context.Context, model llm.Model, audio llm.Resource, stream llm.TranscriberStreamFn, opts ...llm.Opt) (llm.Transcript, llm.Usage, error)

Transcribe returns the speech in the audio as text, and its language, when the model returns it, and its segments, and who spoke them, when they're asked for, and the model returns them, which whisper and Mistral's models do, and OpenAI's diarizing models do with their speakers, and the seconds and tokens used. When the stream function isn't nil, the transcript is streamed, by Mistral's models, and OpenAI's other than whisper, or otherwise the function is called with it once it's transcribed. The request isn't timed out, as audio can take a while, but is cancelled with the context.

func (*Client) Translate added in v0.5.0

func (c *Client) Translate(ctx context.Context, model llm.Model, audio llm.Resource, stream llm.TranscriberStreamFn, opts ...llm.Opt) (llm.Transcript, llm.Usage, error)

Translate returns the speech in the audio, in any language, as English text, as Transcribe does, which only OpenAI's whisper does, and which isn't streamed. Returns ErrNotImplemented for a model which is known not to translate.

func (*Client) URL added in v0.5.0

func (c *Client) URL() *url.URL

URL returns the client's normalized endpoint

func (*Client) WithoutSession added in v0.5.0

func (c *Client) WithoutSession(ctx context.Context, model llm.Model, messages []llm.Message, tools []llm.Tool, stream llm.GeneratorStreamFn, opts ...llm.Opt) (llm.Message, llm.Usage, error)

WithoutSession returns the model's reply to the messages, which must be for a model of this client, and the tokens used, with the Chat Completions API, which OpenAI-compatible servers such as DeepSeek have. The model can call the tools, and the reply has its calls. When the stream function isn't nil, the reply is streamed, and the function is called with each piece of its reasoning and text as it's generated. The request isn't timed out, as a reasoning model can take a while to reply, but is cancelled with the context.

type Model added in v0.5.0

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

Model represents a single model, satisfying the llm.Model interface.

func (*Model) Aliases added in v0.5.0

func (m *Model) Aliases() []string

Aliases returns alternate names for the model, which some OpenAI-compatible servers return, such as Mistral, whose "-latest" names are aliases of a dated model. OpenAI itself doesn't.

func (*Model) Capabilities added in v0.5.0

func (m *Model) Capabilities() llm.ModelCap

Capabilities returns what the server reports the model can do, which Mistral does, as its capabilities, or none of them, with those which are known by the model's name, such as that Mistral's embedding models embed, and OpenAI's models, which it reports nothing of. Mistral doesn't report embedding or translation.

func (*Model) Created added in v0.5.0

func (m *Model) Created() time.Time

Created returns when the model was created, or the zero time if the server doesn't say.

func (*Model) Description added in v0.5.0

func (m *Model) Description() string

Description returns the model's description, or its display name, which some OpenAI-compatible servers return, such as OpenRouter and DeepSeek. OpenAI itself doesn't.

func (*Model) InputTokenLimit added in v0.5.0

func (m *Model) InputTokenLimit() *uint

InputTokenLimit returns the model's context length, for servers which return one. OpenAI itself doesn't.

func (*Model) Meta added in v0.5.0

func (m *Model) Meta() map[string]json.RawMessage

Meta returns provider-specific metadata: all the model's fields, apart from its name and creation time.

func (*Model) Name added in v0.5.0

func (m *Model) Name() string

Name returns the model name, e.g. "gpt-4o"

func (*Model) OutputTokenLimit added in v0.5.0

func (m *Model) OutputTokenLimit() *uint

OutputTokenLimit returns the model's maximum number of output tokens, for servers which return one. OpenAI itself doesn't.

func (*Model) Provider added in v0.5.0

func (m *Model) Provider() llm.Provider

Provider returns the client this model came from.

Jump to

Keyboard shortcuts

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