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

Create Wallet

Before creating a Web3 agent, you’ll need a Solana wallet. You can either create a new one or import an existing wallet.

Create a New Wallet

Use the Galadriel CLI to create a new wallet:

galadriel wallet create --path <YOUR_SOLANA_KEYPAIR_PATH>

And get airdropped by running:

galadriel wallet airdrop

For details about how to use the wallet, see wallet setup.

Agent Code

Let’s extend the web search agent with Web3 capabilities to fetch real-time market data and execute an onchain swap.

Modify your script to include:

from galadriel.tools.web3.onchain.solana import raydium
from galadriel.tools.web3.market_data import dexscreener

We’ll use Dexscreener to fetch real-time market data and Raydium for executing token swaps based on market conditions.

Dexscreener is a popular Web3 platform for monitoring cryptocurrency market data including prices, trading volumes, and new token listings.

Raydium is Solana’s leading decentralized exchange (DEX) that enables automated market making and token swaps with deep liquidity.

First, we need to initialize the Solana wallet that was created in the previous step to allow agents to access it. This wallet will be used to sign transactions and interact with the Solana blockchain.

solana_wallet = SolanaWallet(key_path=os.getenv("SOLANA_KEY_PATH"))

Next, update the CodeAgent initialization with Web3-specific tools:

agent = CodeAgent(
    model=model,
    tools=[
        dexscreener.SearchTokenPairTool(),
        raydium.SwapTokenTool(solana_wallet)
    ],
    additional_authorized_imports=["json"]
)

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

Now let’s update the client’s input to ask a Web3-specific question that will utilize our new market data tools:

client = SimpleMessageClient(
    "What is the price of DAIGE today? Buy 0.001 SOL of DAIGE if the price is below 0.5 USD"
)

Running the Agent

Execute the script:

python agent.py

When you run the script, the agent will:

  1. Fetch the current market data for DAIGE token and its Raydium trading pairs
  2. Check if the current price meets our condition (below 0.5 USD)
  3. If the condition is met, execute a swap of 0.001 SOL to DAIGE tokens
  4. Output the transaction hash of the completed swap

The agent will output something like:

Final answer: 24grLTGoL5utVrhAeVpxqCPCgu2VHFk1h9QhEG315ojnKRuSA6NGZ6RHQbNAMGvQnuYHXKbTDPbt4S4sBRtDwM65

You can verify this transaction on Solana Explorer by visiting https://explorer.solana.com/ or https://solscan.io/.

🎉 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! 🚀