Build with Onchain AI
Last updated
Last updated
Onchain AI Oracle empowers blockchain applications with seamless access to AI-powered services directly via smart contracts. Developers can easily integrate sophisticated AI processing capabilities into their smart contracts by interacting with the Onchain AI Oracle smart contract. The following section outlines how developers can practically build and integrate applications with the Onchain AI service, demonstrating the essential interactions and code patterns.
Browser wallet (e.g., MetaMask)
Open Remix IDE, create a new Solidity file named yourSmartContract.sol
, and paste the following interface definitions:
Below the interfaces, define your smart contract structure:
Add the method for requesting AI processing:
Now, add the method to fetch AI-generated responses:
Compile your contract:
Select Solidity compiler version 0.8.19
.
Click Compile.
Deploy your contract:
Choose your wallet network in MetaMask (e.g., Ethereum testnet).
In Remix's deployment tab, enter:
aiOracleAddress
: Address of your deployed Onchain AI Oracle contract.
Click Deploy, and confirm the transaction in MetaMask.
Interact using Remix IDE after deployment:
Request AI result:
Enter the prompt
text.
Choose responseType
:
0
for Text
1
for Image
2
for Audio
Click requestAIResult
and confirm transaction.
Fetch AI-generated response:
Get the requestId
from the ResultRequested
event emitted.
Enter the requestId
into fetchAIResponse(requestId)
to retrieve your AI-generated content.
Congratulations! You've successfully integrated your smart contract with the Onchain AI Oracle. By following these straightforward steps, you've set up a robust foundation that allows you to effortlessly leverage powerful AI capabilities—such as generating text, images, or audio—directly within your smart contracts. Happy building!
Remix IDE ()