forked from 0xPlaygrounds/rig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debate.rs
100 lines (85 loc) · 3.17 KB
/
debate.rs
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
use std::env;
use anyhow::Result;
use rig::{
agent::Agent,
completion::{Chat, Message},
providers::{cohere, openai},
};
struct Debater {
gpt_4: Agent<openai::CompletionModel>,
coral: Agent<cohere::CompletionModel>,
}
impl Debater {
fn new(position_a: &str, position_b: &str) -> Self {
let openai_client =
openai::Client::new(&env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY not set"));
let cohere_client =
cohere::Client::new(&env::var("COHERE_API_KEY").expect("COHERE_API_KEY not set"));
Self {
gpt_4: openai_client.agent("gpt-4").preamble(position_a).build(),
coral: cohere_client
.agent("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.chat(&prompt_a, history_a.clone()).await?;
println!("GPT-4:\n{}", resp_a);
history_a.push(Message {
role: "user".into(),
content: prompt_a.clone(),
});
history_a.push(Message {
role: "assistant".into(),
content: resp_a.clone(),
});
println!("================================================================");
let resp_b = self.coral.chat(&resp_a, history_b.clone()).await?;
println!("Coral:\n{}", resp_b);
println!("================================================================");
history_b.push(Message {
role: "user".into(),
content: resp_a.clone(),
});
history_b.push(Message {
role: "assistant".into(),
content: resp_b.clone(),
});
last_resp_b = Some(resp_b)
}
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(())
}