Build Your First AI Agent Contract
Last updated
Last updated
COPYRIGHT © 2024 PHALA.LTD ALL RIGHTS RESERVED. May Phala be with you!
If you like video tutorials, here is one of our latest workshops on building your first AI Agent Contract. In this tutorial, you will learn:
How to get an OpenAI API Key at https://red-pill.ai
Clone the AI Agent Contract template repo
Build and test your Agent Script
Launch and interact with your Agent Script through the Phala Agent Gateway
The RedPill 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
🔒 Private: Host API keys and user privacy at ease
💎 Unstoppable: Powered by IPFS and Phala's 40k+ decentralized TEE workers
🔥 @hono/tiny Support: a small, simple, and ultrafast web framework built on Web Standards.
🧪 Vite Test Framework: Vite Testing Framework support, but you're free to change the test framework to your desire.
We recommend using @hono/tiny to avoid a large bundle size and the 20MB final artifact limitation.
This guide will focus on the following topics:
Build and Deploy Your AI Agent Contract
Build and deploy your Agent Contract that is deployed to IPFS and served through the Phala Agent Gateway executed on Phala Network
Use/Interact with Your AI Agent Contract
The Agent Gateway will fetch the Agent Contract code/prompt located on IPFS.
Interact with your agent hosted through Phala's Agent Gateway (https://wapo-testnet.phala.network/ipfs/<CID>).
Clone git repo or use degit to get the source code.
Install dependencies
Create .env
file with the default ThirdWeb API key for publishing your Agent Contract to IPFS
Get an API Key from Redpill
Note
There is a default RedPill API Key provided in the .env.example file. This API key is rate limited and if you run into an error that displays
Insufficient funds
, reach out to the Phala Team on discord.
Go to https://red-pill.ai/dashboard and claim your API Key by swapping some ETH for wGPT at https://app.uniswap.org/explore/tokens/base/0x74F62Bc1961028C22b8080961c6534f4eDD49D6C
Video Tutorial: https://youtu.be/ZoJwbLNhbWE
In default.json file replace YOUR_API_KEY
with your API Key. The default has a rate limit. If you want access to a RedPill code, reach out to the Phala Team.
Build your Agent
Test your Agent locally
Expected Test Results
Upload your compiled AI Agent code to IPFS.
Upon a successful upload, the command should show the URL to access your AI Agent.
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.
Here is an example:
Did Thirdweb fail to publish?
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.
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.
You can test it with curl
.
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.
Run command to set the secrets
Expected output:
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.
Here is an example:
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.
Expected output:
Check the Hono
docs for information on accessing URL queries
or body
from a post
request.
We recommend using @hono/tiny to avoid a large bundle size and the 20MB final artifact limitation.
Example
To debug your agent, you can use the following command:
After executing this command then you should see some output in the terminal to show the logs of requests to your agent.
To create logs in your Agent Contract, you can use the following syntax in your index.ts
file.
For more information check the MDN docs on console
object.
Run a local testnet with docker
support. All you need to do to get a local testnet started is run:
Running the local testnet may return an error if port 8000
is already in use.
Make a Request to Your Local Build
Add Secrets to Your Local Build
Check The Logs of Your Local Build
Congratulations! You have deployed and interacted with your first AI Agent Contract on Phala Network! Now let's move to a more Web3-centric agent to execute transactions onchain by importing the Viem SDK into the AI Agent Contract.