Spaces:
Paused
Paused
:gem: [Feature] MessageComposer: Merge or split messages for OpenAI API format
Browse files- apis/chat_api.py +10 -25
- conversations/__init__.py +1 -0
- conversations/message_composer.py +70 -0
apis/chat_api.py
CHANGED
@@ -6,6 +6,7 @@ from conversations import (
|
|
6 |
ConversationConnector,
|
7 |
ConversationCreator,
|
8 |
ConversationSession,
|
|
|
9 |
)
|
10 |
|
11 |
|
@@ -113,37 +114,21 @@ class ChatAPIApp:
|
|
113 |
default=[{"role": "user", "content": "Hello, who are you?"}],
|
114 |
description="(list) Messages",
|
115 |
)
|
116 |
-
sec_access_token: str = Field(
|
117 |
-
default="",
|
118 |
-
description="(str) Sec Access Token",
|
119 |
-
)
|
120 |
-
client_id: str = Field(
|
121 |
-
default="",
|
122 |
-
description="(str) Client ID",
|
123 |
-
)
|
124 |
-
conversation_id: str = Field(
|
125 |
-
default="",
|
126 |
-
description="(str) Conversation ID",
|
127 |
-
)
|
128 |
-
invocation_id: int = Field(
|
129 |
-
default=0,
|
130 |
-
description="(int) Invocation ID",
|
131 |
-
)
|
132 |
|
133 |
def chat_completions(self, item: ChatCompletionsPostItem):
|
|
|
|
|
|
|
134 |
connector = ConversationConnector(
|
135 |
conversation_style=item.model,
|
136 |
-
sec_access_token=
|
137 |
-
client_id=
|
138 |
-
conversation_id=
|
139 |
-
invocation_id=
|
140 |
)
|
141 |
|
142 |
-
|
143 |
-
|
144 |
-
prompt = item.messages[-1]["content"]
|
145 |
-
else:
|
146 |
-
prompt = item.messages[-1]["content"]
|
147 |
|
148 |
return EventSourceResponse(
|
149 |
connector.stream_chat(prompt=prompt, yield_output=True),
|
|
|
6 |
ConversationConnector,
|
7 |
ConversationCreator,
|
8 |
ConversationSession,
|
9 |
+
MessageComposer,
|
10 |
)
|
11 |
|
12 |
|
|
|
114 |
default=[{"role": "user", "content": "Hello, who are you?"}],
|
115 |
description="(list) Messages",
|
116 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
def chat_completions(self, item: ChatCompletionsPostItem):
|
119 |
+
creator = ConversationCreator()
|
120 |
+
creator.create()
|
121 |
+
|
122 |
connector = ConversationConnector(
|
123 |
conversation_style=item.model,
|
124 |
+
sec_access_token=creator.sec_access_token,
|
125 |
+
client_id=creator.client_id,
|
126 |
+
conversation_id=creator.conversation_id,
|
127 |
+
invocation_id=0,
|
128 |
)
|
129 |
|
130 |
+
message_composer = MessageComposer()
|
131 |
+
prompt = message_composer.merge(item.messages)
|
|
|
|
|
|
|
132 |
|
133 |
return EventSourceResponse(
|
134 |
connector.stream_chat(prompt=prompt, yield_output=True),
|
conversations/__init__.py
CHANGED
@@ -1,3 +1,4 @@
|
|
1 |
from .conversation_connector import ConversationConnector
|
2 |
from .conversation_creator import ConversationCreator
|
3 |
from .conversation_session import ConversationSession
|
|
|
|
1 |
from .conversation_connector import ConversationConnector
|
2 |
from .conversation_creator import ConversationCreator
|
3 |
from .conversation_session import ConversationSession
|
4 |
+
from .message_composer import MessageComposer
|
conversations/message_composer.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
from pprint import pprint
|
3 |
+
|
4 |
+
|
5 |
+
class MessageComposer:
|
6 |
+
def __init__(self):
|
7 |
+
pass
|
8 |
+
|
9 |
+
def merge(self, messages) -> str:
|
10 |
+
self.messages = messages
|
11 |
+
self.merged_str = ""
|
12 |
+
for message in messages:
|
13 |
+
role = message["role"]
|
14 |
+
if role.lower() in ["system", "user"]:
|
15 |
+
role_str = "me"
|
16 |
+
elif role.lower() in ["assistant", "bot"]:
|
17 |
+
role_str = "you"
|
18 |
+
else:
|
19 |
+
role_str = "unknown"
|
20 |
+
content = message["content"]
|
21 |
+
self.merged_str += f"`{role_str}`:\n{content}\n\n"
|
22 |
+
self.merged_str += "`you`:\n"
|
23 |
+
return self.merged_str
|
24 |
+
|
25 |
+
def split(self, merged_str) -> list:
|
26 |
+
self.messages = []
|
27 |
+
self.merged_str = merged_str
|
28 |
+
pattern = r"`(?P<role>me|you)`:\n(?P<content>.*)\n+"
|
29 |
+
matches = re.finditer(pattern, self.merged_str, re.MULTILINE)
|
30 |
+
for match in matches:
|
31 |
+
role = match.group("role")
|
32 |
+
|
33 |
+
if role == "me":
|
34 |
+
role_str = "user"
|
35 |
+
elif role == "you":
|
36 |
+
role_str = "assistant"
|
37 |
+
else:
|
38 |
+
role_str = "unknown"
|
39 |
+
|
40 |
+
self.messages.append(
|
41 |
+
{
|
42 |
+
"role": role_str,
|
43 |
+
"content": match.group("content"),
|
44 |
+
}
|
45 |
+
)
|
46 |
+
return self.messages
|
47 |
+
|
48 |
+
|
49 |
+
if __name__ == "__main__":
|
50 |
+
composer = MessageComposer()
|
51 |
+
messages = [
|
52 |
+
{
|
53 |
+
"role": "system",
|
54 |
+
"content": "You are a LLM developed by OpenAI. Your name is GPT-4.",
|
55 |
+
},
|
56 |
+
{"role": "user", "content": "Hello, who are you?"},
|
57 |
+
{"role": "assistant", "content": "I am a bot."},
|
58 |
+
{"role": "user", "content": "What is your name?"},
|
59 |
+
{"role": "assistant", "content": "My name is Bing."},
|
60 |
+
{"role": "user", "content": "Tell me a joke."},
|
61 |
+
{"role": "assistant", "content": "What is a robot's favorite type of music?"},
|
62 |
+
{
|
63 |
+
"role": "user",
|
64 |
+
"content": "How many questions have I asked? Please list them.",
|
65 |
+
},
|
66 |
+
]
|
67 |
+
merged_str = composer.merge(messages)
|
68 |
+
print(merged_str)
|
69 |
+
pprint(composer.split(merged_str))
|
70 |
+
print(composer.merge(composer.split(merged_str)))
|