Фиксы
This commit is contained in:
parent
90bc7fb188
commit
2591e12a27
82
main.py
82
main.py
|
@ -10,6 +10,10 @@ from urllib.parse import urlparse
|
||||||
PORT = 8081
|
PORT = 8081
|
||||||
HOST = "127.0.0.1"
|
HOST = "127.0.0.1"
|
||||||
|
|
||||||
|
def replace_with_array(match, urls):
|
||||||
|
index = int(match.group(1)) - 1
|
||||||
|
return f" [{urlparse(urls[index]).hostname}]({urls[index]})"
|
||||||
|
|
||||||
def prepare_response(*json_objects):
|
def prepare_response(*json_objects):
|
||||||
response = b""
|
response = b""
|
||||||
|
|
||||||
|
@ -168,7 +172,7 @@ class SSEHandler(web.View):
|
||||||
message = response["arguments"][0]["messages"][0]
|
message = response["arguments"][0]["messages"][0]
|
||||||
match message.get("messageType"):
|
match message.get("messageType"):
|
||||||
case "InternalSearchQuery":
|
case "InternalSearchQuery":
|
||||||
print(f"Поиск в Бинге:\n{message['hiddenText']}\n\n")
|
print(f"Поиск в Бинге:", message['hiddenText'])
|
||||||
case "InternalSearchResult":
|
case "InternalSearchResult":
|
||||||
if 'hiddenText' in message:
|
if 'hiddenText' in message:
|
||||||
search = message['hiddenText'] = message['hiddenText'][len("```json\n"):]
|
search = message['hiddenText'] = message['hiddenText'][len("```json\n"):]
|
||||||
|
@ -205,39 +209,38 @@ class SSEHandler(web.View):
|
||||||
print("\nСообщение отозвано.")
|
print("\nСообщение отозвано.")
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
if stream:
|
content = message['text'][wrote:]
|
||||||
def replace_with_array(match):
|
content = content.replace('\\"', '"')
|
||||||
index = int(match.group(1)) - 1
|
placeholder_number = r'\^(\d+)\^'
|
||||||
return f" [{urlparse(urls[index]).hostname}]({urls[index]})"
|
|
||||||
|
|
||||||
content = message['text'][wrote:]
|
if got_number:
|
||||||
placeholder_number = r'\^(\d+)\^'
|
if "]" not in content:
|
||||||
|
content = placeholder_wrap + content
|
||||||
if got_number:
|
else:
|
||||||
placeholder_flag = False
|
content = placeholder_wrap
|
||||||
if not re.findall(']', content):
|
got_number = False
|
||||||
content = placeholder_wrap + content
|
else:
|
||||||
else:
|
if "[" in content:
|
||||||
content = placeholder_wrap
|
|
||||||
got_number = False
|
|
||||||
if content == "[":
|
|
||||||
placeholder_flag = True
|
placeholder_flag = True
|
||||||
number_matches = re.findall(placeholder_number, content)
|
|
||||||
|
|
||||||
if number_matches:
|
number_matches = re.findall(placeholder_number, content)
|
||||||
if placeholder_flag:
|
|
||||||
placeholder_wrap = re.sub(placeholder_number,
|
if number_matches:
|
||||||
replace_with_array,
|
if placeholder_flag:
|
||||||
message['text'][wrote:]
|
placeholder_wrap = re.sub(placeholder_number,
|
||||||
)
|
lambda match: replace_with_array(match, urls),
|
||||||
got_number = True
|
message['text'][wrote:]
|
||||||
else:
|
)
|
||||||
content = re.sub(placeholder_number,
|
got_number = True
|
||||||
replace_with_array,
|
placeholder_flag = False
|
||||||
message['text'][wrote:]
|
else:
|
||||||
)
|
content = re.sub(placeholder_number,
|
||||||
|
lambda match: replace_with_array(match, urls),
|
||||||
|
message['text'][wrote:]
|
||||||
|
)
|
||||||
|
if not (placeholder_flag or got_number):
|
||||||
|
if stream:
|
||||||
|
|
||||||
if not placeholder_flag:
|
|
||||||
data = {
|
data = {
|
||||||
"id": self.id,
|
"id": self.id,
|
||||||
"object": "chat.completion.chunk",
|
"object": "chat.completion.chunk",
|
||||||
|
@ -255,14 +258,13 @@ class SSEHandler(web.View):
|
||||||
}
|
}
|
||||||
|
|
||||||
await self.response.write(prepare_response(data))
|
await self.response.write(prepare_response(data))
|
||||||
else:
|
else:
|
||||||
non_stream_response += message['text'][wrote:]
|
non_stream_response += content
|
||||||
|
|
||||||
print(message["text"][wrote:], end="")
|
print(message["text"][wrote:], end="")
|
||||||
wrote = len(message["text"])
|
wrote = len(message["text"])
|
||||||
|
|
||||||
if "suggestedResponses" in message:
|
if "suggestedResponses" in message:
|
||||||
print(message)
|
|
||||||
suggested_responses = '\n'.join(x["text"] for x in message["suggestedResponses"])
|
suggested_responses = '\n'.join(x["text"] for x in message["suggestedResponses"])
|
||||||
suggested_responses = "\n```" + suggested_responses + "```"
|
suggested_responses = "\n```" + suggested_responses + "```"
|
||||||
if stream:
|
if stream:
|
||||||
|
@ -286,20 +288,6 @@ class SSEHandler(web.View):
|
||||||
else:
|
else:
|
||||||
await self.response.write(prepare_response(end_data, "DONE"))
|
await self.response.write(prepare_response(end_data, "DONE"))
|
||||||
else:
|
else:
|
||||||
#will fix the duplication later
|
|
||||||
def replace_with_array(match):
|
|
||||||
index = int(match.group(1)) - 1
|
|
||||||
return f" [{urlparse(urls[index]).hostname}]({urls[index]})"
|
|
||||||
|
|
||||||
placeholder_number = r'\[\^(\d+)\^\]'
|
|
||||||
number_matches = re.findall(placeholder_number, non_stream_response)
|
|
||||||
|
|
||||||
if number_matches:
|
|
||||||
non_stream_response = re.sub(placeholder_number,
|
|
||||||
replace_with_array,
|
|
||||||
non_stream_response
|
|
||||||
)
|
|
||||||
|
|
||||||
if suggestion:
|
if suggestion:
|
||||||
non_stream_response = non_stream_response + suggested_responses
|
non_stream_response = non_stream_response + suggested_responses
|
||||||
await self.response.write(
|
await self.response.write(
|
||||||
|
|
Loading…
Reference in New Issue