forked from 0xPlaygrounds/rig
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebate.rs
More file actions
96 lines (91 loc) 路 3.37 KB
/
Copy pathdebate.rs
File metadata and controls
96 lines (91 loc) 路 3.37 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
use anyhow::Result;
use rig::prelude::*;
use rig::{
agent::Agent,
completion::Prompt,
message::Message,
providers::{cohere, openai},
};
struct Debater {
gpt_4: Agent<openai::responses_api::ResponsesCompletionModel>,
coral: Agent<cohere::CompletionModel>,
}
impl Debater {
fn new(position_a: &str, position_b: &str) -> Result<Self> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.with_target(false)
.init();
let openai_client = openai::Client::from_env()?;
let cohere_client = cohere::Client::from_env()?;
Ok(Self {
gpt_4: openai_client
.agent(openai::GPT_4)
.preamble(position_a)
.build(),
coral: cohere_client
.agent(cohere::COMMAND_R)
.preamble(position_b)
.build(),
})
}
async fn rounds(&self, n: usize) -> Result<()> {
let mut history_a: Vec<Message> = vec![];
let mut history_b: Vec<Message> = vec![];
let mut last_resp_b: Option<String> = None;
for _ in 0..n {
let prompt_a = if let Some(msg_b) = &last_resp_b {
msg_b.clone()
} else {
"Plead your case!".into()
};
let resp_a = self
.gpt_4
.prompt(prompt_a.as_str())
.with_history(&history_a)
.extended_details()
.await?;
// Extract updated history for next iteration
history_a = resp_a
.messages
.map(|m| m.into_iter().collect())
.unwrap_or_default();
println!("GPT-4:\n{}", resp_a.output);
println!("================================================================");
let resp_b = self
.coral
.prompt(resp_a.output.as_str())
.with_history(&history_b)
.extended_details()
.await?;
// Extract updated history for next iteration
history_b = resp_b
.messages
.map(|m| m.into_iter().collect())
.unwrap_or_default();
println!("Coral:\n{}", resp_b.output);
println!("================================================================");
last_resp_b = Some(resp_b.output)
}
Ok(())
}
}
#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
// Create model
let debator = Debater::new(
"You believe that religion is a useful concept. \
This could be for security, financial, ethical, philosophical, metaphysical, religious or any kind of other reason. \
You choose what your arguments are. \
I will argue against you and you must rebuke me and try to convince me that I am wrong. \
Make your statements short and concise.",
"You believe that religion is a harmful concept. \
This could be for security, financial, ethical, philosophical, metaphysical, religious or any kind of other reason. \
You choose what your arguments are. \
I will argue against you and you must rebuke me and try to convince me that I am wrong. \
Make your statements short and concise.",
)?;
// Run the debate for 4 rounds
debator.rounds(4).await?;
Ok(())
}