Host your AI Agent Contract on Phala's decentralized serverless cloud.
Explore the docs Β»
View Demo
Β·
Report Bug
Β·
Discord
The 'mbd 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
Install dependencies
npm install
Create .env
file with the default ThirdWeb API key for publishing your Agent Contract to IPFS
cp .env.example .env
Build your Agent
npm run build
Test your Agent locally. Get a 'mbd Key from https://console.mbd.xyz/dashboard
npm run test
Expected Test Results
INPUT: {"method":"POST","path":"/ipfs/CID","queries":{},"secret":{"mbdApiKey":"mbd-7abc882c5c21647894fdcf0158ada934c9f8116db673759c5659614959005eb2"},"headers":{},"body":"{\"scoring\":\"like\",\"user_id\":\"12470\",\"top_k\":25,\"return_ai_labels\":true,\"return_metadata\":false}"}
POST RESULT: {
status: 200,
body: '{"body":[{"score":0.0007551,"labels":{"topics":["diaries_daily_life"],"sentiment":["positive"],"emotion":["joy","optimism"],"moderation":[]},"item_id":"0x83d7cd9ed67ee2e3ba4bd4dc5d73c142d15f473a"},{"score":0.0004989,"labels":{"topics":["business_entrepreneurs"],"sentiment":["negative"],"emotion":["joy"],"moderation":[]},"item_id":"0x46c3dd863f80e96e8d4a4132db425f79a4b64c77"},{"score":0.0004544,"labels":{"topics":["diaries_daily_life"],"sentiment":["positive"],"emotion":["joy","love","optimism"],"moderation":[]},"item_id":"0xc6a17393cf77e51f31eb1081a6a939cf9f95fe55"},{"score":0.0004505,"labels":{"topics":["food_dining"],"sentiment":["neutral"],"emotion":[],"moderation":[]},"item_id":"0xd85e34c8878105471e4e2f30a87088bbc3cb6059"},{"score":0.0004446,"labels":{"topics":["food_dining"],"sentiment":["positive"],"emotion":["joy"],"moderation":[]},"item_id":"0x7c9e8acb91e5f2f3f875c5cf4998801b01d5d1ac"},{"score":0.0003669,"labels":{"topics":["business_entrepreneurs","science_technology"],"sentiment":["positive"],"emotion":["joy","optimism"],"moderation":[]},"item_id":"0x3b38c9e59b3d316a1777d803070d88f36a1bfc51"},{"score":0.0003238,"labels":{"topics":["food_dining"],"sentiment":["positive"],"emotion":["joy","optimism"],"moderation":[]},"item_id":"0x9bf31fe5de25f423663db306820ef13e0584ad76"},{"score":0.0003202,"labels":{"topics":["business_entrepreneurs"],"sentiment":["neutral"],"emotion":[],"moderation":[]},"item_id":"0xb960ac130806814dc643b6528d7ab7244fde16b3"},{"score":0.0003061,"labels":{"topics":["food_dining"],"sentiment":["positive"],"emotion":["joy","optimism"],"moderation":[]},"item_id":"0x9e123b8a13918529c59c838affd8a0f4163e1233"},{"score":0.0003017,"labels":{"topics":[],"sentiment":["neutral"],"emotion":[],"moderation":[]},"item_id":"0x97906c211fa5f48d4377ddc1e2b5547e428b4c8e"},{"score":0.0002771,"labels":{"topics":[],"sentiment":["neutral"],"emotion":[],"moderation":[]},"item_id":"0x5581cfca4704754735962c72c01d4e768b9ba0c4"},{"score":0.0002708,"labels":{"topics":["sports"],"sentiment":["neutral"],"emotion":["optimism"],"moderation":[]},"item_id":"0x5b8a48d7c62eece2b08fb802f774f6484652cbd9"},{"score":0.0002556,"labels":{"topics":["business_entrepreneurs"],"sentiment":["positive"],"emotion":["anticipation","joy","optimism"],"moderation":[]},"item_id":"0x83dd9be074a55dfa186660085cae8e793e9d38cb"},{"score":0.0002548,"labels":{"topics":["fitness_health"],"sentiment":["positive"],"emotion":["optimism"],"moderation":[]},"item_id":"0x919add6dff1be577d0174155325ee173af58c64f"},{"score":0.0002489,"labels":{"topics":["food_dining"],"sentiment":["neutral"],"emotion":[],"moderation":[]},"item_id":"0xcc31a87c6bb481fb24b8572c1ff7cb7337792e64"},{"score":0.0002356,"labels":{"topics":["other_hobbies"],"sentiment":["positive"],"emotion":["joy","optimism"],"moderation":[]},"item_id":"0x8c4d6aedefc19a6faf9136198b091384bfdff682"},{"score":0.0002327,"labels":{"topics":["fitness_health"],"sentiment":["neutral"],"emotion":["joy"],"moderation":[]},"item_id":"0x102df553577d4b41126a0b35f2b0be9cb6d6bf31"},{"score":0.0002179,"labels":{"topics":["business_entrepreneurs"],"sentiment":["neutral"],"emotion":["anticipation"],"moderation":[]},"item_id":"0x0349c5fe7783eda17d73d6cb9d144970bb9c5368"},{"score":0.000215,"labels":{"topics":["sports"],"sentiment":["positive"],"emotion":["joy","optimism"],"moderation":[]},"item_id":"0x6fbfa668ca16a8717051762b1c5536068f6f0316"},{"score":0.000213,"labels":{"topics":["travel_adventure"],"sentiment":["positive"],"emotion":["joy","optimism"],"moderation":[]},"item_id":"0x513014dafb2df1097308620aa79430b98a924962"},{"score":0.0002117,"labels":{"topics":["diaries_daily_life","travel_adventure"],"sentiment":["neutral"],"emotion":["joy","optimism"],"moderation":[]},"item_id":"0x49aa4deb035cfb154db60a7c8a7fb28bec9fa398"},{"score":0.0002096,"labels":{"topics":["food_dining"],"sentiment":["positive"],"emotion":["joy","optimism"],"moderation":[]},"item_id":"0xff0745fbe03d3dc80a96b54a057c5198c2b03dfd"},{"score":0.000209,"labels":{"topics":["diaries_daily_life","fitness_health"],"sentiment":["positive"],"emotion":["joy","optimism"],"moderation":[]},"item_id":"0xea4e1c9f4cabddbee3a1d4970278db6e335a0e55"},{"score":0.0002025,"labels":{"topics":["sports"],"sentiment":["positive"],"emotion":["joy","optimism"],"moderation":[]},"item_id":"0xb125b56924cdf7f540a1dec556ed641baecc8a23"},{"score":0.0002013,"labels":{"topics":["arts_culture"],"sentiment":["neutral"],"emotion":["joy","optimism"],"moderation":[]},"item_id":"0xcf673cbe97a99f745901bfcd52d9ab6a116e3b7c"}]}',
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
}
Now you are ready to publish your agent, add secrets, and interact with your agent in the following steps:
- Execute: 'npm run publish-agent'
- Set secrets: 'npm run set-secrets'
- Go to the url produced by setting the secrets (e.g. https://wapo-testnet.phala.network/ipfs/QmPQJD5zv3cYDRM25uGAVjLvXGNyQf9Vonz7rqkQB52Jae?key=b092532592cbd0cf)
Upload your compiled AI Agent code to IPFS.
npm run publish-agent
Upon a successful upload, the command should show the URL to access your AI Agent.
β Compiled successfully.
1.36 KB dist/index.js
Running command: npx thirdweb upload dist/index.js
This may require you to log into thirdweb and will take some time to publish to IPFS...
$$\ $$\ $$\ $$\ $$\
$$ | $$ | \__| $$ | $$ |
$$$$$$\ $$$$$$$\ $$\ $$$$$$\ $$$$$$$ |$$\ $$\ $$\ $$$$$$\ $$$$$$$\
\_$$ _| $$ __$$\ $$ |$$ __$$\ $$ __$$ |$$ | $$ | $$ |$$ __$$\ $$ __$$\
$$ | $$ | $$ |$$ |$$ | \__|$$ / $$ |$$ | $$ | $$ |$$$$$$$$ |$$ | $$ |
$$ |$$\ $$ | $$ |$$ |$$ | $$ | $$ |$$ | $$ | $$ |$$ ____|$$ | $$ |
\$$$$ |$$ | $$ |$$ |$$ | \$$$$$$$ |\$$$$$\$$$$ |\$$$$$$$\ $$$$$$$ |
\____/ \__| \__|\__|\__| \_______| \_____\____/ \_______|\_______/
π thirdweb v0.14.12 π
- Uploading file to IPFS. This may take a while depending on file sizes.
β Successfully uploaded file to IPFS.
β Files stored at the following IPFS URI: ipfs://QmUG4mpRuqPAgWdAnv1mFWDXfnUfumiR7Xp76EqgLJr5VA
β Open this link to view your upload: https://b805a9b72767504353244e0422c2b5f9.ipfscdn.io/ipfs/bafybeicx7fwuavddpc3ihi5kb5mjg4glwz4vxwuf5fk7cpntjqf2k32aje/
Agent Contract deployed at: https://wapo-testnet.phala.network/ipfs/QmUG4mpRuqPAgWdAnv1mFWDXfnUfumiR7Xp76EqgLJr5VA
If your agent requires secrets, ensure to do the following:
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]'
Logs folder created.
Deployment information updated in ./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.Here is an example:
{ "date": "2024-08-29T20:28:20.081Z", "cid": "QmYzBTdQNPewdhD9GdBJ9TdV7LVhrh9YVRiV8aBup7qZGu", "url": "https://wapo-testnet.phala.network/ipfs/QmYzBTdQNPewdhD9GdBJ9TdV7LVhrh9YVRiV8aBup7qZGu" }
New to Thirdweb?
We use thirdweb Storage to host IPFS contents. If you are new to thirdweb, the command will guide you to create your account or login to your existing account from the browser.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.
THIRDWEB_API_KEY="YOUR_THIRDWEB_API_KEY"
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
.
curl https://wapo-testnet.phala.network/ipfs/<your-cid>
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, make sure to get a 'mbd Key from https://console.mbd.xyz/dashboard
- Edit the default.json file or create a new JSON file in the
./secrets
folder and add your secrets to it.
{
"mbdApiKey": "YOUR_MBD_API_KEY"
}
- Run command to set the secrets
npm run set-secrets
# or if you have a custom JSON file
npm run set-secrets <path-to-json-file>
Expected output:
Storing secrets...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 219 0 68 100 151 99 220 --:--:-- --:--:-- --:--:-- 319
{"token":"1e4d12a87503081d","key":"680665645b9f4e90","succeed":true}
Secrets set successfully. Go to the URL below to interact with your agent:
https://wapo-testnet.phala.network/ipfs/QmUG4mpRuqPAgWdAnv1mFWDXfnUfumiR7Xp76EqgLJr5VA?key=680665645b9f4e90
Log entry added to secrets.log
βΉοΈ 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:
2024-08-29T20:30:35.480Z, CID: [QmYzBTdQNPewdhD9GdBJ9TdV7LVhrh9YVRiV8aBup7qZGu], Token: [37a0f3f344a3bbf7], Key: [343e2a7dc130fedf], URL: [https://wapo-testnet.phala.network/ipfs/QmYzBTdQNPewdhD9GdBJ9TdV7LVhrh9YVRiV8aBup7qZGu?key=343e2a7dc130fedf]
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.
curl https://wapo-testnet.phala.network/vaults/<key>/<token>
Expected output:
{"data":{"mbdApiKey":"<MBD_API_KEY>"},"succeed":true}
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:
const query = req.queries.chatQuery[0] as string;
The example at https://wapo-testnet.phala.network/ipfs/QmcQbQzJop8m9uqGJTRmxVCEPoWtzx6NkNJRA12CZ6vFyT?key=b7fd2b6c4bf008cc&chatQuery=When%20did%20humans%20land%20on%20the%20moon will have a value of When did humans land on the moon
. queries
can have any field name, so chatQuery
is just an example of a field name and not a mandatory name, but remember to update your index.ts
file logic to use your expected field name.
To debug your agent, you can use the following command:
curl https://wapo-testnet.phala.network/logs/all/ipfs/<CID>
After executing this command then you should see some output in the terminal to show the logs of requests to your agent.
2024-09-04T03:18:34.758Z [95f5ec53-3d71-4bb5-bbb6-66065211102c] [REPORT] END Request: Duration: 166ms
2024-09-04T03:18:34.758Z [95f5ec53-3d71-4bb5-bbb6-66065211102c] [INFO] 'Is signature valid? ' true
2024-09-04T03:18:34.758Z [95f5ec53-3d71-4bb5-bbb6-66065211102c] [INFO] 'Verifying Signature with PublicKey ' '0xC1BF8dB4D06416c43Aca3deB289CF7CC0aAFF540'
2024-09-04T03:18:34.758Z [95f5ec53-3d71-4bb5-bbb6-66065211102c] [REPORT] START Request: GET /ipfs/QmfLpQjxAMsppUX9og7xpmfSKZAZ8zuWJV5g42DmpASSWz?key=0e26a64a1e805bfd&type=verify&data=tintinland%20message%20to%20sign&signature=0x34c4d8c83406e7a292ecc940d60b34c9b11024db10a8872c753b9711cd6dbc8f746da8be9bc2ae0898ebf8f49f48c2ff4ba2a851143c3e4b371647eed32f707b1b
2024-09-04T03:17:15.238Z [768b6fda-f9f1-463f-86bd-a948e002bf80] [REPORT] END Request: Duration: 183ms
2024-09-04T03:17:15.238Z [768b6fda-f9f1-463f-86bd-a948e002bf80] [INFO] 'Signature: 0x34c4d8c83406e7a292ecc940d60b34c9b11024db10a8872c753b9711cd6dbc8f746da8be9bc2ae0898ebf8f49f48c2ff4ba2a851143c3e4b371647eed32f707b1b'
2024-09-04T03:17:15.238Z [768b6fda-f9f1-463f-86bd-a948e002bf80] [INFO] 'Signing data [tintinland message to sign] with Account [0xC1BF8dB4D06416c43Aca3deB289CF7CC0aAFF540]'
2024-09-04T03:17:15.238Z [768b6fda-f9f1-463f-86bd-a948e002bf80] [REPORT] START Request: GET /ipfs/QmfLpQjxAMsppUX9og7xpmfSKZAZ8zuWJV5g42DmpASSWz?key=0e26a64a1e805bfd&type=sign&data=tintinland%20message%20to%20sign
2024-09-04T03:16:38.507Z [3717d307-bff0-4fc0-bc98-8f66c33dd46f] [REPORT] END Request: Duration: 169ms
2024-09-04T03:16:38.507Z [3717d307-bff0-4fc0-bc98-8f66c33dd46f] [REPORT] START Request: GET /ipfs/QmfLpQjxAMsppUX9og7xpmfSKZAZ8zuWJV5g42DmpASSWz?key=0e26a64a1e805bfd
2024-09-04T03:15:00.375Z [793f58f9-f24f-4580-8ebc-04debb7d727f] [REPORT] END Request: Duration: 158ms
2024-09-04T03:15:00.375Z [793f58f9-f24f-4580-8ebc-04debb7d727f] [REPORT] START Request: GET /ipfs/QmfLpQjxAMsppUX9og7xpmfSKZAZ8zuWJV5g42DmpASSWz?key=0e26a64
a1e805bfd
To create logs in your Agent Contract, you can use the following syntax in your index.ts
file.
// info logs
console.log('info log message!')
// error logs
console.error('error log message!')
For more information check the MDN docs on console
object.
What packages can I use in the AI Agent server?
- Most of the npm packages are supported: viem, onchainkit, β¦.
- Some packages with some advanced features are not supported:
- Memory usage over 100MB
- Web Assembly
- Browser only features: local storage, service workers, etc
Whatβs the spec of the Javascript runtime?
- The code runs inside a tailored QuickJS engine
- Available features: ES2023, async, fetch, setTimeout, setInterval, bigint
- Tech spec doc
Why is the serverless platform secure?
- Your AI Agent code on is fully secure, private, and permissionless. Nobody can manipulate your program, steal any data from it, or censor it.
- Security: The code is executed in the decentralized TEE network running on Phala Network. It runs code inside a secure blackbox (called enclave) created by the CPU. It generates cryptographic proofs verifiable on Phala blockchain. It proves that the hosted code is exactly the one you deployed.
- Privacy: You can safely put secrets like API keys or user privacy on Phala Network. The code runs inside TEE hardware blackboxs. The memory of the program is fully encrypted by the TEE. It blocks any unauthorized access to your data.
- Learn more at Phala Network Homepage