|
| 1 | +use rig::{agent::AgentBuilder, completion::Prompt, loaders::FileLoader}; |
| 2 | +use rig_bedrock::{ |
| 3 | + client::{Client, ClientBuilder}, |
| 4 | + completion::AMAZON_NOVA_LITE, |
| 5 | +}; |
| 6 | +use tracing::info; |
| 7 | + |
| 8 | +mod common; |
| 9 | + |
| 10 | +/// Runs 4 agents based on AWS Bedrock (derived from the agent_with_grok example) |
| 11 | +#[tokio::main] |
| 12 | +async fn main() -> Result<(), anyhow::Error> { |
| 13 | + tracing_subscriber::fmt() |
| 14 | + .with_max_level(tracing::Level::INFO) |
| 15 | + .with_target(false) |
| 16 | + .init(); |
| 17 | + |
| 18 | + info!("Running basic agent"); |
| 19 | + basic().await?; |
| 20 | + |
| 21 | + info!("\nRunning agent with tools"); |
| 22 | + tools().await?; |
| 23 | + |
| 24 | + info!("\nRunning agent with loaders"); |
| 25 | + loaders().await?; |
| 26 | + |
| 27 | + info!("\nRunning agent with context"); |
| 28 | + context().await?; |
| 29 | + |
| 30 | + info!("\n\nAll agents ran successfully"); |
| 31 | + Ok(()) |
| 32 | +} |
| 33 | + |
| 34 | +async fn client() -> Client { |
| 35 | + ClientBuilder::new().build().await |
| 36 | +} |
| 37 | + |
| 38 | +async fn partial_agent() -> AgentBuilder<rig_bedrock::completion::CompletionModel> { |
| 39 | + let client = client().await; |
| 40 | + client.agent(AMAZON_NOVA_LITE) |
| 41 | +} |
| 42 | + |
| 43 | +/// Create an AWS Bedrock agent with a system prompt |
| 44 | +async fn basic() -> Result<(), anyhow::Error> { |
| 45 | + let agent = partial_agent() |
| 46 | + .await |
| 47 | + .preamble("Answer with json format only") |
| 48 | + .build(); |
| 49 | + |
| 50 | + let response = agent.prompt("Describe solar system").await?; |
| 51 | + info!("{}", response); |
| 52 | + |
| 53 | + Ok(()) |
| 54 | +} |
| 55 | + |
| 56 | +/// Create an AWS Bedrock with tools |
| 57 | +async fn tools() -> Result<(), anyhow::Error> { |
| 58 | + let calculator_agent = partial_agent() |
| 59 | + .await |
| 60 | + .preamble("You must only do math by using a tool.") |
| 61 | + .max_tokens(1024) |
| 62 | + .tool(common::Adder) |
| 63 | + .build(); |
| 64 | + |
| 65 | + info!( |
| 66 | + "Calculator Agent: add 400 and 20\nResult: {}", |
| 67 | + calculator_agent.prompt("add 400 and 20").await? |
| 68 | + ); |
| 69 | + |
| 70 | + Ok(()) |
| 71 | +} |
| 72 | + |
| 73 | +async fn context() -> Result<(), anyhow::Error> { |
| 74 | + let model = client().await.completion_model(AMAZON_NOVA_LITE); |
| 75 | + |
| 76 | + // Create an agent with multiple context documents |
| 77 | + let agent = AgentBuilder::new(model) |
| 78 | + .preamble("Answer the question") |
| 79 | + .context("Definition of a *flurbo*: A flurbo is a green alien that lives on cold planets") |
| 80 | + .context("Definition of a *glarb-glarb*: A glarb-glarb is a ancient tool used by the ancestors of the inhabitants of planet Jiro to farm the land.") |
| 81 | + .context("Definition of a *linglingdong*: A term used by inhabitants of the far side of the moon to describe humans.") |
| 82 | + .build(); |
| 83 | + |
| 84 | + // Prompt the agent and print the response |
| 85 | + let response = agent.prompt("What does \"glarb-glarb\" mean?").await?; |
| 86 | + |
| 87 | + info!("What does \"glarb-glarb\" mean?\n{}", response); |
| 88 | + |
| 89 | + Ok(()) |
| 90 | +} |
| 91 | + |
| 92 | +/// Based upon the `loaders` example |
| 93 | +/// |
| 94 | +/// This example loads in all the rust examples from the rig-core crate and uses them as\\ |
| 95 | +/// context for the agent |
| 96 | +async fn loaders() -> Result<(), anyhow::Error> { |
| 97 | + let model = client().await.completion_model(AMAZON_NOVA_LITE); |
| 98 | + |
| 99 | + // Load in all the rust examples |
| 100 | + let examples = FileLoader::with_glob("rig-core/examples/*.rs")? |
| 101 | + .read_with_path() |
| 102 | + .ignore_errors() |
| 103 | + .into_iter(); |
| 104 | + |
| 105 | + // Create an agent with multiple context documents |
| 106 | + let agent = examples |
| 107 | + .fold(AgentBuilder::new(model), |builder, (path, content)| { |
| 108 | + builder.context(format!("Rust Example {:?}:\n{}", path, content).as_str()) |
| 109 | + }) |
| 110 | + .preamble("Answer the question") |
| 111 | + .build(); |
| 112 | + |
| 113 | + // Prompt the agent and print the response |
| 114 | + let response = agent |
| 115 | + .prompt("Which rust example is best suited for the operation 1 + 2") |
| 116 | + .await?; |
| 117 | + |
| 118 | + info!("{}", response); |
| 119 | + |
| 120 | + Ok(()) |
| 121 | +} |
0 commit comments