Repository navigation
Expand file tree
/
Copy pathexample_agent.py
More file actions
56 lines (53 loc) · 1.73 KB
/
Copy pathexample_agent.py
File metadata and controls
56 lines (53 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from datetime import datetime
from uuid import uuid4
import os
from dotenv import load_dotenv
# from openai import OpenAI
from uagents.setup import fund_agent_if_low
from uagents import Context, Protocol, Agent
from uagents_core.contrib.protocols.chat import (
ChatAcknowledgement,
ChatMessage,
StartSessionContent,
EndSessionContent,
AgentContent,
TextContent,
chat_protocol_spec,
)
load_dotenv()
agent = Agent(
name="xiaoje",
seed="abcdefghijklmn349287482937op033202304230430240234304",
port=8001,
mailbox=True,
)
protocol = Protocol(spec=chat_protocol_spec)
fund_agent_if_low(agent.wallet.address())
# Chat protocol so you can message through asi one and agentverse
@protocol.on_message(ChatMessage)
async def handle_message(ctx: Context, sender: str, msg: ChatMessage):
await ctx.send(
sender,
ChatAcknowledgement(timestamp=datetime.now(), acknowledged_msg_id=msg.msg_id),
)
response = "meowwww"
# is_start_of_chat: AgentContent = isinstance(msg.content[-1], StartSessionContent)
is_user_message = isinstance(msg.content[-1], TextContent)
ctx.logger.info(msg)
if is_user_message:
await ctx.send(sender, ChatMessage(
timestamp=datetime.now(),
msg_id=uuid4(),
content=[
TextContent(type="text", text=response),
# This will end the chat session
# EndSessionContent(type="end-session")
]
))
@protocol.on_message(ChatAcknowledgement)
async def handle_ack(ctx: Context, sender: str, msg: ChatAcknowledgement):
pass
# I believe you have to have this to register it to AgentVerse
agent.include(protocol, publish_manifest=True)
if __name__ == "__main__":
agent.run()