Skip to main content

Client

Struct Client 

Source
pub struct Client<S = HyperTransport> { /* private fields */ }
Expand description

A client of the TypeSafe API.

Build one with Client::builder, or with Client::from_env when the environment holds everything. Cloning a client is cheap - the settings and the transport are shared behind one reference count - so a clone per task is the way to use one from many tasks, and all of them share one connection pool.

S is the transport. The default, HyperTransport, is an HTTP/2 client over TLS; any tower service over http requests is accepted through ClientBuilder::build_with_service.

Every request runs on the caller’s Tokio runtime, which needs its time driver enabled: each attempt has a deadline, and HTTP/2 keep-alive pings run on a timer.

Implementations§

Source§

impl Client<HyperTransport>

Source

pub fn builder() -> ClientBuilder

A builder for a client; every setting it leaves unset comes from the environment, then from the SDK’s default.

Source

pub fn from_env() -> Result<Self, Error>

A client configured by the environment alone: TYPESAFE_API_KEY, and optionally TYPESAFE_BASE_URL and TYPESAFE_DEFAULT_MODEL.

§Errors

Returns an ErrorKind::Config error when no API key is set, a value is unusable, or a variable is not UTF-8.

Source§

impl<S> Client<S>
where S: HttpService,

Source

pub fn system_one<'a, T>( &'a self, state: &'a T, questions: &'a PreparedQuestions, ) -> SystemOne<'a, S, T>
where T: Serialize + ?Sized,

A System One request asking questions about state.

state is anything that serializes to a JSON string, object or array; it is encoded when the request is sent, straight into the body. The request is configured with the builder’s methods and sent with send.

Source

pub fn models(&self) -> Models<'_, S>

The models resource.

Source

pub async fn warm_up(&self) -> Result<(), Error>

Lists the models once and drops the answer.

That checks the API key and leaves an open connection in the pool, so requests started together afterwards share it instead of each opening one. Call it before a burst of concurrent requests.

§Errors

Returns what ListModels::send returns: an authentication failure, for a key the API refuses.

Source§

impl<S> Client<S>
where S: HttpService,

Asking a QuestionSet.

Source

pub fn ask<'a, Q>( &'a self, state: &'a (impl Serialize + ?Sized), ) -> SystemOne<'a, S, impl Serialize + ?Sized, Q>
where Q: QuestionSet,

A System One request that asks the questions of Q about state and decodes the answers into a Q.

It is system_one with Q’s prepared questions, made typed as Q: the same builder, configured and sent the same way.

The state’s type is not a type parameter of this method, so that ask::<Ticket>(&state) names only the question set. The price is that the state’s type is opaque in the returned builder’s type: a function that takes the builder takes it as a generic. Where the type must be named, client.system_one(&state, Ticket::prepared()).typed::<Ticket>() is the same request with the state’s own type.

use std::time::Duration;

use typesafe_sdk::{Client, NoulAnswer, QuestionSet};

#[derive(QuestionSet)]
struct Spam {
    #[noul(instructions = "Is this message spam?")]
    spam: NoulAnswer,
}

let client = Client::builder().api_key("your-api-key").build()?;
let request = client.ask::<Spam>("Buy now!").timeout(Duration::from_secs(2));
// `request.send().await?` needs a Tokio runtime and returns a
// `SystemOneResponse<Spam>`, whose `answers().spam` is the answer.
drop(request);

Trait Implementations§

Source§

impl<S> Clone for Client<S>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S: Debug> Debug for Client<S>

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

The endpoints, the default model, the deadline, the response limit and the names of the default headers, then the transport. Never the API key and never a header value.

Auto Trait Implementations§

§

impl<S = HyperTransport> !RefUnwindSafe for Client<S>

§

impl<S = HyperTransport> !UnwindSafe for Client<S>

§

impl<S> Freeze for Client<S>
where Arc<Shared<S>>: Freeze,

§

impl<S> Send for Client<S>
where Arc<Shared<S>>: Send,

§

impl<S> Sync for Client<S>
where Arc<Shared<S>>: Sync,

§

impl<S> Unpin for Client<S>
where Arc<Shared<S>>: Unpin,

§

impl<S> UnsafeUnpin for Client<S>
where Arc<Shared<S>>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more