Skip to main content

Connector

Struct Connector 

Source
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

Source

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.

Source

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);
Source

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.

Source

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.

Source

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));
Source

pub fn is_provided(&self, vendor: ProviderId) -> bool

Whether a provider is registered for vendor’s models.

Source

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.

Source

pub fn catalog(&self) -> &Catalog

The catalog.

Source

pub fn catalog_mut(&mut self) -> &mut Catalog

The catalog, to add or change models in place, keeping the providers and sign-ins.

Source

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.

Source

pub fn plan(&self, spec: &ModelSpec) -> Option<&'static str>

The plan of the first sign-in that would serve spec (SignIn::plan).

Source

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(_)));
Source

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.

Source

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§

Source§

impl Clone for Connector

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 Default for Connector

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

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

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WasmCompatSend for T
where T: Send,

Source§

impl<T> WasmCompatSync for T
where T: Sync,

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