Documentation
¶
Overview ¶
Package genai is the opiniated high performance professional-grade AI package for Go.
It provides a generic interface to interact with various LLM providers, while allowing full access to each provider's full capabilities.
Check out the examples for a quick start.
Example (GenSyncWithToolCallLoop_with_custom_HTTP_Header) ¶
package main
import (
"context"
"fmt"
"log"
"net/http"
"time"
"github.com/maruel/genai"
"github.com/maruel/genai/adapters"
"github.com/maruel/genai/providers/anthropic"
"github.com/maruel/roundtrippers"
)
func main() {
// Modified version of the example in package adapters, with a custom header.
//
// As of June 2025, interleaved thinking can be enabled with a custom header.
// https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking#interleaved-thinking
wrapper := func(h http.RoundTripper) http.RoundTripper {
return &roundtrippers.Header{
Transport: h,
Header: http.Header{"anthropic-beta": []string{"interleaved-thinking-2025-05-14"}},
}
}
ctx := context.Background()
c, err := anthropic.New(ctx, &genai.ProviderOptions{Model: "claude-sonnet-4-20250514"}, wrapper)
if err != nil {
log.Fatal(err)
}
msgs := genai.Messages{genai.NewTextMessage("What season is Montréal currently in?")}
opts := genai.OptionsTools{
Tools: []genai.ToolDef{locationClockTime},
// Force the LLM to do a tool call first.
Force: genai.ToolCallRequired,
}
newMsgs, _, err := adapters.GenSyncWithToolCallLoop(ctx, c, msgs, &opts)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", newMsgs[len(newMsgs)-1].String())
}
var locationClockTime = genai.ToolDef{
Name: "get_today_date_current_clock_time",
Description: "Get the current clock time and today's date.",
Callback: func(ctx context.Context, e *location) (string, error) {
if e.Location != "Montréal" {
return "ask again with Montréal", nil
}
return time.Now().Format("Monday 2006-01-02 15:04:05"), nil
},
}
type location struct {
Location string `json:"location" json_description:"Location to ask the current time in"`
}
Index ¶
- Constants
- type CacheEntry
- type Citation
- type CitationSource
- type CitationType
- type Doc
- type FinishReason
- type Job
- type Logprob
- type Message
- func (m *Message) Accumulate(mf Reply) error
- func (m *Message) Decode(x any) error
- func (m *Message) DoToolCalls(ctx context.Context, tools []ToolDef) (Message, error)
- func (m *Message) GoString() string
- func (m *Message) IsZero() bool
- func (m *Message) Reasoning() string
- func (m *Message) Role() string
- func (m *Message) String() string
- func (m *Message) UnmarshalJSON(b []byte) error
- func (m *Message) Validate() error
- type Messages
- type Modalities
- type Modality
- type Model
- type Options
- type OptionsAudio
- type OptionsImage
- type OptionsText
- type OptionsTools
- type OptionsVideo
- type Provider
- type ProviderCapabilities
- type ProviderOptions
- type ProviderPing
- type ProviderUnwrap
- type RateLimit
- type RateLimitPeriod
- type RateLimitType
- type Reply
- type Request
- type Result
- type ToolCall
- type ToolCallRequest
- type ToolCallResult
- type ToolDef
- type Usage
Examples ¶
Constants ¶
const ( // ModelNone explicitly tells the provider to not automatically select a model. The use case is when the // only intended call is ListModel(), thus there's no point into selecting a model automatically. ModelNone = "NONE" // ModelCheap requests the provider to automatically select the cheapest model it can find. ModelCheap = "CHEAP" // ModelGood requests the provider to automatically select a good every day model that has a good // performance/cost trade-off. ModelGood = "GOOD" // ModelSOTA requests the provider to automatically select the best state-of-the-art model // it can find. ModelSOTA = "SOTA" )
Model markers to pass to ProviderOptions.Model.
const ( // ModalityAudio is support for audio formats like MP3, WAV, Opus, Flac, etc. ModalityAudio = scoreboard.ModalityAudio // ModalityDocument is support for PDF with multi-modal comprehension, both images and text. This includes // code blocks. ModalityDocument = scoreboard.ModalityDocument // ModalityImage is support for image formats like PNG, JPEG, often single frame GIF, and WEBP. ModalityImage = scoreboard.ModalityImage // ModalityText is for raw text. ModalityText = scoreboard.ModalityText // ModalityVideo is support for video formats like MP4 or MKV. ModalityVideo = scoreboard.ModalityVideo )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CacheEntry ¶
CacheEntry is one file (or GenSync request) cached on the provider for reuse.
type Citation ¶
type Citation struct {
// CitedText is the text that was cited.
CitedText string `json:"cited_text,omitzero"`
// StartIndex is the starting character position of the citation in the answer (0-based).
StartIndex int64 `json:"start_index,omitzero"`
// EndIndex is the ending character position of the citation in the answer (0-based, exclusive).
EndIndex int64 `json:"end_index,omitzero"`
// Sources contains information about the source documents or tools that support this citation.
Sources []CitationSource `json:"sources,omitzero"`
// contains filtered or unexported fields
}
Citation represents a reference to source material that supports content. It provides a unified interface for different provider citation formats.
Normally one of CitedText or StartIndex/EndIndex is set.
type CitationSource ¶
type CitationSource struct {
// Type indicates the source type.
Type CitationType `json:"type,omitzero"`
// ID is a unique identifier for the source (e.g., document ID, tool call ID).
ID string `json:"id,omitzero"`
// Title is the human-readable title of the source.
Title string `json:"title,omitzero"`
// URL is the web URL for the source, if applicable.
URL string `json:"url,omitzero"`
// Snippet is a snippet from the source, if applicable. It is the web search query for CitationWebQuery.
Snippet string `json:"snippet,omitzero"`
// StartCharIndex is the starting character position of the citation in the sourced document (0-based).
StartCharIndex int64 `json:"start_index,omitzero"`
// EndCharIndex is the ending character position of the citation in the the sourced document (0-based, exclusive).
EndCharIndex int64 `json:"end_index,omitzero"`
// StartPageNumber is the starting page number of the citation in the sourced document (1-based).
StartPageNumber int64 `json:"start_page_number,omitzero"`
EndPageNumber int64 `json:"end_page_number,omitzero"`
// StartBlockIndex is the starting block index of the citation in the sourced document (0-based).
StartBlockIndex int64 `json:"start_block_index,omitzero"`
EndBlockIndex int64 `json:"end_block_index,omitzero"`
// Date is the date of the source, if applicable.
Date string `json:"date,omitzero"`
// Metadata contains additional source-specific information.
// For document sources: document index, page numbers, etc.
// For tool sources: tool output, function name, etc.
// For web sources: encrypted index, search result info, etc.
Metadata map[string]any `json:"metadata,omitzero"`
// contains filtered or unexported fields
}
CitationSource represents a source that supports a citation.
func (*CitationSource) IsZero ¶
func (cs *CitationSource) IsZero() bool
func (*CitationSource) Validate ¶
func (cs *CitationSource) Validate() error
Validate ensures the citation source is valid.
type CitationType ¶
type CitationType int32
CitationType is a citation that a model returned as part of its reply.
const ( // CitationWebQuery is an query used as part of a web search. CitationWebQuery CitationType = iota + 1 // CitationWeb is an URL from a web search. CitationWeb // CitationWebImage is an URL to an image from a web search. CitationWebImage // CitationDocument is from a document provided as input or explicitly referenced. CitationDocument // CitationTool is when the provider refers to the result of a tool call in its answer. CitationTool )
type Doc ¶
type Doc struct {
// Filename is the name of the file. For many providers, only the extension
// is relevant. They only use mime-type, which is derived from the filename's
// extension. When an URL is provided or when the object provided to Document
// implements a method with the signature `Name() string`, like an
// `*os.File`, Filename is optional.
Filename string `json:"filename,omitzero"`
// Src is raw document data. It is perfectly fine to use a bytes.NewReader() or *os.File.
Src io.ReadSeeker `json:"bytes,omitzero"`
// URL is the reference to the raw data. When set, the mime-type is derived from the URL.
URL string `json:"url,omitzero"`
// contains filtered or unexported fields
}
Doc is a document.
func (*Doc) GetFilename ¶
GetFilename returns the filename to use for the document, querying the Document's name if available.
func (*Doc) MarshalJSON ¶
func (*Doc) Read ¶
Read reads the document content into memory.
It returns the mime type, the raw bytes and an error if any.
func (*Doc) UnmarshalJSON ¶
type FinishReason ¶
type FinishReason string
FinishReason is the reason why the model stopped generating tokens.
It can be one of the well known below or a custom value.
const ( // FinishedStop means the LLM was done for the turn. Some providers confuse it with // FinishedStopSequence. FinishedStop FinishReason = "stop" // FinishedLength means the model reached the maximum number of tokens allowed as set in // OptionsText.MaxTokens or as limited by the provider. FinishedLength FinishReason = "length" // FinishedToolCalls means the model called one or multiple tools and needs the replies to continue the turn. FinishedToolCalls FinishReason = "tool_calls" // FinishedStopSequence means the model stopped because it saw a stop word as listed in OptionsText.Stop. FinishedStopSequence FinishReason = "stop" // FinishedContentFilter means the model stopped because the reply got caught by a content filter. FinishedContentFilter FinishReason = "content_filter" // Pending means that it's not finished yet. For use with ProviderGenAsync. Pending FinishReason = "pending" )
type Logprob ¶
type Logprob struct {
ID int64 `json:"id,omitempty"` // Input token ID.
Text string `json:"text,omitempty"` // Text in UTF-8.
Logprob float64 `json:"logprob"` // Log probability of the token. It should normally be non-zero but sometimes it is.
}
Logprob represents a single log probability information for a token.
One of ID or Text must be set.
type Message ¶
type Message struct {
// Requests is the content from the user.
//
// It is normally a single message. It is more frequently multiple items when using multi-modal content.
Requests []Request `json:"request,omitzero"`
// User must only be used when sent by the user. Only some provider (e.g. OpenAI, Groq, DeepSeek) support it.
User string `json:"user,omitzero"`
// Replies is the message from the LLM.
//
// It is generally a single reply with text or a tool call. Some models can emit multiple content blocks,
// either multi modal or multiple text blocks: a code block and a different block with an explanantion. Some
// models can emit multiple tool calls at once.
Replies []Reply `json:"reply,omitzero"`
// ToolCallResult is the result for a tool call that the LLM requested to make.
//
// These messages are generated by the "computer".
ToolCallResults []ToolCallResult `json:"tool_call_results,omitzero"`
// contains filtered or unexported fields
}
Message is a part of an exchange with a LLM.
It is effectively a union, with the exception of the User field that can be set with In.
func NewTextMessage ¶
NewTextMessage is a shorthand function to create a Message with a single text block.
func (*Message) Accumulate ¶
Accumulate adds a Reply to the message being streamed.
It is used by GenStream. There's generally no need to call it by end users.
func (*Message) Decode ¶
Decode decodes the JSON message into the struct.
Requires using either ReplyAsJSON or DecodeAs in the OptionsText.
Note: this doesn't verify the type is the same as specified in OptionsText.DecodeAs.
func (*Message) DoToolCalls ¶
DoToolCalls processes all the ToolCall in the Reply if any.
Returns a Message to be added back to the list of messages, only if msg.IsZero() is true.
func (*Message) GoString ¶
GoString returns a JSON representation of the reply for debugging purposes.
func (*Message) String ¶
String is a short hand to get the request or reply content as text.
It ignores reasoning or multi-modal content.
func (*Message) UnmarshalJSON ¶
UnmarshalJSON adds validation during decoding.
type Messages ¶
type Messages []Message
Messages is a list of valid messages in an exchange with a LLM.
The messages should be alternating between user input, assistant replies, tool call requests and computer tool call results. The exception in the case of multi-user discussion, with different Users.
type Modalities ¶
type Modalities []scoreboard.Modality
Modalities represents the modality supported by the provider in a specific scenario. It can be multiple modalities in multi-modals scenarios.
func (Modalities) String ¶
func (m Modalities) String() string
func (Modalities) Validate ¶
func (m Modalities) Validate() error
type Modality ¶
type Modality = scoreboard.Modality
Modality is a modality supported by the provider.
It is aliased from scoreboard.Modality to avoid a circular dependency.
type Model ¶
type Model interface {
GetID() string
String() string
// Context returns the number of tokens the model can process as input.
Context() int64
}
Model represents a served model by the provider.
Use Provider.ListModels() to get a list of models.
type Options ¶
type Options interface {
// Validate ensures the options object is valid.
Validate() error
}
Options is options that can be provided to a Provider interface.
type OptionsAudio ¶
type OptionsAudio struct {
// Seed for the random number generator. Default is 0 which means
// non-deterministic.
Seed int64
// contains filtered or unexported fields
}
func (*OptionsAudio) Validate ¶
func (o *OptionsAudio) Validate() error
type OptionsImage ¶
type OptionsImage struct {
// Seed for the random number generator. Default is 0 which means
// non-deterministic.
Seed int64
Width int
Height int
// PollInterval is the time interval to poll the image generation progress when using GenSync.
PollInterval time.Duration
// contains filtered or unexported fields
}
OptionsImage is a list of frequent options supported by most ProviderDoc. Each provider is free to support more options through a specialized struct.
func (*OptionsImage) Validate ¶
func (o *OptionsImage) Validate() error
Validate ensures the completion options are valid.
type OptionsText ¶
type OptionsText struct {
// Temperature adjust the creativity of the sampling. Generally between 0 and 2.
Temperature float64
// TopP adjusts correctness sampling between 0 and 1. The higher the more diverse the output.
TopP float64
// MaxTokens is the maximum number of tokens to generate. Used to limit it
// lower than the default maximum, for budget reasons.
MaxTokens int64
// TopLogprobs requests to return the top logprobs in the reply.
TopLogprobs int64
// SystemPrompt is the prompt to use for the system role.
SystemPrompt string
// Seed for the random number generator. Default is 0 which means
// non-deterministic.
Seed int64
// TopK adjusts sampling where only the N first candidates are considered.
TopK int64
// Stop is the list of tokens to stop generation.
Stop []string
// ReplyAsJSON enforces the output to be valid JSON, any JSON. It is
// important to tell the model to reply in JSON in the prompt itself.
ReplyAsJSON bool
// DecodeAs enforces a reply with a specific JSON structure. It must be a pointer to a struct that can be
// decoded by encoding/json and can have jsonschema tags.
//
// It is important to request the model to "reply in JSON" in the prompt itself.
//
// It is recommended to use jsonschema_description tags to describe each
// field or argument.
//
// Use jsonschema:"enum=..." to enforce a specific value within a set.
//
// Use omitempty to make the field optional.
//
// See https://github.com/invopop/jsonschema#example for more examples.
DecodeAs any
// contains filtered or unexported fields
}
OptionsText is a list of frequent options supported by most Provider with text output modality. Each provider is free to support more options through a specialized struct.
The first group are options supported by (nearly) all providers.
The second group are options supported only by some providers. Using them may cause the chat operation to return a base.ErrNotsupported.
The third group are options supported by a few providers and a few models on each, that will slow down generation (increase latency) and will increase token use (cost).
func (*OptionsText) Validate ¶
func (o *OptionsText) Validate() error
Validate ensures the completion options are valid.
type OptionsTools ¶
type OptionsTools struct {
// Tools is the list of tools that the LLM can request to call.
Tools []ToolDef
// Force tells the LLM a tool call must be done, or not.
Force ToolCallRequest
// WebSearch specifies if websearch should be enabled. It is generally disabled by default except for
// perplexity.
//
// # Warning
//
// This will become a structure to provide information about included and excluded domains, and the user's
// location.
WebSearch bool
}
func (*OptionsTools) Validate ¶
func (o *OptionsTools) Validate() error
Validate ensures the completion options are valid.
type OptionsVideo ¶
type OptionsVideo struct {
// Duration of the video to generate, if supported.
//
// Veo 2 supports only between 5 and 8 seconds and Veo 3 only supports 8 seconds.
Duration time.Duration
// PollInterval is the time interval to poll the image generation progress when using GenSync.
PollInterval time.Duration
// contains filtered or unexported fields
}
func (*OptionsVideo) Validate ¶
func (o *OptionsVideo) Validate() error
type Provider ¶
type Provider interface {
// Name returns the name of the provider.
Name() string
// ModelID returns the model currently used by the provider. It can be an empty string.
ModelID() string
// OutputModalities returns the output modalities supported by this specific client configuration.
//
// This states what kind of output the model will generate (text, audio, image, video). It varies per
// provider and models. The vast majority of providers and models support only output modality like
// text-only, image-only, etc.
OutputModalities() Modalities
// Capabilities returns the optional capabilities this provider supports.
Capabilities() ProviderCapabilities
// Scoreboard returns what the provider supports.
//
// Some models have more features than others, e.g. some models may be text-only while others have vision or
// audio support.
//
// The client code may be the limiting factor for some models, and not the provider itself.
//
// The values returned here should have gone through a smoke test to make sure they are valid.
Scoreboard() scoreboard.Score
// HTTPClient returns the underlying http client. It may be necessary to use it to fetch the results from
// the provider. An example is retrieving Veo 3 generated videos from Gemini requires the authentication
// headers to be set.
HTTPClient() *http.Client
// GenSync runs generation synchronously.
//
// Multiple options can be mixed together, both standard ones like *OptionsImage, *OptionsText,
// *OptionsTools and provider-specialized options struct, e.g. *anthropic.Options, *gemini.Options.
GenSync(ctx context.Context, msgs Messages, opts ...Options) (Result, error)
// GenStream runs generation synchronously, yielding the fragments of replies as the server sends them.
//
// No need to accumulate the fragments into a Message since the Result contains the accumulated message.
GenStream(ctx context.Context, msgs Messages, opts ...Options) (iter.Seq[Reply], func() (Result, error))
// ListModels returns the list of models the provider supports. Not all providers support it, some will
// return an ErrorNotSupported. For local providers like llamacpp and ollama, they may return only the
// model currently loaded.
ListModels(ctx context.Context) ([]Model, error)
// GenAsync requests a generation and returns a pending job that can be polled.
//
// Requires ProviderCapabilities.GenAsync to be set. Returns base.ErrNotSupported otherwise.
GenAsync(ctx context.Context, msgs Messages, opts ...Options) (Job, error)
// PokeResult requests the state of the job.
//
// When the job is still pending, Result.Usage.FinishReason is Pending.
//
// Requires ProviderCapabilities.GenAsync to be set. Returns base.ErrNotSupported otherwise.
PokeResult(ctx context.Context, job Job) (Result, error)
// CacheAddRequest caches a request.
//
// Requires ProviderCapabilities.Caching to be set. Returns base.ErrNotSupported otherwise.
//
// # Warning
//
// May be changed in the future.
CacheAddRequest(ctx context.Context, msgs Messages, name, displayName string, ttl time.Duration, opts ...Options) (string, error)
// CacheList lists the caches entries.
//
// Requires ProviderCapabilities.Caching to be set. Returns base.ErrNotSupported otherwise.
//
// # Warning
//
// May be changed in the future.
CacheList(ctx context.Context) ([]CacheEntry, error)
// CacheDelete deletes a cache entry.
//
// Requires ProviderCapabilities.Caching to be set. Returns base.ErrNotSupported otherwise.
//
// # Warning
//
// May be changed in the future.
CacheDelete(ctx context.Context, name string) error
}
Provider is the base interface that all provider interfaces embed.
The first group contains local methods. Calling these methods will not make an HTTP request.
The second group is supported by the majority of providers.
The rest is supported by a limited number of providers.
Example (GenSync_audio) ¶
package main
import (
"context"
"fmt"
"log"
"os"
"strings"
"github.com/maruel/genai"
"github.com/maruel/genai/providers/gemini"
)
func main() {
// Supported by Gemini, OpenAI.
// Using a free small model for testing.
// See https://ai.google.dev/gemini-api/docs/models/gemini?hl=en
ctx := context.Background()
c, err := gemini.New(ctx, &genai.ProviderOptions{Model: "gemini-2.5-flash-lite"}, nil)
if err != nil {
log.Fatal(err)
}
f, err := os.Open("internal/internaltest/testdata/mystery_word.mp3")
if err != nil {
log.Fatal(err)
}
defer f.Close()
msgs := genai.Messages{
{
Requests: []genai.Request{
{Text: "What is the word said? Reply with only the word."},
{Doc: genai.Doc{Src: f}},
},
},
}
resp, err := c.GenSync(ctx, msgs)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Heard: %v\n", strings.TrimRight(strings.ToLower(resp.String()), "."))
// This would Output: Heard: orange
}
Example (GenSync_pdf) ¶
package main
import (
"context"
"fmt"
"log"
"os"
"strings"
"github.com/maruel/genai"
"github.com/maruel/genai/providers/gemini"
)
func main() {
// Supported by Anthropic, Gemini, Mistral, OpenAI.
// Using a free small model for testing.
// See https://ai.google.dev/gemini-api/docs/models/gemini?hl=en
ctx := context.Background()
c, err := gemini.New(ctx, &genai.ProviderOptions{Model: "gemini-2.5-flash-lite"}, nil)
if err != nil {
log.Fatal(err)
}
f, err := os.Open("internal/internaltest/testdata/hidden_word.pdf")
if err != nil {
log.Fatal(err)
}
defer f.Close()
msgs := genai.Messages{
{
Requests: []genai.Request{
{Text: "What is the word? Reply with only the word."},
{Doc: genai.Doc{Src: f}},
},
},
}
resp, err := c.GenSync(ctx, msgs)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Hidden word in PDF: %v\n", strings.ToLower(resp.String()))
// This would Output: Hidden word in PDF: orange
}
Example (GenSync_video) ¶
package main
import (
"context"
"fmt"
"log"
"os"
"strings"
"github.com/maruel/genai"
"github.com/maruel/genai/providers/gemini"
)
func main() {
// Supported by Gemini, TogetherAI.
// Using a free small model for testing.
// See https://ai.google.dev/gemini-api/docs/models/gemini?hl=en
ctx := context.Background()
c, err := gemini.New(ctx, &genai.ProviderOptions{Model: "gemini-2.5-flash"}, nil)
if err != nil {
log.Fatal(err)
}
f, err := os.Open("internal/internaltest/testdata/animation.mp4")
if err != nil {
log.Fatal(err)
}
defer f.Close()
// TogetherAI seems to require separate messages for text and images.
msgs := genai.Messages{
genai.NewTextMessage("What is the word? Reply with exactly and only one word."),
{Requests: []genai.Request{{Doc: genai.Doc{Src: f}}}},
}
resp, err := c.GenSync(ctx, msgs)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Saw: %v\n", strings.ToLower(resp.String()))
// This would Output: Saw: banana
}
Example (GenSync_vision) ¶
package main
import (
"bytes"
"context"
"fmt"
"log"
"os"
"github.com/maruel/genai"
"github.com/maruel/genai/providers/gemini"
)
func main() {
// Supported by Anthropic, Gemini, Groq, Mistral, Ollama, OpenAI, TogetherAI.
// Using a free small model for testing.
// See https://ai.google.dev/gemini-api/docs/models/gemini?hl=en
ctx := context.Background()
c, err := gemini.New(ctx, &genai.ProviderOptions{Model: "gemini-2.5-flash-lite"}, nil)
if err != nil {
log.Fatal(err)
}
bananaJpg, err := os.ReadFile("internal/internaltest/testdata/banana.jpg")
if err != nil {
log.Fatal(err)
}
msgs := genai.Messages{
{
Requests: []genai.Request{
{Text: "Is it a banana? Reply with only the word yes or no."},
{Doc: genai.Doc{Filename: "banana.jpg", Src: bytes.NewReader(bananaJpg)}},
},
},
}
resp, err := c.GenSync(ctx, msgs)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Banana: %v\n", resp.String())
// This would Output: Banana: yes
}
type ProviderCapabilities ¶
type ProviderCapabilities struct {
// GenAsync indicates the provider supports GenAsync and PokeResult for batch operations.
GenAsync bool
// Caching indicates the provider supports CacheAddRequest, CacheList, and CacheDelete.
Caching bool
// contains filtered or unexported fields
}
ProviderCapabilities describes optional capabilities a provider supports.
type ProviderOptions ¶
type ProviderOptions struct {
// APIKey provides an API key to authenticate to the server.
//
// Most providers require an API key, and the client will look at an environment variable
// "<PROVIDER>_API_KEY" to use as a default value if unspecified.
APIKey string `json:"apikey,omitzero" yaml:"apikey,omitzero"`
// AccountID provides an account ID key. Rarely used (only Cloudflare).
AccountID string `json:"accountid,omitzero" yaml:"accountid,omitzero"`
// Remote is the remote address to access the service.
//
// It is mostly used by locally hosted services (llamacpp, ollama) or for generic client (openaicompatible).
Remote string `json:"remote,omitzero" yaml:"remote,omitzero"`
// Model either specifies an exact the model ID to use, request the provider to select a model on your
// behalf or explicitly ask for no model.
//
// To use automatic model selection, pass ModelCheap to use a cheap model, ModelGood for a good
// everyday model or ModelSOTA to use its state of the art (SOTA) model. In this case, the provider
// internally call ListModels() to discover models and select the right one based on its heuristics.
// Provider that do not support ListModels, e.g. bfl or perplexity, will use an hardcoded list.
//
// When unspecified, i.e. with "", it defaults to automatic model selection with ModelGood.
//
// There are two ways to disable automatic model discovery and selection: specify a model ID or use
// ModelNone.
//
// Keep in mind that as providers cycle through new models, it's possible a specific model ID is not
// available anymore or that the default model changes.
Model string `json:"model,omitzero" yaml:"model,omitzero"`
// OutputModalities is the list of output modalities you request the model to support.
//
// Most provider support text output only. Most models support output of only one modality, either text,
// image, audio or video. But a few models do support both text and images.
//
// When unspecified, it defaults to all modalities supported by the provider and the selected model.
//
// Even when Model is set to a specific model ID, a ListModels call may be made to discover its supported
// output modalities for providers that support multiple output modalities.
//
// OutputModalities can be set when Model is set to ModelNone to test if a provider support this modality without
// causing a ListModels call.
OutputModalities Modalities `json:"modalities,omitzero" yaml:"modalities,omitzero"`
// PreloadedModels is a list of models that are preloaded into the provider, to replace the call to
// ListModels, for example with automatic model selection and modality detection.
//
// This is mostly used for unit tests or repeated client creation to save on HTTP requests.
PreloadedModels []Model
// contains filtered or unexported fields
}
ProviderOptions contains all the options to connect to a model provider.
All fields are optional, but some provider do require some of the fields.
func (*ProviderOptions) Validate ¶
func (p *ProviderOptions) Validate() error
type ProviderPing ¶
type ProviderPing interface {
Provider
// Ping enables confirming that the provider is accessible, without incurring cost. This is useful for local
// providers to detect if they are accessible or not.
Ping(ctx context.Context) error
}
ProviderPing represents a provider that you can ping.
type ProviderUnwrap ¶
type ProviderUnwrap interface {
Unwrap() Provider
}
ProviderUnwrap is exposed when the Provider is actually a wrapper around another one, like adapters.ProviderReasoning or ProviderUsage. This is useful when looking for other interfaces.
type RateLimit ¶
type RateLimit struct {
Type RateLimitType
Period RateLimitPeriod
Limit int64
Remaining int64
Reset time.Time
}
RateLimit contains the limit, remaining, and reset values for a metric.
type RateLimitPeriod ¶
type RateLimitPeriod int32
RateLimitPeriod defines the time period for a rate limit.
const ( PerOther RateLimitPeriod = iota // For non-standard periods PerMinute PerDay PerMonth )
type RateLimitType ¶
type RateLimitType int32
RateLimitType defines the type of rate limit.
const ( Requests RateLimitType = iota + 1 Tokens )
type Reply ¶
type Reply struct {
// Text is the content of the text message.
Text string `json:"text,omitzero"`
// Doc can be audio, video, image, PDF or any other format, including reference text.
Doc Doc `json:"doc,omitzero"`
// Citation contains references to source material that support the content.
Citation Citation `json:"citation,omitzero"`
// Reasoning is the reasoning done by the LLM.
Reasoning string `json:"reasoning,omitzero"`
// ToolCall is a tool call that the LLM requested to make.
ToolCall ToolCall `json:"tool_call,omitzero"`
// Opaque is added to keep continuity on the processing. A good example is Anthropic's extended thinking, or
// server-side tool calling. It must be kept during an exchange.
//
// A message with only Opaque set is valid. It can be used in combination with other fields. This field is
// specific to both the provider and the model.
//
// The data must be JSON-serializable.
Opaque map[string]any `json:"opaque,omitzero"`
// contains filtered or unexported fields
}
Reply is a block of information returned by the provider.
Normally only one of the field must be set. The exception is the Opaque field.
Reply generally represents content returned by the provider, like a block of text or a document returned by the model. It can be a silent tool call request. It can also be an opaque block. A good example is traces of server side tool calling like WebSearch or MCP tool calling.
func (*Reply) GoString ¶
GoString returns a JSON representation of the reply for debugging purposes.
func (*Reply) UnmarshalJSON ¶
type Request ¶
type Request struct {
// Text is the content of the text message.
Text string `json:"text,omitzero"`
// Doc can be audio, video, image, PDF or any other format, including reference text.
Doc Doc `json:"doc,omitzero"`
// contains filtered or unexported fields
}
Request is a block of content in the message meant to be visible in a chat setting.
It is effectively a union, only one of the 2 related field groups can be set.
func (*Request) UnmarshalJSON ¶
type Result ¶
type Result struct {
Message
Usage Usage
// Logprobs is a list of multiple log probabilities, each for a token.
//
// The first item of each subslice is the chosen token. The next items are the candidates not chosen.
//
// Some providers only return the probability for the chosen tokens and not for the candidates.
Logprobs [][]Logprob
}
Result is the result of a completion.
It is a Message along with Usage metadata about the operation. It optionally include Logprobs if requested and the provider support it.
type ToolCall ¶
type ToolCall struct {
ID string `json:"id,omitzero"` // Unique identifier for the tool call. Necessary for parallel tool calling.
Name string `json:"name,omitzero"` // Tool being called.
Arguments string `json:"arguments,omitzero"` // encoded as JSON
// Opaque is added to keep continuity on the processing. A good example is Anthropic's extended thinking. It
// must be kept during an exchange.
//
// A message with only Opaque set is valid.
Opaque map[string]any `json:"opaque,omitzero"`
// contains filtered or unexported fields
}
ToolCall is a tool call that the LLM requested to make.
func (*ToolCall) Call ¶
Call invokes the ToolDef.Callback with arguments from the ToolCall, returning the result string.
It decodes the ToolCall.Arguments and passes it to the ToolDef.Callback.
func (*ToolCall) UnmarshalJSON ¶
type ToolCallRequest ¶
type ToolCallRequest int
ToolCallRequest determines if we want the LLM to request a tool call.
const ( // ToolCallAny is the default, the model is free to choose if a tool is called or not. For some models (like // llama family), it may be a bit too "tool call happy". ToolCallAny ToolCallRequest = iota // ToolCallRequired means a tool call is required. Don't forget to change the value after sending the // response! ToolCallRequired // ToolCallNone means that while tools are described, they should not be called. It is useful when a LLM did // tool calls, got the response and now it's time to generate some text to present to the end user. ToolCallNone )
type ToolCallResult ¶
type ToolCallResult struct {
ID string `json:"id,omitzero"`
Name string `json:"name,omitzero"`
Result string `json:"result,omitzero"`
// contains filtered or unexported fields
}
ToolCallResult is the result for a tool call that the LLM requested to make.
func (*ToolCallResult) UnmarshalJSON ¶
func (t *ToolCallResult) UnmarshalJSON(b []byte) error
func (*ToolCallResult) Validate ¶
func (t *ToolCallResult) Validate() error
Validate ensures the tool result is valid.
type ToolDef ¶
type ToolDef struct {
// Name must be unique among all tools.
Name string
// Description must be a LLM-friendly short description of the tool.
Description string
// Callback is the function to call with the inputs.
// It must accept a context.Context one struct pointer as input: (ctx context.Context, input *struct{}). The
// struct must use json_schema to be serializable as JSON.
// It must return the result and an error: (string, error).
Callback any
// InputSchemaOverride overrides the schema deduced from the Callback's second argument. It's meant to be
// used when an enum or a description is set dynamically, or with complex if/then/else that would be tedious
// to describe as struct tags.
//
// It is okay to initialize Callback, then take the return value of GetInputSchema() to initialize InputSchemaOverride, then mutate it.
InputSchemaOverride *jsonschema.Schema
// contains filtered or unexported fields
}
ToolDef describes a tool that the LLM can request to use.
func (*ToolDef) GetInputSchema ¶
func (t *ToolDef) GetInputSchema() *jsonschema.Schema
GetInputSchema returns the json schema for the input argument of the callback.
func (*ToolDef) Validate ¶
Validate ensures the tool definition is valid.
For the Name field, it uses the rule according to https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/implement-tool-use#example-simple-tool-definition
type Usage ¶
type Usage struct {
// Token usage for the current request.
InputTokens int64
InputCachedTokens int64
ReasoningTokens int64
OutputTokens int64
TotalTokens int64
// FinishReason indicates why the model stopped generating tokens.
FinishReason FinishReason
// Limits contains a list of rate limit details from the provider.
Limits []RateLimit
}
Usage from the LLM provider.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package adapters includes multiple adapters to convert one ProviderFoo interface into another one.
|
Package adapters includes multiple adapters to convert one ProviderFoo interface into another one. |
|
Package base is awesome sauce to reduce code duplication across most providers.
|
Package base is awesome sauce to reduce code duplication across most providers. |
|
cmd
|
|
|
cache-mgr
command
Command cache-mgr fetches and prints out the list of files stored on the selected provider.
|
Command cache-mgr fetches and prints out the list of files stored on the selected provider. |
|
list-models
command
Command list-models fetches and prints out the list of models from the selected providers.
|
Command list-models fetches and prints out the list of models from the selected providers. |
|
llama-serve
command
Command llama-serve fetches a model from HuggingFace and runs llama-server.
|
Command llama-serve fetches a model from HuggingFace and runs llama-server. |
|
scoreboard
command
Command scoreboard generates a scoreboard for every providers supported.
|
Command scoreboard generates a scoreboard for every providers supported. |
|
examples
|
|
|
aud-txt_to_txt
command
|
|
|
img-txt_to_img
command
|
|
|
img-txt_to_img-txt
command
|
|
|
img-txt_to_txt
command
|
|
|
img-txt_to_txt_local
command
|
|
|
img-txt_to_vid
command
|
|
|
txt_to_img
command
|
|
|
txt_to_txt_any
command
|
|
|
txt_to_txt_citations
command
|
|
|
txt_to_txt_decode-json
command
|
|
|
txt_to_txt_logprobs
command
|
|
|
txt_to_txt_quota
command
|
|
|
txt_to_txt_stream
command
|
|
|
txt_to_txt_sync
command
|
|
|
txt_to_txt_sync_multi
command
|
|
|
txt_to_txt_thinking
command
|
|
|
txt_to_txt_tool-manual
command
|
|
|
txt_to_txt_tool-stream
command
|
|
|
txt_to_txt_tool-sync
command
|
|
|
txt_to_txt_websearch-stream
command
|
|
|
txt_to_txt_websearch-sync
command
|
|
|
vid-txt_to_txt
command
|
|
|
Package httprecord provides safe HTTP recording logic for users that was to understand the API and do smoke tests.
|
Package httprecord provides safe HTTP recording logic for users that was to understand the API and do smoke tests. |
|
Package internal is awesome sauce.
|
Package internal is awesome sauce. |
|
bb
Package bb is a separate package so it can be imported by genai while being internal and exported so cmp.Diff() isn't unhappy.
|
Package bb is a separate package so it can be imported by genai while being internal and exported so cmp.Diff() isn't unhappy. |
|
internaltest
Package internaltest is awesome sauce for unit testing.
|
Package internaltest is awesome sauce for unit testing. |
|
myrecorder
Package myrecorder has HTTP recording logic.
|
Package myrecorder has HTTP recording logic. |
|
sse
Package sse provides Server-Sent Events (SSE) processing utilities.
|
Package sse provides Server-Sent Events (SSE) processing utilities. |
|
Package providers is the root of all standard providers.
|
Package providers is the root of all standard providers. |
|
anthropic
Package anthropic implements a client for the Anthropic API, to use Claude.
|
Package anthropic implements a client for the Anthropic API, to use Claude. |
|
bfl
Package bfl implements a client for Black Forest Labs API.
|
Package bfl implements a client for Black Forest Labs API. |
|
cerebras
Package cerebras implements a client for the Cerebras API.
|
Package cerebras implements a client for the Cerebras API. |
|
cloudflare
Package cloudflare implements a client for the Cloudflare AI API.
|
Package cloudflare implements a client for the Cloudflare AI API. |
|
cohere
Package cohere implements a client for the Cohere API.
|
Package cohere implements a client for the Cohere API. |
|
deepseek
Package deepseek implements a client for the DeepSeek API.
|
Package deepseek implements a client for the DeepSeek API. |
|
gemini
Package gemini implements a client for Google's Gemini API.
|
Package gemini implements a client for Google's Gemini API. |
|
groq
Package groq implements a client for the Groq API.
|
Package groq implements a client for the Groq API. |
|
huggingface
Package huggingface implements a client for the HuggingFace serverless inference API.
|
Package huggingface implements a client for the HuggingFace serverless inference API. |
|
llamacpp
Package llamacpp implements a client for the llama-server native API, not the OpenAI compatible one.
|
Package llamacpp implements a client for the llama-server native API, not the OpenAI compatible one. |
|
llamacpp/llamacppsrv
Package llamacppsrv downloads and starts llama-server from llama.cpp, directly from GitHub releases.
|
Package llamacppsrv downloads and starts llama-server from llama.cpp, directly from GitHub releases. |
|
mistral
Package mistral implements a client for the Mistral API.
|
Package mistral implements a client for the Mistral API. |
|
ollama
Package ollama implements a client for the Ollama API.
|
Package ollama implements a client for the Ollama API. |
|
ollama/ollamasrv
Package ollamasrv downloads and starts ollama directly from GitHub releases.
|
Package ollamasrv downloads and starts ollama directly from GitHub releases. |
|
openaichat
Package openaichat implements a client for the OpenAI Chat Completion API.
|
Package openaichat implements a client for the OpenAI Chat Completion API. |
|
openaicompatible
Package openaicompatible implements a minimal client for "OpenAI-compatible" providers.
|
Package openaicompatible implements a minimal client for "OpenAI-compatible" providers. |
|
openairesponses
Package openairesponses implements a client for the OpenAI Responses API.
|
Package openairesponses implements a client for the OpenAI Responses API. |
|
perplexity
Package perplexity implements a client for the Perplexity API.
|
Package perplexity implements a client for the Perplexity API. |
|
pollinations
Package pollinations implements a client for the Pollinations API.
|
Package pollinations implements a client for the Pollinations API. |
|
togetherai
Package togetherai implements a client for the Together.ai API.
|
Package togetherai implements a client for the Together.ai API. |
|
Package scoreboard declares the structures to define a scoreboard.
|
Package scoreboard declares the structures to define a scoreboard. |
|
Package smoke runs a smoke test to generate a scoreboard.Scenario.
|
Package smoke runs a smoke test to generate a scoreboard.Scenario. |
|
smoketest
Package smoketest runs a scoreboard in test mode.
|
Package smoketest runs a scoreboard in test mode. |




