feat(nlp): 重构 RAG 聊天模式并添加新功能

- 重构 RAG 聊天模式,支持知识、日记和检测等多种模式
- 新增日记和检测模式的实现
- 更新 API 接口和前端交互逻辑,支持新的聊天模式
- 优化 prompts.yml 文件结构,更好地支持不同模式的提示模板
This commit is contained in:
Havoc412 2024-11-20 13:26:31 +08:00
parent 89bb14a298
commit 679d30dc7b
5 changed files with 29 additions and 16 deletions

View File

@ -2,7 +2,10 @@ package consts
const (
// TAG animal/ 接口模式
AnimalModePrefer string = "prefer"
AnimalModePrefer string = "prefer" // 根据用户行为记录优先返回【偏好目标】
// TAG rag/
// TAG rag/chat 接口模式; 配合 yml 文件的书写习惯。
RagChatModeKnowledge string = "Knowledge"
RagChatModeDiary string = "Diary" // 查询路遇资料等
RagChatModeDetect string = "Detect" // 辅助 catface 的辨认功能;
)

View File

@ -112,7 +112,7 @@ func (r *Rag) ChatSSE(context *gin.Context) {
// 3. LLM answer
go func() {
err := nlp.ChatKnoledgeRAG(docs[0].Content, query, ch, client)
err := nlp.ChatRAG(docs[0].Content, query, ch, client)
if err != nil {
variable.ZapLog.Error("ChatKnoledgeRAG error", zap.Error(err))
}
@ -145,6 +145,12 @@ func (r *Rag) ChatWebSocket(context *gin.Context) {
query := context.Query("query")
token := context.Query("token")
// INFO 查询模式
mode := context.Query("mode")
if mode == "" {
mode = consts.RagChatModeKnowledge
}
if token == "" {
token = variable.SnowFlake.GetIdAsString()
}
@ -192,7 +198,7 @@ func (r *Rag) ChatWebSocket(context *gin.Context) {
return
}
// 2. ES TopK // TODO 这里需要特化选取不同知识库的文档;目前是依靠显式的路由。
// 2. ES TopK // INFO 这里需要特化选取不同知识库的文档;目前是依靠显式的路由。
docs, err := curd.CreateDocCurdFactory().TopK(embedding, 1)
if err != nil || len(docs) == 0 {
variable.ZapLog.Error("ES TopK error", zap.Error(err))
@ -228,7 +234,7 @@ func (r *Rag) ChatWebSocket(context *gin.Context) {
ch := make(chan string) // TIP 建立通道。
go func() {
err := nlp.ChatKnoledgeRAG(docs[0].Content, query, ch, clientInfo.Client)
err := nlp.ChatRAG(docs[0].Content, query, mode, ch, clientInfo.Client)
if err != nil {
variable.ZapLog.Error("ChatKnoledgeRAG error", zap.Error(err))
}
@ -253,5 +259,5 @@ func (r *Rag) ChatWebSocket(context *gin.Context) {
}
func (r *Rag) HelpDetectCat(context *gin.Context) {
// TODO
// TODO 也许也可以同样掉上面那个接口了。
}

View File

@ -15,7 +15,7 @@ type Chat struct {
Token string `form:"token" json:"token"` // UPDATE 暂时不想启用 user 的 token就先单独处理。
Mode string `form:"mode" json:"mode"`
CatsId string `form:"cats_id" json:"cats_id"` //
CatsId string `form:"cats_id" json:"cats_id"` // [日记 || 辨别] 模式下使用的参数。
}
func (c Chat) CheckParams(context *gin.Context) {

View File

@ -16,9 +16,9 @@ func GenerateTitle(content string, client *zhipu.ChatCompletionService) string {
}
// ChatKnoledgeRAG 使用 RAG 模型进行知识问答
func ChatKnoledgeRAG(doc, query string, ch chan<- string, client *zhipu.ChatCompletionService) error {
func ChatRAG(doc, query, mode string, ch chan<- string, client *zhipu.ChatCompletionService) error {
// 读取配置文件中的 KnoledgeRAG 模板
promptTemplate := variable.PromptsYml.GetString("Prompt.KnoledgeRAG")
promptTemplate := variable.PromptsYml.GetString("Prompt.RAG." + mode)
// 替换模板中的占位符
message := strings.Replace(promptTemplate, "{question}", query, -1)

View File

@ -7,15 +7,19 @@ Prompt:
4. 你所服务的前端仅支持简单的文本解析,所以你只能用基本的文本格式回答;
5. 如果知识库中的信息无法回答用户的问题就说知识库中未找到合适的资料用户可以选择联系小保的官方QQ账号3144089037尝试咨询
6. 知识库的信息会在消息队列里以 system 的方式和用户 user 区分,做好判断。
知识库的组成有:
1. 关于猫狗流浪动物的科普知识;
2. 日常生活中同学们上传的日记;
以下是不要使用的要求:
1. 不要使用诸如markdown等文本解析方式的标签比如**。" # TODO 不确定 6 会不会有用。
Title: "请根据以下长文本生成一个合适的标题不需要书名号长度10字内"
KnoledgeRAG: "使用以知识库来回答用户的问题,如果无法回答,请回答知识库中未找到符合的资料,我不知道。
问题: {question}
可参考的知识库:
···
{context}
···
如果给定的知识库无法让你做出回答,请回答知识库中未找到符合的资料,我不知道。"
RAG:
Knowledge: "使用以知识库来回答用户的问题,如果无法回答,请回答知识库中未找到符合的资料,我不知道。
问题: {question}
可参考的知识库:
···
{context}
···
如果给定的知识库无法让你做出回答,请回答知识库中未找到符合的资料,我不知道。"