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
impl<'a> DynClientBuilder
Sourcepub fn new() -> Self
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.
Sourcepub fn empty() -> Self
pub fn empty() -> Self
Generate a new instance of DynClientBuilder
with no client factories registered.
Sourcepub fn register(self, client_factory: ClientFactory) -> Self
pub fn register(self, client_factory: ClientFactory) -> Self
Register a new ClientFactory
Sourcepub fn register_all(
self,
factories: impl IntoIterator<Item = ClientFactory>,
) -> Self
pub fn register_all( self, factories: impl IntoIterator<Item = ClientFactory>, ) -> Self
Register multiple ClientFactories
Sourcepub fn build(
&self,
provider: &str,
) -> Result<Box<dyn ProviderClient>, ClientBuildError>
pub fn build( &self, provider: &str, ) -> Result<Box<dyn ProviderClient>, ClientBuildError>
Returns a (boxed) specific provider based on the given provider.
Sourcepub fn build_val(
&self,
provider: &str,
provider_value: ProviderValue,
) -> Result<Box<dyn ProviderClient>, ClientBuildError>
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.
Sourcepub fn parse(&self, id: &'a str) -> Result<(&'a str, &'a str), ClientBuildError>
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”).
Sourcepub fn completion(
&self,
provider: &str,
model: &str,
) -> Result<BoxCompletionModel<'a>, ClientBuildError>
pub fn completion( &self, provider: &str, model: &str, ) -> Result<BoxCompletionModel<'a>, ClientBuildError>
Get a boxed completion model based on the provider and model.
Sourcepub fn agent(
&self,
provider: &str,
model: &str,
) -> Result<BoxAgentBuilder<'a>, ClientBuildError>
pub fn agent( &self, provider: &str, model: &str, ) -> Result<BoxAgentBuilder<'a>, ClientBuildError>
Get a boxed agent based on the provider and model..
Sourcepub fn agent_with_api_key_val<P>(
&self,
provider: &str,
model: &str,
provider_value: P,
) -> Result<BoxAgentBuilder<'a>, ClientBuildError>where
P: Into<ProviderValue>,
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.
Sourcepub fn embeddings(
&self,
provider: &str,
model: &str,
) -> Result<Box<dyn EmbeddingModelDyn + 'a>, ClientBuildError>
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.
Sourcepub 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>,
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.
Sourcepub fn transcription(
&self,
provider: &str,
model: &str,
) -> Result<Box<dyn TranscriptionModelDyn + 'a>, ClientBuildError>
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.
Sourcepub 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>,
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.
Sourcepub fn id<'id>(
&'a self,
id: &'id str,
) -> Result<ProviderModelId<'a, 'id>, ClientBuildError>
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.