Expand description
Agent implementation for serdes-ai.
The agent is the core abstraction for building AI applications. It provides:
- Model orchestration
- Tool registration and execution
- Structured output parsing
- Retry logic and error handling
- Usage tracking and limits
§Example
ⓘ
use serdes_ai_agent::{agent, EndStrategy};
use serdes_ai_models::openai::OpenAIChatModel;
// Create a simple agent
let model = OpenAIChatModel::new("gpt-4o", "sk-...");
let agent = agent(model)
.system_prompt("You are a helpful assistant.")
.temperature(0.7)
.build();
// Run the agent
let result = agent.run("Hello!", ()).await?;
println!("Response: {}", result.output());§With Tools
ⓘ
use serdes_ai_agent::agent;
use serdes_ai_tools::ToolReturn;
let agent = agent(model)
.system_prompt("You can search the web.")
.tool_fn(
"search",
"Search the web for information",
serde_json::json!({
"type": "object",
"properties": {
"query": {"type": "string"}
},
"required": ["query"]
}),
|ctx, args: serde_json::Value| {
let query = args["query"].as_str().unwrap();
Ok(ToolReturn::text(format!("Results for: {}", query)))
},
)
.build();§Structured Output
ⓘ
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct Analysis {
sentiment: String,
score: f64,
}
let agent = agent(model)
.output_type::<Analysis>()
.build();
let result = agent.run("Analyze: I love Rust!", ()).await?;
println!("Sentiment: {} ({})", result.output.sentiment, result.output.score);Re-exports§
pub use agent::Agent;pub use agent::EndStrategy;pub use agent::InstrumentationSettings;pub use agent::RegisteredTool;pub use agent::ToolExecutor;pub use builder::agent;pub use builder::agent_with_deps;pub use builder::AgentBuilder;pub use builder::ModelConfig;pub use context::generate_run_id;pub use context::RunContext;pub use context::RunUsage;pub use context::UsageLimits;pub use errors::AgentBuildError;pub use errors::AgentRunError;pub use errors::OutputParseError;pub use errors::OutputValidationError;pub use errors::UsageLimitError;pub use history::ChainedProcessor;pub use history::FilterHistory;pub use history::FnProcessor;pub use history::HistoryProcessor;pub use history::SummarizeHistory;pub use history::TruncateByTokens;pub use history::TruncateHistory;pub use instructions::AsyncInstructionFn;pub use instructions::AsyncSystemPromptFn;pub use instructions::DateTimeInstruction;pub use instructions::InstructionBuilder;pub use instructions::InstructionFn;pub use instructions::StaticInstruction;pub use instructions::StaticSystemPrompt;pub use instructions::SyncInstructionFn;pub use instructions::SyncSystemPromptFn;pub use instructions::SystemPromptFn;pub use output::AsyncValidator;pub use output::ChainedValidator;pub use output::DefaultOutputSchema;pub use output::JsonOutputSchema;pub use output::LengthValidator;pub use output::NonEmptyValidator;pub use output::OutputMode;pub use output::OutputSchema;pub use output::OutputValidator;pub use output::SyncValidator;pub use output::TextOutputSchema;pub use output::ToolOutputSchema;pub use run::AgentRun;pub use run::AgentRunResult;pub use run::CompressionStrategy;pub use run::ContextCompression;pub use run::RunOptions;pub use run::StepResult;pub use stream::AgentStream;pub use stream::AgentStreamEvent;
Modules§
- agent
- Main Agent type.
- builder
- Agent builder pattern.
- context
- Run context and state management.
- errors
- Agent-specific error types.
- history
- Message history processing.
- instructions
- System prompts and dynamic instruction generation.
- output
- Output validation and parsing.
- prelude
- Prelude for common imports.
- run
- Agent run execution.
- stream
- Streaming agent execution.
Structs§
- Cancellation
Token - A token which can be used to signal a cancellation request to one or more tasks.