PageSourceSearch

https://innovationlab.fetch.ai/resources/assets/js/a24c03f1.59239c48.js

js fetch.ai collected 2026-09-24 09:39:30 UTC 9,833 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunkinnovation_labs=self.webpackChunkinnovation_labs||[]).push([[1769],{51630:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>c,contentTitle:()=>r,default:()=>l,frontMatter:()=>o,metadata:()=>a,toc:()=>d});const a=JSON.parse('{"id":"agent-communication/uagent-uagent-communication","title":"uAgent to uAgent Communication","description":"In the uAgents Framework, agents can be triggered in multiple ways using different types of handlers. These handlers act as decorators, enabling agents to execute specific functions based on predefined conditions. In this section, we will explore two handlers. Below are the available handlers which can be used with uAgents:","source":"@site/versioned_docs/version-1.0.2/agent-communication/uagent-uagent-communication.md","sourceDirName":"agent-communication","slug":"/agent-communication/uagent-uagent-communication","permalink":"/resources/docs/1.0.2/agent-communication/uagent-uagent-communication","draft":false,"unlisted":false,"tags":[],"version":"1.0.2","frontMatter":{"id":"uagent-uagent-communication","title":"uAgent to uAgent Communication"},"sidebar":"tutorialSidebar","previous":{"title":"AI Agent Creation","permalink":"/resources/docs/1.0.2/agent-creation/sdk-creation"},"next":{"title":"AI Agent to AI Agent Communication","permalink":"/resources/docs/1.0.2/agent-communication/sdk-sdk-communication"}}');var s=t(74848),i=t(28453);const o={id:"uagent-uagent-communication",title:"uAgent to uAgent Communication"},r="uAgent \u2194 uAgent Communication",c={},d=[{value:"on_event",id:"on_event",level:2},{value:"on_event("startup")",id:"on_eventstartup",level:3},{value:"on_event("shutdown")",id:"on_eventshutdown",level:3},{value:"on_message()",id:"on_message",level:2},{value:"uAgent1 Script",id:"uagent1-script",level:3},{value:"uAgent2 Script",id:"uagent2-script",level:3},{value:"Running agents",id:"running-agents",level:3},{value:"Terminal 1",id:"terminal-1",level:4},{value:"Terminal 2",id:"terminal-2",level:4},{value:"Expected Output",id:"expected-output",level:3}];function g(e){const n={code:"code",h1:"h1",h2:"h2",h3:"h3",h4:"h4",header:"header",li:"li",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,i.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(n.header,{children:(0,s.jsx)(n.h1,{id:"uagent--uagent-communication",children:"uAgent \u2194 uAgent Communication"})}),"\n",(0,s.jsx)(n.p,{children:"In the uAgents Framework, agents can be triggered in multiple ways using different types of handlers. These handlers act as decorators, enabling agents to execute specific functions based on predefined conditions. In this section, we will explore two handlers. Below are the available handlers which can be used with uAgents:"}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsx)(n.li,{children:"on_event()"}),"\n",(0,s.jsx)(n.li,{children:"on_message()"}),"\n"]}),"\n",(0,s.jsx)(n.h2,{id:"on_event",children:"on_event"}),"\n",(0,s.jsx)(n.p,{children:"Agents can respond to events such as initialization and termination. The startup and shutdown handlers are used by the uAgents library to manage these events, ensuring that specific actions are executed when an agent starts or stops."}),"\n",(0,s.jsx)(n.h3,{id:"on_eventstartup",children:'on_event("startup")'}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-python",children:'@agent.on_event("startup")\nasync def introduce_agent(ctx: Context):\n    ctx.logger.info(f"Hello, I\'m agent {agent.name} and my address is {agent.address}.")\n    ...\n'})}),"\n",(0,s.jsx)(n.h3,{id:"on_eventshutdown",children:'on_event("shutdown")'}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-python",children:'@agent.on_event("shutdown")\nasync def introduce_agent(ctx: Context):\n    ctx.logger.info(f"Hello, I\'m agent {agent.name} and I am shutting down")\n    ...\n'})}),"\n",(0,s.jsx)(n.h2,{id:"on_message",children:"on_message()"}),"\n",(0,s.jsx)(n.p,{children:"In this section, we will explore the on_message() decorator, which allows us to send messages between microservice agents in a structured way. We will create two microservice agents and enable them to communicate with each other."}),"\n",(0,s.jsx)(n.p,{children:"Let's create agent1 who will send a message to agent2 on startup."}),"\n",(0,s.jsx)(n.h3,{id:"uagent1-script",children:"uAgent1 Script"}),"\n",(0,s.jsxs)(n.p,{children:["Please remember to have the ",(0,s.jsx)(n.strong,{children:"uagents"})," package installed in the terminal in order to create and run uAgents."]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-py",metastring:"title='uagent1.py'",children:"from uagents import Agent, Context, Model\n\n# Data model (envolope) which you want to send from one agent to another\nclass Message(Model):\n    message : str\n    field : int\n\nmy_first_agent = Agent(\n    name = 'My First Agent',\n    port = 5050,\n    endpoint = ['http://localhost:5050/submit']\n)\n\nsecond_agent = 'your_second_agent_address'\n\n@my_first_agent.on_event('startup')\nasync def startup_handler(ctx : Context):\n    ctx.logger.info(f'My name is {ctx.agent.name} and my address  is {ctx.agent.address}')\n    await ctx.send(second_agent, Message(message = 'Hi Second Agent, this is the first agent.'))\n\nif __name__ == \"__main__\":\n    my_first_agent.run()\n"})}),"\n",(0,s.jsx)(n.h3,{id:"uagent2-script",children:"uAgent2 Script"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-py",metastring:"title='uagent2.py'",children:"from uagents import Agent, Context, Model\n\nclass Message(Model):\n    message : str\n\nmy_second_agent = Agent(\n    name = 'My Second Agent',\n    port = 5051,\n    endpoint = ['http://localhost:5051/submit']\n)\n\n@my_second_agent.on_event('startup')\nasync def startup_handler(ctx : Context):\n    ctx.logger.info(f'My name is {ctx.agent.name} and my address  is {ctx.agent.address}')\n\n@my_second_agent.on_message(model = Message)\nasync def message_handler(ctx: Context, sender : str, msg: Message):\n    ctx.logger.info(f'I have received a message from {sender}.')\n    ctx.logger.info(f'I have received a message {msg.message}.')\n\nif __name__ == \"__main__\":\n    my_second_agent.run()\n"})}),"\n",(0,s.jsxs)(n.p,{children:["We need to define the data Model which is ",(0,s.jsx)(n.code,{children:"class Message"})," in both the sender and receiver agents. The receiving agent must include an ",(0,s.jsx)(n.code,{children:"on_message"}
1)," handler that correctly implements this data model. It is crucial to ensure that both agents use the exact same data model for seamless communication."]}),"\n",(0,s.jsx)(n.h3,{id:"running-agents",children:"Running agents"}),"\n",(0,s.jsx)(n.p,{children:"Now that we have created both agents, let's run them in separate terminals. Agent 2 is on the receiver's end so we have to start agent2 first. Whenever agent1 is started it will send message to agent 2 and it will receive and handle the message."}),"\n",(0,s.jsx)(n.h4,{id:"terminal-1",children:"Terminal 1"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{children:"python3 agent2.py\n"})}),"\n",(0,s.jsx)(n.h4,{id:"terminal-2",children:"Terminal 2"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{children:"python3 agent1.py\n"})}),"\n",(0,s.jsx)(n.h3,{id:"expected-output",children:"Expected Output"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{children:"abhi@Fetchs-MacBook-Pro testing % python3 second_agent.py \nINFO:     [My Second Agent]: Starting agent with address: agent1qtgc4vqn4ehh88hct0umnnqeg36m5722hc4e63lwy573kjtqee7qg5afmap\nINFO:     [My Second Agent]: My name is My Second Agent and my address  is agent1qtgc4vqn4ehh88hct0umnnqeg36m5722hc4e63lwy573kjtqee7qg5afmap\nINFO:     [My Second Agent]: Agent inspector available at https://agentverse.ai/inspect/?uri=http%3A//127.0.0.1%3A5051&address=agent1qtgc4vqn4ehh88hct0umnnqeg36m5722hc4e63lwy573kjtqee7qg5afmap\nINFO:     [My Second Agent]: Starting server on http://0.0.0.0:5051 (Press CTRL+C to quit)\nINFO:     [My Second Agent]: Registration on Almanac API successful\nINFO:     [My Second Agent]: Almanac contract registration is up to date!\nINFO:     [My Second Agent]: I have received a message from agent1q0kpqwd5q7akt9utnw540h293zhw063ua90m66rkx5lg98wq4zrjkhmgwd7.\nINFO:     [My Second Agent]: I have received a message Hi Second Agent, this is the first agent..\n"})}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{children:"abhi@Fetchs-MacBook-Pro testing % python3 first_agent.py\nINFO:     [My First Agent]: Starting agent with address: agent1q0kpqwd5q7akt9utnw540h293zhw063ua90m66rkx5lg98wq4zrjkhmgwd7\nINFO:     [My First Agent]: My name is My First Agent and my address  is agent1q0kpqwd5q7akt9utnw540h293zhw063ua90m66rkx5lg98wq4zrjkhmgwd7\nINFO:     [My First Agent]: Agent inspector available at https://agentverse.ai/inspect/?uri=http%3A//127.0.0.1%3A5050&address=agent1q0kpqwd5q7akt9utnw540h293zhw063ua90m66rkx5lg98wq4zrjkhmgwd7\nINFO:     [My First Agent]: Starting server on http://0.0.0.0:5050 (Press CTRL+C to quit)\nINFO:     [My First Agent]: Registration on Almanac API successful\nINFO:     [My First Agent]: Almanac contract registration is up to date!\n"})}),"\n",(0,s.jsx)(n.p,{children:"In this way we can establish a communicate between two uAgents."})]})}function l(e={}){const{wrapper:n}={...(0,i.R)(),...e.components};return n?(0,s.jsx)(n,{...e,children:(0,s.jsx)(g,{...e})}):g(e)}},28453:(e,n,t)=>{t.d(n,{R:()=>o,x:()=>r});var a=t(96540);const s={},i=a.createContext(s);function o(e){const n=a.useContext(i);return a.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function r(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:o(e.components),a.createElement(i.Provider,{value:n},e.children)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.