Documentation
¶
Overview ¶
Package provider is the port through which tutor reaches the AI providers. Each provider has an adapter that implements it, and a provider's SDK is imported by its adapter alone: that is how the SDKs reach api's build only through tutor. The library does not build the adapters in: a service gives it the Dial of each adapter it takes, so that a service of one provider's models carries no other provider's SDK.
Index ¶
- Constants
- Variables
- func Billed(resp Response, err error) bool
- func CheckImages(r Request, most int) error
- func CheckTools(r Request) error
- func Checked(err error, refused Refusal, status int) error
- func FitPatches(width, height, side, edge, most int) (int, int)
- func HTTPClient() *http.Client
- func NoRetry(classify func(status int, body []byte, at time.Time) Refusal, last *Refusal) ...
- func Place(x1, y1, x2, y2, width, height int) ([4]int64, bool)
- func Turn(src image.Image, degrees int) *image.RGBA
- type CacheWrite
- type Dial
- type Fitter
- type Image
- type Limit
- type LimitError
- type Model
- type Photos
- type Prices
- type Refusal
- type Request
- type Response
- type Tool
- type ToolChoice
Constants ¶
const MaxPhoto = 4 << 20
MaxPhoto is the most bytes of a photo that Photo decodes: the 4 MiB FindTasks of spec's contract between app and api takes.
const ResponseHeaderTimeout = 10 * time.Minute
ResponseHeaderTimeout is how long an adapter waits for the headers of an answer once its request is sent: the time both SDKs give their own client, which the adapters do not take, since it comes with the defaults they take from the environment. It does not bound the answer's body, so a stream goes on for as long as it lasts.
Variables ¶
var ErrKeyRefused = errors.New("the provider refused the key")
ErrKeyRefused is a provider's refusal of the key itself: it is wrong, revoked or expired. Sending the call again does not help either.
var ErrNoAnswer = errors.New("no answer")
ErrNoAnswer is an answer that came back without the JSON asked for: the model refused, ran out of tokens before it finished, or called no tool it was offered.
var ErrNoRights = errors.New("the key has no rights to the model")
ErrNoRights is a provider's answer to the check of a key that the key is right and has no rights to the model: of its organization, workspace or project.
Functions ¶
func Billed ¶
Billed reports whether the tokens of a call's response are its whole bill. They are not for an answer that broke off before the provider gave the final count of its tokens (Partial), nor for a call that its context cut off before any bill came: the model may have spent tokens all the same. A caller that caps its spending counts such a call at the most it could cost.
func CheckImages ¶
CheckImages refuses the pictures of a request that an adapter sending at most pictures will not send: none, more than most, one without its PNG or JPEG or with both, or, of two or more, one without its label. A request marked NoPicture is the one that has none, and it is refused with a picture or with a blank text, which asks nothing. Of several pictures each has a label, as Anthropic advises, so that the text can name them.
func CheckTools ¶ added in v0.1.67
CheckTools refuses the tools of a request that an adapter which sends them will not send: a request with a schema too, a choice other than ToolAuto, a choice with no tools, and a tool with no name, a name twice or no schema of an object.
func Checked ¶ added in v0.1.24
Checked is the error of the check of a key (Model.Check) that failed with err, with the refusal NoRetry read and the HTTP status of the provider's answer, 0 when none came: the refusal — of the key, or for a limit, which a right key may meet —, ErrNoRights for a 403, or err itself. A 404 is no fault of the key: it may be a model the provider no longer has.
func FitPatches ¶ added in v0.1.59
FitPatches is the largest size, keeping the aspect of width × height px, whose patches of side px — ⌈w / side⌉ × ⌈h / side⌉ — are no more than most, and neither of whose sides, rounded up to a whole patch, exceeds edge; the size itself when it fits. It is the rule by which Anthropic gives the size it shrinks a picture to, its reference implementation searched along the long edge, the short one rounded half to even, in its guide Vision; an adapter gives its provider's side, edge and most.
func HTTPClient ¶ added in v0.1.29
HTTPClient is the client the adapters send their requests by. Its transport is the adapters' own, not http.DefaultTransport, so that what a process puts there — an instrumentation that writes spans and sends the trace context to the provider — does not reach a call. What it takes from the process — the proxy of HTTPS_PROXY and NO_PROXY, the certificates of SSL_CERT_FILE and SSL_CERT_DIR — is the standard library's and the deployment's, as for every client of the process, and not a provider's.
func NoRetry ¶ added in v0.1.24
func NoRetry(classify func(status int, body []byte, at time.Time) Refusal, last *Refusal) func(*http.Request, func(*http.Request) (*http.Response, error)) (*http.Response, error)
NoRetry is the middleware of an adapter's SDK that reads each error response of the provider by classify, at the time the response's Date header names, or the clock's when it names none, keeps the last refusal it found in *last, and marks a refusal with the header x-should-retry: false, which both SDKs heed (internal/requestconfig of each): a refusal for a limit or of the key is not sent again, while a plain 429, 408, 409 or 5xx still is. It reads the start of the body of an error response alone, and gives the SDK the body as it came, the error of a read that broke off too, so that the SDK handles it as its own.
func Place ¶
Place is where a box that a model gave in pixels of a picture stands on it, as an adapter tells it: [ymin, xmin, ymax, xmax] in whole thousandths of the picture's height and width, from 0 to 1000, the low corner rounded down and the high one up. The box, x1, y1 its top-left corner and x2, y2 its bottom-right, in order, is first clamped to the picture of width × height px, and divided by that size, the size of the picture sent and not one padded to a multiple of 28 px, as spec reads Anthropic's guide to coordinates. A box with no area on the picture, or a picture of no size, has no place.
func Turn ¶ added in v0.1.61
Turn is src turned by degrees clockwise on the screen, its y down, about its centre onto the smallest upright rectangle that holds it, W' = round(W·|cos θ| + H·|sin θ|) by H' = round(W·|sin θ| + H·|cos θ|), the corners white, as app turns its photo by the turn FindTasks of spec's contract gives: each pixel's centre is taken back to the source and sampled bilinearly, white beyond it. A quarter turn moves the pixels as they are. A turned picture of more than maxPhotoPixels is shrunk to them, keeping its aspect, so that it takes no more memory than the largest photo decoded: a turn of 45° of a photo of 3200 × 2400 px would take some 3960 × 3960. The frames are in thousandths, at any scale, and the model is sent a picture smaller still.
Types ¶
type CacheWrite ¶ added in v0.1.24
CacheWrite is the input tokens a call wrote to the cache to keep for TTL: a provider prices a write by how long the cache keeps it.
type Dial ¶ added in v0.1.22
Dial is an adapter as the library takes it: it makes the client of a model of its provider by the model's exact ID and a key, and refuses an ID it does not call.
type Fitter ¶ added in v0.1.59
type Fitter interface {
// Fit is the largest size, keeping the aspect, at which a photo of width × height px comes to
// the model as it is sent: the size itself when it fits.
Fit(width, height int) (int, int)
}
Fitter is a model that takes a photo at a size of its own: an adapter whose provider would shrink a larger picture itself says how large a photo comes to the model unshrunk, so that the model's pixels are the photo's and a box it gives in them lands where it points.
type Image ¶
Image is a picture of a request, as PNG, or a photo of a page as JPEG, which Photo makes, and the label that introduces it to the model, such as "Image 1: the sheet as it was finished." A picture has one of the two. A picture without a label goes without one: a request of one picture needs none.
func Photo ¶ added in v0.1.59
Photo is a photo of a page as a request sends it to model: decoded from a JPEG or a PNG of at most MaxPhoto bytes, shrunk to the size the model takes it at unshrunk when the model is a Fitter, and encoded anew as a JPEG, so that no metadata of the file it came in — where it was taken, the device, the time — reaches the provider. It returns the picture and its size in pixels, by which the text of the request names it and a box the model gives is placed (Place). The EXIF orientation of a JPEG is not applied: app draws every photo upright and sends no metadata.
type Limit ¶ added in v0.1.24
type Limit string
Limit is why a provider refused a call for a limit: one only the owner of the key can lift, each in its own way.
const ( // LimitOwner is a spend limit the owner set: of the organization, or of a workspace or project. LimitOwner Limit = "owner_limit" // LimitTier is the provider's own cap of the usage tier. LimitTier Limit = "tier" // LimitBalance is a prepaid balance spent. LimitBalance Limit = "balance" // LimitBilling is a fault of billing or payment. LimitBilling Limit = "billing" )
The limits a provider refuses for.
type LimitError ¶ added in v0.1.24
LimitError is a provider's refusal for a limit, with when access returns, where the provider's terms say it, and the provider's error that said it. Sending the call again does not help until the owner of the key lifts the limit, or access returns, so the adapter does not let its SDK send it again.
func (*LimitError) Error ¶ added in v0.1.24
func (e *LimitError) Error() string
func (*LimitError) Unwrap ¶ added in v0.1.24
func (e *LimitError) Unwrap() error
type Model ¶
type Model interface {
// Name is how a person sees the model named, such as "Claude Opus 5.5".
Name() string
// Prices are what its tokens cost.
Prices() Prices
// Ask sends the request and returns the answer.
Ask(ctx context.Context, r Request) (Response, error)
// Check checks the client's key by a request of the provider without any learner's content,
// which costs nothing: nil when the provider took the key, else ErrKeyRefused for a
// wrong, revoked or expired key, ErrNoRights for a key without rights to the model, or the
// error of a provider that did not answer (Checked). An adapter does not let its SDK send it
// again after a refusal (NoRetry).
Check(ctx context.Context) error
}
Model is one model of one provider.
type Photos ¶ added in v0.1.59
type Photos struct {
// contains filtered or unexported fields
}
Photos is one photo as each model of a cascade takes it, as Photo makes it: decoded once, on the first For or Turned, turned once for each angle, and encoded once for each size a model fits it to. It is not safe for concurrent use.
func (*Photos) For ¶ added in v0.1.59
For is the photo as Photo gives it to m: a photo that failed to decode fails each time.
type Prices ¶ added in v0.1.24
type Prices struct {
Input, Output, CacheRead float64
CacheWrite map[time.Duration]float64
LongAbove int64
Long *Prices
Read string
}
Prices are what a model's tokens cost, in US dollars a million, as its provider's pricing page gives them on the day Read names, as YYYY-MM-DD: input, output, a read from the cache and a write to it by how long the cache keeps it. Where a prompt longer than LongAbove tokens, the cache counted in, pays other prices, Long holds them, or nil when they were not read; LongAbove is 0 where the model has no such prices.
func (Prices) Cost ¶ added in v0.1.24
Cost is the cost in US dollars of the tokens of a response, and whether the prices hold them all: not when they were never read, Read empty, nor when the prompt is longer than LongAbove and the long prices were not read, nor when the cache kept a write for a time the prices name no price of. It is an estimate: the provider bills.
func (Prices) WorstInput ¶ added in v0.1.24
WorstInput is the most a token of the input can cost: its own price or that of a write to the cache, whichever is higher, for a call whose bill is not known.
type Refusal ¶ added in v0.1.24
Refusal is what an adapter reads from a provider's error response: the limit it refused for, with when access returns, zero where the provider's terms do not say; or whether it refused the key; or neither.
type Request ¶
type Request struct {
Instructions string
Images []Image
Text string
SchemaName string
Schema map[string]any
MaxOutputTokens int64
Effort string
NoThinking bool
// NoPicture marks a request of a text alone, such as the assessment of a text an earlier call
// read: it has no picture, and its text is not blank. Any other request has a picture.
NoPicture bool
// CacheInstructions marks instructions that are the same text in every request of their kind and
// hold nothing of the learner's — the platform's words, as those of the tutor's words —, so that
// the Claude adapter caches the prompt up to the instructions and no further: never the pictures
// or the text after them. The GPT adapter sends nothing for it, and OpenAI may cache by
// itself more of a request than its instructions.
CacheInstructions bool
// Tools are the tools the model may call in place of an answer of the schema: a request with
// tools has no schema, and its answer is the input of the one tool the model called. ToolChoice
// is how it may call them, ToolAuto alone, which an empty one is; an adapter that sends no tools
// refuses a request with any before a call.
Tools []Tool
ToolChoice ToolChoice
}
Request is one question to a model about pictures: instructions, the pictures in the order the model is to read them, each after its label, a text after them, the JSON schema the answer must follow, the most output tokens the answer may take, reasoning included — which bounds what a call can cost — the effort the model should spend on it, one of the levels its adapter names, or empty for the model's own default, and whether the model is to answer without thinking first. An adapter refuses before any call what it will not send: an answer without thinking at that effort, or pictures CheckImages refuses. How many pictures an adapter sends is its own limit, and may be less than its model takes.
The SDKs encode a map's keys in sorted order, and a model writes the fields of its answer in the order of the schema. A value of the schema may be a json.RawMessage, which goes out as it is written: that is how a schema keeps its own order.
type Response ¶
type Response struct {
JSON string
Tool string
Model string
InputTokens int64
CacheWrites []CacheWrite
CacheReadTokens int64
OutputTokens int64
ThinkingTokens int64
FirstToken time.Duration
Retries int
Partial bool
}
Response is a model's answer: JSON that follows the request's schema, the model the provider says answered, the tokens the call took — the input tokens without the cache, those written to the cache by how long it keeps them and those read from it, which add up to the input with no token counted twice, and of the output tokens, those the model spent thinking —, when the first text of the answer came, counted from when the attempt that answered was sent, or zero where the adapter does not measure it, and how many times the SDK sent the request again after a failed attempt. A call that fails with ErrNoAnswer still returns its tokens, since they are billed. Partial is a streamed answer that began and broke off before the provider gave the final count of its tokens: the tokens it holds are those counted so far, and the bill is not known. Of a request with tools, JSON is the input of the tool the model called, and Tool its name.
type Tool ¶ added in v0.1.67
Tool is a tool a request offers a model: its name, what it does, which the model reads, and the JSON schema of its input, of type object. A value of the schema may be a json.RawMessage, as in Request.Schema.
type ToolChoice ¶ added in v0.1.67
type ToolChoice string
ToolChoice is how a model may call the tools of a request.
const ToolAuto ToolChoice = "auto"
ToolAuto lets the model decide whether to call a tool, and which: the one choice that leaves it its thinking, which a forced call turns off.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package claude is the adapter of Anthropic's Claude models, through the official Go SDK.
|
Package claude is the adapter of Anthropic's Claude models, through the official Go SDK. |
|
catalog
Package catalog is the Claude models the adapter of package provider/claude calls, and their prices and efforts, without Anthropic's SDK: the library and the eval runner name and price a model by it, so that whatever takes them takes the SDK only with the adapter itself.
|
Package catalog is the Claude models the adapter of package provider/claude calls, and their prices and efforts, without Anthropic's SDK: the library and the eval runner name and price a model by it, so that whatever takes them takes the SDK only with the adapter itself. |
|
Package gpt is the adapter of OpenAI's GPT models, through the official Go SDK and the Responses API.
|
Package gpt is the adapter of OpenAI's GPT models, through the official Go SDK and the Responses API. |