feat(ai): 添加stripMarkdown函数以移除消息中的Markdown格式

This commit is contained in:
liyp 2024-09-07 16:18:20 +08:00
parent 554949feb7
commit cbdaba63a9

View file

@ -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