pub struct Client { /* private fields */ }Expand description
A client of the TypeSafe API.
A client holds the settings of each request. A clone shares the connections, the cache, the limit on requests, and the total of tokens. A builder method changes only its own client.
.ask on a question set uses the global client. Its first use makes it with
Client::from_env. Client::set_global replaces it.
§Examples
use std::time::Duration;
use system2::{Client, noul};
let client = Client::from_env()?
.model("jev-1.14.0")
.timeout(Duration::from_secs(30));
let urgent = noul!("Is the ticket urgent?");
let answer = client.ask(urgent, "The site is down.").await?;Implementations§
Source§impl Client
impl Client
Sourcepub fn from_env() -> Result<Self, EnvError>
pub fn from_env() -> Result<Self, EnvError>
Makes a client from the environment.
It reads five variables. It removes whitespace from the start and the end of each one. An empty variable counts as not set.
TYPESAFE_API_KEY: the key. With no key, only the cache answers.TYPESAFE_BASE_URL: the base URL.TYPESAFE_DEFAULT_MODEL: the model.SYSTEM2_CACHE: the path of a cache file. With no path, the client has no cache.SYSTEM2_OFFLINE:1makes the client offline.
§Errors
EnvError::InvalidApiKeyif the key is not a valid HTTP header value.EnvError::InvalidBaseUrlif the base URL is not an absolute HTTP or HTTPS URL.EnvError::Cacheif the client cannot open the cache file.
Sourcepub fn api_key(self, api_key: impl Into<String>) -> Self
pub fn api_key(self, api_key: impl Into<String>) -> Self
Sets the API key. The default is no key.
It removes whitespace from the start and the end of the key, and an empty key counts as no
key. The client needs a key only for a request that it sends. ask returns
AskError::MissingApiKey with no key, and AskError::InvalidApiKey if the key is not
a valid HTTP header value.
Sourcepub fn base_url(self, base_url: impl Into<String>) -> Self
pub fn base_url(self, base_url: impl Into<String>) -> Self
Sets the base URL of the API. The default is https://api.typesafe.ai.
https://openrouter.ai/api sends each request through OpenRouter, which serves Jev with
the same API. Give the client an OpenRouter key with api_key or
TYPESAFE_API_KEY.
ask returns AskError::InvalidBaseUrl if the URL is not an absolute HTTP or HTTPS URL.
Sourcepub fn model(self, model: impl Into<String>) -> Self
pub fn model(self, model: impl Into<String>) -> Self
Sets the model. The default is jev-latest.
The client does not validate the name. The API refuses an unknown model, and ask returns
AskError::Http.
Sourcepub fn timeout(self, timeout: Duration) -> Self
pub fn timeout(self, timeout: Duration) -> Self
Sets the timeout of each attempt, from the send to the end of the response. The default is 10 s.
Sourcepub fn max_retries(self, max_retries: u32) -> Self
pub fn max_retries(self, max_retries: u32) -> Self
Sets the number of attempts after the first. The default is 2.
A 408, a 429, a 5xx, a timeout, or a failed connection gets a retry. The wait comes from
the retry-after-ms or Retry-After header, or else from a backoff from 0.5 s to 5 s. If
a header asks for a wait of more than 30 s, the client returns the error.
Sourcepub fn max_concurrent_requests(self, max_concurrent_requests: usize) -> Self
pub fn max_concurrent_requests(self, max_concurrent_requests: usize) -> Self
Sets the number of requests that the client and its clones send at the same time. The default is 8.
A request over the limit waits. An answer from the cache does not wait. A limit of 0 is a limit of 1. On a clone, it gives a new limit, which only the later clones of that clone share.
Sourcepub fn long_context(self, long_context: bool) -> Self
pub fn long_context(self, long_context: bool) -> Self
Raises the token limits of the API. The default is false.
It sends the header TypeSafe-Danger-Zone-Long-Context: true. OpenRouter always has the
long context, so this changes nothing there.
Sourcepub fn compression(self, compression: Compression) -> Self
pub fn compression(self, compression: Compression) -> Self
Sets the compression of each request body. The default is Compression::Zstd.
Sourcepub fn cache(self, path: impl Into<PathBuf>) -> Result<Self, CacheError>
pub fn cache(self, path: impl Into<PathBuf>) -> Result<Self, CacheError>
Opens a cache file, and makes it the cache of the client. The default is no cache.
The cache is a JSON Lines file. Each line holds the SHA-256 of a request body and the response. A request with an equal body gets the stored response, with no network. A response goes into the file only after its answers are valid. The client reads the file only here, so it does not see the lines that another process adds later.
It makes the file if the file does not exist, but not its folder. A file that the client
can only read, such as on a read-only file system, still gives its responses. Then ask
returns AskError::Cache for a new response. On a clone, it gives a new cache, which
only the later clones of that clone share.
§Errors
CacheError::Readif the client cannot read or make the file.CacheError::Writeif the last line of a writable file has no newline, and the client cannot add one.CacheError::InvalidLineif a line is not the hash of a request and its response.
Sourcepub fn offline(self, offline: bool) -> Self
pub fn offline(self, offline: bool) -> Self
Makes the client answer only from the cache. The default is false.
An offline request that is not in the cache returns AskError::Offline.
Sourcepub fn trace_bodies(self, trace_bodies: bool) -> Self
pub fn trace_bodies(self, trace_bodies: bool) -> Self
Writes the body of each request in a TRACE event. The default is false.
The event has the target system2::body, and the body is in its field system2.body. A
body holds the state, which is often private.
Sourcepub fn usage(&self) -> Usage
pub fn usage(&self) -> Usage
Returns the total of tokens of the client and its clones.
It counts the tokens of each response from the API, also when the answers or the model are not valid. An answer from the cache adds no tokens.
Sourcepub fn set_global(self) -> Client
pub fn set_global(self) -> Client
Makes the client the global client, and returns a clone of it.
A request that .ask started before keeps its client. A clone of the old global client
does not change.
Sourcepub fn global() -> Result<Client, EnvError>
pub fn global() -> Result<Client, EnvError>
Returns a clone of the global client.
If there is no global client, it makes one with Client::from_env, as the first .ask
does. A clone shares the total of tokens, so its usage is the total of
each .ask since the last set_global.
§Errors
Returns the EnvError of Client::from_env if there is no global client, and the
environment has a bad value.
Sourcepub fn ask<Q: QuestionSet, S: Serialize + ?Sized>(
&self,
question_set: Q,
state: &S,
) -> AskFuture<Q> ⓘ
pub fn ask<Q: QuestionSet, S: Serialize + ?Sized>( &self, question_set: Q, state: &S, ) -> AskFuture<Q> ⓘ
Sourcepub fn ask_with_receipt<Q: QuestionSet, S: Serialize + ?Sized>(
&self,
question_set: Q,
state: &S,
) -> AskWithReceiptFuture<Q> ⓘ
pub fn ask_with_receipt<Q: QuestionSet, S: Serialize + ?Sized>( &self, question_set: Q, state: &S, ) -> AskWithReceiptFuture<Q> ⓘ
Asks a question set with this client, and returns a Receipt next to the answers.
It is the same as Ask::ask_with_receipt, which uses the
global client.
§Errors
The future returns an AskError as Client::ask does.
§Panics
The future panics if it sends a request outside a Tokio runtime.