Expand description
Async Rust SDK for the TypeSafe AI API.
The crate is published as typesafe-sdk-rust because typesafe-sdk is
already taken on crates.io; the library it builds is typesafe_sdk, so
callers write use typesafe_sdk::....
§Asking questions
A call asks a set of named questions about a state. The set is built once,
validated and serialized by Questions::prepare, and the resulting
PreparedQuestions is reused by every call that asks it. A Client
sends it: Client::system_one makes a request, whose methods set the
model, the deadline, extra headers and extra body members, and
send sends it and decodes the answers.
use std::time::Duration;
use typesafe_sdk::{Choice, Client, Noul, Questions, Score};
let questions = Questions::new()
.noul("billing", Noul::new().instructions("Is this about billing?"))
.choice("tone", Choice::new(["calm", "angry"]).instructions("What is the tone?"))
.score("urgency", Score::new(["can wait", "this week", "today"]))
.prepare()?;
assert_eq!(questions.names().collect::<Vec<_>>(), ["billing", "tone", "urgency"]);
// `Client::from_env()` reads the same settings from TYPESAFE_API_KEY and
// friends. Building connects to nothing.
let client = Client::builder().api_key("your-api-key").build()?;
let state = "I was charged twice for one order.";
let request = client
.system_one(state, &questions)
.model("jev-latest")
.timeout(Duration::from_secs(2))
.header("x-team", "billing");
// Sending needs a Tokio runtime; this example stops before it.
async fn ask(request: typesafe_sdk::SystemOne<'_, typesafe_sdk::HyperTransport, str>)
-> Result<f64, typesafe_sdk::Error> {
let response = request.send().await?;
Ok(response.answers().noul("billing").map_or(0.0, |answer| answer.noul()))
}
drop(ask(request));§Runtime requirements
Every network operation is async and expects a Tokio runtime whose
time driver is enabled (#[tokio::main], or a Builder with
enable_time() / enable_all()). Per-attempt deadlines and HTTP/2
keep-alive both arm timers, and Tokio panics when a timer is created on a
runtime without that driver.
§Safety
The crate is #![forbid(unsafe_code)]. Dependencies that use unsafe
internally are confined to single modules so that swapping one out is a
local change.
Re-exports§
pub use crate::client::Client;pub use crate::client::ClientBuilder;pub use crate::content::Content;pub use crate::content::ContentError;pub use crate::de::AnswerContext;pub use crate::de::AnswerSet;pub use crate::error::ApiError;pub use crate::error::ApiErrorKind;pub use crate::error::Error;pub use crate::error::ErrorKind;pub use crate::error::ResponseValidationError;pub use crate::models::ListModels;pub use crate::models::ListModelsResponse;pub use crate::models::ModelMetadata;pub use crate::models::Models;pub use crate::question::Choice;pub use crate::question::Noul;pub use crate::question::PreparedQuestions;pub use crate::question::Question;pub use crate::question::Questions;pub use crate::question::RawQuestion;pub use crate::question::Score;pub use crate::request::SystemOne;pub use crate::response::Answer;pub use crate::response::Answers;pub use crate::response::ChoiceAnswer;pub use crate::response::NoulAnswer;pub use crate::response::ResponseMeta;pub use crate::response::ScoreAnswer;pub use crate::response::SystemOneResponse;pub use crate::response::Usage;pub use crate::retry::RetryPolicy;pub use crate::retry::StatusSet;pub use crate::transport::Body;pub use crate::transport::BoxError;pub use crate::transport::HttpService;pub use crate::transport::HttpVersion;pub use crate::transport::HyperResponseFuture;pub use crate::transport::HyperTransport;pub use crate::transport::ResponseBody;pub use crate::question::QuestionSet;
Modules§
- client
- The client: what a caller holds, clones and shares.
- constants
- The names and defaults the SDK’s behaviour is pinned to: the environment variables, headers, API paths and defaults.
- content
- The value the API calls “string, object or array” content.
- de
- Reading an answer set in one pass.
- error
- What a call can fail with, and how a failure renders.
- models
- The models endpoint.
- question
- The questions a call asks, and the shapes the API accepts them in.
- request
- Building one request and sending it.
- response
- What a call answers with.
- retry
- When a failed attempt is worth repeating, and how long to wait first.
- transport
- The seam between this crate and whatever sends the bytes.
Structs§
- Decode
Error - A JSON document could not be decoded into the expected type.
- Encode
Error - A value could not be encoded as JSON.
- RawJson
- An owned piece of JSON text that travels through the SDK unchanged.
Enums§
- Decode
Error Kind - The reason a JSON document could not be decoded.
Derive Macros§
- Question
Set - Implements
QuestionSetandAnswerSetfor a struct with one field per question.