Power Your dApp with ChatGPT
Gelato Team
•
Jun 27, 2023

The combination of AI and Web3 has the potential to address existing challenges in the field of AI while unlocking numerous innovative opportunities. By bringing AI onto the blockchain, smart contracts gain the ability to make autonomous decisions and predictions based on dynamic on-chain data. This can lead to entirely new functionalities, all without human input. AI agents may also use crypto infrastructure for payments and to access digital resources like storage, compute, and bandwidth.
For example, imagine an AI programmed to write code for companies. This AI, working 24/7, could be employed through blockchain-based smart contracts and automatically paid for each pull request it successfully merges. This not only shows a real-world application but also signals a new era in software development.
Across sectors, this union of AI and Web3 is already creating ripples. In DeFi, machine learning powers predictive analysis and dynamic parameter adjustments. In gaming, AI enhances player experiences through adaptive content. In social media, AI drives automated posting and personalized content feeds. These are just a few examples of what’s possible.
In this tutorial, we’ll explore how Gelato’s Web3 Functions enable smart contracts to harness the OpenAI ChatGPT API for generating creative content. With Web3 Functions, smart contracts can seamlessly access off-chain data. In our demo application, Gelato Web3 Functions connect to the OpenAI API to generate content and automate posts every 8 hours.
AI-enabled Content Posting
The workflow begins with the LensGelatoGPT smart contract, which manages prompts for AI content creation. It communicates with LensHub.sol
, the core Lens Protocol contract, to ensure actions are performed by the rightful profile owners.
Gelato Web3 Functions then:
Fetch prompts from LensGelatoGPT
Generate content via OpenAI
Validate metadata with LensClient
Upload metadata to IPFS via Web3.Storage
Call
LensHub.post()
to publish content on Lens
The result: automated AI-generated content appears on Lenster, a social app built with Lens Protocol — all executed by Gelato Web3 Functions.
Prerequisites
Access to Gelato Web3 Functions beta
Installed Node.js v16+, NPM, and Git
A Web3 wallet (e.g. MetaMask)
Accounts on Alchemy, Web3.Storage, and OpenAI
Set Up Your Dev Environment
Configure Secrets
Fill in the following in your .env
file:
Alchemy API ID
Private Key from your wallet
OpenAI API Key
Web3.Storage API Key
LensGelatoGPT Smart Contract
The contract validates profile ownership and manages prompts:
Other functions include stopPrompt()
, getPaginatedPrompts()
, and collectFee()
.
LensGelatoGPT Web3 Function
This Web3 Function fetches prompts, calls OpenAI to generate text, uploads the result to IPFS, and posts content to LensHub.
Deploy & Run
Deploy the contract on Polygon Mumbai:
Deploy the Web3 Function:
You’ll get an IPFS CID. Use it to create a new task in the Gelato dashboard: https://beta.app.gelato.cloud/new-task
Create a Web3 Function Task via UI
Go to the provided link with your CID.
Enter user arguments (
lensGelatoGPT
,lensHubAddress
,collectModule
).Select the network (e.g. Mumbai).
Add secrets (Web3.Storage and OpenAI keys).
Name the task and confirm creation with your wallet.
Monitor Your Task
Use the Web3 Functions Task dashboard to track:
Executions per run
Logs from your code
Storage state and secrets
Conclusion
We explored how to integrate AI and blockchain by combining OpenAI, Web3.Storage, Lens Protocol, and Gelato. This workflow enables automated, AI-powered content generation and posting — a tangible example of the convergence of AI and Web3.
Happy coding, and welcome to the internet of tomorrow!
Dive Deeper with Gelato
Web3 Functions let developers create serverless, decentralized applications with ease.
Join our community on Discord
Try Web3 Functions today in private beta
Read the Web3 Functions documentation
Follow our in-depth tutorial
Apply here to test Gelato’s Web3 Functions