1"use strict";(self.webpackChunkinnovation_labs=self.webpackChunkinnovation_labs||[]).push([[907],{6695:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>l,contentTitle:()=>o,default:()=>h,frontMatter:()=>a,metadata:()=>s,toc:()=>c});const s=JSON.parse('{"id":"examples/asione/asi1-chat-system","title":"ASI1 Chat System","description":"This guide explains how the ASI1 Chat System facilitates real-time communication between autonomous agents and external APIs to process user queries efficiently.","source":"@site/versioned_docs/version-1.0.3/examples/asione/asi1-chat-system.md","sourceDirName":"examples/asione","slug":"/examples/asione/asi1-chat-system","permalink":"/resources/docs/1.0.3/examples/asione/asi1-chat-system","draft":false,"unlisted":false,"tags":[],"version":"1.0.3","frontMatter":{"id":"asi1-chat-system","title":"ASI1 Chat System"},"sidebar":"tutorialSidebar","previous":{"title":"AI Language Tutor with ASI-1 Mini","permalink":"/resources/docs/1.0.3/examples/asione/asi1-mini-language-tutor"},"next":{"title":"DeFi AI Agent Starter Guide","permalink":"/resources/docs/1.0.3/examples/asione/asi-defi-ai-agent"}}');var i=t(74848),r=t(28453);const a={id:"asi1-chat-system",title:"ASI1 Chat System"},o="ASI1 Chat System using uAgents Framework To Communicate with HuggingFace",l={},c=[{value:"Overview",id:"overview",level:2},{value:"Prerequisites",id:"prerequisites",level:2},{value:"Server Agent Script",id:"server-agent-script",level:2},{value:"Script Breakdown (server.py)",id:"script-breakdown-serverpy",level:3},{value:"Importing Required Libraries",id:"importing-required-libraries",level:4},{value:"Defining Data Models",id:"defining-data-models",level:4},{value:"Initializing the Server Agent",id:"initializing-the-server-agent",level:4},{value:"ASI1 API Integration Function",id:"asi1-api-integration-function",level:4},{value:"Event Handlers",id:"event-handlers",level:4},{value:"Running the Server Agent",id:"running-the-server-agent",level:4},{value:"Client Agent Script",id:"client-agent-script",level:2},{value:"Script Breakdown (client.py)",id:"script-breakdown-clientpy",level:3},{value:"Importing Required Libraries",id:"importing-required-libraries-1",level:4},{value:"Defining Data Models",id:"defining-data-models-1",level:4},{value:"Initializing the Client Agent",id:"initializing-the-client-agent",level:4},{value:"Event Handlers",id:"event-handlers-1",level:4},{value:"Running the Client Agent",id:"running-the-client-agent",level:4},{value:"Running the System",id:"running-the-system",level:2},{value:"Troubleshooting Common Issues",id:"troubleshooting-common-issues",level:2},{value:"GitHub Repository",id:"github-repository",level:2}];function d(e){const n={a:"a",admonition:"admonition",code:"code",h1:"h1",h2:"h2",h3:"h3",h4:"h4",header:"header",li:"li",ol:"ol",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,r.R)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(n.header,{children:(0,i.jsx)(n.h1,{id:"asi1-chat-system-using-uagents-framework-to-communicate-with-huggingface",children:"ASI1 Chat System using uAgents Framework To Communicate with HuggingFace"})}),"\n",(0,i.jsx)(n.p,{children:"This guide explains how the ASI1 Chat System facilitates real-time communication between autonomous agents and external APIs to process user queries efficiently."}),"\n",(0,i.jsx)(n.h2,{id:"overview",children:"Overview"}),"\n",(0,i.jsx)(n.p,{children:"This project demonstrates how to create a multi-agent system using the uAgents framework and ASI1 Mini. The system, consisting of a server agent and a client agent, is designed to:"}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:"Enable interactive query handling using the ASI1 API."}),"\n",(0,i.jsx)(n.li,{children:"Allow users to send queries about Hugging Face models."}),"\n",(0,i.jsx)(n.li,{children:"Process responses through a server-client architecture."}),"\n",(0,i.jsx)(n.li,{children:"Demonstrate agent-to-agent communication patterns."}),"\n"]}),"\n",(0,i.jsx)(n.h2,{id:"prerequisites",children:"Prerequisites"}),"\n",(0,i.jsx)(n.p,{children:"Before running this project, ensure you have:"}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:"Python 3.11 installed."}),"\n",(0,i.jsxs)(n.li,{children:["uAgents library installed. If not already installed, use:","\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-bash",children:"pip install uagents\n"})}),"\n"]}),"\n",(0,i.jsxs)(n.li,{children:["Requests library installed:","\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-bash",children:"pip install requests\n"})}),"\n"]}),"\n",(0,i.jsxs)(n.li,{children:["A valid API key for ASI1. Obtain your API Key ",(0,i.jsx)(n.a,{href:"../../asione/asi1-mini-getting-started#how-to-get-an-api-key",children:"here"}),"."]}),"\n",(0,i.jsxs)(n.li,{children:["Environment variable set for your ASI1 API key:","\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-bash",children:'export ASI1_API_KEY="your_api_key_here"\n'})}),"\n"]}),"\n"]}),"\n",(0,i.jsx)(n.h2,{id:"server-agent-script",children:"Server Agent Script"}),"\n",(0,i.jsx)(n.p,{children:"The Server Agent is responsible for receiving queries from the Client Agent, processing them using the ASI1 API, and returning the responses."}),"\n",(0,i.jsx)(n.h3,{id:"script-breakdown-serverpy",children:"Script Breakdown (server.py)"}),"\n",(0,i.jsx)(n.h4,{id:"importing-required-libraries",children:"Importing Required Libraries"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-python",children:"import requests\nimport os\nfrom uagents import Agent, Context, Model\n"})}),"\n",(0,i.jsx)(n.h4,{id:"defining-data-models",children:"Defining Data Models"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-python",children:"# Request model\nclass ASI1Query(Model):\n query: str\n sender_address: str\n\n# Response model\nclass ASI1Response(Mo
1del):\n response: str # Response from ASI1 API\n"})}),"\n",(0,i.jsx)(n.h4,{id:"initializing-the-server-agent",children:"Initializing the Server Agent"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-python",children:"# Define the main agent\nmainAgent = Agent(\n name='asi1_chat_agent',\n port=5068,\n endpoint='http://localhost:5068/submit',\n seed='asi1_chat_seed'\n)\n"})}),"\n",(0,i.jsx)(n.h4,{id:"asi1-api-integration-function",children:"ASI1 API Integration Function"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-python",children:'def get_asi1_response(query: str) -> str:\n """\n Sends a query to ASI1 API and returns the response.\n """\n # Get API key from environment variable\n api_key = os.environ.get("ASI1_API_KEY")\n if not api_key:\n return "Error: ASI1_API_KEY environment variable not set"\n \n headers = {\n "Authorization": f"Bearer {api_key}",\n "Content-Type": "application/json"\n }\n\n data = {\n "model": "asi1-mini", # Select appropriate ASI1 model\n "messages": [\n {"role": "system", "content": "You are a helpful AI assistant."},\n {"role": "user", "content": query}\n ]\n }\n\n try:\n response = requests.post("https://api.asi1.ai/v1/chat/completions", json=data, headers=headers)\n if response.status_code == 200:\n result = response.json()\n if "choices" in result and len(result["choices"]) > 0:\n return result["choices"][0]["message"]["content"].strip()\n else:\n return "ASI1 API returned an empty response."\n else:\n return f"ASI1 API Error: {response.status_code}, {response.text}"\n except Exception as e:\n return f"ASI1 API Error: {str(e)}"\n'})}),"\n",(0,i.jsx)(n.h4,{id:"event-handlers",children:"Event Handlers"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-python",children:"@mainAgent.on_event('startup')\nasync def startup_handler(ctx: Context):\n ctx.logger.info(f'Agent {ctx.agent.name} started at {ctx.agent.address}')\n\n# Handler for receiving query\[email protected]_message(model=ASI1Query)\nasync def handle_query(ctx: Context, sender: str, msg: ASI1Query):\n ctx.logger.info(f\"Received query from {sender}: {msg.query}\")\n\n # Call ASI1 API for the response\n answer = get_asi1_response(msg.query)\n\n # Respond back with the answer from ASI1\n await ctx.send(sender, ASI1Response(response=answer))\n"})}),"\n",(0,i.jsx)(n.h4,{id:"running-the-server-agent",children:"Running the Server Agent"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-python",children:'if __name__ == "__main__":\n mainAgent.run()\n'})}),"\n",(0,i.jsx)(n.h2,{id:"client-agent-script",children:"Client Agent Script"}),"\n",(0,i.jsx)(n.p,{children:"The Client Agent acts as the user interface, collecting input from the user and sending it to the Server Agent for processing."}),"\n",(0,i.jsx)(n.h3,{id:"script-breakdown-clientpy",children:"Script Breakdown (client.py)"}),"\n",(0,i.jsx)(n.h4,{id:"importing-required-libraries-1",children:"Importing Required Libraries"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-python",children:"from uagents import Agent, Context, Model\n"})}),"\n",(0,i.jsx)(n.h4,{id:"defining-data-models-1",children:"Defining Data Models"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-python",children:"# Query model to send to the server agent\nclass ASI1Query(Model):\n query: str\n sender_address: str\n\n# Response model to receive from the server agent\nclass ASI1Response(Model):\n response: str\n"})}),"\n",(0,i.jsx)(n.h4,{id:"initializing-the-client-agent",children:"Initializing the Client Agent"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-python",children:"# Client agent setup\nclientAgent = Agent(\n name='asi1_client_agent',\n port=5070,\n endpoint='http://localhost:5070/submit',\n seed='asi1_client_seed'\n)\n\n# Server agent address (update with actual address if needed)\nSERVER_AGENT_ADDRESS = \"agent1q0usc8uc5hxes4ckr8624ghdxpn0lvxkgex44jtfv32x2r7ymx8sgg8yt2g\" # Replace with the actual address of your server agent\n"})}),"\n",(0,i.jsx)(n.h4,{id:"event-handlers-1",children:"Event Handlers"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-python",children:'@clientAgent.on_event(\'startup\')\nasync def startup_handler(ctx: Context):\n ctx.logger.info(f\'Client Agent {ctx.agent.name}
1 started at {ctx.agent.address}\')\n\n # Get user input\n user_query = input("Ask something: ")\n\n # Send the query to the server agent\n await ctx.send(SERVER_AGENT_ADDRESS, ASI1Query(query=user_query, sender_address=ctx.agent.address))\n ctx.logger.info(f"Query sent to server agent: {user_query}")\n\[email protected]_message(model=ASI1Response)\nasync def handle_response(ctx: Context, sender: str, msg: ASI1Response):\n ctx.logger.info(f"Response received from {sender}: {msg.response}")\n print(f"Response from ASI1 API: {msg.response}")\n'})}),"\n",(0,i.jsx)(n.h4,{id:"running-the-client-agent",children:"Running the Client Agent"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-python",children:'if __name__ == "__main__":\n clientAgent.run()\n'})}),"\n",(0,i.jsx)(n.h2,{id:"running-the-system",children:"Running the System"}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.strong,{children:"1. Set Environment Variable for API Key"})}),"\n",(0,i.jsx)(n.p,{children:"Before running the agents, set the ASI1 API key as an environment variable:"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-bash",children:'export ASI1_API_KEY="your_api_key_here"\n'})}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.strong,{children:"2. Start the Server Agent"})}),"\n",(0,i.jsx)(n.p,{children:"Open a terminal window and run:"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-bash",children:"python server.py\n"})}),"\n",(0,i.jsx)(n.p,{children:"You should see output similar to:"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{children:"INFO: [asi1_chat_agent]: Starting agent with address: agent1q0usc8uc5hxes4ckr8624ghdxpn0lvxkgex44jtfv32x2r7ymx8sgg8yt2g\nINFO: [asi1_chat_agent]: Agent asi1_chat_agent started at agent1q0usc8uc5hxes4ckr8624ghdxpn0lvxkgex44jtfv32x2r7ymx8sgg8yt2g\nINFO: [asi1_chat_agent]: Agent inspector available at https://agentverse.ai/inspect/?uri=http%3A//127.0.0.1%3A5068&address=agent1q0usc8uc5hxes4ckr8624ghdxpn0lvxkgex44jtfv32x2r7ymx8sgg8yt2g\nINFO: [asi1_chat_agent]: Starting server on http://0.0.0.0:5068 (Press CTRL+C to quit)\nINFO: [asi1_chat_agent]: Registration on Almanac API successful\nINFO: [asi1_chat_agent]: Almanac contract registration is up to date!\n"})}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.strong,{children:"3. Start the Client Agent"})}),"\n",(0,i.jsx)(n.p,{children:"Open a new terminal window and run:"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-bash",children:"python client.py\n"})}),"\n",(0,i.jsx)(n.p,{children:"You should see output similar to:"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{children:"INFO: [asi1_client_agent]: Starting agent with address: agent1qdszugnhqtadhg38tgxnzlyards0qpf0mx5x4l4ms3qq9g8fadxfgxv5dfr\nINFO: [asi1_client_agent]: Client Agent asi1_client_agent started at agent1qdszugnhqtadhg38tgxnzlyards0qpf0mx5x4l4ms3qq9g8fadxfgxv5dfr\nAsk something: \n"})}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.strong,{children:"4. Interact with the System"})}),"\n",(0,i.jsx)(n.p,{children:"When prompted, enter a query about Hugging Face models, for example:"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{children:"Ask something: Image Classification\n"})}),"\n",(0,i.jsx)(n.p,{children:"The client will send this query to the server agent:"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{children:"INFO: [asi1_client_agent]: Query sent to server agent: Image Classification\nINFO: [asi1_client_agent]: Agent inspector available at https://agentverse.ai/inspect/?uri=http%3A//127.0.0.1%3A5070&address=agent1qdszugnhqtadhg38tgxnzlyards0qpf0mx5x4l4ms3qq9g8fadxfgxv5dfr\nINFO: [asi1_client_agent]: Starting server on http://0.0.0.0:5070 (Press CTRL+C to quit)\nINFO: [asi1_client_agent]: Registration on Almanac API successful\nINFO: [asi1_client_agent]: Almanac contract registration is up to date!\n"})}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.strong,{children:"5. View the Response"})}),"\n",(0,i.jsx)(n.p,{children:"The server agent will process the query and send back a response:"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{children:"INFO: [asi1_chat_agent]: Received query from agent1qdszugnhqtadhg38tgxnzlyards0qpf0mx5x4l4ms3qq9g8fadxfgxv5dfr: Image Classification\n"})}),"\n",(0,i.jsx)(n.p,{children:"The client agent will display the response:"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{children:'INFO: [asi1_client_agent]: Response received from agent1q0usc8uc5hxes4ckr8624ghdxpn0lvxkgex44jtfv32x2r7ymx8sgg8yt2g: It\'s difficult to give a definitive "top 5" list for most downloaded and highest-rated models on Hugging Face for Image Classification as these metrics change frequently. As of my last knowledge update, these are some of the highly regarded and popular models for image classification you could find on Hugging Face:\n\n1. **ViT (Vision Transformer):** Developed by Google Research, ViT models are known for their strong performance on image classification tasks, leveraging the transformer architecture originally designed for natural language processing. They are often pre-trained on large datasets and then fine-tuned for specific image classifications.\n\n2. **ResNet (Residual Network):** Developed by Microsoft Research, various ResNet architectures (e.g., ResNet50, ResNet101, ResNet152) are widely used. ResNet introduced the concept of skip connections, allowing for the training of very deep networks and achieving excellent performance on image classification.\n\n3. **EfficientNet:** Developed by Google Research, EfficientNet models focus on optimizing accuracy and efficie
1ncy. They are known for achieving high performance with relatively fewer parameters compared to other models.\n\n4. **Inception:** Also developed by Google, Inception models (e.g., InceptionV3) utilize inception modules with multiple convolutional filters operating at different scales to capture features at various levels of detail within an image.\n\n5. **MobileNet:** Developed by Google, MobileNet models are designed specifically for mobile and embedded vision applications. They are known for their efficiency and smaller model sizes, allowing for deployment on resource-constrained devices while still maintaining reasonable accuracy for image classification.\n\nKeep in mind that popularity and download counts on Hugging Face are dynamic. I recommend checking the Hugging Face model hub directly for the most up-to-date information on downloads and ratings. You can sort models by different metrics there.\n'})}),"\n",(0,i.jsx)(n.h2,{id:"troubleshooting-common-issues",children:"Troubleshooting Common Issues"}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.strong,{children:"API Key Not Found"})}),"\n",(0,i.jsx)(n.p,{children:'If you see the error "ASI1_API_KEY environment variable not set", make sure you\'ve set the environment variable correctly:'}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-bash",children:'export ASI1_API_KEY="your_api_key_here"\n'})}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.strong,{children:"Connection Issues"})}),"\n",(0,i.jsx)(n.p,{children:"If the client agent cannot connect to the server agent:"}),"\n",(0,i.jsxs)(n.ol,{children:["\n",(0,i.jsx)(n.li,{children:"Ensure the server agent is running and note its address."}),"\n",(0,i.jsxs)(n.li,{children:["Update the ",(0,i.jsx)(n.code,{children:"SERVER_AGENT_ADDRESS"})," in the client.py script with the correct address."]}),"\n",(0,i.jsx)(n.li,{children:"Check that both agents are running on their specified ports (5068 and 5070)."}),"\n"]}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.strong,{children:"API Response Errors"})}),"\n",(0,i.jsx)(n.p,{children:"If you receive API errors:"}),"\n",(0,i.jsxs)(n.ol,{children:["\n",(0,i.jsx)(n.li,{children:"Verify your ASI1 API key is valid and has not expired."}),"\n",(0,i.jsx)(n.li,{children:"Check your internet connection."}),"\n",(0,i.jsx)(n.li,{children:"Ensure the ASI1 API service is operational."}),"\n"]}),"\n",(0,i.jsx)(n.h2,{id:"github-repository",children:"GitHub Repository"}),"\n",(0,i.jsxs)(n.p,{children:["For the complete code, visit the ",(0,i.jsx)(n.a,{href:"https://github.com/Atharva-Pore/AP_uAgents/tree/main/asi1_chat_system",children:"ASI1 Chat System Repository"}),"."]}),"\n",(0,i.jsx)(n.admonition,{type:"note",children:(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.strong,{children:"Note:"})," You can learn more about ASI1 Mini APIs ",(0,i.jsx)(n.a,{href:"https://docs.asi1.ai/docs/",children:(0,i.jsx)(n.strong,{children:"here"})})," and about uAgents ",(0,i.jsx)(n.a,{href:"/resources/docs/1.0.3/agent-creation/uagent-creation",children:(0,i.jsx)(n.strong,{children:"here"})}),"."]})})]})}function h(e={}){const{wrapper:n}={...(0,r.R)(),...e.components};return n?(0,i.jsx)(n,{...e,children:(0,i.jsx)(d,{...e})}):d(e)}},28453:(e,n,t)=>{t.d(n,{R:()=>a,x:()=>o});var s=t(96540);const i={},r=s.createContext(i);function a(e){const n=s.useContext(r);return s.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function o(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:a(e.components),s.createElement(r.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.