agentic_huge_data_base / wiki
页面 Cognee · 4.5 用户反馈与交互跟踪·DeepWiki 中文全文译文

4.5 · 用户反馈与交互跟踪(User Feedback and Interaction Tracking)

记忆管道与知识图谱构建 · 聚焦本章的模块关系、源码依据与实现要点。

项目Cognee 章节4.5 状态全文译文 模块记忆与上下文、图谱与关系、系统架构、测试、发布与运维
源码线索
  • cognee/infrastructure/llm/prompts/agent_trace_feedback_summary_system.txt
  • cognee/infrastructure/llm/prompts/feedback_detection_system.txt
  • cognee/infrastructure/session/feedback_detection.py
  • cognee/infrastructure/session/feedback_models.py
  • cognee/infrastructure/session/session_manager.py
  • cognee/memify_pipelines/persist_sessions_in_knowledge_graph.py
  • cognee/modules/agent_memory/sanitization.py
  • cognee/shared/exceptions/__init__.py
  • cognee/shared/exceptions/exceptions.py
  • cognee/shared/usage_logger.py
模块标签
  • 记忆与上下文
  • 图谱与关系
  • 系统架构
  • 测试、发布与运维
  • 存储与持久化

章节正文

用户反馈与交互跟踪

用户反馈与交互追踪

相关源文件

本章引用的主要源码文件:

  • cognee/infrastructure/llm/prompts/agent_trace_feedback_summary_system.txt
  • cognee/infrastructure/llm/prompts/feedback_detection_system.txt
  • cognee/infrastructure/session/feedback_detection.py
  • cognee/infrastructure/session/feedback_models.py
  • cognee/infrastructure/session/session_manager.py
  • cognee/memify_pipelines/persist_sessions_in_knowledge_graph.py
  • cognee/modules/agent_memory/sanitization.py
  • cognee/shared/exceptions/__init__.py
  • cognee/shared/exceptions/exceptions.py
  • cognee/shared/usage_logger.py
  • cognee/tasks/memify/cognify_session.py
  • cognee/tasks/memify/extract_user_sessions.py
  • cognee/tests/integration/infrastructure/session/test_session_manager_fs.py
  • cognee/tests/integration/infrastructure/session/test_session_manager_redis.py
  • cognee/tests/integration/infrastructure/session/test_session_persistence_memify_integration.py
  • cognee/tests/test_conversation_history.py
  • cognee/tests/unit/infrastructure/session/test_session_manager.py
  • cognee/tests/unit/modules/memify_tasks/test_cognify_session.py
  • cognee/tests/unit/modules/memify_tasks/test_extract_user_sessions.py

本文档描述了 Cognee 用于追踪用户交互、管理对话会话以及收集 AI 生成响应反馈的系统。该系统支持跨多个查询的持久化记忆,使 AI 能够维护上下文并随时间推移从用户反馈中学习。它与检索策略集成,记录每个响应所涉及的知识图谱元素。

有关检索策略本身的信息,请参阅搜索类型概览。有关图谱元素如何存储和查询的详细信息,请参阅图数据库适配器

目的与范围

用户反馈与交互追踪系统提供三个主要功能:

  1. 对话会话管理:使用 SessionManager 维护会话内多个用户查询的对话历史和上下文 cognee/infrastructure/session/session_manager.py:104-107
  2. 交互日志记录:通过 add_qa 记录查询-答案对,以及用于生成每个响应的图谱元素(节点、边)的元数据 cognee/infrastructure/session/session_manager.py:141-152
  3. 反馈收集:使用基于大语言模型(LLM)的检测机制,检测并加权用户对响应的反馈(正面/负面),以改进未来的检索 cognee/infrastructure/session/feedback_detection.py:11-18

核心概念

会话标识

会话通过 session_id 字符串进行标识。如果未提供,系统默认使用 "default_session" cognee/infrastructure/session/session_manager.py:132-134。当会话处于活动状态时,系统会:

  • 加载最近 $N$ 条对话记录(默认 10 条)作为上下文 cognee/infrastructure/session/session_manager.py:125-130
  • 通过 used_graph_element_ids 追踪每个响应中使用的图谱元素,该字段存储 node_idsedge_ids 列表 cognee/infrastructure/session/session_manager.py:151-152
交互结构

每个保存的交互(问答条目)由 SessionQAEntry 建模 cognee/infrastructure/databases/cache/models.py。关键字段包括:

