Skip to main content

Crate typesafe

Crate typesafe 

Source
Expand description

A Rust client for TypeSafe’s System One API, the API behind Jev. It is unofficial. Its behaviour follows the official Python SDK: the same environment variables and defaults, the same retry policy, and the same error kinds.

System One models don’t generate text. You send some state and a set of typed questions, and get back one typed answer per question, with probabilities your code acts on:

QuestionAsksAnswer
Noulwhether a condition holdsNoulAnswer: the probability of yes
Choicewhich one of a defined setChoiceAnswer: option, probabilities, confidence
Scorehow far along ordered levelsScoreAnswer: weighted level, probabilities, confidence
use serde_json::json;
use typesafe::{Choice, Noul, Questions, Score};

let client = typesafe::blocking::Client::new(); // reads TYPESAFE_API_KEY

let questions = Questions::new()
    .ask("billing", Noul::new("Is this ticket about billing?"))
    .ask("tone", Choice::new("What is the customer's tone?").options(["calm", "frustrated", "angry"]))
    .ask("urgency", Score::new("How urgent is this ticket?", ["Can wait", "This week", "Today"]));

let response = client.system_one(
    &json!({"ticket": "I was charged twice. Please fix this ASAP."}),
    &questions,
)?;

let tone = response.choice("tone").unwrap();
if response.noul("billing").unwrap().yes() && tone.confidence > 0.5 {
    println!("billing, {} ({:.2})", tone.choice, tone.confidence);
}

§Features

  • async (default): Client, for a tokio runtime
  • blocking: blocking::Client, for synchronous code
  • rustls (default) or native-tls: the TLS backend

Modules§

blocking
A blocking client, for synchronous code such as a game loop or a CLI.

Structs§

CallOptions
Overrides for a single call.
Choice
A question that selects one option from a defined set. The answer is a ChoiceAnswer.
ChoiceAnswer
The answer to a Choice.
Client
An async client for the System One API. It needs a tokio runtime with the time driver enabled, which #[tokio::main] provides.
Config
Connection settings for a client.
Error
Why a call failed.
Model
A model name the account can send, from models().
Noul
A yes/no question. The answer, a NoulAnswer, is the probability that the answer is yes.
NoulAnswer
The answer to a Noul: noul is the probability of yes, from 0 to 1.
Questions
The questions for one call, each under an id. Answers come back under the same ids.
Response
The result of a System One call.
RetryPolicy
When and how long to wait before retrying. The defaults match TypeSafe’s official SDKs:
Score
A question that rates the state against ordered levels, worst or least first. The answer is a ScoreAnswer.
ScoreAnswer
The answer to a Score.
Usage
Tokens used by a call. Only input tokens are billed.

Enums§

Answer
Any one answer. Its variant matches the question that asked it.
ErrorKind
What kind of failure an Error is.
Question
Any one question.

Constants§

DEFAULT_BASE_URL
DEFAULT_MODEL
DEFAULT_TIMEOUT
ENV_API_KEY
ENV_BASE_URL
ENV_DEFAULT_MODEL