Hansimov commited on
Commit
16c8074
1 Parent(s): 9c64311

:gem: [Feature] Support system prompt via previousMessages key

Browse files
apis/chat_api.py CHANGED
@@ -128,9 +128,12 @@ class ChatAPIApp:
128
 
129
  message_composer = MessageComposer()
130
  prompt = message_composer.merge(item.messages)
 
131
 
132
  return EventSourceResponse(
133
- connector.stream_chat(prompt=prompt, yield_output=True),
 
 
134
  media_type="text/event-stream",
135
  )
136
 
 
128
 
129
  message_composer = MessageComposer()
130
  prompt = message_composer.merge(item.messages)
131
+ system_prompt = message_composer.system_prompt
132
 
133
  return EventSourceResponse(
134
+ connector.stream_chat(
135
+ prompt=prompt, system_prompt=system_prompt, yield_output=True
136
+ ),
137
  media_type="text/event-stream",
138
  )
139
 
conversations/conversation_connector.py CHANGED
@@ -75,20 +75,23 @@ class ConversationConnector:
75
  )
76
  await self.init_handshake()
77
 
78
- async def send_chathub_request(self, prompt):
79
  payload_constructor = ChathubRequestPayloadConstructor(
80
  prompt=prompt,
81
  conversation_style=self.conversation_style,
82
  client_id=self.client_id,
83
  conversation_id=self.conversation_id,
84
  invocation_id=self.invocation_id,
 
85
  )
86
  self.connect_request_payload = payload_constructor.request_payload
87
  await self.wss_send(self.connect_request_payload)
88
 
89
- async def stream_chat(self, prompt="", yield_output=False):
 
 
90
  await self.connect()
91
- await self.send_chathub_request(prompt)
92
  message_parser = MessageParser(outputer=OpenaiStreamOutputer())
93
  has_output_role_message = False
94
  if yield_output and not has_output_role_message:
 
75
  )
76
  await self.init_handshake()
77
 
78
+ async def send_chathub_request(self, prompt: str, system_prompt: str = None):
79
  payload_constructor = ChathubRequestPayloadConstructor(
80
  prompt=prompt,
81
  conversation_style=self.conversation_style,
82
  client_id=self.client_id,
83
  conversation_id=self.conversation_id,
84
  invocation_id=self.invocation_id,
85
+ system_prompt=system_prompt,
86
  )
87
  self.connect_request_payload = payload_constructor.request_payload
88
  await self.wss_send(self.connect_request_payload)
89
 
90
+ async def stream_chat(
91
+ self, prompt: str = "", system_prompt: str = None, yield_output=False
92
+ ):
93
  await self.connect()
94
+ await self.send_chathub_request(prompt=prompt, system_prompt=system_prompt)
95
  message_parser = MessageParser(outputer=OpenaiStreamOutputer())
96
  has_output_role_message = False
97
  if yield_output and not has_output_role_message:
conversations/message_composer.py CHANGED
@@ -6,20 +6,29 @@ 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:
 
6
  def __init__(self):
7
  pass
8
 
9
+ def merge(self, messages, suffix=True) -> str:
10
  self.messages = messages
11
  self.merged_str = ""
12
+ self.system_prompt = ""
13
  for message in messages:
14
  role = message["role"]
15
+ content = message["content"]
16
+
17
+ if role.lower() in ["system"]:
18
+ self.system_prompt = content
19
+ continue
20
+
21
+ if role.lower() in ["user"]:
22
  role_str = "me"
23
  elif role.lower() in ["assistant", "bot"]:
24
  role_str = "you"
25
  else:
26
  role_str = "unknown"
 
27
  self.merged_str += f"`{role_str}`:\n{content}\n\n"
28
+
29
+ if suffix:
30
+ self.merged_str += "`you`:\n"
31
+
32
  return self.merged_str
33
 
34
  def split(self, merged_str) -> list:
networks/chathub_request_payload_constructor.py CHANGED
@@ -3,14 +3,37 @@ import uuid
3
  from conversations import ConversationStyle
4
 
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  class ChathubRequestPayloadConstructor:
7
  def __init__(
8
  self,
9
- prompt,
10
  client_id: str,
11
  conversation_id: str,
12
  invocation_id: int = 0,
13
  conversation_style: str = ConversationStyle.PRECISE.value,
 
14
  ):
15
  self.prompt = prompt
16
  self.client_id = client_id
@@ -18,6 +41,7 @@ class ChathubRequestPayloadConstructor:
18
  self.invocation_id = invocation_id
19
  self.conversation_style = conversation_style
20
  self.message_id = self.generate_random_uuid()
 
21
  self.construct()
22
 
23
  def generate_random_uuid(self):
@@ -63,6 +87,11 @@ class ChathubRequestPayloadConstructor:
63
  "gencontentv3",
64
  ],
65
  }
 
 
 
 
 
66
  self.request_payload = {
67
  "arguments": [
68
  {
@@ -116,6 +145,7 @@ class ChathubRequestPayloadConstructor:
116
  "plugins": [
117
  {"id": "c310c353-b9f0-4d76-ab0d-1dd5e979cf68"},
118
  ],
 
119
  "traceId": self.generate_random_hex_str(),
120
  "conversationHistoryOptionsSets": [
121
  "autosave",
 
3
  from conversations import ConversationStyle
4
 
5
 
6
+ class SystemPromptContextConstructor:
7
+ # https://github.com/weaigc/bingo/blob/eaebba306d5f68b940e4486ad81897516d0db0f3/src/lib/bots/bing/index.ts#L205-L211
8
+ # https://github.com/weaigc/bingo/blob/eaebba306d5f68b940e4486ad81897516d0db0f3/src/lib/bots/bing/index.ts#L296
9
+ def __init__(self, system_prompt: str = None):
10
+ self.system_prompt = system_prompt
11
+ self.construct()
12
+
13
+ def construct(self):
14
+ if self.system_prompt:
15
+ self.system_context = [
16
+ {
17
+ "author": "user",
18
+ "description": self.system_prompt,
19
+ "contextType": "WebPage",
20
+ "messageType": "Context",
21
+ "messageId": "discover-web--page-ping-mriduna-----",
22
+ }
23
+ ]
24
+ else:
25
+ self.system_context = None
26
+
27
+
28
  class ChathubRequestPayloadConstructor:
29
  def __init__(
30
  self,
31
+ prompt: str,
32
  client_id: str,
33
  conversation_id: str,
34
  invocation_id: int = 0,
35
  conversation_style: str = ConversationStyle.PRECISE.value,
36
+ system_prompt: str = None,
37
  ):
38
  self.prompt = prompt
39
  self.client_id = client_id
 
41
  self.invocation_id = invocation_id
42
  self.conversation_style = conversation_style
43
  self.message_id = self.generate_random_uuid()
44
+ self.system_prompt = system_prompt
45
  self.construct()
46
 
47
  def generate_random_uuid(self):
 
87
  "gencontentv3",
88
  ],
89
  }
90
+
91
+ self.system_context = SystemPromptContextConstructor(
92
+ self.system_prompt
93
+ ).system_context
94
+
95
  self.request_payload = {
96
  "arguments": [
97
  {
 
145
  "plugins": [
146
  {"id": "c310c353-b9f0-4d76-ab0d-1dd5e979cf68"},
147
  ],
148
+ "previousMessages": self.system_context,
149
  "traceId": self.generate_random_hex_str(),
150
  "conversationHistoryOptionsSets": [
151
  "autosave",