forked from 0xPlaygrounds/rig
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenum_dispatch.rs
More file actions
111 lines (92 loc) 路 2.94 KB
/
Copy pathenum_dispatch.rs
File metadata and controls
111 lines (92 loc) 路 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use std::collections::HashMap;
use anyhow::{Result, anyhow};
use rig::agent::Agent;
use rig::client::{CompletionClient, ProviderClient};
use rig::completion::{Prompt, PromptError};
use rig::providers::anthropic::completion::CLAUDE_SONNET_4_6;
use rig::providers::openai::GPT_4O;
use rig::providers::{anthropic, openai};
enum Agents {
Anthropic(Agent<anthropic::completion::CompletionModel>),
OpenAI(Agent<openai::completion::CompletionModel>),
}
impl Agents {
async fn prompt(&self, prompt: &str) -> Result<String, PromptError> {
match self {
Self::Anthropic(agent) => agent.prompt(prompt).await,
Self::OpenAI(agent) => agent.prompt(prompt).await,
}
}
}
struct AgentConfig<'a> {
name: &'a str,
preamble: &'a str,
}
// In production you would likely want to create some sort of `RegistryKey` type instead of
// allowing arbitrary strings, for improved type safety
struct ProviderRegistry(HashMap<&'static str, fn(AgentConfig<'_>) -> Result<Agents>>);
fn anthropic_agent(AgentConfig { name, preamble }: AgentConfig<'_>) -> Result<Agents> {
let agent = anthropic::Client::from_env()?
.agent(CLAUDE_SONNET_4_6)
.name(name)
.preamble(preamble)
.build();
Ok(Agents::Anthropic(agent))
}
fn openai_agent(AgentConfig { name, preamble }: AgentConfig<'_>) -> Result<Agents> {
let agent = openai::Client::from_env()?
.completions_api()
.agent(GPT_4O)
.name(name)
.preamble(preamble)
.build();
Ok(Agents::OpenAI(agent))
}
impl ProviderRegistry {
pub fn new() -> Self {
Self(HashMap::from_iter([
(
"anthropic",
anthropic_agent as fn(AgentConfig<'_>) -> Result<Agents>,
),
(
"openai",
openai_agent as fn(AgentConfig<'_>) -> Result<Agents>,
),
]))
}
pub fn agent(&self, provider: &str, agent_config: AgentConfig<'_>) -> Result<Agents> {
let builder = self
.0
.get(provider)
.ok_or_else(|| anyhow!("unknown provider: {provider}"))?;
builder(agent_config)
}
}
#[tokio::main]
async fn main() -> Result<()> {
let registry = ProviderRegistry::new();
let openai_agent = registry.agent(
"openai",
AgentConfig {
name: "Assistant",
preamble: "You are a helpful assistant",
},
)?;
let anthropic_agent = registry.agent(
"anthropic",
AgentConfig {
name: "Assistant",
preamble: "You are an unhelpful assistant",
},
)?;
let oai_response = openai_agent
.prompt("How much does 4oz of parmesan cheese weigh")
.await?;
println!("Helpful: {oai_response}");
let anthropic_response = anthropic_agent
.prompt("How much does 4oz of parmesan cheese weigh")
.await?;
println!("Unhelpful: {anthropic_response}");
Ok(())
}