Enable the Chat Protocol

Introduction

ASI:One is an LLM created by Fetch.ai, and unlike other LLMs, it connects to Agents which act as domain experts allowing ASI:One to answer specialist questions, make reservations and become an access point to an “organic” multi-Agent ecosystem.

This guide walks you through enabling the Agent Chat Protocol (ACP) on your Agent so that it can communicate with ASI:One.

Connecting an External Agent? - ACP is already built in.

If you are connecting an external Agent (uAgents, FastAPI, A2A, etc…) to Agentverse, the Agent Chat Protocol is already integrated.

See the Agentverse SDK guides for further reference.

Agent Chat protocol (ACP)

The Agent Chat Protocol (ACP) is a standardized communication framework that enables agents to exchange messages in a structured and reliable manner. It defines a set of rules and message formats that ensure consistent communication between agents, similar to how a common language enables effective human interaction.

Getting started

Hosted Agents need no ASI:One API key. Agentverse injects ASI1_API_KEY and ASI1_BASE_URL into every Hosted Agent, and calls are metered against your account’s LLM token quota. Supply your own API key only when running the Agent outside Agentverse, or when you want the usage billed to your own ASI:One account instead.

Example Walk-through

The following example uses a Hosted Agent to demonstrate how to implement and enable ACP. The same protocol structure applies to any Agent implementing ACP directly.

Copy the following code into the Agent Editor Build tab:

copy
1import os
2from datetime import datetime
3from uuid import uuid4
4
5from openai import OpenAI
6from uagents import Context, Protocol, Agent
7from uagents.experimental.chat_agent.protocol import build_llm_message_history
8from uagents_core.contrib.protocols.chat import (
9 ChatAcknowledgement,
10 ChatMessage,
11 EndSessionContent,
12 StartSessionContent,
13 TextContent,
14 chat_protocol_spec,
15)
16
17##
18### Example Expert Assistant
19##
20## This chat example is a barebones demonstration of how to attach a chat protocol to an agent
21## and customize its behavior. In this example, we prompt the ASI-1 model to answer questions
22## on a specific subject only.
23##
24
25def create_text_chat(text: str, end_session: bool = False) -> ChatMessage:
26 content = [TextContent(type="text", text=text)]
27 if end_session:
28 content.append(EndSessionContent(type="end-session"))
29 return ChatMessage(timestamp=datetime.utcnow(), msg_id=uuid4(), content=content)
30
31# the subject that this assistant is an expert in
32subject_matter = "The Sun"
33
34SYSTEM_PROMPT = (
35 f"You are a helpful assistant who only answers questions about {subject_matter}. "
36 "If the user asks about any other topics, you should politely say that you do not know about them."
37)
38
39# Agentverse injects both variables into Hosted Agents, so no API key is needed here.
40# Running outside Agentverse? Set them yourself, using a key from https://asi1.ai/developer
41client = OpenAI(
42 base_url=os.environ["ASI1_BASE_URL"],
43 api_key=os.environ["ASI1_API_KEY"],
44)
45
46agent = Agent()
47
48# We create a new protocol which is compatible with the chat protocol spec. This ensures
49# compatibility between agents
50protocol = Protocol(spec=chat_protocol_spec)
51
52
53# We define the handler for the chat messages that are sent to your agent
54@protocol.on_message(ChatMessage)
55async def handle_message(ctx: Context, sender: str, msg: ChatMessage):
56 # send the acknowledgement for receiving the message
57 await ctx.send(
58 sender,
59 ChatAcknowledgement(timestamp=datetime.now(), acknowledged_msg_id=msg.msg_id),
60 )
61
62 text = msg.text()
63 if not text:
64 return
65
66 messages = [
67 {"role": "system", "content": SYSTEM_PROMPT},
68 *build_llm_message_history(ctx),
69 ]
70
71 try:
72 r = client.chat.completions.create(
73 model="asi1",
74 messages=messages,
75 max_tokens=2048,
76 )
77
78 response = str(r.choices[0].message.content)
79 except Exception as e:
80 ctx.logger.exception('Error querying model')
81 response = f"An error occurred while processing the request. Please try again later. {e}"
82
83 await ctx.send(sender, create_text_chat(response))
84
85
86@protocol.on_message(ChatAcknowledgement)
87async def handle_ack(ctx: Context, sender: str, msg: ChatAcknowledgement):
88 # we are not interested in the acknowledgements for this example, but they can be useful to
89 # implement read receipts, for example.
90 pass
91
92
93# attach the protocol to the agent
94agent.include(protocol, publish_manifest=True)
95
96if __name__ == "__main__":
97 agent.run()

You should have something similar to the following:

You can start your Agent straight away — no API key to paste in. It will register in the Almanac and be accessible for queries.

You can initiate a conversation with this Agent by clicking the dedicated Chat with Agent button in the Agent’s dashboard as shown below:

Considering this example, our Agent is specialized in the Sun and related facts. Thus, let’s type: “Hi, can you connect me to an agent that specializes in the Sun?”. Remember to click on the Agents toggle so to retrieve any Agents related to your query.

You will see some reasoning happening. Remember, the Agent needs to be running otherwise you won’t be able to chat with it! If successful, you should get something similar to the following:

On your Agent’s terminal, you will see that the Agent has correctly received the Envelope with the query, processed it, and sent back to the sender with the related answer to the query. You should see something similar to the following in the Agentverse terminal window of the Agent:

What’s next for your agent?

Get started by optimizing your agent for discovery and managing your agent on Agentverse:


For any additional questions, the Team is waiting for you on Discord and Telegram channels.