decisions

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: BSD-2-Clause Imports: 24 Imported by: ? modules, 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

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

View Source
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
)
View Source
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 New

func New(cfg Config) (*Classifier, error)

New builds a Google Decisions classifier.

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) BaseURL

func (c *Client) BaseURL() string

BaseURL is where the questions are sent.

func (*Client) Close

func (c *Client) Close()

Close closes the connection pool.

func (*Client) Connect

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

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.

func (*Client) Model

func (c *Client) Model() string

Model is the decision model the questions go to.

func (*Client) Timeout

func (c *Client) Timeout() time.Duration

Timeout is how long the client waits for an answer, retries included.

func (*Client) Usage

func (c *Client) Usage() Usage

Usage is the tokens used so far, over every request.

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

type NamedQuestion struct {
	Name     string
	Question Question
}

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

func (q Question) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler, leaving criteria out when there are none.

type Usage

type Usage struct {
	// InputTokens is the tokens sent.
	InputTokens int `json:"input_tokens"`
	// OutputTokens is the tokens received.
	OutputTokens int `json:"output_tokens"`
}

Usage is the tokens the Decisions API used: for one request, or over every request a client sent.

Jump to

Keyboard shortcuts

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