The OpenAI AI Agent template is a MINIMAL template to build an AI Agent that can be hosted on Phala Network's decentralized hosting protocol. Unlike Vercel or other FaaS, it allows you to publish your AI Agent compiled code to IPFS and hosts it on a fully decentralized FaaS cloud with the following benefits:
💨 Ship Fast: Build and ship with familiar toolchain in minutes
⛑️ Secure: Execution guarded by rock solid TEE / Intel SGX
🔒 Private: Host API keys and user privacy at ease
💎 Unstoppable: Powered by IPFS and Phala's 35k+ decentralized TEE workers
Getting Started
For this template to work, you will need to signup for a developer account on OpenAI and get and OpenAI API Key.
Prepare
Clone git repo or use degit to get the source code.
Create .env file with the default ThirdWeb API key for publishing your Agent Contract to IPFS
cp.env.example.env
Build your Agent
npmrunbuild
Test your Agent locally
npmruntest
Expected Test Results
INPUT: {"method":"GET","path":"/ipfs/CID","queries":{"chatQuery":["When did humans land on the moon?"],"openAiModel":["gpt-4o"]},"secret":{"openaiApiKey":"YOUR_API_KEY"},"headers":{}}
GETRESULT:{status:200, body: `{"message":"Humans first landed on the moon on July 20, 1969, during NASA's Apollo 11 mission. Astronauts Neil Armstrong and Edwin \\"Buzz\\" Aldrin became the first and second humans to walk on the lunar surface while Michael Collins remained in lunar orbit aboard the Command Module. Neil Armstrong famously declared, \\"That's one small step for man, one giant leap for mankind,\\" as he stepped onto the moon."}`,
headers:{'Content-Type':'application/json','Access-Control-Allow-Origin':'*' }}INPUT: {"method":"POST","path":"/ipfs/CID","queries":{"chatQuery":["When did humans land on the moon?"],"openAiModel":["gpt-4o"]},"secret":{"openaiApiKey":"YOUR_API_KEY"},"headers":{},"body":"{}"}
POSTRESULT:{status:200,body:'{"message":"Not Implemented"}',headers:{'Content-Type':'application/json','Access-Control-Allow-Origin':'*' }}Nowyouarereadytopublishyouragent,addsecrets,andinteractwithyouragentinthefollowingsteps:-Execute:'npm run publish-agent'-Setsecrets:'npm run set-secrets'- Go to the url produced by setting the secrets (e.g. https://wapo-testnet.phala.network/ipfs/QmPQJD5zv3cYDRM25uGAVjLvXGNyQf9Vonz7rqkQB52Jae?key=b092532592cbd0cf)
Publishing Your Agent
Upload your compiled AI Agent code to IPFS.
npmrunpublish-agent
Upon a successful upload, the command should show the URL to access your AI Agent.
✓Compiledsuccessfully.76.86KBdist/index.jsRunningcommand:npxthirdwebuploaddist/index.jsThismayrequireyoutologintothirdwebandwilltakesometimetopublishtoIPFS... $$\ $$\ $$\ $$\ $$\ $$ | $$ |\__| $$ | $$ | $$$$$$\ $$$$$$$\ $$\ $$$$$$\ $$$$$$$ |$$\ $$\ $$\ $$$$$$\ $$$$$$$\ \_$$_| $$ __$$\ $$ |$$ __$$\ $$ __$$ |$$ | $$ | $$ |$$ __$$\ $$ __$$\ $$ | $$ | $$ |$$ |$$ |\__|$$ / $$ |$$ | $$ | $$ |$$$$$$$$ |$$ | $$ | $$ |$$\ $$ | $$ |$$ |$$ | $$ | $$ |$$ | $$ | $$ |$$ ____|$$ | $$ |\$$$$|$$ | $$ |$$ |$$ |\$$$$$$$|\$$$$$\$$$$|\$$$$$$$\ $$$$$$$ |\____/ \__|\__|\__|\__|\_______|\_____\____/ \_______|\_______/💎thirdwebv0.14.12💎-UploadingfiletoIPFS.Thismaytakeawhiledependingonfilesizes.✔SuccessfullyuploadedfiletoIPFS.✔FilesstoredatthefollowingIPFSURI:ipfs://QmdBwRk3YG78thoe9phf43CLAzShYUEiPHnWBu6bkqsJVY✔ Open this link to view your upload: https://b805a9b72767504353244e0422c2b5f9.ipfscdn.io/ipfs/bafybeig4up5mjx6kvkm5dizsv2njpkzk7pileis3kfheyd5wff2m3wbtrm/
AgentContractdeployedat:https://wapo-testnet.phala.network/ipfs/QmdBwRk3YG78thoe9phf43CLAzShYUEiPHnWBu6bkqsJVYIfyouragentrequiressecrets,ensuretodothefollowing:1) Edit the ./secrets/default.json file or create a new JSON file in the ./secrets folder and add your secrets to it.2) Run command: 'npm run set-secrets' or 'npm run set-secrets [path-to-json-file]'Logsfoldercreated.Deploymentinformationupdatedin./logs/latestDeployment.json
Note that your latest deployment information will be logged to in file ./logs/latestDeployment.json. This file is updated every time you publish a new Agent Contract to IPFS. This file is also used to get the IPFS CID of your Agent Contract when setting secrets for your Agent Contract.
If ThirdWeb fails to publish, please signup for your own ThirdWeb account to publish your Agent Contract to IPFS. Signup or login at https://thirdweb.com/dashboard/
Whenever you log into ThirdWeb, create a new API key and replace the default API Key with yours in the .env file.
THIRDWEB_API_KEY="YOUR_THIRDWEB_API_KEY"
Accessing The Published Agent
Once published, your AI Agent is available at the URL: https://wapo-testnet.phala.network/ipfs/<your-cid>. You can get it from the "Publish to IPFS" step.
By default, all the compiled JS code is visible for anyone to view if they look at IPFS CID. This makes private info like API keys, signer keys, etc. vulnerable to be stolen. To protect devs from leaking keys, we have added a field called secret in the Request object. It allows you to store secrets in a vault for your AI Agent to access.
To add your secrets,
Edit the default.json file or create a new JSON file in the ./secrets folder and add your secrets to it.
{"apiKey":"YOUR_OPENAI_API_KEY"}
Run command to set the secrets
npmrunset-secrets# or if you have a custom JSON filenpmrunset-secrets<path-to-json-file>
Note that all your secrets will be logged in file ./logs/secrets.log. This file is updated every time you add new secrets to your Agent Contract. If you have not published an Agent Contract, yet, this command will fail since there is not a CID to map the secrets to.
The API returns a token and a key. The key is the id of your secret. It can be used to specify which secret you are going to pass to your frame. The token can be used by the developer to access the raw secret. You should never leak the token.
To verify the secret, run the following command where key and token are replaced with the values from adding your secret to the vault.
To help create custom logic, we have an array variable named queries that can be accessed in the Request class. To access the queries array variable chatQuery value at index 0, the syntax will look as follows: