Documentation
¶
Overview ¶
Package jev provides a client for the TypeSafe System One API.
It sends a state and a set of typed questions, and returns calibrated answers: a choice from a list, a score on an ordered rubric, or the probability that a statement is true.
Usage:
client, err := jev.New()
if err != nil {
log.Fatal(err)
}
res, err := client.SystemOne(ctx, jev.State(text),
jev.Questions{
"material": jev.Noul("The text describes a material event"),
})
Index ¶
- Variables
- type APIError
- type Answer
- type ChoiceAnswer
- type Client
- func (c *Client) Evaluate(ctx context.Context, req Request) (*Response, error)
- func (c *Client) Map(ctx context.Context, states []Input, questions Questions, opts ...MapOption) []MapResult
- func (c *Client) SystemOne(ctx context.Context, state Input, questions Questions, opts ...RequestOption) (*Response, error)
- type ClientOption
- type Input
- type MapOption
- type MapResult
- type NoulAnswer
- type Question
- type Questions
- type Request
- type RequestOption
- type Response
- type ScoreAnswer
- type Usage
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNoQuestions = errors.New("jev: at least one question is required")
ErrNoQuestions is returned when a request has no questions.
Functions ¶
This section is empty.
Types ¶
type APIError ¶
APIError represents a non-success response from the TypeSafe API. It is a type alias, so callers use errors.As.
type Answer ¶
type Answer interface {
Kind() string
}
Answer is a typed answer. Use a type switch, or the Choice, Score, and Noul accessors on Response.
type ChoiceAnswer ¶
type ChoiceAnswer struct {
Choice string `json:"choice"`
Confidence float64 `json:"confidence"`
Probabilities map[string]float64 `json:"probabilities"`
}
ChoiceAnswer is the answer to a choice question.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a TypeSafe System One client. Must be constructed via New. Safe for concurrent use by multiple goroutines.
API key resolution order:
- WithAPIKey("...")
- $TYPESAFE_API_KEY environment variable
func (*Client) Evaluate ¶
Evaluate sends a request and returns the decoded response. Transient failures (HTTP 429, 529, and 5xx) are retried with exponential backoff.
func (*Client) Map ¶
func (c *Client) Map(ctx context.Context, states []Input, questions Questions, opts ...MapOption) []MapResult
Map evaluates the same questions against many states with bounded concurrency, retries with exponential backoff on retryable failures, and optional rate limiting. Results are returned in input order.
Example ¶
package main
import (
"context"
"fmt"
"github.com/shanehull/go-jev"
)
func main() {
client, _ := jev.New()
states := []jev.Input{
jev.State("The system is down"),
jev.State("Thanks for the quick fix"),
}
results := client.Map(context.Background(), states,
jev.Questions{"negative": jev.Noul("The text expresses a negative experience")},
jev.WithConcurrency(2),
)
for _, result := range results {
if result.Err == nil {
noul, _ := result.Response.Noul("negative")
fmt.Println(noul.Noul)
}
}
}
Output:
func (*Client) SystemOne ¶
func (c *Client) SystemOne(ctx context.Context, state Input, questions Questions, opts ...RequestOption) (*Response, error)
SystemOne evaluates a set of questions against a state in a single request. This is the fan-out primitive: every question is answered in one round trip.
Example ¶
package main
import (
"context"
"fmt"
"github.com/shanehull/go-jev"
)
func main() {
client, _ := jev.New()
res, _ := client.SystemOne(context.Background(), jev.State("I was charged twice, please fix this ASAP."),
jev.Questions{
"category": jev.Choice("What is this about", map[string]string{
"billing": "Payment or subscription issues",
"technical": "Bugs or integration problems",
"other": "Anything else",
}),
"urgent": jev.Noul("The message conveys urgency"),
},
)
category, _ := res.Choice("category")
urgent, _ := res.Noul("urgent")
fmt.Println(category.Choice, urgent.Noul)
}
Output:
type ClientOption ¶
ClientOption configures a Client.
func WithBaseURL ¶
func WithBaseURL(baseURL string) ClientOption
WithBaseURL sets a custom base URL.
func WithHTTPClient ¶
func WithHTTPClient(hc *http.Client) ClientOption
WithHTTPClient sets a custom HTTP client.
func WithMaxAttempts ¶
func WithMaxAttempts(n int) ClientOption
WithMaxAttempts sets the number of attempts made for a single request, including the first. It defaults to 3. Transient failures (HTTP 429, 529, and 5xx) are retried with exponential backoff.
func WithModel ¶
func WithModel(model string) ClientOption
WithModel sets the default model. It defaults to jev-latest.
type Input ¶
type Input struct {
// contains filtered or unexported fields
}
Input is the state evaluated by the model. Construct it with State or Value.
func Value ¶
Value returns a structured input, which is sent as JSON.
Example ¶
package main
import (
"context"
"fmt"
"github.com/shanehull/go-jev"
)
func main() {
client, _ := jev.New()
state := jev.Value(map[string]any{"text": "The build failed.", "env": "production"})
res, _ := client.SystemOne(context.Background(), state,
jev.Questions{"failure": jev.Noul("The text describes a failure")},
)
failure, _ := res.Noul("failure")
fmt.Println(failure.Noul)
}
Output:
type MapOption ¶
type MapOption func(*mapParams)
MapOption configures a Map call.
func WithConcurrency ¶
WithConcurrency sets the number of in-flight requests. It defaults to 4.
func WithMapModel ¶
WithMapModel overrides the model for a Map call.
func WithMaxRetries ¶
WithMaxRetries sets the number of retries after the first attempt for retryable failures. It defaults to 2.
func WithMinInterval ¶
WithMinInterval sets the minimum spacing between requests, a simple rate limit.
type NoulAnswer ¶
type NoulAnswer struct {
Noul float64 `json:"noul"`
}
NoulAnswer is the answer to a noul question.
type Question ¶
type Question struct {
Type string `json:"type"`
Instructions string `json:"instructions"`
Criteria json.RawMessage `json:"criteria,omitempty"`
}
Question is a typed question for the model. Construct it with Choice, Score, or Noul.
func Choice ¶
Choice builds a choice question. The model picks one option and returns a probability for each option.
Example ¶
package main
import (
"context"
"fmt"
"github.com/shanehull/go-jev"
)
func main() {
client, _ := jev.New()
res, _ := client.SystemOne(context.Background(), jev.State("I was charged twice."),
jev.Questions{
"category": jev.Choice("What is this about", map[string]string{
"billing": "Payment or invoice issues",
"technical": "Bugs or integration problems",
}),
})
category, _ := res.Choice("category")
fmt.Println(category.Choice, category.Confidence, category.Probabilities)
}
Output:
func Noul ¶
Noul builds a noul question. The model returns the probability that the statement is true.
Example ¶
package main
import (
"context"
"fmt"
"github.com/shanehull/go-jev"
)
func main() {
client, _ := jev.New()
res, _ := client.SystemOne(context.Background(), jev.State("Please fix this now, customers are blocked."),
jev.Questions{
"urgent": jev.Noul("The message conveys urgency"),
})
urgent, _ := res.Noul("urgent")
fmt.Println(urgent.Noul)
}
Output:
func Score ¶
Score builds a score question. The model places the state on an ordered rubric.
Example ¶
package main
import (
"context"
"fmt"
"github.com/shanehull/go-jev"
)
func main() {
client, _ := jev.New()
res, _ := client.SystemOne(context.Background(), jev.State("The site is completely down."),
jev.Questions{
"severity": jev.Score("How severe is the incident", []string{
"Minor", "Degraded", "Major", "Outage",
}),
})
severity, _ := res.Score("severity")
fmt.Println(severity.Score, severity.Legend)
}
Output:
type Request ¶
Request is a full System One request. Send it directly with Evaluate, or use SystemOne for the common case.
type RequestOption ¶
type RequestOption func(*requestParams)
RequestOption configures a single SystemOne call.
func WithRequestModel ¶
func WithRequestModel(model string) RequestOption
WithRequestModel overrides the model for one call.
type Response ¶
type Response struct {
Model string `json:"model"`
Answers map[string]Answer `json:"answers"`
Usage Usage `json:"usage"`
}
Response is the model's set of answers for one state.
func (*Response) Choice ¶
func (r *Response) Choice(name string) (ChoiceAnswer, bool)
Choice returns the choice answer stored under name.
func (*Response) Noul ¶
func (r *Response) Noul(name string) (NoulAnswer, bool)
Noul returns the noul answer stored under name.
func (*Response) Score ¶
func (r *Response) Score(name string) (ScoreAnswer, bool)
Score returns the score answer stored under name.
func (*Response) UnmarshalJSON ¶
UnmarshalJSON decodes answers by their wire type discriminator.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
batch
command
Command batch evaluates many states concurrently with Map.
|
Command batch evaluates many states concurrently with Map. |
|
choice
command
Command choice routes a message to a team with a choice question.
|
Command choice routes a message to a team with a choice question. |
|
noul
command
Command noul returns the probability that a statement about a text is true.
|
Command noul returns the probability that a statement about a text is true. |
|
score
command
Command score rates an incident note on an ordered severity rubric.
|
Command score rates an incident note on an ordered severity rubric. |
|
structured
command
Command structured sends a JSON state with Value.
|
Command structured sends a JSON state with Value. |
|
systemone
command
Command systemone classifies a support ticket across the three primitives.
|
Command systemone classifies a support ticket across the three primitives. |
|
verification
command
Command verification checks a draft answer against its source, a guardrail pattern that catches unsupported or fabricated claims.
|
Command verification checks a draft answer against its source, a guardrail pattern that catches unsupported or fabricated claims. |
|
Package internal provides the HTTP transport shared by the client.
|
Package internal provides the HTTP transport shared by the client. |