Expand description
Rust client for the TypeSafe AI System One API.
Send a state and a map of typed questions — Noul (yes/no probability), Choice
(one of N labels) and Score (ordered levels) — and get typed answers back.
Behaviour follows the official Python SDK (typesafe-sdk): the same environment variables,
defaults, retry semantics, error classification and forward-compatible decoding.
Enable the blocking feature for a synchronous client in blocking, reqwest-client
to supply your own reqwest::Client, and derive for #[derive(Rubric)]: a struct that
describes the questions and receives the answers (see rubric).
§Example
With the derive feature, a struct is the rubric: each field is a question, and the answers
come back into it, typed.
use typesafe::{ChoiceOf, Client, NoulAnswer, Rubric, RubricChoice, ScoreAnswer};
#[derive(Rubric)]
struct Triage {
#[noul("The message conveys urgency")]
is_urgent: NoulAnswer,
#[choice("Which team should handle this")]
department: ChoiceOf<Department>,
#[score("How frustrated", levels = ["Calm", "Frustrated but civil", "Very angry"])]
frustration: ScoreAnswer,
}
#[derive(Debug, RubricChoice)]
enum Department {
/// Payment or subscription issues
Billing,
/// Bugs or integration problems
Technical,
}
let client = Client::from_env()?; // TYPESAFE_API_KEY
let triage = client
.ask::<Triage>("I've been trying to connect Stripe for 3 days. Please help ASAP.")
.await?;
println!("route to {:?}", *triage.department);
println!("urgent: {}", triage.is_urgent.is_yes(0.8));
println!("frustration: {:.2}", triage.frustration.score);Without the derive, Client::system_one takes a Questions map built at runtime and
returns a SystemOneResponse to look answers up in by name.
Re-exports§
pub use error::ApiError;pub use error::ApiErrorKind;pub use error::Error;pub use error::ResponseValidationError;pub use error::Result;pub use question::Choice;pub use question::Noul;pub use question::NoulCriteria;pub use question::Question;pub use question::Questions;pub use question::Score;pub use response::Answer;pub use response::AnswerKind;pub use response::ChoiceAnswer;pub use response::ListModelsResponse;pub use response::ModelMetadata;pub use response::NoulAnswer;pub use response::ResponseMeta;pub use response::ScoreAnswer;pub use response::SystemOneResponse;pub use response::Usage;pub use retry::RetryPolicy;pub use rubric::AskRequest;pub use rubric::ChoiceOf;pub use rubric::Rubric;pub use rubric::RubricChoice;pub use http;pub use serde_json;
Modules§
- blocking
blocking - Synchronous client (feature
blocking). Each client owns a private current-thread Tokio runtime, shared with its clones; do not call it from inside an async context. - cassette
- Record and replay: System One responses kept on disk, one file per request.
- constants
- Environment-variable names, defaults and protocol constants.
- error
- Error types.
- question
- Typed questions:
Noul,ChoiceandScore. - response
- Answers and response metadata.
- retry
- Retry configuration. Semantics match the Python SDK’s
RetryPolicy(Tenacity-based): exponential backoff with subtractive jitter,Retry-After/retry-after-mssupport, a max retry count, and a total time budget that stops before a sleep that would exceed it. - rubric
- Rubrics as types: a struct describes the questions, and the answers come back into it.
Macros§
- json
- Re-exported so callers can build structured instructions/state without adding a dependency.
Construct a
serde_json::Valuefrom a JSON literal.
Structs§
- Client
- TypeSafe API client. Cheap to clone; clones share the connection pool.
- Client
Builder - Builder for
Client. Explicit settings win over environment variables; empty or whitespace-only environment values are ignored. - List
Models Request - A pending
GET /v1/models. - Models
- The Models resource.
- Status
Code - The HTTP status type of
Error::status,ApiError::status,ResponseMeta::statusandRetryPolicy::statuses, with constants such asStatusCode::TOO_MANY_REQUESTS. An HTTP status code (status-codein RFC 9110 et al.). - System
OneRequest - A pending
POST /v1/systemone. Configure it, then.awaitit (or callsend).
Derive Macros§
- Rubric
derive #[derive(Rubric)]and#[derive(RubricChoice)](featurederive); seerubric. Implementtypesafe::rubric::Rubricfor a struct whose fields are the questions.- Rubric
Choice derive #[derive(Rubric)]and#[derive(RubricChoice)](featurederive); seerubric. Implementtypesafe::rubric::RubricChoice(andFromStr) for an enum whose variants are the options of a choice.