diff --git a/app/global/consts/api_mode.go b/app/global/consts/api_mode.go index f1ca55d..a68d308 100644 --- a/app/global/consts/api_mode.go +++ b/app/global/consts/api_mode.go @@ -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 的辨认功能; ) diff --git a/app/http/controller/web/rag_controller.go b/app/http/controller/web/rag_controller.go index 8fa20bc..3bb7f8d 100644 --- a/app/http/controller/web/rag_controller.go +++ b/app/http/controller/web/rag_controller.go @@ -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 也许也可以同样掉上面那个接口了。 } diff --git a/app/http/validator/web/rag/chat.go b/app/http/validator/web/rag/chat.go index 183069b..b971772 100644 --- a/app/http/validator/web/rag/chat.go +++ b/app/http/validator/web/rag/chat.go @@ -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) { diff --git a/app/service/nlp/func.go b/app/service/nlp/func.go index 653dc25..54bce8b 100644 --- a/app/service/nlp/func.go +++ b/app/service/nlp/func.go @@ -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) diff --git a/config/prompts.yml b/config/prompts.yml index c87e5d4..c32116e 100644 --- a/config/prompts.yml +++ b/config/prompts.yml @@ -7,15 +7,19 @@ Prompt: 4. 你所服务的前端仅支持简单的文本解析,所以你只能用基本的文本格式回答; 5. 如果知识库中的信息无法回答用户的问题,就说知识库中未找到合适的资料,用户可以选择联系小保的官方QQ账号:3144089037,尝试咨询; 6. 知识库的信息会在消息队列里以 system 的方式和用户 user 区分,做好判断。 + 知识库的组成有: + 1. 关于猫狗流浪动物的科普知识; + 2. 日常生活中同学们上传的日记; 以下是不要使用的要求: 1. 不要使用诸如markdown等文本解析方式的标签,比如**。" # TODO 不确定 6 会不会有用。 Title: "请根据以下长文本生成一个合适的标题,不需要书名号,长度10字内:" - KnoledgeRAG: "使用以知识库来回答用户的问题,如果无法回答,请回答知识库中未找到符合的资料,我不知道。 - 问题: {question} - 可参考的知识库: - ··· - {context} - ··· - 如果给定的知识库无法让你做出回答,请回答知识库中未找到符合的资料,我不知道。" \ No newline at end of file + RAG: + Knowledge: "使用以知识库来回答用户的问题,如果无法回答,请回答知识库中未找到符合的资料,我不知道。 + 问题: {question} + 可参考的知识库: + ··· + {context} + ··· + 如果给定的知识库无法让你做出回答,请回答知识库中未找到符合的资料,我不知道。" \ No newline at end of file