-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.py
143 lines (114 loc) · 4.42 KB
/
message.py
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
import asyncio
import base64
import json
from enum import Enum
import util
from PIL import Image
from io import BytesIO
import httpx
from collections import namedtuple
async def convert(msg, model: util.Model_Class):
if isinstance(msg, Message):
return await msg.convert_to(model)
elif isinstance(msg, dict):
return msg
else:
return msg
class Role(Enum):
SYSTEM = "system"
ASSISTANT = "assistant"
USER = "user"
TOOL = "tool"
class Message:
text: str
role: Role
def __init__(self, role, text=None):
self.role = role
self.text = text
async def convert_to(self, model):
return await self.convert_to_openai()
async def convert_to_openai(self):
return {"role": self.role.value, "content": self.text}
@classmethod
def convert_from(cls, self_as_dict):
return cls(self_as_dict["role"], self_as_dict["content"])
def count_tokens(self):
return util.words_to_tokens(self.text)
class PictureMessage(Message):
picture: str
def __init__(self, role, picture, text=None):
super().__init__(role, text)
self.picture = picture
async def convert_to(self, model):
if model == util.weak_gpt_version:
return await Message(self.role, self.text).convert_to(model)
elif model == util.strong_gpt_version:
return await self.convert_to_openai()
elif model in (util.weak_claude_version, util.strong_claude_version):
return await self.convert_to_anthropic()
async def convert_to_openai(self):
return {"role": self.role.value, "content": [
{"type": "text", "text": self.text},
{"type": "image_url", "image_url":{"url":self.picture, "detail":"low"}}
]}
async def convert_to_anthropic(self):
async with httpx.AsyncClient() as client:
res = await client.get(self.picture)
image_bytes_file = BytesIO(res.content)
image = Image.open(image_bytes_file)
image.thumbnail((300, 300))
with BytesIO() as buffer:
image.save(buffer, format="png")
image_bytes = buffer.getvalue()
base64_encoded_image = base64.b64encode(image_bytes).decode("utf-8")
return {"role": self.role.value, "content": [
{"type": "text", "text": self.text},
{"type": "image_url", "image_url": {"url":f"data:image/png;base64,{base64_encoded_image}"}}
]}
@classmethod
def convert_from(cls, self_as_dict):
contents = self_as_dict["content"]
text = next(c["text"] for c in contents if c["type"] == "text")
picture = next(c["image_url"]["url"] for c in contents if c["type"] == "image_url")
return cls(role=self_as_dict["role"], picture=picture, text=text)
class ToolCallMessage(Message):
Call = namedtuple("Call", ["fun", "id", "args"])
calls: [Call]
def __init__(self, calls, text=""):
super().__init__(Role.ASSISTANT, text)
self.calls = calls
async def convert_to(self, model: util.Model_Class):
return await self.convert_openai()
async def convert_openai(self):
return {
"content": self.text,
"role": "assistant",
"tool_calls": [{
"id": call.id,
"type": "function",
"function": {
"name": call.fun.__name__,
"arguments": json.dumps(call.args)
}
}
for call in self.calls]
}
async def get_responses(self, convo):
tasks = (asyncio.create_task(call.fun(**call.args, convo=convo)) for call in self.calls)
responses = await asyncio.gather(*tasks)
return [ToolResponseMessage(ToolResponseMessage.Response(call.fun, call.id, res)) for res, call in zip(responses, self.calls)]
class ToolResponseMessage(Message):
Response = namedtuple("Response", ["fun", "id", "response"])
response: Response
def __init__(self, response):
super().__init__(Role.TOOL, None)
self.response = response
async def convert_to(self, model: util.Model_Class):
return await self.convert_to_openai()
async def convert_to_openai(self):
return {
"role": "tool",
"tool_call_id": self.response.id,
"content": self.response.response,
"name": self.response.fun.__name__
}