DynClientBuilder

Struct DynClientBuilder 

Source
pub struct DynClientBuilder { /* private fields */ }
Expand description

A dynamic client builder. Use this when you need to support creating any kind of client from a range of LLM providers (that Rig supports). Usage:

use rig::{
    client::builder::DynClientBuilder, completion::Prompt, providers::anthropic::CLAUDE_3_7_SONNET,
};
#[tokio::main]
async fn main() {
    let multi_client = DynClientBuilder::new();
    // set up OpenAI client
    let completion_openai = multi_client.agent("openai", "gpt-4o").unwrap();
    let agent_openai = completion_openai
        .preamble("You are a helpful assistant")
        .build();
    // set up Anthropic client
    let completion_anthropic = multi_client.agent("anthropic", CLAUDE_3_7_SONNET).unwrap();
    let agent_anthropic = completion_anthropic
        .preamble("You are a helpful assistant")
        .max_tokens(1024)
        .build();
    println!("Sending prompt: 'Hello world!'");
    let res_openai = agent_openai.prompt("Hello world!").await.unwrap();
    println!("Response from OpenAI (using gpt-4o): {res_openai}");
    let res_anthropic = agent_anthropic.prompt("Hello world!").await.unwrap();
    println!("Response from Anthropic (using Claude 3.7 Sonnet): {res_anthropic}");
}

Implementations§

Source§

impl<'a> DynClientBuilder

Source

pub fn new() -> Self

Generate a new instance of DynClientBuilder. By default, every single possible client that can be registered will be registered to the client builder.

Source

pub fn empty() -> Self

Generate a new instance of DynClientBuilder with no client factories registered.

Source

pub fn register(self, client_factory: ClientFactory) -> Self

Register a new ClientFactory

Source

pub fn register_all( self, factories: impl IntoIterator<Item = ClientFactory>, ) -> Self

Register multiple ClientFactories

Source

pub fn build( &self, provider: &str, ) -> Result<Box<dyn ProviderClient>, ClientBuildError>

Returns a (boxed) specific provider based on the given provider.

Source

pub fn build_val( &self, provider: &str, provider_value: ProviderValue, ) -> Result<Box<dyn ProviderClient>, ClientBuildError>

Returns a (boxed) specific provider based on the given provider.

Source

pub fn parse(&self, id: &'a str) -> Result<(&'a str, &'a str), ClientBuildError>

Parses a provider:model string to the provider and the model separately. For example, openai:gpt-4o will return (“openai”, “gpt-4o”).

Source

pub fn completion( &self, provider: &str, model: &str, ) -> Result<BoxCompletionModel<'a>, ClientBuildError>

Get a boxed completion model based on the provider and model.

Source

pub fn agent( &self, provider: &str, model: &str, ) -> Result<BoxAgentBuilder<'a>, ClientBuildError>

Get a boxed agent based on the provider and model..

Source

pub fn agent_with_api_key_val<P>( &self, provider: &str, model: &str, provider_value: P, ) -> Result<BoxAgentBuilder<'a>, ClientBuildError>
where P: Into<ProviderValue>,

Get a boxed agent based on the provider and model, as well as an API key.

Source

pub fn embeddings( &self, provider: &str, model: &str, ) -> Result<Box<dyn EmbeddingModelDyn + 'a>, ClientBuildError>

Get a boxed embedding model based on the provider and model.

Source

pub fn embeddings_with_api_key_val<P>( &self, provider: &str, model: &str, provider_value: P, ) -> Result<Box<dyn EmbeddingModelDyn + 'a>, ClientBuildError>
where P: Into<ProviderValue>,

Get a boxed embedding model based on the provider and model.

Source

pub fn transcription( &self, provider: &str, model: &str, ) -> Result<Box<dyn TranscriptionModelDyn + 'a>, ClientBuildError>

Get a boxed transcription model based on the provider and model.

Source

pub fn transcription_with_api_key_val<P>( &self, provider: &str, model: &str, provider_value: P, ) -> Result<Box<dyn TranscriptionModelDyn + 'a>, ClientBuildError>
where P: Into<ProviderValue>,

Get a boxed transcription model based on the provider and model.

Source

pub fn id<'id>( &'a self, id: &'id str, ) -> Result<ProviderModelId<'a, 'id>, ClientBuildError>

Get the ID of a provider model based on a provider:model ID.

Trait Implementations§

Source§

impl Default for DynClientBuilder

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<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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: 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: 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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

impl<T> ErasedDestructor for T
where T: 'static,