use crate::ai::model::{Api, Context, Model};
use crate::ai::provider::{
CompleteOptions, Completion, Error, EventStream, Provider, ProviderRegistry,
};
#[derive(Clone)]
pub struct Client {
registry: ProviderRegistry,
http: reqwest::Client,
}
impl Default for Client {
fn default() -> Self {
Client::new()
}
}
impl Client {
pub fn new() -> Self {
Client {
registry: ProviderRegistry::with_builtins(),
http: reqwest::Client::new(),
}
}
pub fn with_registry(registry: ProviderRegistry) -> Self {
Client {
registry,
http: reqwest::Client::new(),
}
}
pub fn registry_mut(&mut self) -> &mut ProviderRegistry {
&mut self.registry
}
fn provider_for(&self, api: Api) -> Result<std::sync::Arc<dyn Provider>, Error> {
self.registry
.get(api)
.ok_or_else(|| Error::Config(format!("no provider registered for api: {api:?}")))
}
pub async fn complete(
&self,
model: &Model,
ctx: &Context,
opts: &CompleteOptions,
) -> Result<Completion, Error> {
self.provider_for(model.api)?
.complete(&self.http, model, ctx, opts)
.await
}
pub async fn stream(
&self,
model: &Model,
ctx: &Context,
opts: &CompleteOptions,
) -> Result<EventStream, Error> {
self.provider_for(model.api)?
.stream(&self.http, model, ctx, opts)
.await
}
pub async fn list_models(&self, provider: &str) -> Result<Vec<Model>, Error> {
match provider {
"lmstudio" => crate::ai::providers::openai::list_models(&self.http).await,
other => Err(Error::Config(format!(
"model discovery is only available for openai-compatible providers like lmstudio, not {other}"
))),
}
}
}