Spaces:
Runtime error
Runtime error
:zap: [Enhance] Prettify format of searching queries and results
Browse files- networks/message_outputer.py +49 -8
- networks/message_parser.py +27 -21
networks/message_outputer.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import json
|
|
|
2 |
|
3 |
|
4 |
class OpenaiStreamOutputer:
|
@@ -30,14 +31,7 @@ class OpenaiStreamOutputer:
|
|
30 |
"finish_reason": None,
|
31 |
}
|
32 |
]
|
33 |
-
elif content_type
|
34 |
-
"Completions",
|
35 |
-
"InternalSearchQuery",
|
36 |
-
"InternalSearchResult",
|
37 |
-
"SuggestedResponses",
|
38 |
-
]:
|
39 |
-
if content_type in ["InternalSearchQuery", "InternalSearchResult"]:
|
40 |
-
content += "\n"
|
41 |
data["choices"] = [
|
42 |
{
|
43 |
"index": 0,
|
@@ -45,6 +39,53 @@ class OpenaiStreamOutputer:
|
|
45 |
"finish_reason": None,
|
46 |
}
|
47 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
elif content_type == "Finished":
|
49 |
data["choices"] = [
|
50 |
{
|
|
|
1 |
import json
|
2 |
+
import re
|
3 |
|
4 |
|
5 |
class OpenaiStreamOutputer:
|
|
|
31 |
"finish_reason": None,
|
32 |
}
|
33 |
]
|
34 |
+
elif content_type == "Completions":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
data["choices"] = [
|
36 |
{
|
37 |
"index": 0,
|
|
|
39 |
"finish_reason": None,
|
40 |
}
|
41 |
]
|
42 |
+
elif content_type == "InternalSearchQuery":
|
43 |
+
search_str = f"Searching: [**{content.strip()}**]\n"
|
44 |
+
data["choices"] = [
|
45 |
+
{
|
46 |
+
"index": 0,
|
47 |
+
"delta": {"content": search_str},
|
48 |
+
"finish_reason": None,
|
49 |
+
}
|
50 |
+
]
|
51 |
+
elif content_type == "InternalSearchResult":
|
52 |
+
invocation = content["invocation"]
|
53 |
+
web_search_results = content["web_search_results"]
|
54 |
+
matches = re.search('\(query="(.*)"\)', invocation)
|
55 |
+
if matches:
|
56 |
+
search_query = matches.group(1)
|
57 |
+
else:
|
58 |
+
search_query = invocation
|
59 |
+
search_str = f"Searching: [**{search_query.strip()}**]"
|
60 |
+
search_results_str_list = []
|
61 |
+
for idx, search_result in enumerate(web_search_results):
|
62 |
+
search_results_str_list.append(
|
63 |
+
f"{idx+1}. [{search_result['title']}]({search_result['url']})"
|
64 |
+
)
|
65 |
+
search_results_str = "\n".join(search_results_str_list)
|
66 |
+
search_results_str = (
|
67 |
+
f"<details>\n"
|
68 |
+
f"<summary>\n{search_str}\n</summary>\n"
|
69 |
+
f"{search_results_str}\n"
|
70 |
+
f"</details>\n"
|
71 |
+
)
|
72 |
+
data["choices"] = [
|
73 |
+
{
|
74 |
+
"index": 0,
|
75 |
+
"delta": {"content": search_results_str},
|
76 |
+
"finish_reason": None,
|
77 |
+
}
|
78 |
+
]
|
79 |
+
elif content_type == "SuggestedResponses":
|
80 |
+
suggestion_texts_str = "\n\n---\n\n**Suggested Questions:**\n"
|
81 |
+
suggestion_texts_str += "\n".join(f"- {item}" for item in content)
|
82 |
+
data["choices"] = [
|
83 |
+
{
|
84 |
+
"index": 0,
|
85 |
+
"delta": {"content": suggestion_texts_str},
|
86 |
+
"finish_reason": None,
|
87 |
+
}
|
88 |
+
]
|
89 |
elif content_type == "Finished":
|
90 |
data["choices"] = [
|
91 |
{
|
networks/message_parser.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
from utils.logger import logger
|
2 |
from networks import OpenaiStreamOutputer
|
3 |
|
@@ -17,7 +18,8 @@ class MessageParser:
|
|
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)
|
@@ -35,37 +37,41 @@ class MessageParser:
|
|
35 |
delta_content, content_type="Completions"
|
36 |
)
|
37 |
if message.get("suggestedResponses"):
|
38 |
-
suggestion_texts_str = (
|
39 |
-
"\n\n---\n\n**Suggested Questions:**\n"
|
40 |
-
)
|
41 |
-
suggestion_texts_str += "\n".join(
|
42 |
-
f"- {item}" for item in suggestion_texts
|
43 |
-
)
|
44 |
suggestions_output = self.outputer.output(
|
45 |
-
|
46 |
content_type="SuggestedResponses",
|
47 |
)
|
48 |
return [completions_output, suggestions_output]
|
49 |
else:
|
50 |
return completions_output
|
51 |
-
|
52 |
# Message: Search Query
|
53 |
elif message_type in ["InternalSearchQuery"]:
|
54 |
-
|
55 |
-
search_str = f"[Searching: [{message_hidden_text}]]"
|
56 |
-
logger.note(search_str)
|
57 |
if return_output:
|
58 |
-
|
59 |
-
|
60 |
-
)
|
|
|
|
|
|
|
61 |
# Message: Internal Search Results
|
62 |
elif message_type in ["InternalSearchResult"]:
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
# Message: Loader status, such as "Generating Answers"
|
70 |
elif message_type in ["InternalLoaderMessage"]:
|
71 |
# logger.note("[Generating answers ...]\n")
|
|
|
1 |
+
import json
|
2 |
from utils.logger import logger
|
3 |
from networks import OpenaiStreamOutputer
|
4 |
|
|
|
18 |
message_type = message.get("messageType")
|
19 |
# Message: Displayed answer
|
20 |
if message_type is None:
|
21 |
+
# content = message["adaptiveCards"][0]["body"][0]["text"]
|
22 |
+
content = message["text"]
|
23 |
delta_content = content[self.delta_content_pointer :]
|
24 |
logger.line(delta_content, end="")
|
25 |
self.delta_content_pointer = len(content)
|
|
|
37 |
delta_content, content_type="Completions"
|
38 |
)
|
39 |
if message.get("suggestedResponses"):
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
suggestions_output = self.outputer.output(
|
41 |
+
suggestion_texts,
|
42 |
content_type="SuggestedResponses",
|
43 |
)
|
44 |
return [completions_output, suggestions_output]
|
45 |
else:
|
46 |
return completions_output
|
|
|
47 |
# Message: Search Query
|
48 |
elif message_type in ["InternalSearchQuery"]:
|
49 |
+
search_query_str = message.get("hiddenText")
|
|
|
|
|
50 |
if return_output:
|
51 |
+
# output_str = self.outputer.output(
|
52 |
+
# search_query_str, content_type="InternalSearchQuery"
|
53 |
+
# )
|
54 |
+
# logger.note(output_str)
|
55 |
+
# return output_str
|
56 |
+
return None
|
57 |
# Message: Internal Search Results
|
58 |
elif message_type in ["InternalSearchResult"]:
|
59 |
+
if message.get("groundingInfo"):
|
60 |
+
web_search_results = message.get("groundingInfo").get(
|
61 |
+
"web_search_results"
|
62 |
+
)
|
63 |
+
invocation = message.get("invocation")
|
64 |
+
if return_output:
|
65 |
+
search_results_str = self.outputer.output(
|
66 |
+
{
|
67 |
+
"invocation": invocation,
|
68 |
+
"web_search_results": web_search_results,
|
69 |
+
},
|
70 |
+
content_type="InternalSearchResult",
|
71 |
+
)
|
72 |
+
data = json.loads(search_results_str)
|
73 |
+
logger.note(data["choices"][0]["delta"]["content"])
|
74 |
+
return search_results_str
|
75 |
# Message: Loader status, such as "Generating Answers"
|
76 |
elif message_type in ["InternalLoaderMessage"]:
|
77 |
# logger.note("[Generating answers ...]\n")
|