-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathchat_proto.py
More file actions
159 lines (107 loc) · 4.07 KB
/
chat_proto.py
File metadata and controls
159 lines (107 loc) · 4.07 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from typing import Any, Literal, TypedDict
from datetime import datetime
from pydantic.v1 import UUID4
from uagents import Model, Protocol, Context
from uuid import uuid4
from ai import get_completion
class Metadata(TypedDict):
# primarily used with hte `Resource` model. This field specifies the mime_type of
# resource that is being referenced. A full list can be found at `docs/mime_types.md`
mime_type: str
# the role of the resource
role: str
class TextContent(Model):
type: Literal["text"]
# The text of the content. The format of this field is UTF-8 encoded strings. Additionally,
# markdown based formatting can be used and will be supported by most clients
text: str
class Resource(Model):
# the uri of the resource
uri: str
# the set of metadata for this resource, for more detailed description of the set of
# fields see `docs/metadata.md`
metadata: dict[str, str]
class ResourceContent(Model):
type: Literal["resource"]
# The resource id
resource_id: UUID4
# The resource or list of resource for this content. typically only a single
# resource will be sent, however, if there are accompanying resources like
# thumbnails and audo tracks these can be additionally referenced
#
# In the case of the a list of resources, the first element of the list is always
# considered the primary resource
resource: Resource | list[Resource]
class MetadataContent(Model):
type: Literal["metadata"]
# the set of metadata for this content, for more detailed description of the set of
# fields see `docs/metadata.md`
metadata: dict[str, str]
class StartSessionContent(Model):
type: Literal["start-session"]
class EndSessionContent(Model):
type: Literal["end-session"]
class StartStreamContent(Model):
type: Literal["start-stream"]
stream_id: UUID4
class EndStreamContent(Model):
type: Literal["start-stream"]
stream_id: UUID4
# The combined agent content types
AgentContent = (
TextContent
| ResourceContent
| MetadataContent
| StartSessionContent
| EndSessionContent
| StartStreamContent
| EndStreamContent
)
class ChatMessage(Model):
# the timestamp for the message, should be in UTC
timestamp: datetime
# a unique message id that is generated from the message instigator
msg_id: UUID4
# the list of content elements in the chat
content: list[AgentContent]
class ChatAcknowledgement(Model):
# the timestamp for the message, should be in UTC
timestamp: datetime
# the msg id that is being acknowledged
acknowledged_msg_id: UUID4
# optional acknowledgement metadata
metadata: dict[str, str] | None = None
def create_text_chat(text: str) -> ChatMessage:
return ChatMessage(
timestamp=datetime.utcnow(),
msg_id=uuid4(),
content=[TextContent(type="text", text=text)],
)
chat_proto = Protocol(name="AgentChatProtcol", version="0.2.1")
struct_output_client_proto = Protocol(
name="StructuredOutputClientProtocol", version="0.1.0"
)
@chat_proto.on_message(ChatMessage)
async def handle_message(ctx: Context, sender: str, msg: ChatMessage):
await ctx.send(
sender,
ChatAcknowledgement(
timestamp=datetime.utcnow(), acknowledged_msg_id=msg.msg_id
),
)
for item in msg.content:
if isinstance(item, StartSessionContent):
ctx.logger.info(f"Got a start session message from {sender}")
continue
elif isinstance(item, TextContent):
ctx.logger.info(f"Got a message from {sender}: {item.text}")
ctx.storage.set(str(ctx.session), sender)
completion = get_completion(prompt=msg.content[0].text)
await ctx.send(sender, create_text_chat(completion))
else:
ctx.logger.info(f"Got unexpected content from {sender}")
@chat_proto.on_message(ChatAcknowledgement)
async def handle_ack(ctx: Context, sender: str, msg: ChatAcknowledgement):
ctx.logger.info(
f"Got an acknowledgement from {sender} for {msg.acknowledged_msg_id}"
)