Build Your First AI dApp
This tutorial aims to guide developers step by step in writing an AegisAI application contract. We'll use AegisAIOracle as an EVM example to demonstrate how to leverage AegisAIEndpoint to implement decentralized AI functionality.
Overview
AegisAIEndpoint serves as a middleware layer responsible for receiving AI requests, forwarding requests to AI nodes for processing through an AI network consensus mechanism, and returning the final results to users. Below, we will build an AegisAI application contract called AegisAIOracle step by step, which processes AI tasks related to data querying.
Step 1: Inherit the IRequestCallbackHandler Interface
First, your application contract needs to inherit the IRequestCallbackHandler
interface. This interface defines the process
function, which AegisAIEndpoint will call after processing a request to return the results to your contract.
IRequestCallbackHandler
: Defines the interface with theprocess
function that your contract must implement.
Step 2: Contract Initialization and Security Callback Handling
In your application contract, you need to initialize necessary parameters, such as the address of the AegisAIEndpoint contract. Additionally, to ensure that only the AegisAIEndpoint contract can call your contract, you need to implement a secure callback handling mechanism.
Initialization
endpoint
: The address of the AegisAIEndpoint contract, with which your contract will interact.
Secure Callback Handling
To ensure that only AegisAIEndpoint can call your contract's process
function, you need to implement a modifier onlyEndpoint
and use it in the process
function.
Step 3: Building AI Requests
To enable AegisAIOracle
to handle AI requests, you first need to allow building a request. We provide two functions, requestWithPrompts
and request
, to initiate requests.
Using the requestWithPrompts Function
This function allows you to specify a URL and a set of prompts to more precisely control the behavior of the AI model.
url: The URL or context reference for the request, used to specify the data source that the AI model needs to process.
prompts: AI instructions or queries that guide the AI model on how to process the data.
deadline: The deadline for the request, after which the request will be considered expired.
Return value:
requestId
, a unique identifier for the request.
Using the request Function
This function allows you to specify only a URL, suitable for simple requests that don't need prompts.
url: The URL or context reference for the request, used to specify the data source that the AI model needs to process.
deadline: The deadline for the request, after which the request will be considered expired.
Return value: requestId, a unique identifier for the request.
Internal Function _request
Both requestWithPrompts
and request
functions call the internal function _request
to create and store requests.
This function primarily checks parameters and status, then temporarily stores the request and updates the request status.
Call Example
Step 4: Consensus Node Pre-processing Requests
For safer and more accurate request processing, the AegisAIOracle contract has designed a consensus network to pre-process user AI requests and submit them to the AI network once a consensus threshold is reached. This mechanism ensures result reliability and decentralization.
Role of Consensus Nodes
In the AegisAIOracle contract, consensus nodes consist of addresses with VALIDATOR_ROLE
permissions:
These validators must be authorized to participate in the request processing workflow.
Validator Response Process
Validators process requests by calling the response
function:
Consensus Mechanism
When enough validators respond to the same request, the system triggers the consensus mechanism:
In the code, after more than half of the validators respond, results can be aggregated and sent to the Endpoint.
Aggregate Results and Submit to AI Network
Once the consensus threshold is reached, the contract aggregates all responses provided by validators and builds a prompt containing all results:
This process primarily includes:
Using
PromptsBuilder
to create new prompts, adding all IPFS CIDs provided by validators to the prompts.Calling the
submitRequest
function ofAegisAIEndpoint
to send the aggregated request to the AI network.Recording the
endpointRequestHash
and updating the request status.
Step 5: Callback Process Function
Next, you need to implement the process
function. This function is key to integrating your application contract with AegisAI. After processing a request, AegisAIEndpoint calls this function and passes the results through the ResponsePacket
parameter.
ResponsePacket
: Contains response data returned by AegisAIEndpoint. Let's look at each field of ResponsePacket in detail:requestHash
:bytes32
type, the request hash returned by AegisAIEndpoint. You can use this hash to track the status of the request.payload
:bytes
type, contains the result data processed by the AI model. Typically, this data is an IPFS CID pointing to a result file stored on IPFS.status
:uint64
type, indicates the status of the request. 0 means the request failed, 1 means the request was successful. Be sure to check this field to ensure the request completed successfully.confirmations
:uint64
type, indicates the number of times the request has been confirmed.
Tip: In the process
function, first check the value of resp.status
. If resp.status
is 0
, it means the request failed, and you need to handle the error accordingly, such as logging an error or triggering a rollback. Only when resp.status
is 1
can you safely process the data in resp.payload
.
Responding to Request Results
When AegisAIEndpoint receives a response from the AI network, it automatically calls the process
function you implemented in Step 3. At this point, you can perform a series of validations and verify the data responded to by the AI network:
These validations include:
Verifying request validity
Checking if response status is successful (
resp.status == 1
)Confirming the request has not expired
Verifying the response payload is not empty
Processing Valid Response Results
If the response passes all validations, the contract extracts the IPFS CID from resp.payload
and stores it as the final result:
The AI network's response typically contains an IPFS CID pointing to the complete results stored on IPFS.
Completing Request Processing
After processing the response, the contract calls the _closeRequest
function to clean up request-related storage and update the status:
Querying Results
Once a request has been processed, users can query results using the query
function:
The contract provides three query methods:
Query directly by request ID
Query by URL (suitable for requests without prompts)
Query by URL and prompts combination (suitable for requests with prompts)
Complete Process Example
Let's illustrate the entire process with an example:
User calls
requestWithPrompts
to make a requestValidator nodes detect the request, process the data, and call the
response
function to submit resultsAfter reaching the consensus threshold, the request is submitted to AegisAIEndpoint
The AI network processes the request and returns results
AegisAIEndpoint calls the
process
function to return results to AegisAIOracleUsers can query the processing results
The result is an IPFS CID pointing to a file containing AI-generated content
Client applications can use this CID to retrieve the complete AI-generated content from IPFS
In this way, the AegisAI application implements a decentralized AI request processing workflow, from users initiating requests, to validators pre-processing and reaching consensus, to AI network processing, and finally returning results to the blockchain, forming a complete closed-loop system.
Last updated