Documentation
¶
Overview ¶
Package jev is a classifier backed by Jev, TypeSafe's hosted classification model.
One Client holds one HTTP/2 connection pool, adds the auth header, retries when Jev is busy, 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
- type Classifier
- func (c *Classifier) AskQuestions(ctx context.Context, state any, ...) (map[string]classifier.Result, *frames.LLMTokenUsage, error)
- func (c *Classifier) Cleanup(ctx context.Context) error
- func (c *Classifier) Client() *Client
- func (c *Classifier) Model() string
- func (c *Classifier) Setup(ctx context.Context) error
- type Client
- func (c *Client) Ask(ctx context.Context, state any, questions []NamedQuestion) (map[string]map[string]any, Usage, error)
- func (c *Client) BaseURL() string
- func (c *Client) Close()
- func (c *Client) Connect(ctx context.Context) error
- func (c *Client) Model() string
- func (c *Client) Timeout() time.Duration
- func (c *Client) Usage() Usage
- type ClientConfig
- type Config
- type NamedQuestion
- type Question
- type Usage
Constants ¶
const ( // DefaultBaseURL is where the API is served. DefaultBaseURL = "https://api.typesafe.ai" // DefaultModel is the Jev model to ask. Pinned so thresholds tuned against // it hold. DefaultModel = "jev-1.13.0" // DefaultTimeout is how long to wait for a reply before giving up. DefaultTimeout = 10 * time.Second )
const MaxChoiceOptions = 255
MaxChoiceOptions is the most options Jev 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 Jev.
Jev's probabilities are calibrated. Build one with an 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.
client, err := jev.NewClient(jev.ClientConfig{APIKey: key})
turn, err := jev.New(jev.Config{Client: client})
voicemail, err := jev.New(jev.Config{Client: client})
func (*Classifier) AskQuestions ¶
func (c *Classifier) AskQuestions( ctx context.Context, state any, questions []classifier.Named[classifier.Question], ) (map[string]classifier.Result, *frames.LLMTokenUsage, error)
AskQuestions implements classifier.Asker, answering the questions in one request.
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) Model ¶
func (c *Classifier) Model() string
Model is the Jev model the questions go to.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is an HTTP client for Jev's systemone endpoint.
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 Jev answers 429 or 529, and counts the tokens every request used in Usage.
Connect opens the connection ahead of the first question, and Close releases it.
func (*Client) Ask ¶
func (c *Client) Ask( ctx context.Context, state any, questions []NamedQuestion, ) (map[string]map[string]any, Usage, error)
Ask sends questions about one state and returns Jev's answers, by the same names and in Jev's own format, and the tokens the request used.
It fails with an error wrapping classifier.ErrClassifier when Jev rejected the request, kept refusing it because it was busy, could not be reached, or left a question unanswered.
func (*Client) Connect ¶
Connect opens the connection to Jev.
It lists the models, the cheapest request there is, so the first question finds the connection already open. It connects once: a client shared by several classifiers is asked by each of them, and only the first call sends anything.
type ClientConfig ¶
type ClientConfig struct {
// APIKey is the Jev API key. Required.
APIKey string `validate:"required"`
// BaseURL is where the API is served; empty uses DefaultBaseURL.
BaseURL string
// Model is the Jev model to ask; empty uses DefaultModel, pinned so
// thresholds tuned against it keep holding. "jev-latest" follows TypeSafe's
// newest release.
Model string
// Timeout is how long to wait for a reply before giving up; zero uses
// DefaultTimeout.
Timeout time.Duration `validate:"min=0"`
// MaxRetries is how many times to retry a request Jev refused because it
// was 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 Jev 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 "JevClassifier#<n>".
Name string
// Model is the Jev model a client of its own asks; empty uses DefaultModel.
Model string
// BaseURL is where a client of its own sends its questions; empty uses
// DefaultBaseURL.
BaseURL string
// Timeout is how long a client of its own waits for an answer; zero uses
// DefaultTimeout.
Timeout time.Duration
}
Config configures a Jev classifier.
type NamedQuestion ¶
NamedQuestion is a question in Jev'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.
Instructions any
// Criteria is what counts as each answer, or nil when the question leaves
// it open.
Criteria any
}
Question is one question in Jev's own format.
func (Question) MarshalJSON ¶
MarshalJSON implements json.Marshaler, leaving criteria out when there are none.