Using Arbitrum with AI Agents

How to interact with Arbitrum using AI Agents

In the previous tutorial we have seen how to use the GizaAgent class to create a simple agent that can interact with the Ethereum blockchain. In this tutorial we will see how to use other chains with the GizaAgent class.

As we rely on ape to interact with the blockchain, we can use any chain that ape supports. The list of supported chains via plugins can be found here.

In this tutorial we will use Arbitrum as an example. The Arbitrum is a layer 2 solution for Ethereum that provides low cost and fast transactions. The Arbitrum is supported by ape and we can use it with the GizaAgent class.

Installation

To follow this tutorial, you must first proceed with the following installation.

Handling Python versions with Pyenv

You should install Giza tools in a virtual environment. If you’re unfamiliar with Python virtual environments, take a look at this guide. A virtual environment makes it easier to manage different projects and avoid compatibility issues between dependencies.

Install Python 3.11 using pyenv

pyenv install 3.11.0

Set Python 3.11 as local Python version:

pyenv local 3.11.0

Create a virtual environment using Python 3.11:

pyenv virtualenv 3.11.0 my-env

Activate the virtual environment:

pyenv activate my-env

Now, your terminal session will use Python 3.11 for this project.

Install Giza

Install Giza CLI

Install the CLI from PyPi

pip install giza-cli

Install Agent SDK

Install the package from PyPi

pip install giza-agents

You'll find more options for installing Giza in the installation guide.

Install Dependencies

You must also install the following dependencies:

pip install -U scikit-learn torch pandas

Create or Login a user to Giza

From your terminal, create a Giza user through our CLI in order to access the Giza Platform:

giza users create

After creating your user, log into Giza:

giza users login

Create an API Key for your user in order to not regenerate your access token every few hours.

giza users create-api-key

Before you begin

You must have a model deployed on Giza. You can follow the tutorial Verifiable MNIST Neural Network to deploy an MNIST model on Giza.

Create an Ape Account (Wallet)

Before we can create an AI Agent, we need to create an account using Ape's framework. We can do this by running the following command:

$ ape accounts generate <account name>
Enhance the security of your account by adding additional random input:
Show mnemonic? [Y/n]: n
Create Passphrase to encrypt account:
Repeat for confirmation:
SUCCESS: A new account '0x766867bB2E3E1A6E6245F4930b47E9aF54cEba0C' with HDPath m/44'/60'/0'/0/0 has been added with the id '<account name>'

This will create a new account under $HOME/.ape/accounts using the keyfile structure from the eth-keyfile library , for more information on the account management, you can refer to the Ape's framework documentation.

In this account generation we will be prompted to enter a passphrase to encrypt the account, this passphrase will be used to unlock the account when needed, so make sure to keep it safe.

We encourage the creation of a new account for each agent, as it will allow you to manage the agent's permissions and access control more effectively, but importing accounts is also possible.

Create an Agent

Now that we have a funded account, we can create an AI Agent. We can do this by running the following command:

giza agents create --endpoint-id <endpoint-id> --name <agent name> --description <agent description>

This command will prompt you to choose the account you want to use with the agent, once you select the account, the agent will be created and you will receive the agent id. The output will look like this:

[giza][2024-04-10 11:50:24.005] Creating agent βœ…
[giza][2024-04-10 11:50:24.006] Using endpoint id to create agent, retrieving model id and version id
[giza][2024-04-10 11:50:53.480] Select an existing account to create the agent.
[giza][2024-04-10 11:50:53.480] Available accounts are:
┏━━━━━━━━━━━━━┓
┃  Accounts   ┃
┑━━━━━━━━━━━━━┩
β”‚ my_account  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter the account name: my_account
{
  "id": 1,
  "name": <agent_name>,
  "description": <agent_description>,
  "parameters": {
    "model_id": <model_id>,
    "version_id": <version_id>,
    "endpoint_id": <endpoint_id>,
    "alias": "my_account"
  },
  "created_date": "2024-04-10T09:51:04.226448",
  "last_update": "2024-04-10T09:51:04.226448"
}

This will create an AI Agent that can be used to interact with the deployed MNIST model.

Use Agents in Arbitrum

Let's start by installing the ape-arbitrum plugin.

 !ape plugins install arbitrum

We can confirm if it has been installed by executing ape networks list in the terminal.

!ape networks list

arbitrum
β”œβ”€β”€ goerli
β”‚   β”œβ”€β”€ alchemy
β”‚   └── geth  (default)
β”œβ”€β”€ local  (default)
β”‚   └── test  (default)
β”œβ”€β”€ mainnet
β”‚   β”œβ”€β”€ alchemy
β”‚   └── geth  (default)
└── sepolia
    β”œβ”€β”€ alchemy
    └── geth  (default)
ethereum  (default)
β”œβ”€β”€ goerli
β”‚   β”œβ”€β”€ alchemy
β”‚   └── geth  (default)
β”œβ”€β”€ local  (default)
β”‚   β”œβ”€β”€ geth
β”‚   └── test  (default)
β”œβ”€β”€ mainnet
β”‚   β”œβ”€β”€ alchemy
β”‚   └── geth  (default)
└── sepolia
    β”œβ”€β”€ alchemy
    └── geth  (default)

Here we can see that we have multiple networks available, including arbitrum. So now we can use it when instantiating the GizaAgent class.

For this execution, we will use Arbitrum mainnet and use a private RPC node, this is because public nodes have small quotas that can easily be reached.

The contract is a verified contract selected at random from arbiscan, the mission is to showcase that we can read properties from this contract, which means that we could also be able to execute a write function.

In this case, as we are only executing a read function we don't need a funded wallet to do anything as we won't sign any transactions.

Remember that we will need to specify the <Account>_PASSPHRASE if you are launching your operation as a script, exporting it will be enough:

Remember that we will need to specify the <Account>_PASSPHRASE if you are launching your operation as a script, exporting it will be enough:

export <Account>_PASSPHRASE=your-passphrase

If you are using it from a notebook you will need to launch the notebook instance from an environment with the passphrase variable or set it in the code prior to importing giza_actions:

import os
os.environ["<Account>_PASSPHRASE"] = "your-passphrase"


from giza.agents import GizaAgent
...

Now we can instantiate the agent:

import os
os.environ["<Account>_PASSPHRASE"] = ...

from giza.agents import GizaAgent

MODEL_ID = ...
VERSION_ID = ...
ACCOUNT = ...
PRIVATE_RPC = ... # This can also be loaded from the environment or a .env file

agent = GizaAgent(
    id=MODEL_ID,
    version_id=VERSION_ID,
    chain=f"arbitrum:mainnet:{PRIVATE_RPC}",
    account=ACCOUNT,
    contracts={
        "random": "0x8606d62fD47473Fad2db53Ce7b2B820FdEab7AAF"
    }
)
```

Now that we have the agent instance we can enter the execute() context and call the read function from an Arbitrum smart contract:

with agent.execute() as contracts:
    result = contracts.random.name()
    print(f"Contract name is: {result}")

What we have learned

This tutorial taught us how to create an AI Agent and interact with Arbitrum.

For this, we needed to:

  • Install the arbitrum plugin

  • Check that the new network is available

  • Got a contract from arbiscan

  • Use an agent to execute a function from an arbitrum smart contract

Now these same steps can be followed to use any other network supported by ape and interact with different chains.

Last updated