forked from 0xPlaygrounds/rig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_parallelization.rs
More file actions
79 lines (69 loc) · 2.17 KB
/
agent_parallelization.rs
File metadata and controls
79 lines (69 loc) · 2.17 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
use std::env;
use rig::pipeline::agent_ops::extract;
use rig::{
parallel,
pipeline::{self, passthrough, Op},
providers::openai::Client,
};
use schemars::JsonSchema;
#[derive(serde::Deserialize, JsonSchema, serde::Serialize)]
struct DocumentScore {
/// The score of the document
score: f32,
}
#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
// Create OpenAI client
let openai_api_key = env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY not set");
let openai_client = Client::new(&openai_api_key);
let manipulation_agent = openai_client
.extractor::<DocumentScore>("gpt-4")
.preamble(
"
Your role is to score a user's statement on how manipulative it sounds between 0 and 1.
",
)
.build();
let depression_agent = openai_client
.extractor::<DocumentScore>("gpt-4")
.preamble(
"
Your role is to score a user's statement on how depressive it sounds between 0 and 1.
",
)
.build();
let intelligent_agent = openai_client
.extractor::<DocumentScore>("gpt-4")
.preamble(
"
Your role is to score a user's statement on how intelligent it sounds between 0 and 1.
",
)
.build();
let chain = pipeline::new()
.chain(parallel!(
passthrough(),
extract(manipulation_agent),
extract(depression_agent),
extract(intelligent_agent)
))
.map(|(statement, manip_score, dep_score, int_score)| {
format!(
"
Original statement: {statement}
Manipulation sentiment score: {}
Depression sentiment score: {}
Intelligence sentiment score: {}
",
manip_score.unwrap().score,
dep_score.unwrap().score,
int_score.unwrap().score
)
});
// Prompt the agent and print the response
let response = chain
.call("I hate swimming. The water always gets in my eyes.")
.await;
println!("Pipeline run: {response:?}");
Ok(())
}