CrewAi
Integration of Agentipy with crewAI
This serves as a practical example of how to integrate Agentipy toolkit with the crewAI multi-agent framework. The integration showcases:
Bringing Blockchain to AI Agents: How to equip AI agents with blockchain capabilities through Agentipy
Tool Integration: How to wrap agentipy functionality as crewAI tools
Configuration-Driven Design: Using YAML files to configure blockchain agents
Integration Pattern
The integration follows this pattern:
# 1. Import both libraries
from crewai import Agent, Crew, Task
from agentipy.agent import SolanaAgentKit
# 2. Create a Solana kit instance
solana_kit = SolanaAgentKit(
private_key=os.getenv("SOLANA_PRIVATE_KEY"),
rpc_url="https://api.devnet.solana.com"
)
# 3. Create tools from agentipy capabilities
@tool("get_sol_balance")
def get_sol_balance() -> str:
# Use agentipy BalanceFetcher to get blockchain data
balance = loop.run_until_complete(BalanceFetcher.get_balance(agent))
return f"The current SOL balance is {balance:.4f} SOL"
# 4. Attach tools to crewAI agents
solana_agent = Agent(
config=agents_config["solana_operator"],
tools=[make_balance_tool(agent=solana_kit)],
llm=ChatOpenAI(model=os.getenv("MODEL"), temperature=0)
)
# 5. Create tasks using these agents
solana_task = Task(
description="Analyze instruction and take action: {user_input}",
agent=solana_agent
)
# 6. Orchestrate with crewAI
crew = Crew(
agents=[solana_agent],
tasks=[solana_task],
process=Process.sequential
)
This pattern demonstrates how to leverage both frameworks' strengths:
agentipy provides blockchain interaction capabilities
crewAI provides agent orchestration, task management, and goal-oriented behavior
Architecture
The repository is structured as follows:
src/solana_agent_agentipy/
├── config/
│ ├── agents.yaml # Agent configuration definitions
│ └── tasks.yaml # Task configuration definitions
├── tools/
│ ├── custom_tool.py # Custom tools for the Solana agent
│ └── __init__.py
├── crew.py # Main crew definition and orchestration
├── main.py # Entry point for running the system
└── __init__.py
Components
1. SolanaWalletCrew
The core component of the system is the SolanaWalletCrew
class defined in crew.py
. This class:
Initializes a SolanaAgentKit with provided credentials
Defines the Solana agent's capabilities and tools
Sets up tasks for the agent to perform
Orchestrates the execution flow in a sequential process
2. Custom Tools
The custom_tool.py
file defines tools that the agent can use to interact with the Solana blockchain, such as:
get_sol_balance
: A tool to fetch and display the current SOL balance of the wallet
3. Configuration
The system uses YAML configuration files to define:
agents.yaml: Defines the Solana operator agent's role, goals, and backstory
tasks.yaml: Defines the operations to be performed based on user input
Setup and Installation
Prerequisites
Python >=3.10 <3.13
UV for dependency management
Installation Steps
Install UV if not already installed:
pip install uv
Clone the repository and navigate to the project directory
Install dependencies (using the crewAI CLI):
crewai install
Create a
.env
file with the following variables:
OPENAI_API_KEY=your_openai_api_key
SOLANA_PRIVATE_KEY=your_solana_private_key
MODEL=gpt-4-turbo
Usage
Run the project from the root folder with:
crewai run
Example input:
"Check my balance and send 1 SOL if I'm above $1000"
This will:
Initialize the SolanaWalletCrew
Process the natural language input
Check the current SOL balance using the Solana blockchain
Analyze if conditional requirements are met
Execute the requested operations if conditions are satisfied
Customization
The system is designed to be easily customizable:
Adding New Agents: Modify the
agents.yaml
file to define additional agents with different roles and capabilitiesDefining New Tasks: Update the
tasks.yaml
file to create new task definitions for your agentsCreating Custom Tools: Extend the tools available to your agents by adding new tools in the
tools/
directoryCustom Logic: Modify the
crew.py
file to implement custom logic, additional tools, or specific arguments
Integration with agentipy
This project leverages the agentipy library to interact with the Solana blockchain, providing:
Wallet management
Balance fetching
Transaction capabilities
Dependencies
crewAI: Framework for creating and orchestrating AI agent systems
agentipy: Solana blockchain interaction toolkit
langchain-openai: LLM integration for natural language processing
python-dotenv: Environment variable management
Advanced Use Cases
The system can be extended to support:
Multi-token wallet management
Complex conditional operations
Integration with other blockchain operations
Custom token transfers and transactions
Troubleshooting
If you encounter issues:
Ensure your Solana private key is valid and has funds on the devnet
Verify your OpenAI API key is correctly set and has sufficient credits
Check that you're using the correct Solana RPC URL (default is devnet)
Last updated