-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathagent_prompt_chaining.rs
40 lines (32 loc) · 1.22 KB
/
agent_prompt_chaining.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
use std::env;
use rig::{
pipeline::{self, Op},
providers::openai::Client,
};
#[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 rng_agent = openai_client.agent("gpt-4")
.preamble("
You are a random number generator designed to only either output a single whole integer that is 0 or 1. Only return the number.
")
.build();
let adder_agent = openai_client.agent("gpt-4")
.preamble("
You are a mathematician who adds 1000 to every number passed into the context, except if the number is 0 - in which case don't add anything. Only return the number.
")
.build();
let chain = pipeline::new()
// Generate a whole number that is either 0 and 1
.prompt(rng_agent)
.map(|x| x.unwrap())
.prompt(adder_agent);
// Prompt the agent and print the response
let response = chain
.call("Please generate a single whole integer that is 0 or 1".to_string())
.await;
println!("Pipeline result: {response:?}");
Ok(())
}