-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserdesai-basic.rs
More file actions
97 lines (85 loc) · 3.89 KB
/
serdesai-basic.rs
File metadata and controls
97 lines (85 loc) · 3.89 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//! Basic tools example - demonstrates tool setup with serdesAI.
//!
//! Shows:
//! - Creating tools individually
//! - Using [`SystemPromptBuilder`] for context generation
//! - Using [`AgentBuilderExt`] to add tools to an agent
//! - Running the agent with tools
//!
//! Run: cargo run --example serdesai-basic -p reloaded-code-serdesai
//!
//! Please note; Sandboxing is not enabled here, the agents are not restricted.
//! See `serdesai-sandbox` example for a more 'sandboxed' approach.
use futures::StreamExt;
use reloaded_code_serdesai::agent_ext::AgentBuilderExt;
use reloaded_code_serdesai::{AbsolutePathResolver, GlobTool, GrepTool, ReadTool};
use reloaded_code_serdesai::{BashTool, SystemPromptBuilder, WebFetchTool, create_todo_tools};
use serdes_ai::prelude::*;
use serdes_ai_models::OpenAIChatModel;
use std::fmt::Write;
// Set your OpenAI API key here or via OPENAI_API_KEY environment variable.
/// Fallback API key if env var is not set. Leave empty to require env var.
const OPENAI_API_KEY: &str = "";
const OPENAI_MODEL: &str = "hf:zai-org/GLM-4.7-Flash";
const OPENAI_BASE_URL: &str = "https://api.synthetic.new/openai/v1";
fn get_openai_api_key() -> String {
std::env::var("OPENAI_API_KEY").unwrap_or_else(|_| OPENAI_API_KEY.to_string())
}
#[tokio::main]
async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
// === Create system prompt builder to track tools ===
let mut pb = SystemPromptBuilder::new()
.working_directory(std::env::current_dir()?.display().to_string());
// === Create todo tools with shared state ===
let (todo_read, todo_write, _state) = create_todo_tools();
// === Build agent with tools - call .system_prompt() last ===
let model =
OpenAIChatModel::new(OPENAI_MODEL, get_openai_api_key()).with_base_url(OPENAI_BASE_URL);
let agent = AgentBuilder::<(), String>::new(model)
.instructions("Use tools to answer; call at least one tool before responding.")
// File operations
.tool(pb.track(ReadTool::new(AbsolutePathResolver)))
.tool(pb.track(GlobTool::new(AbsolutePathResolver)))
.tool(pb.track(GrepTool::new(AbsolutePathResolver)))
// Shell execution
.tool(pb.track(BashTool::host()))
// Web content fetching
.tool(pb.track(WebFetchTool::new()))
// Todo tools with shared state
.tool(pb.track(todo_read))
.tool(pb.track(todo_write))
// System prompt last (after tracking all tools)
.system_prompt(pb.build())
.build();
// === Print tool info ===
println!("=== Agent Ready ({} tools) ===", agent.tools().len());
// === Run the agent ===
println!("\n=== Running Agent ===");
let prompt = "List the Rust files in the current directory using glob";
let mut stream = agent.run_stream(prompt, ()).await?;
fn log_xml(request_id: u32, tag: &str, content: &str) {
let mut line = String::with_capacity(content.len() + tag.len() * 2 + 18);
let _ = write!(line, "<{request_id}:{tag}>{content}</{tag}>");
println!("{line}");
}
let mut request_id = 0u32;
log_xml(request_id, "user", prompt);
request_id = request_id.saturating_add(1);
let mut assistant_message = String::with_capacity(256);
while let Some(event) = stream.next().await {
match event? {
AgentStreamEvent::TextDelta { text, .. } => assistant_message.push_str(&text),
AgentStreamEvent::RequestStart { .. } => assistant_message.clear(),
AgentStreamEvent::ToolCallStart { tool_name, .. } => {
log_xml(request_id, "tool", &tool_name);
request_id = request_id.saturating_add(1);
}
AgentStreamEvent::ResponseComplete { .. } => {
log_xml(request_id, "assistant", &assistant_message);
request_id = request_id.saturating_add(1);
}
_ => {}
}
}
Ok(())
}