字段类型描述
questionstr用户的提问或提示。
answerstrAI 生成的响应。
contextstr使用的图谱上下文或文档片段。
used_graph_element_idsdict包含 node_idsedge_ids 列表的字典 cognee/infrastructure/session/session_manager.py:151-152
feedback_scorefloat用户反馈评分(1-5)cognee/infrastructure/session/feedback_models.py:19-22
feedback_textstr消息被检测为反馈的原因描述 cognee/infrastructure/session/feedback_models.py:15-18
反馈权重

反馈权重通过提升或惩罚图谱元素来影响未来的检索。系统使用 1-5 的评分范围 cognee/infrastructure/llm/prompts/feedback_detection_system.txt:28-28

  • 正面反馈(4-5):对应"正确"或"谢谢"等表述。
  • 负面反馈(1-2):对应"错误"或"不对"等表述。
  • 中性反馈(3):推断评分的默认值。

会话管理架构

SessionManager 作为中央编排器,使用 cache_engine(可以是 RedisAdapterFsCacheAdapter)来存储和检索问答条目 cognee/infrastructure/session/session_manager.py:118-127

会话检索与完成流程
Cognee · 会话检索与完成流程 · 图 1
Cognee · 会话检索与完成流程 · 图 1

来源: cognee/infrastructure/session/session_manager.py:141-152 cognee/infrastructure/session/session_manager.py:34-46 cognee/infrastructure/session/feedback_detection.py:11-18

反馈检测

Cognee 包含一个自动化的反馈检测系统,该系统通过大语言模型(LLM)分析用户消息,以判断用户是否在评估之前的响应 cognee/infrastructure/session/feedback_detection.py:11-18

反馈检测逻辑

大语言模型(LLM)使用专门的提示来区分新问题和针对之前回答的评估 cognee/infrastructure/llm/prompts/feedback_detection_system.txt:1-15

Cognee · 反馈检测逻辑 · 图 2
Cognee · 反馈检测逻辑 · 图 2

来源: cognee/infrastructure/session/feedback_models.py:6-31 cognee/infrastructure/llm/prompts/feedback_detection_system.txt:1-30 cognee/infrastructure/session/feedback_detection.py:11-33

代理追踪

除了标准的问答功能外,Cognee 还追踪"代理追踪"——函数调用和内部推理步骤的序列 cognee/infrastructure/session/session_manager.py:221-235

追踪组件

当代理执行任务时,add_agent_trace_step 会记录以下内容:

  • origin_function:正在执行的代码实体(函数名)cognee/infrastructure/session/session_manager.py:225
  • status:"success" 或 "error" cognee/infrastructure/session/session_manager.py:226
  • memory_query:用于从图谱检索上下文的查询 cognee/infrastructure/session/session_manager.py:228
  • session_feedback:由大语言模型(LLM)根据函数返回值生成的一句话可读摘要 cognee/infrastructure/session/feedback_models.py:33-39

来源: cognee/infrastructure/session/session_manager.py:221-250 cognee/infrastructure/session/feedback_models.py:33-47

使用日志与监控

usage_logger 装饰器和工具函数提供函数执行的异步日志记录到 Redis cognee/shared/usage_logger.py:157-186

使用日志流程
  1. 消毒:对输入和结果进行消毒处理,使其可进行 JSON 序列化,处理 UUIDdatetime 等复杂类型 cognee/shared/usage_logger.py:19-72
  2. 异步日志_log_usage_asynclog_entry 写入缓存引擎,并设置配置中定义的生存时间(TTL)cognee/shared/usage_logger.py:157-208
  3. 指标:通过 _record_session_activity 在关系数据库中记录会话活动,该函数会更新 SessionRecord 行以追踪生命周期心跳 cognee/infrastructure/session/session_manager.py:34-46

来源: cognee/shared/usage_logger.py:19-208 cognee/infrastructure/session/session_manager.py:34-76

知识图谱中的会话持久化

用户会话可以通过 persist_sessions_in_knowledge_graph_pipeline 从临时缓存存储提升到永久知识图谱中 cognee/memify_pipelines/persist_sessions_in_knowledge_graph.py:19-24

  1. 提取extract_user_sessionsSessionManager 缓存中读取数据 cognee/tasks/memify/extract_user_sessions.py
  2. 认知化cognify_session 将会话历史处理为图谱节点和边,并使用 user_sessions_from_cache 节点集进行标记 cognee/memify_pipelines/persist_sessions_in_knowledge_graph.py:28-30
  3. 丰富:会话被添加到指定的数据集中,使未来的搜索能够将过去的对话作为结构化知识进行引用 cognee/memify_pipelines/persist_sessions_in_knowledge_graph.py:58-64

来源:

  • cognee/memify_pipelines/persist_sessions_in_knowledge_graph.py:19-67
  • cognee/tasks/memify/extract_user_sessions.py
  • cognee/tasks/memify/cognify_session.py