Havoc412 ae7edb5e8d 🎨 refactor(rag): 重构 RAG 模型相关代码
- 重构了 rag_controller.go 中的逻辑,使用新的 DocumentHub 结构
- 修改了 encounter.go 中的 Encounter 结构,增加了 explain 标签
- 重写了 rag_websocket.go 中的逻辑,使用新的 DocumentHub 结构
- 新增了 curd_es/encounter_es_curd.go 文件,实现了 Encounter 的 CURD 操作
- 更新了 nlp/func.go 中的 ChatRAG 函数,使用新的 DocumentHub 结构
- 新增了 curd/docs_hub.go 文件,实现了 DocumentHub 的 TopK 方法
- 新增了 utils/data_explain/data_explain_rag.go 文件,实现了结构体到解释字符串的转换
2024-11-20 19:30:11 +08:00

37 lines
1.0 KiB
Go

package nlp
import (
"catface/app/global/variable"
"catface/app/service/nlp/glm"
"catface/app/service/rag/curd"
"fmt"
"strings"
"github.com/yankeguo/zhipu"
)
func GenerateTitle(content string, client *zhipu.ChatCompletionService) string {
message := variable.PromptsYml.GetString("Prompt.Title") + content
title, _ := glm.Chat(message, client)
return title
}
// ChatKnoledgeRAG 使用 RAG 模型进行知识问答
func ChatRAG(query, mode string, dochub curd.DocumentHub, ch chan<- string, client *zhipu.ChatCompletionService) error {
// 读取配置文件中的 KnoledgeRAG 模板
promptTemplate := variable.PromptsYml.GetString("Prompt.RAG." + mode)
// 替换模板中的占位符
message := strings.Replace(promptTemplate, "{question}", query, -1)
message = strings.Replace(message, "{context}", dochub.Explain4LLM(), -1)
// 调用聊天接口
// err := glm.ChatStream(message, ch)
err := glm.BufferedChatStream(message, ch, client)
if err != nil {
return fmt.Errorf("调用聊天接口失败: %w", err)
}
return nil
}