How to build a AI agent on blockchain by Frank Kong

ETHDubai 2025 · 29 Apr 2025 · 27:13

Frank Kong explains how to build an AI agent on a blockchain.

Speakers

Topics

From ETHDubai 2025, hosted by DeFi Dubai.

Transcript

Welcome, welcome to this workshop and good morning. Good morning. So uh in this workshop I will going to spend like a 25 minutes to share with you how to build an AI agent on the blockchain and make this AI agent can receive the messengers the natural language input from the users from the Twitter as a client and then analyze this message and do the do and trigger the actions which is supposed to be triggered. And so before we start I just uh want to make a brief introduction of myself and uh my company chain link. So my name is Frank and I am a developer relation engineer

from the chain link labs and the chain link lab uh sorry uh this one. So for I'm sorry that is okay it's coming. So for chain link labs for those who have ever never heard about this company the chain link is a oracle company we have a decentralized network of oracles and uh our job is to move the data from the web to world like data and the computation resources to the onchain smart contract. So if you want to get the token price, you want to get the data from the server, you want to get the data from the database or whatever you want to get, you just need

to write a JavaScript or any code and send this request to the chain link and the chain link can get whatever you want and uh do the aggregation and feed this result back to the onchain smart contract. It will make your application as a flexible as any web two app is today. So you can actually achieve or implement any business logic on the web 3 world. Okay. So let's start. And by the way because the time is a little bit limited so I will not do the whole demo.

This is a URL to the to this gbook and I list all the details about how to build this AI agent and how to make it work in this gatebook. You can do it from the scratch step by step. It is super easy. Actually I I think you can finish that in the 30 minutes and if there is any hackathon you want to use this uh uh the AI agent as your idea and this could be your framework. You can build your own AI agent based on the framework. Okay cool. So uh first question is uh what is a AI agent and why we want to build an AI agent? So the AI agent comes to the web3 world I think mainly for the abstraction layer. So

usually when we when we build a web3 application and then we want everyone else to come and use our uh web3 applications. The biggest problem and the biggest barrier is that the web 3 app is a little bit too complicated and they need to know the wallet. They need to know the private key. They need to know how to send the transactions to the correct address to the correct functions. And uh if the user do it not in correct way, I think something bad will happen. The security will be compromised. That is a super risky and super difficult for the regular users.

Think about the internet 20 years back. If you ask a user, the regular user to input the IP address and construct all the HTTP request or any protocol request by himself or herself. it is going to be a problem because no one know how to do that. So we build the explorer, we build some buttons, we have some text input, we have the web page to allows the users to use our app on the web on the internet. But for the uh blockchain, for the web3 app, we still in this process.

So AI agent it I think you can understand the AI agent as a assistant or a user friendly UI the user experience to allow the regular user to interact with your web3 application in a very simple way. They just need to input the nature language input like a hey I want to trade my token A to the token B and you just need to type this sentence and send it from the Twitter or from the discord or from the telegram and then the AI agent will understand it and to say which actions need to be triggered by your input and then the the actions will be triggered. So user don't need to know anything about the web 3. They just

need to input what they want to do and that can be achieved and that is a abstraction layer to just abstract all the complexities provided by the web3 provided by the blockchain and that is what we want to build. So in order to build a uh AI agent uh there are some tools maybe you can consider to use and first if you want to build a AI engine agent you need to a framework of the AI agent and in the framework we need three things. The first one is a client you need to provide a interface for the users to use. Maybe you just run a Twitter account. This Twitter account can be your uh user interface and people can input uh from their Twitter account

and tag your account and input what whatever they want and this is called the client. The second thing is LLM the large language model that is the brain of this AI agent. All the messengers from the user should be analyzed and understood by the LLM and LLM will know which action need to be triggered and what to do with a user's request. And the third thing is the action or plug-in. You can define some plugins or actions that can be done as a capability or functionality as your AI agent. For example, not all AI agents can handle the web3 transactions. Just some of them

