Build with Onchain AI
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.

Prerequisites
Remix IDE (https://remix.ethereum.org)
Browser wallet (e.g., MetaMask)
Step 1: Import Dependencies
Open Remix IDE, create a new Solidity file named yourSmartContract.sol, and paste the following interface definitions:
Step 2: Define Your AI Service Contract
Below the interfaces, define your smart contract structure:
Step 3: Implement AI Request Method
Add the method for requesting AI processing:
Step 4: Implement AI Response Fetching
Now, add the method to fetch AI-generated responses:
Step 5: Deploy Contract on Remix
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.
Step 6: Interacting with the Contract
Interact using Remix IDE after deployment:
Request AI result:
Enter the
prompttext.Choose
responseType:0for Text1for Image2for Audio
Click
requestAIResultand confirm transaction.
Fetch AI-generated response:
Get the
requestIdfrom theResultRequestedevent emitted.Enter the
requestIdintofetchAIResponse(requestId)to retrieve your AI-generated content.
Conclusion
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!
Last updated