From 06b0e800a2e9c8ddaf6d1a4d2a395c0d6609a2f0 Mon Sep 17 00:00:00 2001 From: binary-husky Date: Sun, 21 Jan 2024 12:19:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B8=B2=E6=9F=93=E7=9A=84?= =?UTF-8?q?=E5=B0=8FBUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core_functional.py | 1 + shared_utils/advanced_markdown_format.py | 52 ++++++++++++------------ 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/core_functional.py b/core_functional.py index 5e67eee..d318add 100644 --- a/core_functional.py +++ b/core_functional.py @@ -35,6 +35,7 @@ def get_core_functions(): "Suffix": dedent("\n"+f''' ============================== + 使用mermaid flowchart对以上文本进行总结,概括上述段落的内容以及内在逻辑关系,例如: 以下是对以上文本的总结,以mermaid flowchart的形式展示: diff --git a/shared_utils/advanced_markdown_format.py b/shared_utils/advanced_markdown_format.py index 1194495..103319b 100644 --- a/shared_utils/advanced_markdown_format.py +++ b/shared_utils/advanced_markdown_format.py @@ -32,30 +32,6 @@ code_highlight_configs = { } -def text_divide_paragraph(text): - """ - 将文本按照段落分隔符分割开,生成带有段落标签的HTML代码。 - """ - pre = '
' - suf = "
" - if text.startswith(pre) and text.endswith(suf): - return text - - if "```" in text: - # careful input - return text - elif "" in text: - # careful input - return text - else: - # whatever input - lines = text.split("\n") - for i, line in enumerate(lines): - lines[i] = lines[i].replace(" ", " ") - text = "
".join(lines) - return pre + text + suf - - def tex2mathml_catch_exception(content, *args, **kwargs): try: content = tex2mathml(content, *args, **kwargs) @@ -301,11 +277,35 @@ def close_up_code_segment_during_stream(gpt_reply): else: return gpt_reply + +def compat_non_markdown_input(text): + """ + 改善非markdown输入的显示效果,例如将空格转换为 ,将换行符转换为
等。 + """ + + if "```" in text: + # careful input:markdown输入 + return text + elif "" in text: + # careful input:html输入 + return text + else: + # whatever input:非markdown输入 + lines = text.split("\n") + for i, line in enumerate(lines): + lines[i] = lines[i].replace(" ", " ") # 空格转换为  + text = "
".join(lines) # 换行符转换为
+ return text + + +@lru_cache(maxsize=128) # 使用lru缓存 def simple_markdown_convertion(txt): pre = '
' suf = "
" if txt.startswith(pre) and txt.endswith(suf): return txt # 已经被转化过,不需要再次转化 + + txt = compat_non_markdown_input(txt) # 兼容非markdown输入 txt = markdown.markdown( txt, extensions=["pymdownx.superfences", "tables", "pymdownx.highlight"], @@ -313,6 +313,7 @@ def simple_markdown_convertion(txt): ) return pre + txt + suf + def format_io(self, y): """ 将输入和输出解析为HTML格式。将y中最后一项的输入部分段落化,并将输出部分的Markdown和数学公式转换为HTML格式。 @@ -322,9 +323,6 @@ def format_io(self, y): i_ask, gpt_reply = y[-1] i_ask = apply_gpt_academic_string_mask(i_ask, mode="show_render") gpt_reply = apply_gpt_academic_string_mask(gpt_reply, mode="show_render") - # 输入部分太自由,预处理一波 - if i_ask is not None: - i_ask = text_divide_paragraph(i_ask) # 当代码输出半截的时候,试着补上后个``` if gpt_reply is not None: gpt_reply = close_up_code_segment_during_stream(gpt_reply)