Repository navigation
Expand file tree
/
Copy pathdoc.go
More file actions
107 lines (107 loc) · 4.83 KB
/
Copy pathdoc.go
File metadata and controls
107 lines (107 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Package jev is an HTTP client for the TypeSafe System One API.
//
// Jev is TypeSafe's System One model. You send it a state (text or JSON) and
// one or more named questions. It answers every question in that call and
// returns a probability with each answer. It does not generate text.
// Since Jev 1.14 the state can also hold an image, and a box question returns
// where in that image one thing is. The HTTP contract is the [API reference];
// the ideas behind it are under [System One] and [primitives].
//
// Create a client with [New]. The API key comes from [WithAPIKey] or from
// the TYPESAFE_API_KEY environment variable.
//
// client, err := jev.New()
// resp, err := client.SystemOne(ctx, jev.Request{
// State: "I was charged twice. Please fix this ASAP.",
// Questions: jev.Questions{
// "billing": jev.Noul{Instructions: "Is this ticket about billing?"},
// "team": jev.Choice{
// Instructions: "Which team should handle this?",
// Criteria: map[string]any{
// "billing": "Payments, invoices, refunds",
// "technical": nil,
// },
// },
// "urgency": jev.Score{
// Instructions: "How urgent is this ticket?",
// Criteria: []string{"can wait", "this week", "today"},
// },
// },
// })
// if billing, ok := resp.Noul("billing"); ok {
// fmt.Println(billing.Noul)
// }
//
// [Client.SystemOneAs] decodes that same JSON into a struct of your own.
// It is a generic method, so it needs Go 1.27.
//
// One question at a time can use [Client.Noul], [Client.Choice], [Client.Score],
// [Client.Box], [Client.Classify], or [Client.Rate]. Each calls SystemOne with
// the question named "answer". The As forms call SystemOneAs.
//
// # Images and boxes
//
// A PNG or JPEG becomes an [Image] through [ImageFile], [ImageBytes], or
// [ImageReader]. Put the *Image anywhere a string can go: a value in the
// state, a question's Instructions, or a [Choice] option. The model reads a
// reference string in its place, the bytes travel with the request as
// multipart/form-data, and the client adds the TypeSafe-Danger-Zone-Images
// header; see [images]. [TextOf] mentions an image inside a sentence.
//
// A [Box] question finds one thing in an image and answers with a
// [BoxAnswer]: four edges as fractions of the image and a confidence; see
// [box]. A box comes back even when the target is absent, so pair it with a
// [Noul] when that is possible. Box needs Jev 1.14, so pass
// WithModel([ModelPreview]) until jev-latest moves there. Images themselves
// work on Jev 1.13 and 1.14.
//
// photo, err := jev.ImageFile("kitchen.jpg")
// resp, err := client.SystemOne(ctx, jev.Request{
// State: map[string]any{"photo": photo},
// Questions: jev.Questions{
// "mug_visible": jev.Noul{Instructions: "Is there a coffee mug in `photo`?"},
// "mug": jev.Box{Instructions: "the coffee mug in `photo`"},
// },
// }, jev.WithModel(jev.ModelPreview))
// if mug, ok := resp.Box("mug"); ok {
// fmt.Println(mug.Confidence, mug.Rect(width, height))
// }
//
// # Long context
//
// [WithLongContext] raises the input limit from 64k tokens per request and
// 32k per question to 262k, by sending the TypeSafe-Danger-Zone-Long-Context
// header. The API documents it as slower and less accurate at the same price
// per token, so it is off by default and is meant as a per-call option; see
// [context length].
//
// # Rate limits
//
// The API limits each account to a number of requests per minute and input
// tokens per second; the current figures are under [current models]. The
// client paces itself under those figures by default, with a [Limiter] built
// from [DefaultRateLimit], so a burst of calls queues instead of failing with
// 429. When the server does answer 429 or 529 with Retry-After, every caller
// on that limiter pauses until then. Set [WithRateLimit] to change the
// figures, or share one [NewLimiter] between clients on the same key with
// [WithRateLimiter]. See [handling rate limits].
//
// # Retries and errors
//
// The client retries 408, 429, and 5xx responses, including 529, with
// exponential backoff, as the API asks. Context cancellation is not retried.
// Failed responses are an [*APIError] and match a status sentinel such as
// [ErrRateLimited] through errors.Is; see [errors]. Credential headers are
// redacted from logs. Request and response bodies are not, except that image
// bytes are never logged.
//
// [API reference]: https://docs.typesafe.ai/api
// [System One]: https://docs.typesafe.ai/concepts/system-one
// [primitives]: https://docs.typesafe.ai/primitives
// [images]: https://docs.typesafe.ai/images
// [box]: https://docs.typesafe.ai/primitives/box
// [context length]: https://docs.typesafe.ai/models#context-length
// [current models]: https://docs.typesafe.ai/models#current-models
// [handling rate limits]: https://docs.typesafe.ai/api#handling-rate-limits
// [errors]: https://docs.typesafe.ai/api#errors
package jev