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 DynClientBuilder
impl DynClientBuilder
pub fn image_generation<'a>( &self, provider: &str, model: &str, ) -> Result<BoxImageGenerationModel<'a>, ClientBuildError>
image
only.Source§impl DynClientBuilder
impl DynClientBuilder
pub fn audio_generation<'a>( &self, provider: &str, model: &str, ) -> Result<BoxAudioGenerationModel<'a>, ClientBuildError>
audio
only.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.
Sourcepub async fn stream_completion(
&self,
provider: &str,
model: &str,
request: CompletionRequest,
) -> Result<StreamingCompletionResponse<()>, ClientBuildError>
pub async fn stream_completion( &self, provider: &str, model: &str, request: CompletionRequest, ) -> Result<StreamingCompletionResponse<()>, ClientBuildError>
Stream a completion request to the specified provider and model.
§Arguments
provider
- The name of the provider (e.g., “openai”, “anthropic”)model
- The name of the model (e.g., “gpt-4o”, “claude-3-sonnet”)request
- The completion request containing prompt, parameters, etc.
§Returns
A future that resolves to a streaming completion response
Sourcepub async fn stream_prompt(
&self,
provider: &str,
model: &str,
prompt: impl Into<Message> + Send,
) -> Result<StreamingCompletionResponse<()>, ClientBuildError>
pub async fn stream_prompt( &self, provider: &str, model: &str, prompt: impl Into<Message> + Send, ) -> Result<StreamingCompletionResponse<()>, ClientBuildError>
Stream a simple prompt to the specified provider and model.
§Arguments
provider
- The name of the provider (e.g., “openai”, “anthropic”)model
- The name of the model (e.g., “gpt-4o”, “claude-3-sonnet”)prompt
- The prompt to send to the model
§Returns
A future that resolves to a streaming completion response
Sourcepub async fn stream_chat(
&self,
provider: &str,
model: &str,
prompt: impl Into<Message> + Send,
chat_history: Vec<Message>,
) -> Result<StreamingCompletionResponse<()>, ClientBuildError>
pub async fn stream_chat( &self, provider: &str, model: &str, prompt: impl Into<Message> + Send, chat_history: Vec<Message>, ) -> Result<StreamingCompletionResponse<()>, ClientBuildError>
Stream a chat with history to the specified provider and model.
§Arguments
provider
- The name of the provider (e.g., “openai”, “anthropic”)model
- The name of the model (e.g., “gpt-4o”, “claude-3-sonnet”)prompt
- The new prompt to send to the modelchat_history
- The chat history to include with the request
§Returns
A future that resolves to a streaming completion response
Trait Implementations§
Auto Trait Implementations§
impl Freeze for DynClientBuilder
impl RefUnwindSafe for DynClientBuilder
impl !Send for DynClientBuilder
impl !Sync for DynClientBuilder
impl Unpin for DynClientBuilder
impl UnwindSafe for DynClientBuilder
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
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> 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