Hansimov commited on
Commit
6ad10eb
1 Parent(s): 3a1b095

:recycle: [Refactor] Separate MessageParser for data type 1

Browse files
Files changed (2) hide show
  1. conversation_connector.py +3 -38
  2. message_parser.py +47 -0
conversation_connector.py CHANGED
@@ -7,6 +7,7 @@ import urllib
7
 
8
  from conversation_creator import ConversationCreator
9
  from chathub_request_constructor import ChathubRequestConstructor
 
10
  from logger.logger import logger
11
 
12
 
@@ -86,7 +87,7 @@ class ConversationConnector:
86
  await self.init_wss_connection()
87
  await self.send_chathub_request(prompt)
88
 
89
- delta_content_pointer = 0
90
  while not self.wss.closed:
91
  response_lines_str = await self.wss.receive_str()
92
 
@@ -102,43 +103,7 @@ class ConversationConnector:
102
 
103
  # Stream: Meaningful Messages
104
  if data.get("type") == 1:
105
- arguments = data["arguments"][0]
106
- if arguments.get("throttling"):
107
- throttling = arguments.get("throttling")
108
- # pprint.pprint(throttling)
109
- if arguments.get("messages"):
110
- for message in arguments.get("messages"):
111
- message_type = message.get("messageType")
112
- # Message: Displayed answer
113
- if message_type is None:
114
- content = message["adaptiveCards"][0]["body"][0]["text"]
115
- delta_content = content[delta_content_pointer:]
116
- logger.line(delta_content, end="")
117
- delta_content_pointer = len(content)
118
- # Message: Suggested Questions
119
- if message.get("suggestedResponses"):
120
- logger.note("\n\nSuggested Questions: ")
121
- for suggestion in message.get("suggestedResponses"):
122
- suggestion_text = suggestion.get("text")
123
- logger.file(f"- {suggestion_text}")
124
- # Message: Search Query
125
- elif message_type in ["InternalSearchQuery"]:
126
- message_hidden_text = message["hiddenText"]
127
- logger.note(f"\n[Searching: [{message_hidden_text}]]")
128
- # Message: Internal Search Results
129
- elif message_type in ["InternalSearchResult"]:
130
- logger.note("[Analyzing search results ...]")
131
- # Message: Loader status, such as "Generating Answers"
132
- elif message_type in ["InternalLoaderMessage"]:
133
- logger.note("[Generating answers ...]\n")
134
- # Message: Render Cards for Webpages
135
- elif message_type in ["RenderCardRequest"]:
136
- continue
137
- # Message: Not Implemented
138
- else:
139
- raise NotImplementedError(
140
- f"Not Supported Message Type: {message_type}"
141
- )
142
  # Stream: List of whole conversation messages
143
  elif data.get("type") == 2:
144
  if data.get("item"):
 
7
 
8
  from conversation_creator import ConversationCreator
9
  from chathub_request_constructor import ChathubRequestConstructor
10
+ from message_parser import MessageParser
11
  from logger.logger import logger
12
 
13
 
 
87
  await self.init_wss_connection()
88
  await self.send_chathub_request(prompt)
89
 
90
+ message_parser = MessageParser()
91
  while not self.wss.closed:
92
  response_lines_str = await self.wss.receive_str()
93
 
 
103
 
104
  # Stream: Meaningful Messages
105
  if data.get("type") == 1:
106
+ message_parser.parse(data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  # Stream: List of whole conversation messages
108
  elif data.get("type") == 2:
109
  if data.get("item"):
message_parser.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from logger.logger import logger
2
+
3
+
4
+ class MessageParser:
5
+ def __init__(self) -> None:
6
+ self.delta_content_pointer = 0
7
+
8
+ def parse(self, data):
9
+ arguments = data["arguments"][0]
10
+
11
+ if arguments.get("throttling"):
12
+ throttling = arguments.get("throttling")
13
+ # pprint.pprint(throttling)
14
+
15
+ if arguments.get("messages"):
16
+ for message in arguments.get("messages"):
17
+ message_type = message.get("messageType")
18
+ # Message: Displayed answer
19
+ if message_type is None:
20
+ content = message["adaptiveCards"][0]["body"][0]["text"]
21
+ delta_content = content[self.delta_content_pointer :]
22
+ logger.line(delta_content, end="")
23
+ self.delta_content_pointer = len(content)
24
+ # Message: Suggested Questions
25
+ if message.get("suggestedResponses"):
26
+ logger.note("\n\nSuggested Questions: ")
27
+ for suggestion in message.get("suggestedResponses"):
28
+ suggestion_text = suggestion.get("text")
29
+ logger.file(f"- {suggestion_text}")
30
+ # Message: Search Query
31
+ elif message_type in ["InternalSearchQuery"]:
32
+ message_hidden_text = message["hiddenText"]
33
+ logger.note(f"\n[Searching: [{message_hidden_text}]]")
34
+ # Message: Internal Search Results
35
+ elif message_type in ["InternalSearchResult"]:
36
+ logger.note("[Analyzing search results ...]")
37
+ # Message: Loader status, such as "Generating Answers"
38
+ elif message_type in ["InternalLoaderMessage"]:
39
+ logger.note("[Generating answers ...]\n")
40
+ # Message: Render Cards for Webpages
41
+ elif message_type in ["RenderCardRequest"]:
42
+ continue
43
+ # Message: Not Implemented
44
+ else:
45
+ raise NotImplementedError(
46
+ f"Not Supported Message Type: {message_type}"
47
+ )