Examples
This section provides practical examples to help you understand how to use Warden and its features effectively. From running agents locally to exporting and importing agents, these examples cover a wide range of use cases.
Example 1: Running GPT-4-Free Locally
Step 1: Start the GPT-4-Free API
Ensure that the GPT-4-Free API is running locally. Start it using:
g4f api
Step 2: Run the Warden Framework
Launch the Warden application:
go run main.go
Step 3: Select CLI Chatbot
From the interactive menu, select the CLI Chatbot option and start chatting with the agent powered by GPT-4-Free.
Example 2: Exporting and Importing Agents
Exporting an Agent
Agents can be serialized into .agent
files for reuse or sharing. For example:
agent.ExportToFile("my_agent.agent")
This command saves the agent’s configuration and memory to a file named my_agent.agent
.
Importing an Agent
To import an agent from a .agent
file:
loadedAgent := agent.ImportFromFile("my_agent.agent")
The imported agent is now ready for use.
Example 3: Using a Custom Tool
Step 1: Create a Custom Tool
Define a custom tool for retrieving weather data:
type WeatherTool struct {}
func (w *WeatherTool) Execute(input string) string {
return "The weather today is sunny."
}
Step 2: Register the Tool
Register the tool with the agent:
agent.RegisterTool("weather", &WeatherTool{})
Step 3: Use the Tool
Invoke the tool during an agent interaction:
response := agent.UseTool("weather", "What’s the weather?")
fmt.Println(response) // Outputs: The weather today is sunny.
Example 4: Multi-Agent Collaboration
Step 1: Create Two Agents
Create two agents with distinct capabilities:
agent1 := NewAgent("GPT-4 Agent")
agent2 := NewAgent("Claude Agent")
Step 2: Share Memory Between Agents
Enable shared memory so agents can collaborate:
sharedMemory := NewSharedMemory()
agent1.SetMemory(sharedMemory)
agent2.SetMemory(sharedMemory)
Step 3: Collaborate on a Task
Agent 1 stores data:
agent1.Memory.Save("task", "Complete project report")
Agent 2 retrieves and uses the data:
task := agent2.Memory.Retrieve("task")
fmt.Println(task) // Outputs: Complete project report
Example 5: Posting Tweets via Twitter Integration
Step 1: Configure Twitter API Credentials
Set your Twitter API credentials as environment variables:
export TWITTER_API_KEY=your_twitter_api_key
export TWITTER_API_SECRET=your_twitter_api_secret
Step 2: Use the Twitter Tool
Generate and post a tweet:
tweet := agent.GenerateResponse("What should I tweet today?")
twitterTool.PostTweet(tweet)
Conclusion
These examples demonstrate the flexibility and power of Warden for building AI-powered agents. Experiment with these use cases and adapt them to suit your specific requirements.