Skip to main content

Crate typesafe

Crate typesafe 

Source
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§

blockingblocking
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, Choice and Score.
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-ms support, 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::Value from a JSON literal.

Structs§

Client
TypeSafe API client. Cheap to clone; clones share the connection pool.
ClientBuilder
Builder for Client. Explicit settings win over environment variables; empty or whitespace-only environment values are ignored.
ListModelsRequest
A pending GET /v1/models.
Models
The Models resource.
StatusCode
The HTTP status type of Error::status, ApiError::status, ResponseMeta::status and RetryPolicy::statuses, with constants such as StatusCode::TOO_MANY_REQUESTS. An HTTP status code (status-code in RFC 9110 et al.).
SystemOneRequest
A pending POST /v1/systemone. Configure it, then .await it (or call send).

Derive Macros§

Rubricderive
#[derive(Rubric)] and #[derive(RubricChoice)] (feature derive); see rubric. Implement typesafe::rubric::Rubric for a struct whose fields are the questions.
RubricChoicederive
#[derive(Rubric)] and #[derive(RubricChoice)] (feature derive); see rubric. Implement typesafe::rubric::RubricChoice (and FromStr) for an enum whose variants are the options of a choice.