Advanced Usages of OAO
In this tutorial we'll modify Prompt contract to support nested inference request. NestedCallback contract should execute multiple inference requests in 1 transaction. In our example, it will call Llama3 model first, then use inference result as the prompt to another request to StableDiffusion model.
The main goal of this tutorial is to understand what changes we need to make to Prompt contract in order to implement logic for various use cases.
Prerequisites
Good understanding of Interaction with OAO - Tutorial
Use Cases
Some of the use cases for nested inference call could be:
generate the prompt with LLM for AIGC (AI Generated Content) NFT
extract data from the data set then visual data with different models
add transcript to video, then translate it to different languages with different models
💡 This is executed within a single transaction, which simplifies user experience when interacting with your AI dapp.
Implementation Steps
modify
CalculateAIResult
method to support multiple requestsmodify
aiOracleCallback
with the logic to handle second inference request
💡 When estimating gas cost for the callback, we should take both models into the consideration.
CalculateAIResult
As we now have additional function parameter for second model id. Not that we encode and forward model2Id
as a callback data in aiOracle.requestCallback
call.
aiOralceCallback
The main change here is the within "if" block. If the callback data (model2Id
) is returned, we want to execute second inference request to OAO.
Output from the first inference call, will be passed to second one. This allows for interesting use cases, where you can combine text-to-text (eg. Llama3) and text-to-image (eg. Stable-Diffusion) models.
If nested inference call is not successful the whole function will revert.
💡 When interacting with the contract from the client side, we need to pass cumulative fee (for both models), then for each inference call we need to pass part of that cumulative fee. This is why we are calling estimateFee
for model2Id
.
Interaction with Contract
This is an example of contract interaction from Foundry testing environment. Note that we're estimating fee for both models and passing cumulative amount during the function call (we're passing slightly more to ensure that the call will execute if the gas price changes).
Conclusion
In this tutorial we explained what functions need to be changed to implement different logic in your Prompt contract. Depending on the use case, new state variables and events could be defined as well.
You can also check the full implementation of PromptNestedInference.
Last updated