用户反馈与交互跟踪
用户反馈与交互追踪
相关源文件
本章引用的主要源码文件:
cognee/infrastructure/llm/prompts/agent_trace_feedback_summary_system.txtcognee/infrastructure/llm/prompts/feedback_detection_system.txtcognee/infrastructure/session/feedback_detection.pycognee/infrastructure/session/feedback_models.pycognee/infrastructure/session/session_manager.pycognee/memify_pipelines/persist_sessions_in_knowledge_graph.pycognee/modules/agent_memory/sanitization.pycognee/shared/exceptions/__init__.pycognee/shared/exceptions/exceptions.pycognee/shared/usage_logger.pycognee/tasks/memify/cognify_session.pycognee/tasks/memify/extract_user_sessions.pycognee/tests/integration/infrastructure/session/test_session_manager_fs.pycognee/tests/integration/infrastructure/session/test_session_manager_redis.pycognee/tests/integration/infrastructure/session/test_session_persistence_memify_integration.pycognee/tests/test_conversation_history.pycognee/tests/unit/infrastructure/session/test_session_manager.pycognee/tests/unit/modules/memify_tasks/test_cognify_session.pycognee/tests/unit/modules/memify_tasks/test_extract_user_sessions.py
本文档描述了 Cognee 用于追踪用户交互、管理对话会话以及收集 AI 生成响应反馈的系统。该系统支持跨多个查询的持久化记忆,使 AI 能够维护上下文并随时间推移从用户反馈中学习。它与检索策略集成,记录每个响应所涉及的知识图谱元素。
有关检索策略本身的信息,请参阅搜索类型概览。有关图谱元素如何存储和查询的详细信息,请参阅图数据库适配器。
目的与范围
用户反馈与交互追踪系统提供三个主要功能:
- 对话会话管理:使用
SessionManager维护会话内多个用户查询的对话历史和上下文cognee/infrastructure/session/session_manager.py:104-107。 - 交互日志记录:通过
add_qa记录查询-答案对,以及用于生成每个响应的图谱元素(节点、边)的元数据cognee/infrastructure/session/session_manager.py:141-152。 - 反馈收集:使用基于大语言模型(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_ids和edge_ids列表cognee/infrastructure/session/session_manager.py:151-152。
交互结构
每个保存的交互(问答条目)由 SessionQAEntry 建模 cognee/infrastructure/databases/cache/models.py。关键字段包括:
| 字段 | 类型 | 描述 |
|---|---|---|
question | str | 用户的提问或提示。 |
answer | str | AI 生成的响应。 |
context | str | 使用的图谱上下文或文档片段。 |
used_graph_element_ids | dict | 包含 node_ids 和 edge_ids 列表的字典 cognee/infrastructure/session/session_manager.py:151-152。 |
feedback_score | float | 用户反馈评分(1-5)cognee/infrastructure/session/feedback_models.py:19-22。 |
feedback_text | str | 消息被检测为反馈的原因描述 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(可以是 RedisAdapter 或 FsCacheAdapter)来存储和检索问答条目 cognee/infrastructure/session/session_manager.py:118-127。
会话检索与完成流程
来源: 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/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。
使用日志流程
- 消毒:对输入和结果进行消毒处理,使其可进行 JSON 序列化,处理
UUID和datetime等复杂类型cognee/shared/usage_logger.py:19-72。 - 异步日志:
_log_usage_async将log_entry写入缓存引擎,并设置配置中定义的生存时间(TTL)cognee/shared/usage_logger.py:157-208。 - 指标:通过
_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。
- 提取:
extract_user_sessions从SessionManager缓存中读取数据cognee/tasks/memify/extract_user_sessions.py。 - 认知化:
cognify_session将会话历史处理为图谱节点和边,并使用user_sessions_from_cache节点集进行标记cognee/memify_pipelines/persist_sessions_in_knowledge_graph.py:28-30。 - 丰富:会话被添加到指定的数据集中,使未来的搜索能够将过去的对话作为结构化知识进行引用
cognee/memify_pipelines/persist_sessions_in_knowledge_graph.py:58-64。
来源:
cognee/memify_pipelines/persist_sessions_in_knowledge_graph.py:19-67cognee/tasks/memify/extract_user_sessions.pycognee/tasks/memify/cognify_session.py