use serde::Serialize;
use crate::Client;
use crate::config::{CallOptions, ClientConfig};
use crate::error::Error;
use crate::types::{ModelCard, Questions, SystemOneRequest, SystemOneResponse};
#[cfg_attr(docsrs, doc(cfg(feature = "blocking")))]
#[derive(Debug)]
pub struct BlockingClient {
client: Client,
rt: tokio::runtime::Runtime,
}
impl BlockingClient {
pub fn new(config: ClientConfig) -> Result<Self, Error> {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.map_err(|err| Error::InvalidRequest(format!("failed to create runtime: {err}")))?;
let client = Client::new(config)?;
Ok(Self { client, rt })
}
pub fn from_env() -> Result<Self, Error> {
Self::new(ClientConfig::default())
}
pub fn new_with_env(
config: ClientConfig,
lookup: impl FnMut(&str) -> Option<String>,
) -> Result<Self, Error> {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.map_err(|err| Error::InvalidRequest(format!("failed to create runtime: {err}")))?;
let client = Client::new_with_env(config, lookup)?;
Ok(Self { client, rt })
}
pub fn system_one(
&self,
state: impl Serialize,
questions: Questions,
) -> Result<SystemOneResponse, Error> {
self.rt.block_on(self.client.system_one(state, questions))
}
pub fn system_one_with(
&self,
req: &SystemOneRequest,
opts: CallOptions,
) -> Result<SystemOneResponse, Error> {
self.rt.block_on(self.client.system_one_with(req, opts))
}
#[must_use]
pub fn models(&self) -> BlockingModels<'_> {
BlockingModels { client: self }
}
pub fn warm_up(&self) -> Result<(), Error> {
self.rt.block_on(self.client.warm_up())
}
#[must_use]
pub fn default_model(&self) -> &str {
self.client.default_model()
}
#[must_use]
pub fn base_url(&self) -> &crate::Url {
self.client.base_url()
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "blocking")))]
#[derive(Debug)]
pub struct BlockingModels<'a> {
client: &'a BlockingClient,
}
impl BlockingModels<'_> {
pub fn list(&self) -> Result<Vec<ModelCard>, Error> {
self.client.rt.block_on(self.client.client.models().list())
}
pub fn list_with(&self, opts: &CallOptions) -> Result<Vec<ModelCard>, Error> {
self.client
.rt
.block_on(self.client.client.models().list_with(opts))
}
}