Documentation
¶
Overview ¶
Package typesafe is an unofficial Go client for the TypeSafe AI API.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Answer ¶
type Answer interface {
// contains filtered or unexported methods
}
Answer is one of ChoiceAnswer, ScoreAnswer, or NoulAnswer.
type ChoiceAnswer ¶
type ChoiceAnswer struct {
Choice string `json:"choice"`
Confidence float64 `json:"confidence"`
Probabilities map[string]float64 `json:"probabilities"`
}
ChoiceAnswer answers a Choice question.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client talks to the TypeSafe AI API.
func NewClient ¶
NewClient builds a Client from environment variables and options. It fails if no API key is configured.
func (*Client) ListModels ¶
ListModels returns the available models via GET /v1/models.
Example ¶
package main
import (
"context"
"fmt"
"log"
typesafe "github.com/atharvamhaske/typesafe-sdk-go"
)
func main() {
client, err := typesafe.NewClient()
if err != nil {
log.Fatal(err)
}
models, err := client.ListModels(context.Background())
if err != nil {
log.Fatal(err)
}
for _, m := range models {
fmt.Println(m.Name, m.Description)
}
}
Output:
func (*Client) SystemOne ¶
func (c *Client) SystemOne(ctx context.Context, state string, questions map[string]Question, opts ...SystemOneOption) (*SystemOneResponse, error)
SystemOne asks questions about a state via POST /v1/systemone. When the client was built with WithCache, an identical request within the cache's TTL is served without a network call.
Example ¶
package main
import (
"context"
"fmt"
"log"
typesafe "github.com/atharvamhaske/typesafe-sdk-go"
)
func main() {
client, err := typesafe.NewClient() // reads TYPESAFE_API_KEY
if err != nil {
log.Fatal(err)
}
resp, err := client.SystemOne(context.Background(),
"I was charged twice. Please refund the duplicate charge today.",
map[string]typesafe.Question{
"category": typesafe.Choice{
Instructions: "Categorize the message",
Criteria: map[string]string{"billing": "Billing issue", "technical": "Technical issue"},
},
"urgency": typesafe.Score{
Instructions: "Rate urgency",
Criteria: []string{"Can wait", "Needs attention"},
},
"is_dupe": typesafe.Noul{
Instructions: "Is this a duplicate charge?",
Criteria: map[string]string{"true": "Duplicate", "false": "Not a duplicate"},
},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(resp.Choices()["category"].Choice)
fmt.Println(resp.Scores()["urgency"].Score)
fmt.Println(resp.Nouls()["is_dupe"].Noul)
}
Output:
type Error ¶
type Error struct {
StatusCode int
Detail json.RawMessage // HTTPValidationError detail, if any
Body []byte
}
Error is a non-2xx API response.
type Model ¶
type Model struct {
Name string `json:"name"`
Description string `json:"description"`
ReleaseDate string `json:"release_date"`
}
Model describes an available TypeSafe model.
type NoulAnswer ¶
type NoulAnswer struct {
Noul float64 `json:"noul"`
}
NoulAnswer answers a Noul question.
type Option ¶
type Option func(*Client)
Option configures a Client.
func WithAPIKey ¶
WithAPIKey sets the API key, overriding TYPESAFE_API_KEY.
func WithBaseURL ¶
WithBaseURL sets the base URL, overriding TYPESAFE_BASE_URL.
func WithCache ¶ added in v0.2.0
WithCache enables an opt-in, in-memory cache for SystemOne responses, keyed by the request body and evicted after ttl. Disabled by default.
func WithHTTPClient ¶
WithHTTPClient sets the underlying *http.Client.
func WithMaxRetries ¶ added in v0.2.0
WithMaxRetries sets how many times a retryable failure (network error, 429, or 5xx) is retried, with backoff honoring any Retry-After header. Default 3.
type Question ¶
type Question interface {
// contains filtered or unexported methods
}
Question is one of Choice, Score, or Noul.
type ScoreAnswer ¶
type ScoreAnswer struct {
Score float64 `json:"score"`
Confidence float64 `json:"confidence"`
Legend map[string]string `json:"legend"`
Probabilities map[string]float64 `json:"probabilities"`
}
ScoreAnswer answers a Score question.
type SystemOneOption ¶
type SystemOneOption func(*systemOneRequest)
SystemOneOption configures a single SystemOne call.
func WithRequestModel ¶
func WithRequestModel(model string) SystemOneOption
WithRequestModel overrides the client's default model for this call.
type SystemOneResponse ¶
type SystemOneResponse struct {
Model string `json:"model"`
Usage Usage `json:"usage"`
Answers map[string]Answer `json:"-"`
}
SystemOneResponse is the result of a SystemOne call.
func (*SystemOneResponse) Choices ¶
func (r *SystemOneResponse) Choices() map[string]ChoiceAnswer
Choices returns the choice-typed answers by question key.
func (*SystemOneResponse) Nouls ¶
func (r *SystemOneResponse) Nouls() map[string]NoulAnswer
Nouls returns the noul-typed answers by question key.
func (*SystemOneResponse) Scores ¶
func (r *SystemOneResponse) Scores() map[string]ScoreAnswer
Scores returns the score-typed answers by question key.
func (*SystemOneResponse) UnmarshalJSON ¶
func (r *SystemOneResponse) UnmarshalJSON(data []byte) error
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
gamemove
command
Command gamemove picks the next move in a toy Snake game, following the same pattern as the dozens of Jev-plays-a-game demos in the community (jev-snake, jev-tetris, Jev Pac-Man, and others): the game engine owns the rules and state, and one Choice question per tick picks the move.
|
Command gamemove picks the next move in a toy Snake game, following the same pattern as the dozens of Jev-plays-a-game demos in the community (jev-snake, jev-tetris, Jev Pac-Man, and others): the game engine owns the rules and state, and one Choice question per tick picks the move. |
|
listmodels
command
Command listmodels prints the available TypeSafe models.
|
Command listmodels prints the available TypeSafe models. |
|
prlabel
command
Command prlabel labels a pull request diff by its conceptual scope, following the same pattern as jev-pr-labeler and commit-miner in the Jev community: a Choice question over the diff instead of line counts.
|
Command prlabel labels a pull request diff by its conceptual scope, following the same pattern as jev-pr-labeler and commit-miner in the Jev community: a Choice question over the diff instead of line counts. |
|
spamfilter
command
Command spamfilter scores a message for spam, following the same pattern as the many Jev-powered moderation and antispam bots in the community: one Noul question per message, thresholded in code.
|
Command spamfilter scores a message for spam, following the same pattern as the many Jev-powered moderation and antispam bots in the community: one Noul question per message, thresholded in code. |
|
systemone
command
Command systemone asks typed questions about a support message.
|
Command systemone asks typed questions about a support message. |
