Documentation
¶
Overview ¶
Package decisions is a classifier backed by Google's Decisions API.
One Client holds one HTTP/2 connection pool, bounds every request in time, and counts the tokens every request used. Several Classifiers can share one. A Classifier turns each question into a request and each reply into a result, through a Client.
Index ¶
- Constants
- type Classifier
- func (c *Classifier) AskQuestions(ctx context.Context, state any, ...) (map[string]classifier.Result, *frames.LLMTokenUsage, error)
- func (c *Classifier) AudioSampleRate() int
- func (c *Classifier) Cleanup(ctx context.Context) error
- func (c *Classifier) Client() *Client
- func (c *Classifier) FileTypes() []string
- func (c *Classifier) Model() string
- func (c *Classifier) Setup(ctx context.Context) error
- func (c *Classifier) SupportsAudio() bool
- func (c *Classifier) SupportsImages() bool
- type Client
- func (c *Client) Ask(ctx context.Context, input any, questions []NamedQuestion) (map[string]map[string]any, Usage, error)
- func (c *Client) BaseURL() string
- func (c *Client) Close()
- func (c *Client) Connect(ctx context.Context) error
- func (c *Client) Model() string
- func (c *Client) Timeout() time.Duration
- func (c *Client) Usage() Usage
- type ClientConfig
- type Config
- type NamedQuestion
- type Question
- type Usage
Constants ¶
const ( // DefaultBaseURL is where the API is served. DefaultBaseURL = "https://generativelanguage.googleapis.com" // DefaultModel is the decision model to ask. DefaultModel = "gemini-decision-1" // DefaultTimeout is how long to wait for an answer, retries included, // before giving up. DefaultTimeout = 10 * time.Second )
const MaxChoiceOptions = 255
MaxChoiceOptions is the most options the Decisions API takes in one choice question.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Classifier ¶
type Classifier struct {
*classifier.Base
// contains filtered or unexported fields
}
Classifier answers questions by asking Google's Decisions API.
Structured state, instructions and descriptions are sent as JSON text. Build one with a Gemini API key to get a client of its own, or pass a Client to share one between several classifiers. A client the classifier created is closed in Cleanup; a shared one is left to whoever made it.
The Decisions API sees images, hears clips of audio, sent as 16 kHz WAV, and reads files of the types Classifier.FileTypes lists: PDFs, videos, and images and audio in other formats, as bytes or by URL. The inputs follow the state in the order they're given.
c, err := decisions.New(decisions.Config{APIKey: key})
func (*Classifier) AskQuestions ¶
func (c *Classifier) AskQuestions( ctx context.Context, state any, questions []classifier.Named[classifier.Question], inputs []classifier.Input, ) (map[string]classifier.Result, *frames.LLMTokenUsage, error)
AskQuestions implements classifier.Asker, answering the questions in one request.
func (*Classifier) AudioSampleRate ¶
func (c *Classifier) AudioSampleRate() int
AudioSampleRate is 16 kHz: clips are sent at it, which keeps requests small.
func (*Classifier) Cleanup ¶
func (c *Classifier) Cleanup(ctx context.Context) error
Cleanup closes the client if this classifier created it.
func (*Classifier) Client ¶
func (c *Classifier) Client() *Client
Client is the client this classifier asks through.
func (*Classifier) FileTypes ¶
func (c *Classifier) FileTypes() []string
FileTypes is the file types the Decisions API reads: PDFs, videos, and images and audio in many formats.
func (*Classifier) Model ¶
func (c *Classifier) Model() string
Model is the decision model the questions go to.
func (*Classifier) Setup ¶
func (c *Classifier) Setup(ctx context.Context) error
Setup opens the connection to Google ahead of the first question. A connection that cannot be opened now is only a warning: the first question opens it itself.
func (*Classifier) SupportsAudio ¶
func (c *Classifier) SupportsAudio() bool
SupportsAudio reports that the Decisions API can hear audio.
func (*Classifier) SupportsImages ¶
func (c *Classifier) SupportsImages() bool
SupportsImages reports that the Decisions API can see images.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is an HTTP client for Google's Decisions API.
It holds one HTTP/2 connection pool, so many small requests share a connection and can be in flight at the same time. The connection is kept open between questions, so a gap between them does not cost a new TLS handshake. It retries with backoff when Google is busy or can't be reached, within one time bound for the whole request, and counts the tokens every request used in Usage. Errors carry the reason Google gave.
Connect opens the connection ahead of the first question, and Close releases it.
func NewClient ¶
func NewClient(cfg ClientConfig) (*Client, error)
NewClient builds a Decisions API client.
func (*Client) Ask ¶
func (c *Client) Ask( ctx context.Context, input any, questions []NamedQuestion, ) (map[string]map[string]any, Usage, error)
Ask sends questions about one state and returns Google's answers, by the same names and in Google's own format, and the tokens the request used.
input is what the questions are about, as the request's input: text, or a list of content blocks.
It fails with an error wrapping classifier.ErrClassifier when Google rejected the request, kept refusing it because it was busy, could not be reached, did not answer in time, blocked the input, or left a question unanswered.
func (*Client) Connect ¶
Connect opens the connection to Google.
It looks up the model, a cheap request that also checks the key and whether it has access to the model. It connects once: a client shared by several classifiers is asked by each of them, and only the first call sends anything.
type ClientConfig ¶
type ClientConfig struct {
// APIKey is the Gemini API key. Required.
APIKey string `validate:"required"`
// BaseURL is where the API is served; empty uses DefaultBaseURL.
BaseURL string
// Model is the decision model to ask; empty uses DefaultModel.
Model string
// Timeout is how long to wait for an answer, retries included, before
// giving up; zero uses DefaultTimeout.
Timeout time.Duration `validate:"min=0"`
// MaxRetries is how many times to retry a request that failed for a reason
// that may pass, such as Google being busy; nil uses 3.
MaxRetries *int `validate:"omitempty,min=0"`
// HTTPClient sends the requests; nil builds one that keeps HTTP/2
// connections open between questions.
HTTPClient *http.Client
}
ClientConfig configures a Client.
type Config ¶
type Config struct {
// APIKey is the Gemini API key, when the classifier should have a client
// of its own. One of APIKey and Client is required.
APIKey string
// Client is a client to share between several classifiers.
Client *Client
// Name names the classifier in its metrics; empty uses
// "GoogleDecisionsClassifier#<n>".
Name string
// Model is the decision model a client of its own asks; empty uses
// DefaultModel.
Model string
// Timeout is how long a client of its own waits for an answer; zero uses
// DefaultTimeout.
Timeout time.Duration
}
Config configures a Google Decisions classifier.
type NamedQuestion ¶
NamedQuestion is a question in Google's format with the name its answer comes back under.
type Question ¶
type Question struct {
// Type is "noul", "choice" or "score".
Type string
// Instructions is what is being asked, as text.
Instructions string
// Criteria is what counts as each answer, or nil when the question leaves
// it open.
Criteria any
}
Question is one question in Google's own format.
func (Question) MarshalJSON ¶
MarshalJSON implements json.Marshaler, leaving criteria out when there are none.