Build a Uniswap Docs Retrieval Agent with SwarmZero.ai

This Uniswap Documentation Retrieval Agent allows users to build a specialized Q&A Retrieval-Augmented Generation (RAG) agent that sources information directly from Uniswap’s official documentation. The agent is designed to provide accurate, timely, and user-friendly responses to questions about Uniswap.

This tutorial will guide you through setting up, configuring, and extending a Dune Analytics agent using the uniswap-retrieval-agent repository from SwarmZero.ai.

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.).

  • A basic understanding of Python programming.

  • Uniswap Documentation to use as a reference for answering user queries.

  • API keys from OpenAI, Anthropic, and Mistral if you plan to use these services.

Step-by-Step Tutorial for Building a Uniswap Agent with SwarmZero.ai

Here is a detailed guide to building the Uniswap Docs Retrieval Agent:

Cloning the Repository

To begin, clone the uniswap-retrieval-agent repository to your local machine. This will give you access to the code and resources needed to run and customize the Uniswap Agent.

Step 1: Open your terminal or command prompt. Ensure Git is installed on your system.

Step 2: Run the following commands:

git clone https://github.com/hivenetwork-ai/example-agents/
cd uniswap-retrieval-agent

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:

python -m venv ./venv

Step 2: Activate the virtual environment

  • Command Prompt:

    venv\Scripts\activate.bat
  • PowerShell:

    venv\Scripts\Activate.ps1
  • On Unix/Mac:

    source venv/bin/activate

Installing Dependencies

With the virtual environment activated, install the required dependencies:

pip install -r requirements.txt

Run the command below to install the optional dependencies:

pip install git+https://github.com/hivenetwork-ai/hive-agent-py.git@main#egg=hive-agent[web3]

Setting Up Environment Variables

Step 1: Create a new file called .env in the root directory:

touch .env

Step 2: Copy the contents of .env.example into your new .env file.

Step 3: Add your API keys to the .env file:

OPENAI_API_KEY=your_openai_api_key

These steps will ensure your environment is properly configured to run the Uniswap Docs Retreival Agent.

Understanding the Code Structure

Before running and customizing the agent, it's essential to understand the structure of the uniswap_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. Copy this to create your .env file and add the necessary keys.

  • hive_config.toml: Contains configuration settings for the agent, such as API keys and other adjustable settings.

  • uniswap-retrieval-agent.py: This is the main script that runs the agent. It contains the core logic for fetching Uniswap documentation, processing it, and answering user queries.

  • requirements.txt: Lists all the dependencies required to run the agent.

Building the Uniswap Agent (uniswap-retrieval-agent.py)

In this section, you will now understand the logic and the process of building a Uniswap Docs AI agent with SwarmZero.ai.

Step 1: Import Necessary Libraries

Start by importing the required libraries. These include libraries for handling environment variables, logging, and interacting with the HiveAgent.

from hive_agent import HiveAgent
import os
import logging

Step 2: Configure Logging

Set up logging to help you monitor the execution of your program.

logging.basicConfig(level=logging.INFO)

Step 3: Define the Utility Function to Get the Configuration Path

Define a utility function to get the absolute path to your configuration file.

def get_config_path(filename):
    return os.path.abspath(os.path.join(os.path.dirname(__file__), filename))

Step 4: Set Up Instruction for the Uniswap Agent

Create an instruction string that defines the role of the agent. The agent's purpose is to provide answers based on Uniswap documentation.

instruction = """Welcome to the Uniswap Documentation Q&A Assistant! Your role is to provide accurate and concise answers to user queries based on the official Uniswap documentation. When responding, keep the following guidelines in mind:

1. Accuracy: Ensure your answers are directly based on the most current Uniswap documentation. Reference specific sections or pages when necessary.
2. Clarity: Communicate in simple, straightforward language. Avoid technical jargon unless it is necessary, and explain any complex concepts clearly.
3. Promptness: Aim to provide answers quickly, keeping your responses to the point to respect the user’s time.
4. User Engagement: Encourage users to ask follow-up questions if they need further clarification on a topic. Provide links to relevant sections of the documentation where they can read more in-depth information.
5. Updates and Feedback: Stay updated with the latest changes in the Uniswap platform and documentation. Promptly incorporate these updates into your responses. Encourage users to provide feedback on the accuracy and helpfulness of the information provided."""

Step 5: Create and Run the HiveAgent

Finally, create an instance of the HiveAgent, passing in the functions, instructions, and configuration path. Then, run the agent.

my_agent = HiveAgent(
    name="uniswap-retrieval-agent",
    functions=[],
    instruction=instruction,
    config_path=get_config_path("hive_config.toml"),
    retrieve=True,
    required_exts=[".md"],
)
my_agent.run()

Running the Agent

With the environment set-up and a basic understanding of the code structure, you are now ready to run the agent.

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:

(venv) python uniswap-retrieval-agent.py

Step 2: Open the Swagger Docs

http://localhost:8000/docs

Step 3: Choose the POST /api/vi/chat and test it:

curl --location 'localhost:8000/api/v1/chat' \
--header 'Content-Type: application/json' \
--data '{
    "user_id": "user123",
    "session_id": "session123",
    "chat_data": {
        "messages": [
            { "role": "user", "content": "Explain the concept of immutability in the context of Uniswap’s smart contracts." }
        ]
    }
}'

This will send a request to the API and provide you with a response, allowing you to verify the agent’s functionality.

Output

As the next steps, you can proceed with testing if required.

Wrap Up

Congratulations! You've successfully set up, configured, and customized your Uniswap Docs Retrieval Agent using SwarmZero.ai.

Last updated