forked from 0xPlaygrounds/rig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_autonomous.rs
More file actions
46 lines (36 loc) · 1.33 KB
/
agent_autonomous.rs
File metadata and controls
46 lines (36 loc) · 1.33 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
use rig::providers::openai::Client;
use schemars::JsonSchema;
use std::env;
#[derive(Debug, serde::Deserialize, JsonSchema, serde::Serialize)]
struct Counter {
/// The score of the document
number: u32,
}
#[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 agent = openai_client.extractor::<Counter>("gpt-4")
.preamble("
Your role is to add a random number between 1 and 64 (using only integers) to the previous number.
")
.build();
let mut number: u32 = 0;
let mut interval = tokio::time::interval(std::time::Duration::from_secs(1));
// Loop the agent and allow it to run autonomously. If it hits the target number (2000 or above)
// we then terminate the loop and return the number
// Note that the tokio interval is to avoid being rate limited
loop {
// Prompt the agent and print the response
let response = agent.extract(&number.to_string()).await.unwrap();
if response.number >= 2000 {
break;
} else {
number += response.number
}
interval.tick().await;
}
println!("Finished with number: {number:?}");
Ok(())
}