This commit is contained in:
Junlin He
2023-11-29 23:09:33 +08:00
committed by GitHub
parent ccc2b41035
commit 317ed1b4bf

View File

@ -677,30 +677,31 @@ class ChatBot:
# print(r.choices[0]["delta"]) # print(r.choices[0]["delta"])
if "content" in r.choices[0]["delta"]: if "content" in r.choices[0]["delta"]:
word = r.choices[0]["delta"]["content"] word = r.choices[0]["delta"]["content"]
text.append(word) if word is not None:
whole_text.append(word) text.append(word)
whole_text.append(word)
# Check if the current output ends with \n # Check if the current output ends with \n
if re.search(r'[\n]', word[-1]): if re.search(r'[\n]', word[-1]):
sentence = ''.join(text).strip() sentence = ''.join(text).strip()
if re.search(r'`{3}$', sentence) and code_block: if re.search(r'`{3}$', sentence) and code_block:
# print("code: " , code_block , ", sentence: " + sentence) # print("code: " , code_block , ", sentence: " + sentence)
send_back_message(self.user_id, sentence) send_back_message(self.user_id, sentence)
text = []
code_block = False
else:
if re.search(r'`{3}', sentence) and not code_block:
code_block = True
if re.search(r'\n', sentence):
sentences = text.split("\n")
send_back_message(self.user_id, sentences[0])
sentence = sentences[1]
if not code_block:
sentence = sentence.replace("\n", "")
send_stream(self.user_id, sentence)
text = [] text = []
code_block = False
else:
if re.search(r'`{3}', sentence) and not code_block:
code_block = True
if re.search(r'\n', sentence):
sentences = text.split("\n")
send_back_message(self.user_id, sentences[0])
sentence = sentences[1]
if not code_block:
sentence = sentence.replace("\n", "")
send_stream(self.user_id, sentence)
text = []
text = ''.join(text) text = ''.join(text)
if len(text) > 0: if len(text) > 0:
send(self.user_id, text, stream=self.stream) send(self.user_id, text, stream=self.stream)