can do that. So in order to define your AI agent, actually you are define the functionality of this AI agent. So with these three requirements uh there is a framework called Alisa. uh that is a framework provided by the AI6Z and that is a opensource project uh which is contributed by a lot of the individual developers and with this framework you can easily define the clients define the LLM and define the actions you want to do and the next one is chaining function as I mentioned before the chain link is a Oracle service and based on our Oracle network we provide a lot of the different service and the chain functions is one

of the chain link services. The chain link functions you can send a request to the DO the decentralized oracle network and the decentralized Oracle network will receive that request and execute this request within its own environment and all the nodes within this network will do the same thing. Maybe get some data from the database or maybe do some computation which is expensive on the blockchain. And after this uh uh script is executed, every node will get a result and they will aggregate these results together to generate a report with their signatures and this final report will be submitted to the onchain

smart contract. The onchain smart contract which is deployed by the chain link will verify if this requ if this report is valid or not. If this report is valid then this data can be consumed by the onchain smart contract from other consumers and that is the role of the chaining functions. So next important question we need to answer is why do we need chaining functions in our AI agent? Because uh in the process like you you can imagine that you use the Twitter to input some message to this AI agent and AI agent will has its own private key and the agent will use its own private key to

construct a transaction with a signature of this private key and this transaction can be sent by the AI agent itself and then the transaction will be executed on the onchain smart contract. Why do we still need the chain functions to relay the data from the database to the onch smart contract? Well, the re think about the whole reason we want to use the blockchain. We want to use a decentralized network to build our application is because we want our application to be built on the network which is a very transparent, tamperproof and decentralized. it is a fair to everyone and uh if you rely on the single point to send a transaction from

the offchain world it could be risky right if something bad happens to the AI agent if the LLM does not make the correct analyze analysis maybe the wrong transactions could be sent from the AI agent so we need a lay we need another layer to verify if the transaction is valid or not so in the AI agent And we have the database to save some data. And we have some logics on the smart contract to check if the transactions from the AI agent is valid or not. And if we want to check if the transaction is valid or not, we need the data from the database. And the chaining functions will help you to do this to fetch the data from the database to the onchain

smart contract in uh in a very decentralized manner. And the whole process can be verified on the blockchain by anyone. Okay, let's see the very high level architecture. Uh click that. Okay, cool. Uh so this is the architecture we are going to build. Actually I'm not go going to build in this 20 minutes but I list every details on the gate book. So we have a three domains. The first one in the left hand that is the front end.

I mentioned in the front end we only need a Twitter account and then we connect our AI agent to this Twitter account as a client and the user can input anything they want from from their Twitter to this AI agent connected Twitter account and this nature language input can be analyzed and understood by the brain of the AI agent that's going to be LLM. You can use the open AI or you can use the anthropic or maybe the grow any one you like. And when this brain just see there is some specific action need to be triggered based on the input you submit then this action will

be triggered and we have the selfdefined plug-in in the framework and then this uh AI agent will construct a new transaction. This transaction will have all the necessary data like a smart contract address, the function name and everything we need to send a transaction. And then the transaction will be sent to this one the second domain the blockchain is this uh the back end for the asset and there is a smart contract called a chain link fun uh chain link function consumers and this function will have a two main function uh this smart contract I'm sorry has two main functions send request and fulfillment so the send request will collect all the necessary

data and emit an event this event is actually a request because it will be detected by the DO here. It is a decentralized oracle network uh from the chain link and if there is an event emitted the do will detect it and then it will say oh someone submit a request and uh this JavaScript code or any code you want the D to do will be sent to this D and the D will run this script maybe get data maybe do the computation for example in this scenario the D will fetch the data from the database and to check if This record matches user input in the Twitter. And if these two records

match, it will return the result to say, "Oh, good. You can just uh uh pass this request." And then the chain function consumer smart contract will receive the result to see the request is valid from the user and a new NFT will be minted to this user's address. So from the very first to the very uh very end of the process the user just need to submit the nature language input here and then he or she will get a NFT on the blockchain.

