swiftide_agents/
lib.rs

1// show feature flags in the generated documentation
2// https://doc.rust-lang.org/rustdoc/unstable-features.html#extensions-to-the-doc-attribute
3#![cfg_attr(docsrs, feature(doc_cfg))]
4#![cfg_attr(docsrs, feature(doc_auto_cfg))]
5#![doc(html_logo_url = "https://github.com/bosun-ai/swiftide/raw/master/images/logo.png")]
6
7//! Swiftide agents are a flexible way to build fast and reliable AI agents.
8//!
9//! # Features
10//!
11//! * **Tools**: Tools can be defined as functions using the `#[tool]` attribute macro, the `Tool`
12//!   derive macro, or manually implementing the `Tool` trait.
13//! * **Hooks**: At various stages of the agent lifecycle, hooks can be defined to run custom logic.
14//!   These are defined when building the agent, and each take a closure.
15//! * **Context**: Agents operate in an `AgentContext`, which is a shared state between tools and
16//!   hooks. The context is responsible for managing the completions and interacting with the
17//!   outside world.
18//! * **Tool Execution**: A context takes a tool executor (local by default) to execute its tools
19//!   on. This enables tools to be run i.e. in containers, remote, etc.
20//! * **System prompt defaults**: `SystemPrompt` provides a default, customizable prompt for the
21//!   agent. If you want to provider your own prompt, the builder takes anything that converts into
22//!   a `Prompt`, including strings.
23//! * **Open Telemetry**: Agents are fully instrumented with open telemetry.
24//!
25//! # Example
26//!
27//! ```ignore
28//! # use swiftide_agents::Agent;
29//! # use swiftide_integrations as integrations;
30//! # async fn run() -> Result<(), Box<dyn std::error::Error>> {
31//! let openai = integrations::openai::OpenAI::builder()
32//!     .default_prompt_model("gpt-4o-mini")
33//!     .build()?;
34//!
35//! Agent::builder()
36//!     .llm(&openai)
37//!     .before_completion(move |_,_|
38//!         Box::pin(async move {
39//!                 println!("Before each tool");
40//!                 Ok(())
41//!             })
42//!     )
43//!     .build()?
44//!     .query("What is the meaning of life?")
45//!     .await?;
46//! # return Ok(());
47//!
48//! # }
49//! ```
50//!
51//! Agents run in a loop as long as they have new messages to process.
52mod agent;
53mod default_context;
54pub mod errors;
55pub mod hooks;
56mod state;
57pub mod system_prompt;
58pub mod tools;
59mod util;
60
61pub use agent::{Agent, AgentBuilder, AgentBuilderError};
62pub use default_context::DefaultContext;
63pub use state::{State, StopReason};
64
65#[cfg(any(test, debug_assertions))]
66pub mod test_utils;