From cbdaba63a9bd565db528bdd3a411338da80b371d Mon Sep 17 00:00:00 2001 From: liyp Date: Sat, 7 Sep 2024 16:18:20 +0800 Subject: [PATCH] =?UTF-8?q?feat(ai):=20=E6=B7=BB=E5=8A=A0stripMarkdown?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E4=BB=A5=E7=A7=BB=E9=99=A4=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E4=B8=AD=E7=9A=84Markdown=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workers/ai.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/workers/ai.go b/workers/ai.go index 27159b9..66a3b98 100644 --- a/workers/ai.go +++ b/workers/ai.go @@ -79,7 +79,7 @@ func (a *AI) GetMsg() string { return "请求失败" } // println(resp.Choices[0].Message.Content) - return msg + resp.Choices[0].Message.Content + return msg + stripMarkdown(resp.Choices[0].Message.Content) } else { // 匹配回复消息 pattern := `^\[CQ:reply,id=(-?\d+)\]` @@ -159,7 +159,7 @@ func (a *AI) GetMsg() string { msg += resp.Choices[0].Message.Content } - return msg + return stripMarkdown(msg) } models, err := client.ListModels(context.Background()) if err != nil { @@ -178,6 +178,22 @@ func (a *AI) GetMsg() string { // return handleModelRequest(OPENAI_API_KEY, OPENAI_BaseURL) } +func stripMarkdown(text string) string { + // 移除代码块,但保留代码内容 + re := regexp.MustCompile("```(.*?)```") + text = re.ReplaceAllString(text, "$1") + + // 移除粗体 + re = regexp.MustCompile(`\*\*(.*?)\*\*`) + text = re.ReplaceAllString(text, "$1") + + // 移除下划线 + re = regexp.MustCompile("__(.*?)__") + text = re.ReplaceAllString(text, "$1") + + return text +} + func getConfig() (string, string, string) { var OPENAI_API_KEY, OPENAI_BaseURL, MODEL string