They don't need to install MetaMask. They don't need to know the uh know the how to how to construct a transaction. they don't need to even click a button and by the AI agent uh they can submit their request very easily and this will help you to uh just allow more users more end users to use your application and even more I I didn't do in this demo but maybe you can consider use an AA wallet and with that user don't need to need a public key never they just need to submit their request but for this use case for the gbook I still use a public key, but you can just extend it if you want.

Okay. Let me go. Yeah, here are I list some prerequisites and the setup. You just need a NodeJS and you you need to install the PMP on your machine and get some test tokens for the Avalanche Fuji or if you get you can get any uh tokens on on some other test nets, it's okay. But this test net should be supported by chain link functions because the chain link uh does not support all chains. It has a list of the supported chains. You can check that on the chain official documentation. The URL is a docs.chain.link link and super easy to remember and you can check all the supported the

blockchains uh from the chain link and also you because we need to use LLM you need a LM uh large language model API key and the Google Gemini is a is free and if you have the open AI or if you have a group you can still use the API key from other large language model and uh yeah I will go go through all the process has to create uh to create this AI agent. It's it's super easy. And the first one because we uh in this scenario we save some gift and gift secret code in our database and we can share this secret code to the friends or to our users and a user who knows the

secret code and uh uh has a Twitter. Uh this user can submit a sentence like oh please just give me the NFT to this address. My secret code is blah blah blah. And this message will be analyzed by the AI agent. If the data is correct, if the secret code submitted by the user matches the secret code saved in the database, the AI agent will know that is a valid result and and the chaining functions will run and get the and call the mint function of the USC 721 and the M NFT for this user's address.

And in order to have the database I used the superbase uh it is a light version of the firebase. You you can if you have a AWS account you can al also use your AWS Dynamo DB or any database to create a table to save three columns the primary key the gift name and a gift code and create a table to insert some some value into this table. And uh uh what I inputed here is uh I have a three gift the one month premium, $50 discount, $100 discount. And for each gift, I will have the secret code for each of them. And um if the user know

this secret code, I'm really sorry about that. Uh if the user know the secret code of a specific gift and a user need to user can input this secret code and uh his address in the input and uh it this will be submitted to the AI agent and this is where the secret code will be checked by the chaining functions because uh the chinking functions need to verify if the transactions submitted by the AI agent is valid or not. So it's a super easy and create account in the any your favorite database and create a table with three columns the primary key the gift name and a gift secret code and

that's it and after the table is created you need to promise this table this the records saved in this table can be accessed by the external users usually in the superbase we have the role level security and uh you need to set it as a read permission permitted for everyone And if you use another database like in the Dynamo DB in the AWS you you you can choose this access with AM account and set some permission group and make sure this data can be accessed by the outside users. And after that uh in the second step we need to deploy the smart contract. This part this smart contract will have the two features. Uh the first

feature is the chain link features. So this smart contract will inherit these two. The first one is a function client. Uh as I mentioned below before in that architecture, if you want to send and receive the request and feedback uh and result from the chain link functions, you need to promise your smart contract at least has these two functions. The first one is a send request. This one and you need to uh just uh tell the tell the training functions what you want to do and you can write this code in the JavaScript. I hardcoded here. This is a JavaScript

code but I I set it as a string in the solidity. You can see that uh I just make a HTTP request and I give it uh my URL to the superbase I just created in the last step. and I use the get method and then I can also define how I want to handle the results from the database. I will see that u use a find and to encode this result with a string data type and then return this encoded data back to the onchan smart contract and this data this uh I'm sorry this source code will be executed in the environment of the chaining functions the chaining

functions is consists of the multiple nodes the oracle node every oracle node will run this script submitted by you And then the result will be uh sent back to this function the fulfill request. The second important function you need have in the chain uh chaining function client smart contract and it will you will get this response as a parameter and then you can do whatever you want to this response and to see if the response is a valid signal or invalid signal. If that is a valid signal, it will be uh the the safe mint will be called and that is a second smart contract we are going to inherit.

