Examples
Introduction
This project showcases a Rust-based framework designed for interacting with multiple AI agents. The framework provides an extensible and modular architecture, allowing users to instantiate, interact with, and manage different types of AI-driven agents with ease.
Features
Multi-Agent Management
- Supports multiple agents, each with distinct personalities and capabilities.
- Agents can be dynamically added, removed, and modified.
Versatile Agent Roles
Agents can function in different modes:
- Chat Mode: Standard AI conversation.
- Coder Mode: AI optimized for programming-related interactions.
- Twitter Mode: AI tailored for social media interactions.
Agents can switch between these modes dynamically, allowing for flexible usage.
Import & Export Functionality
- Agents can be saved and loaded from files, allowing users to persist and restore configurations.
- Supports easy integration with external data sources.
Demonstration
Running the Showcase
Startup
- Running the program initializes a list of AI agents and prints the system logo.
Agent Interaction
- Users can chat with an agent by specifying its index.
- Agents can switch between different roles dynamically.
Data Persistence
- Agents can be exported for future use.
- Previously saved agents can be imported and used immediately.
Example Implementations
Coding Agent - Simple Addition
// Example showing the coding agent building a simple Rust program that adds two numbers together
use agents::gpt4free::GPT4FreeAgent;
use agents::agent_trait::AgentTrait;
#[tokio::main]
async fn main() {
let mut agent = GPT4FreeAgent::new("builder");
agent.convert_to_coder();
let command = "!build:adder.rs build me a rust program that will add together two user inputs and then print the result";
match agent.send_message(command).await {
Ok(reply) => println!("GPT: {}", reply),
Err(err) => eprintln!("Error: {}", err),
}
}
Coding Agent - Hello World
// Example showing the coding agent building a simple Rust program that prints "Hello, World!" to the console
use agents::gpt4free::GPT4FreeAgent;
use agents::agent_trait::AgentTrait;
#[tokio::main]
async fn main() {
let mut agent = GPT4FreeAgent::new("builder");
agent.convert_to_coder();
let command = "!build:hello_world.rs build me a hello world program in rust";
match agent.send_message(command).await {
Ok(reply) => println!("GPT: {}", reply),
Err(err) => eprintln!("Error: {}", err),
}
}
Import & Export Agents
// Example showing how to export and import an agent with verifications
use agents::gpt4free::GPT4FreeAgent;
use std::error::Error;
use agents::agent_trait::AgentTrait;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let mut agent = GPT4FreeAgent::new("dummy");
agent.convert_to_coder();
agent.set_temperature(0.7);
agent.set_max_tokens(1000);
agent.add_system_msg("You are a Rust expert.");
let export_path = "export/dummy.agent";
agent.export_to_file(export_path)?;
println!("Agent exported to {}", export_path);
let imported_agent = GPT4FreeAgent::import_from_file(export_path)?;
println!("Agent imported from {}", export_path);
assert_eq!(agent.get_model(), imported_agent.get_model());
assert_eq!(agent.get_provider(), imported_agent.get_provider());
assert_eq!(agent.get_temperature(), imported_agent.get_temperature());
assert_eq!(agent.get_max_tokens(), imported_agent.get_max_tokens());
assert_eq!(agent.get_system_messages(), imported_agent.get_system_messages());
assert_eq!(agent.is_coder_agent(), imported_agent.is_coder_agent());
println!("Exported and imported successfully!");
Ok(())
}
Posting a Tweet
// This example shows how a user can post a tweet
use utils::post_twitter::post_tweet;
#[tokio::main]
async fn main() {
let consumer_key = "your_api_key";
let consumer_secret = "your_api_secret";
let access_token = "your_access_token";
let access_token_secret = "access_token_secret";
let tweet_text = "allowing agents to post directly on X";
match post_tweet(consumer_key, consumer_secret, access_token, access_token_secret, tweet_text).await {
Ok(()) => println!("Successfully posted tweet!"),
Err(e) => eprintln!("Failed to post tweet: {}", e),
}
}
Conclusion
This showcase highlights the capabilities of this Rust framework for AI agent interaction. By providing a structured and extensible approach to managing intelligent agents, it opens the door for numerous real-world applications, from automation to AI-powered assistance.