pub struct Connector { /* private fields */ }Expand description
The models an application can use and how each is reached: a catalog,
rig’s built-in one by default, whose models connect through the
provider registered for their vendor (Self::provide), else with the
environment’s keys, else through the first SignIn that serves them,
each on an HTTP client wrapped by its Transport. Cloning is cheap.
It is Send and Sync natively; on browser wasm its providers,
sign-ins and wrappers may hold JavaScript values instead.
Implementations§
Source§impl Connector
impl Connector
Sourcepub fn new(catalog: Catalog) -> Self
pub fn new(catalog: Catalog) -> Self
A connector for the models of catalog, such as the built-in one
with a user’s overrides laid over it.
Sourcepub fn wrap_http(
&mut self,
wrap: impl Fn(DynHttpClient) -> DynHttpClient + WasmCompatSend + WasmCompatSync + 'static,
)
pub fn wrap_http( &mut self, wrap: impl Fn(DynHttpClient) -> DynHttpClient + WasmCompatSend + WasmCompatSync + 'static, )
Wraps the HTTP client every model of this connector is reached through, outside the wrappers added before. Connections made before the call are not rebuilt.
use rig_core::catalog::Connector;
use rig_core::http_client::DynHttpClient;
let mut connector = Connector::default();
// A wrapper sees every request and response, such as to log them.
connector.wrap_http(|inner: DynHttpClient| inner);Sourcepub fn transport(&self) -> &Transport
pub fn transport(&self) -> &Transport
The transport this connector’s models are reached through, for a host that builds a model of its own beside them.
Sourcepub fn add_sign_in(&mut self, sign_in: impl SignIn)
pub fn add_sign_in(&mut self, sign_in: impl SignIn)
Falls back to sign_in for a model the environment has no key for
and no sign-in added earlier serves.
Sourcepub fn provide(&mut self, vendor: ProviderId, provider: impl Provide)
pub fn provide(&mut self, vendor: ProviderId, provider: impl Provide)
Reaches every model of vendor (any of its protocol families)
through provider, before the environment. A model the provider
has no key for (ConnectError::MissingKey) falls back to the
sign-ins, as one the environment has no key for does. A later
provider for the same vendor replaces this one: to wrap it instead,
take it first with Self::provider.
use rig_core::catalog::{Catalog, Connector, ModelSpec};
use rig_core::providers::registry::{ConnectOptions, ProviderId};
let corp = ProviderId::declare("corp")?;
let mut connector = Connector::default();
connector.catalog_mut().insert(ModelSpec::new(corp, "big").with_tools(true));
// A built-in vendor at another address.
let ollama = ProviderId::catalog("ollama").ok_or("known")?;
connector.provide(ollama, ConnectOptions::new().base_url("http://gpu:11434/v1"));
assert!(connector.is_provided(ollama) && !connector.is_provided(corp));Sourcepub fn is_provided(&self, vendor: ProviderId) -> bool
pub fn is_provided(&self, vendor: ProviderId) -> bool
Whether a provider is registered for vendor’s models.
Sourcepub fn provider(&self, vendor: ProviderId) -> Option<Arc<dyn Provide>>
pub fn provider(&self, vendor: ProviderId) -> Option<Arc<dyn Provide>>
The provider registered for vendor’s models, if any: what a
provider that wraps it, such as one that changes the key, delegates
to.
Sourcepub fn catalog_mut(&mut self) -> &mut Catalog
pub fn catalog_mut(&mut self) -> &mut Catalog
The catalog, to add or change models in place, keeping the providers and sign-ins.
Sourcepub fn connect(
&self,
reference: &str,
) -> Result<(Arc<ModelSpec>, ErasedHandler), ConnectError>
pub fn connect( &self, reference: &str, ) -> Result<(Arc<ModelSpec>, ErasedHandler), ConnectError>
The catalog model reference names, as Catalog::resolve finds
it, and that model as an effect handler: from the provider
registered for its vendor, else its provider’s client built from the
environment, on the shared HTTP client of the reqwest feature, or,
without a key in either, the handler of a sign-in that serves it. The
HTTP client is wrapped by Self::wrap_http’s wrappers.
§Errors
ConnectError::NotFound for a model the catalog does not list,
what the registered provider reports, else what
Catalog::connect_with reports, when no sign-in serves a model
the provider or the environment has no key for.
Sourcepub fn plan(&self, spec: &ModelSpec) -> Option<&'static str>
pub fn plan(&self, spec: &ModelSpec) -> Option<&'static str>
The plan of the first sign-in that would serve spec
(SignIn::plan).
Sourcepub fn status(&self, spec: &ModelSpec) -> ModelStatus
pub fn status(&self, spec: &ModelSpec) -> ModelStatus
Whether spec can serve an agent now, and why not: it must call
tools and connect as Self::connect would connect it.
use rig_core::catalog::{Connector, ModelSpec, ModelStatus};
use rig_core::providers::registry::ProviderId;
let corp = ProviderId::declare("status-doc-corp")?;
let connector = Connector::default();
let chat = ModelSpec::new(corp, "chat");
assert_eq!(connector.status(&chat), ModelStatus::NoTools);
let agent = chat.with_tools(true);
assert!(matches!(connector.status(&agent), ModelStatus::Unavailable(_)));Sourcepub fn reachable(&self) -> Vec<&ModelSpec>
pub fn reachable(&self) -> Vec<&ModelSpec>
Catalog models that call tools and can be reached through a registered provider, the environment or a sign-in: first those of providers registered, with a key set or signed in, then those of providers that need none (local servers such as Ollama), which a picker can mark “no key needed”. The check builds the handler exactly as a request would, once per provider.
Sourcepub fn statuses(&self) -> Vec<(&ModelSpec, ModelStatus)>
pub fn statuses(&self) -> Vec<(&ModelSpec, ModelStatus)>
Every catalog model with its ModelStatus: first the
reachable ones in that order, then the others in
catalog order. Each provider is checked once.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Connector
impl !UnwindSafe for Connector
impl Freeze for Connector
impl Send for Connector
impl Sync for Connector
impl Unpin for Connector
impl UnsafeUnpin for Connector
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more