Build a Dune Agent with SwarmZero.ai
Dune Analytics is a powerful platform for exploring and visualizing blockchain data. It allows users to write SQL queries to extract insights from blockchain transactions and smart contracts. The platform provides tools for creating custom dashboards and sharing data visualizations with others. It is widely used by analysts, developers, and researchers to track and analyze blockchain metrics and trends.
Imagine having an agent search for indexes and crypto narratives using the DUNE API, providing updated market data. With this Dune Agent, you can access detailed insights into the ALPHA, Beta, and Gamma Indexes, as well as Daily, Weekly, Monthly, and Quarterly Crypto Narratives. This offers comprehensive and timely information on the latest crypto trends, enabling you to make informed decisions and stay ahead in the crypto market.
This tutorial will guide you through setting up, configuring, and extending a Dune Analytics agent using the dune_agent repository from SwarmZero.ai.
Watch Our Demo here:
Prerequisites
Before starting, ensure you have the following:
Python 3.8 or higher installed
Git installed
An IDE or text editor (VS Code, PyCharm, etc.)
Basic understanding of Python programming
Dune Analytics account and API key
API keys from OpenAI, Anthropic, and Mistral if you plan to use these services
Step-By-Step Tutorial for Building a Dune Agent with SwarmZero.ai
Here is a detailed guide to building the Dune Agent:
Cloning the Repository
To begin, we need to clone the dune_agent repository to our local machine. This will give you access to the code and resources needed to run and customize the Dune Agent.
Step 1: Open your terminal or command prompt. Ensure you have Git installed on your system.
Step 2: Run the following commands:
These commands will download the repository and navigate into its directory, setting up the base environment to start working on the agent.
Setting Up the Environment
Now that you have the repository cloned, the next step is to set up the environment to run the agent. This involves creating a virtual environment, installing dependencies, and configuring environment variables.
Step 1: Create a virtual environment:
Step 2: Activate the virtual environment
Command Prompt:
PowerShell:
On Unix/Mac:
Installing Dependencies
With the virtual environment activated, install the required dependencies:
Setting Up Environment Variables
Step 1: Create a new file called .env in the root directory:
Step 2: Copy the contents of .env.example into your new .env file.
Step 3: Add your API keys to the .env file:
These steps will ensure your environment is properly configured to run the Dune Agent.
Understanding the Code Structure
Before running and customizing the agent, it's essential to understand the structure of the dune_agent repository. This will help you navigate through the code and make necessary modifications efficiently.
Overview of Key Files and Directories:
.env.example: This file provides an example of the environment variables required for the project. You should copy this file to create your .env file and add the necessary API keys.
hive_config.toml: Contains configuration settings for the agent, such as API keys, query parameters, and other adjustable settings. This file is essential for configuring the agent's behavior.
hive_crypto_agent.py: This is the main script that runs the agent. It contains the core logic for fetching data from Dune Analytics, processing it, and outputting results.
requirements.txt: Lists all the dependencies required to run the agent. These are the libraries and tools the agent relies on.
requirements-dev.txt: Lists the development dependencies needed for testing and development purposes. This file is useful for setting up a development environment.
Building the Dune Agent (hive_crypto_agent.py)
In this section, you will now understand the logic and the process of building an AI agent with SwarmZero.ai.
Step 1: Import Necessary Libraries
Start by importing all the required libraries. These include libraries for handling environment variables, making HTTP requests, logging, and interacting with the HiveAgent.
Step 2: Load Environment Variables
Load your environment variables from a .env file. This file should contain your Dune API key, which will be used to authenticate your requests.
Step 3: Configure Logging
Set up logging to help you monitor the execution of your program. This will output logs to the console, showing information such as API responses and errors.
Step 4: Define the Utility Function to Get the Configuration Path
Define a utility function to get the absolute path to your configuration file. This helps in managing file paths across different environments.
Step 5: Define Function to Execute Dune Queries
Create a function to execute queries on the Dune Analytics API. This function sends a POST request to the Dune API to initiate the execution of a query.
Step 6: Define the Function to Wait for Query Execution
This function checks the status of a query execution at regular intervals until it is complete or a maximum number of attempts is reached.
Step 7: Define the Function to Fetch Query Results
After the query execution is complete, this function retrieves the results of the query from the Dune API.
Step 8: Define the Function to Run the Dune Query
Combine the previous functions into a single process that executes a query, waits for its completion, and then retrieves the results.
Step 9: Define Functions to Fetch Specific Index Data
Create functions that fetch data for specific indexes (Alpha, Beta, Gamma, etc.) by calling the run_dune_query function with the appropriate query ID.
Step 10: Define Query IDs
Define a dictionary that maps each index to its corresponding query ID on Dune Analytics.
Step 11: Define Instruction String
Create a string containing instructions for the HiveAgent, explaining what the agent is responsible for and providing additional context about the indexes.
Step 12: Create and Run the HiveAgent
Finally, create an instance of HiveAgent, passing in the functions, instructions, and configuration path. Then, run the agent.
Running the Agent
With the environment set up and a basic understanding of the code structure, you are now ready to run the agent and see it in action.
Ensure your virtual environment is activated. You can test your agent by calling its Chat API endpoint to see the result.
Here’s an example using curl:
Step 1: Run the API server:
Step 2: Open the Swagger Docs
Step 3: Choose the POST /api/vi/chat and test it:
This will send a request to the API and provide you with a response, allowing you to verify the agent’s functionality.
Output
You can also chat with the Dune Agent using SwarmZero’s UI. You can set it up using the instructions in this repository.
As the next steps, you can proceed with testing if required.
Wrap Up
Congratulations! You've successfully set up, configured, and customized your Dune Agent using SwarmZero.ai.
Last updated