Create an AI Agent to Mint an MNIST NFT
Agents are entities designed to assist users in interacting with Smart Contracts by managing the proof verification of verifiable ML models and executing these contracts using Ape's framework.
Agents serve as intermediaries between users and Smart Contracts, facilitating seamless interaction with verifiable ML models and executing associated contracts. They handle the verification of proofs, ensuring the integrity and authenticity of data used in contract execution.
In this tutorial, we will create an AI Agent to mint an MNIST NFT. Using an existing MNIST endpoint, we will perfom a prediction on the MNIST dataset and mint an NFT based on the prediction.
This tutorial can be thought as the continuation of the previous tutorial Verifiable MNIST Neural Network.
Installation
To follow this tutorial, you must first proceed with the following installation.
Setup
From your terminal, create a Giza user through our CLI in order to access the Giza Platform:
After creating your user, log into Giza:
Create an API Key for your user in order to not regenerate your access token every few hours.
Before you begin
We assume that you already have an MNIST inference endpoint deployed on Giza. If not, please follow the Verifiable MNIST Neural Network tutorial to deploy an MNIST endpoint.
Creating 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:
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.
Funding the account
Before we can create an AI Agent, we need to fund the account with some ETH. You can do this by sending some ETH to the account address generated in the previous step.
In this case we will use Sepolia testnet, you can get some testnet ETH from a faucet like Alchemy Sepolia Faucet or LearnWeb3 Faucet. This faucets will ask for security meassures to make sure that you are not a bot like having a specific amount of ETH in mainnet or a Github account. There are many faucets available, you can choose the one that suits you the best.
Once, we can recieve the testnet ETH and we have a funded account, we can proceed to create an AI Agent.
Creating an AI Agent
Now that we have a funded account, we can create an AI Agent. We can do this by running the following command:
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:
This will create an AI Agent that can be used to interact with the deployed MNIST model.
How to use the AI Agent
Now that the agent is created we can start using it through the Agent SDK. As we will be using the agent to mint a MNIST NFT, we will need to provide the MNIST image to the agent and preprocess it before sending it to the model.
Now we will start creating the functions that are necessary to create a program that will:
Load and preprocess the MNIST image
Create an instance of an agent
Predict the MNIST image using the agent
Access the prediction result
Mint an NFT based on the prediction result
To load and preprocess the image we can use the function that we already created in the previous tutorial Verifiable MNIST Neural Network.
Now, we will create a function to create an instance of the agent. The agent is an extension of the GizaModel
class, so we can execute the predict
as if we were using a model, but the agents needs more information:
chain: The chain where the contract and account are deployed
contracts: This is a dictionary in the form of
{"contract_alias": "contract_address"}
that contains the contract alias and address.
This contract_alias
is the alias that we will use when executing the contract through code.
This new task
will execute the predict
method of the agent following the same format as a GizaModel
instance. But in this case, the agent will return an AgentResult
object that contains the prediction result, request id and multiple utilities to handle the verification of the prediction.
Once we have the result, we need to access it. The AgentResult
object contains the value
attribute that contains the prediction result. In this case, the prediction result is a number from 0 to 9 that represents the digit that the model predicted.
When we access the value, the execution will be blocked until the proof of the prediction has been created and once created it is verified. If the proof is not valid, the execution will raise an exception.
This task is more for didactic purposes, to showcase in the workspaces the time that it can take to verify the proof. In a real scenario, you can use the AgentResult
value directly in any other task.
Finally, we will create a task to mint an NFT based on the prediction result. This task will use the prediction result to mint an NFT with the image of the MNIST digit predicted.
To execute the contract we have the GizaAgent.execute
context that will yield all the initialized contracts and the agent instance. This context will be used to execute the contract as it handles all the connections to the nodes, networks, and contracts.
In our instantiation of the agent we added an mnist
alias to access the contract to ease its use. For example:
Now that we have all the steps defined, we can create the function to execute.
Remember that we should have kept the passphrase in a safe place? Now it is time to use it. For learning purposes, we will use the passphrase in the code, but in a real scenario, you should keep it safe and not hardcode it in the code.
Now let's execute mint_nft_with_prediction
.
Using Ape's default networks (chains) relies on public RPC nodes that could hit request limits and make the execution of the contract fail. For a better experience think about using a private RPC node with higher quotas
What we have learned
In this tutorial, we learned how to create an AI Agent to mint an MNIST NFT. We created an AI Agent using Ape's framework and interacted with the agent to mint an NFT based on the prediction result of an MNIST image.
We learned how to load and preprocess the MNIST image, create an instance of the agent, predict the MNIST image using the agent, access the prediction result, and mint an NFT based on the prediction result.
Last updated