фиксы
This commit is contained in:
parent
4702df56c7
commit
636731a07b
113
main.py
113
main.py
|
@ -5,12 +5,44 @@ import random
|
||||||
import string
|
import string
|
||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
|
import sys
|
||||||
from urllib.parse import urlparse
|
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):
|
|
||||||
|
class LinkReplacer:
|
||||||
|
def __init__(self):
|
||||||
|
self.placeholder_wrap = ""
|
||||||
|
self.i = 0
|
||||||
|
self.urls = []
|
||||||
|
self.stash = ""
|
||||||
|
self.regex = r'\^(\d+)\^'
|
||||||
|
|
||||||
|
def process(self, content, urls):
|
||||||
|
|
||||||
|
if "[" not in content and self.i == 0:
|
||||||
|
return content
|
||||||
|
|
||||||
|
self.stash += content
|
||||||
|
|
||||||
|
if "[" in content:
|
||||||
|
self.i = 1
|
||||||
|
return ""
|
||||||
|
elif self.i == 1 and re.search(self.regex, self.stash):
|
||||||
|
self.i = 2
|
||||||
|
return ""
|
||||||
|
elif self.i == 1 and not re.search(self.regex, self.stash):
|
||||||
|
self.i = 0
|
||||||
|
return self.stash
|
||||||
|
elif self.i == 2:
|
||||||
|
result = re.sub(r'\[\^(\d+)\^\]', lambda match: create_hyperlink(match, urls), self.stash)
|
||||||
|
self.i = 0
|
||||||
|
self.stash = ""
|
||||||
|
return result
|
||||||
|
|
||||||
|
def create_hyperlink(match, urls):
|
||||||
index = int(match.group(1)) - 1
|
index = int(match.group(1)) - 1
|
||||||
return f" [{urlparse(urls[index]).hostname}]({urls[index]})"
|
return f" [{urlparse(urls[index]).hostname}]({urls[index]})"
|
||||||
|
|
||||||
|
@ -78,7 +110,6 @@ class SSEHandler(web.View):
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
# Return JSON response
|
|
||||||
return web.json_response(data)
|
return web.json_response(data)
|
||||||
|
|
||||||
async def post(self):
|
async def post(self):
|
||||||
|
@ -154,10 +185,8 @@ class SSEHandler(web.View):
|
||||||
async def output():
|
async def output():
|
||||||
print("\nФормируется запрос...")
|
print("\nФормируется запрос...")
|
||||||
|
|
||||||
|
link_replacer = LinkReplacer()
|
||||||
non_stream_response = ""
|
non_stream_response = ""
|
||||||
placeholder_wrap = ""
|
|
||||||
placeholder_flag = False
|
|
||||||
got_number = False
|
|
||||||
wrote = 0
|
wrote = 0
|
||||||
|
|
||||||
async for final, response in chatbot.ask_stream(
|
async for final, response in chatbot.ask_stream(
|
||||||
|
@ -189,7 +218,6 @@ class SSEHandler(web.View):
|
||||||
case None:
|
case None:
|
||||||
if "cursor" in response["arguments"][0]:
|
if "cursor" in response["arguments"][0]:
|
||||||
print("\nОтвет от сервера:\n")
|
print("\nОтвет от сервера:\n")
|
||||||
wrote = 0
|
|
||||||
if message.get("contentOrigin") == "Apology":
|
if message.get("contentOrigin") == "Apology":
|
||||||
if stream and wrote == 0:
|
if stream and wrote == 0:
|
||||||
await self.response.write(prepare_response(filtered_data))
|
await self.response.write(prepare_response(filtered_data))
|
||||||
|
@ -211,57 +239,35 @@ class SSEHandler(web.View):
|
||||||
else:
|
else:
|
||||||
content = message['text'][wrote:]
|
content = message['text'][wrote:]
|
||||||
content = content.replace('\\"', '"')
|
content = content.replace('\\"', '"')
|
||||||
placeholder_number = r'\^(\d+)\^'
|
|
||||||
|
|
||||||
if got_number:
|
if 'urls' in vars():
|
||||||
if "]" not in content:
|
if urls:
|
||||||
content = placeholder_wrap + content
|
content = link_replacer.process(content, urls)
|
||||||
else:
|
|
||||||
content = placeholder_wrap
|
if stream:
|
||||||
got_number = False
|
|
||||||
|
data = {
|
||||||
|
"id": self.id,
|
||||||
|
"object": "chat.completion.chunk",
|
||||||
|
"created": self.created,
|
||||||
|
"model": "gpt-4",
|
||||||
|
"choices": [
|
||||||
|
{
|
||||||
|
"delta": {
|
||||||
|
"content": content
|
||||||
|
},
|
||||||
|
"index": 0,
|
||||||
|
"finish_reason": "null"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
await self.response.write(prepare_response(data))
|
||||||
else:
|
else:
|
||||||
if "[" in content:
|
non_stream_response += content
|
||||||
placeholder_flag = True
|
|
||||||
|
|
||||||
number_matches = re.findall(placeholder_number, content)
|
|
||||||
|
|
||||||
if number_matches:
|
|
||||||
if placeholder_flag:
|
|
||||||
placeholder_wrap = re.sub(placeholder_number,
|
|
||||||
lambda match: replace_with_array(match, urls),
|
|
||||||
message['text'][wrote:]
|
|
||||||
)
|
|
||||||
got_number = True
|
|
||||||
placeholder_flag = False
|
|
||||||
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:
|
|
||||||
|
|
||||||
data = {
|
|
||||||
"id": self.id,
|
|
||||||
"object": "chat.completion.chunk",
|
|
||||||
"created": self.created,
|
|
||||||
"model": "gpt-4",
|
|
||||||
"choices": [
|
|
||||||
{
|
|
||||||
"delta": {
|
|
||||||
"content": content
|
|
||||||
},
|
|
||||||
"index": 0,
|
|
||||||
"finish_reason": "null"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
await self.response.write(prepare_response(data))
|
|
||||||
else:
|
|
||||||
non_stream_response += content
|
|
||||||
|
|
||||||
print(message["text"][wrote:], end="")
|
print(message["text"][wrote:], end="")
|
||||||
|
sys.stdout.flush()
|
||||||
wrote = len(message["text"])
|
wrote = len(message["text"])
|
||||||
|
|
||||||
if "suggestedResponses" in message:
|
if "suggestedResponses" in message:
|
||||||
|
@ -305,6 +311,7 @@ class SSEHandler(web.View):
|
||||||
await self.response.write(prepare_response(filtered_data, end_data))
|
await self.response.write(prepare_response(filtered_data, end_data))
|
||||||
print("Сработал фильтр.")
|
print("Сработал фильтр.")
|
||||||
await chatbot.close()
|
await chatbot.close()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await output()
|
await output()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
Loading…
Reference in New Issue