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