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
- type Client
- func (c *Client) Embed(ctx context.Context, model llm.Model, messages []llm.Message, opts ...llm.Opt) ([]llm.Vector, llm.Usage, error)
- func (c *Client) GetModel(ctx context.Context, name string) (llm.Model, error)
- func (c *Client) ListModels(ctx context.Context) ([]llm.Model, error)
- func (*Client) Name() string
- func (c *Client) Ping(ctx context.Context) error
- func (c *Client) Recognize(ctx context.Context, model llm.Model, resource llm.Resource, opts ...llm.Opt) (llm.Document, llm.Usage, error)
- func (c *Client) Self() llm.Provider
- func (c *Client) Transcribe(ctx context.Context, model llm.Model, audio llm.Resource, ...) (llm.Transcript, llm.Usage, error)
- func (c *Client) Translate(ctx context.Context, model llm.Model, audio llm.Resource, ...) (llm.Transcript, llm.Usage, error)
- func (c *Client) URL() *url.URL
- func (c *Client) WithoutSession(ctx context.Context, model llm.Model, messages []llm.Message, tools []llm.Tool, ...) (llm.Message, llm.Usage, error)
- type Model
- func (m *Model) Aliases() []string
- func (m *Model) Capabilities() llm.ModelCap
- func (m *Model) Created() time.Time
- func (m *Model) Description() string
- func (m *Model) InputTokenLimit() *uint
- func (m *Model) Meta() map[string]json.RawMessage
- func (m *Model) Name() string
- func (m *Model) OutputTokenLimit() *uint
- func (m *Model) Provider() llm.Provider
Constants ¶
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 ¶
func New ¶
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 ¶
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 ¶
ListModels returns the models available from the provider.
func (*Client) Ping ¶
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.
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) 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) 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
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
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
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
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
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) OutputTokenLimit ¶ added in v0.5.0
OutputTokenLimit returns the model's maximum number of output tokens, for servers which return one. OpenAI itself doesn't.