Make sure this function is a NFT smart contract. Uh the NFT can be minted within this smart contract and then the NFT will be minted under the user's address. if that uh secret code matches the I mean the secret code submitted by user and a secret code saved on the database matches. Okay. So this is a smart contract you can build and you can implement any logics any business logic you want to implement in the send and the fulfill uh functions and you don't need to follow everything I write here because sometimes you may maybe you want to add a action to transfer the token from the blockchain A to the blockchain B or do some do some trade trade token A

to the token B or maybe do something else you don't need to follow everything I just uh want to let everyone know that how you can implement this uh functions and business logic with the chain functions. And the next one is to uh because uh to register the uh chain consumer smart contract on the chain link subscription because you want to use the service from the chain link. So you need to pay for that. The way to pay for that is to create a subscription under the chaining functions and add a balance add some tokens native token or link token as a balance for this subscription and then to add your smart contract as a consumer in that subscription list. So it it will make

sure the chain functions the balance in the chic function subscription can be consumed by the uh smart contract in that consumer list. Okay, after that is done, another important topic is that how we can expose the sensitive data to the chic functions because you want to chic functions to uh get the data from the database but the database has some permission group and it cannot be accessed by anyone. It only can be accessed by the person who can provide API key. But this API key cannot be written on the smart contract. Right? If you write a API key on a smart contract that is transparent and everyone else

will know what is API key. So the way to upload our API key or any other sensitive data to the chain functions is to first encrypt this API key or the sensitive data with a public key from the DO from the chain functions and then this secret will be encrypted secret and after that you can upload this encrypted secret to the D or to your own server that can be accessed by the chain functions and if anyone want to use this secret, this encrypted secret, this person should have the public uh the private keys of all the Oracle node in the network. So that will make your

sensitive data super super secure and that is the first the sensitive data firstly cannot be accessed by anyone. Second, if anyone get it, uh this secret cannot be decrypted. Okay, we have a official script to help you to uh manage the script to upload this script and to to just let the do know what where to find it. And I have all the codes within that GitHub repo. You can just go to this one and uh you you have this URL so you know where to get it. Okay, after all of this is set, you can start the AI agent. just uh use the pmppm start to start a AI agent and that is running and

uh you need to collect the Twitter as a client for this AI agent and when it is uh started you will see from your Twitter like AI agent starts at some time step and then you can tag this Twitter to just input the thing please give me the gift on the address blah blah blah and the gift code is this one. And when this message is sent from you, it will be a memory of this AI agent and the brain of the AI agent LLM will use a prompt engineering to understand what you really want to do. And uh if you add a prompt engineering like if the user

input something like this you need to you need to you don't need to do it exactly the same thing but just let the AI agent to know that what kind of the message will trigger which action and that's it and the AI agent will know when when this message is sent from the Twitter the AI agent will know oh the action like uh mint NFT should be called or get gift should be called and it will We uh call the chic functions to uh get a uh data from the database to check if the two these two two pieces of data matches or not. If that is a match, the AI agent will send a transaction and uh return the transaction hash ID and other

details back to the Twitter and your end user will know if that is successful or not. And next you can check that if this request is good or not in the training functions UI and uh finally if that is uh correct if everything is good is valid you will see that the NFT will mint it for your address. So the whole process is super easy for the users. They just need to type what they want to do from a social media and the AI agent will receive this message and understand that and decide which action should be triggered and then if the result is good the action is triggered and the result will be will will be good and the user

will get what they want and except for the everything I talked today you can also just implement what you want in your own smart contract. For example, you want to do a uh a AA wallet, you want to do a um bridge, you want to do a uh uh automatic trading, you can just uh expose your functionality, your capabilities through the AI agent to the regular users. And uh and then it's uh that's it. That is uh pretty much I want to talk today and I will be around if you have any questions or something you want to talk with me. uh please just uh feel free to come uh to come to me and I'm glad if we can talk about the technologies and uh thanks for your time

and thanks for your patience. Thank you.