Skip to content

Commit

Permalink
Merge pull request #46 from silent-cipher/example/polygon-agent
Browse files Browse the repository at this point in the history
Example: Using polygon with agents
  • Loading branch information
Gonmeso authored May 14, 2024
2 parents fb809fa + be283fa commit 09bda5d
Showing 1 changed file with 281 additions and 0 deletions.
281 changes: 281 additions & 0 deletions examples/agents/using_polygon.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Using Polygon with Agents\n",
"\n",
"In the previous tutorial, we have seen how to use the `GizaAgent` class to create a simple agent that can interact with the Arbitrum blockchain. In this tutorial, we will learn how to use other chains with the `GizaAgent` class.\n",
"\n",
"As we rely on `ape` to interact with the blockchain, we can use any chain that it supports. The list of supported chains via plugins can be found [here](https://docs.apeworx.io/).\n",
"\n",
"In this tutorial, we will use `Polygon` as an example. `Polygon` is a layer 2 solution for Ethereum that provides low cost and fast transactions. It is supported by `ape` and we can use it with the `GizaAgent` class.\n",
"\n",
"## Before you begin\n",
"\n",
"1. Python 3.11 or later must be installed on your machine\n",
"2. Giza CLI must be installed on your machine. You can install it by running `pip install giza-cli`\n",
"3. Actions-SDK should be installed. You can install it by running `pip install giza-actions`\n",
"4. You must have an active Giza account. If you don't have one, you can create one [here](https://cli.gizatech.xyz/examples/basic).\n",
"5. You must have a model deployed on Giza. You can follow the tutorial [Build a Verifiable Neural Network with Giza Actions](https://actions.gizatech.xyz/tutorials/build-a-verifiable-neural-network-with-giza-actions) to deploy an MNIST model on Giza.\n",
"6. During the tutorial, you will need to interact with the Giza CLI, Ape's framework, and provide multiple inputs, like `model-id`, `version-id`, `account name`, etc.\n",
"7. You must be logged in to the Giza CLI or have an API KEY.\n",
"\n",
"## Installing the required libraries\n",
"\n",
"Let's start by installing the required libraries.\n",
"\n",
"```bash\n",
"pip install giza-actions\n",
"```\n",
"\n",
"This will install the `giza-actions` library, which contains the necessary tools to create an AI Agent.\n",
"\n",
"## Creating an account\n",
"\n",
"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:\n",
"\n",
"```bash\n",
"$ ape accounts generate <account name>\n",
"Enhance the security of your account by adding additional random input:\n",
"Show mnemonic? [Y/n]: n\n",
"Create Passphrase to encrypt account:\n",
"Repeat for confirmation:\n",
"SUCCESS: A new account '0x766867bB2E3E1A6E6245F4930b47E9aF54cEba0C' with HDPath m/44'/60'/0'/0/0 has been added with the id '<account name>'\n",
"```\n",
"\n",
"This will create a new account under `$HOME/.ape/accounts` using the keyfile structure from the [eth-keyfile library](https://github.com/ethereum/eth-keyfile)\n",
". For more information on the account management, you can refer to the [Ape's framework documentation](https://docs.apeworx.io/ape/stable/userguides/accounts.html#keyfile-accounts).\n",
"\n",
"During account generation we will be prompted to enter a passphrase to encrypt the account, which will be used to unlock the said account when needed, so **make sure to keep it safe**.\n",
"\n",
"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.\n",
"\n",
"## Creating an AI Agent\n",
"\n",
"Now that we have a funded account, we can create an AI Agent. This can be done by running the following command:\n",
"\n",
"```bash\n",
"giza agents create --endpoint-id <endpoint-id> --name <agent name> --description <agent description>\n",
"\n",
"# or if you don't have endpoint-id (but will be deprecated)\n",
"\n",
"giza agents create --model-id <model-id> --version-id <version-id> --name <agent name> --description <agent description>\n",
"\n",
"```\n",
"\n",
"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:\n",
"\n",
"```console\n",
"[giza][2024-04-24 21:46:35.009] Creating agent ✅ \n",
"[giza][2024-04-24 21:46:35.011] Using endpoint id to create agent, retrieving model id and version id\n",
"[giza][2024-04-24 21:46:35.935] Select an existing account to create the agent.\n",
"[giza][2024-04-24 21:46:35.937] Available accounts are:\n",
"┏━━━━━━━━━━━━━┓\n",
"┃ Accounts ┃\n",
"┡━━━━━━━━━━━━━┩\n",
"│ my_account │\n",
"└─────────────┘\n",
"Enter the account name: my_account\n",
"{\n",
" \"id\": 1,\n",
" \"name\": <agent_name>,\n",
" \"description\": <agent_description>,\n",
" \"parameters\": {\n",
" \"model_id\": <model_id>,\n",
" \"version_id\": <version_id>,\n",
" \"endpoint_id\": <endpoint_id>,\n",
" \"account\": \"my_account\"\n",
" },\n",
" \"created_date\": \"2024-04-10T09:51:04.226448\",\n",
" \"last_update\": \"2024-04-10T09:51:04.226448\"\n",
"}\n",
"```\n",
"\n",
"This will create an AI Agent that can be used to interact with the deployed MNIST model.\n",
"\n",
"## Use Agents in Polygon\n",
"\n",
"Let's start by installing the `ape-polygon` plugin."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!ape plugins install polygon"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can confirm whether it has been installed, by running `ape networks list` in the terminal."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1;32methereum\u001b[0m\u001b[1;2;39m (default)\u001b[0m\n",
"├── \u001b[1;32mgoerli\u001b[0m\n",
"│ └── \u001b[1;32mgeth\u001b[0m\u001b[1;2;39m (default)\u001b[0m\n",
"├── \u001b[1;32mlocal\u001b[0m\u001b[1;2;39m (default)\u001b[0m\n",
"│ ├── \u001b[1;32mgeth\u001b[0m\n",
"│ └── \u001b[1;32mtest\u001b[0m\u001b[1;2;39m (default)\u001b[0m\n",
"├── \u001b[1;32mmainnet\u001b[0m\n",
"│ └── \u001b[1;32mgeth\u001b[0m\u001b[1;2;39m (default)\u001b[0m\n",
"└── \u001b[1;32msepolia\u001b[0m\n",
" └── \u001b[1;32mgeth\u001b[0m\u001b[1;2;39m (default)\u001b[0m\n",
"\u001b[1;32mpolygon\u001b[0m\n",
"├── \u001b[1;32mamoy\u001b[0m\n",
"│ └── \u001b[1;32mgeth\u001b[0m\u001b[1;2;39m (default)\u001b[0m\n",
"├── \u001b[1;32mlocal\u001b[0m\u001b[1;2;39m (default)\u001b[0m\n",
"│ └── \u001b[1;32mtest\u001b[0m\u001b[1;2;39m (default)\u001b[0m\n",
"├── \u001b[1;32mmainnet\u001b[0m\n",
"│ └── \u001b[1;32mgeth\u001b[0m\u001b[1;2;39m (default)\u001b[0m\n",
"└── \u001b[1;32mmumbai\u001b[0m\n",
" └── \u001b[1;32mgeth\u001b[0m\u001b[1;2;39m (default)\u001b[0m\n"
]
}
],
"source": [
"!ape networks list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here, we can see that we have multiple networks available, including `polygon`. So now we can use it when instantiating the `GizaAgent` class.\n",
"\n",
"For this execution, we will use the Polygon mainnet and a private RPC node. This is because public nodes have small quotas that can easily be reached.\n",
"\n",
"The contract is a verified contract selected at random from [polygonscan](https://polygonscan.com/). The aim is to show that we can read properties from this contract, which means that we could also execute a write function.\n",
"\n",
"In this case, as we are only executing a read function, there is no need for a funded wallet because we won't be signing any transactions.\n",
"\n",
"Remember that you will need to specify the `<Account>_PASSPHRASE`. If you wish to launch your operation as a script, exporting it will be enough:\n",
"\n",
"```bash\n",
"export <ACCOUNT>_PASSPHRASE=your-passphrase\n",
"```\n",
"\n",
"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 before importing `giza_actions`:\n",
"\n",
"```python\n",
"import os\n",
"os.environ[\"<ACCOUNT>_PASSPHRASE\"] = \"your-passphrase\"\n",
"\n",
"from giza_actions.agent import GizaAgent\n",
"...\n",
"```\n",
"\n",
"Now we can instantiate the agent:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"os.environ[\"<ACCOUNT>_PASSPHRASE\"] = ...\n",
"\n",
"\n",
"from giza_actions.agent import GizaAgent\n",
"\n",
"MODEL_ID = ...\n",
"VERSION_ID = ...\n",
"ACCOUNT = ...\n",
"PRIVATE_RPC = ... # This can also be loaded from the environment or a .env file\n",
"\n",
"agent = GizaAgent(\n",
" id=MODEL_ID,\n",
" version_id=VERSION_ID,\n",
" chain=f\"polygon:mainnet:{PRIVATE_RPC}\",\n",
" account=ACCOUNT,\n",
" contracts={\n",
" \"random\": \"0x577f6076E558818A5dF21Ce4acdE9A9623eC0b4c\"\n",
" }\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that we have the agent instance, we can enter the `execute()` context and call the read function from an Polygon smart contract:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO: Connecting to a 'bor' node.\n",
"WARNING: Danger! This account will now sign any transaction it's given.\n",
"Contract name is: Balancer 80SD-20maticX\n"
]
}
],
"source": [
"with agent.execute() as contracts:\n",
" result = contracts.random.name()\n",
" print(f\"Contract name is: {result}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## What we have learned\n",
"\n",
"In this tutorial, we learned how to create an AI Agent and interact with Polygon.\n",
"\n",
"For this, we needed to:\n",
"\n",
"* Install the `polygon` plugin\n",
"* Check that the new network is available\n",
"* Got a contract from `polygonscan`\n",
"* Use an agent to execute a function from an `polygon` smart contract\n",
"\n",
"Now these same steps can be followed to use any other network supported by `ape` and interact with different chains."
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 09bda5d

Please sign in to comment.