This quickstart covers:

  1. Setting up the dev environment
  2. Creating and running a simple agent that uses the web search tool
  3. Creating and running a Web3 agent that uses the Dexscreener tool

Setup

1. Requirements

Python >=3.10

2. Dev Environment

First, set up your local environment and then install the galadriel Python package.

python -m venv venv
source venv/bin/activate
pip install galadriel

Create a Simple Web Search Agent

1. Agent Code

Create a file named agent.py with the following Python code.

Add your OpenAI API Key.

import asyncio
from galadriel import AgentRuntime, CodeAgent
from galadriel.clients import SimpleMessageClient
from galadriel.core_agent import LiteLLMModel, DuckDuckGoSearchTool

# Set up the model (replace with your API key)
model = LiteLLMModel(model_id="gpt-4o", api_key="<YOUR_OPENAI_API_KEY>")

# Create the AI agent with web search capabilities
agent = CodeAgent(
    model=model,
    tools=[DuckDuckGoSearchTool()],
)

# Define a simple terminal client so you can chat with the agent
client = SimpleMessageClient("What is the capital of Estonia?")

# Set up and run the agent runtime
runtime = AgentRuntime(
    agent=agent,
    inputs=[client],
    outputs=[client],
)

asyncio.run(runtime.run())

Running the Agent

Execute the script:

python agent.py

When the agents starts to execute the task, it prints the output similar to this one:

╭────────────────────────────────── New run ───────────────────────────────────╮
│                                                                              │
│ What is the capital of Estonia?                                              │
│                                                                              │
╰─ LiteLLMModel - gpt-4o ──────────────────────────────────────────────────────╯
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 1 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 ─ Executing parsed code: ─────────────────────────────────────────────────────
  capital_estonia = web_search(query="capital of Estonia")
  print(capital_estonia)
 ──────────────────────────────────────────────────────────────────────────────

Which will be followed by more steps until the results reaches the output

======== simple_message_client.post_output ========
request: content='What is the capital of Estonia?'
response: content='Tallinn'

How it Works

This simple agent receives a question from the client and executes a series of reasoning steps to provide an answer. It autonomously determines when to search the web.

It leverages:

  • gpt-4o model from OpenAI for agent reasoning.
  • SimpleMessageClient to send and receive messages.
  • AgentRuntime to connect the agent to the client and execute tasks.
  • DuckDuckGoSearchTool for web-based information retrieval.

Create a Web3 Agent

Agent Code

Let’s extend the web search agent with Web3 capabilities to fetch real-time market data.

Modify your script to include:

from galadriel.tools.web3 import dexscreener

We are going to use Dexscreener to fetch market data. Dexscrener is a widely used Web3 tool to monitor crypto market data like prices, new tokens, etc. Modify the CodeAgent initialization:

agent = CodeAgent(
    model=model,
    tools=[
        DuckDuckGoSearchTool(),
        dexscreener.fetch_market_data
    ],
    additional_authorized_imports=["json"]
)

Note additional_authorized_imports=["json"] parameter in the CodeAgent initialization. It is required because dexscreener.fetch_market_data imports json module and the Python interpreter doesn’t allow imports by default outside of a safe list.

Update the client’s input by asking a Web3-specific question:

client = SimpleMessageClient("What are the top tokens on the market today?")

Running the Agent

Execute the script:

python agent.py

You should receive an output similar to:

Answer: The top tokens by 24-hour volume are: 'Trump's Tax Company', 'dogwifouthat', 'THE DARK KNIGHT', and 'BRITISH DOG'

🎉 Gratz on Building Your First Agent!

You’ve successfully built both a generic web search agent and a Web3-focused agent using Galadriel.

But this is just scratching the surface - what’s next? To unlock more capabilities for your agent, check out these resources:

Happy coding! 🚀