agentic_huge_data_base / wiki
页面 Cognee · 4.6 Agentic 与 Skill-Based 召回·DeepWiki 中文全文译文

4.6 · Agentic 与 Skill-Based 召回(Agentic and Skill-Based Retrieval)

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

项目Cognee 章节4.6 状态全文译文 模块检索、召回与索引、入库与解析、图谱与关系、智能体运行时
源码线索
  • cognee/api/v1/search/search.py
  • cognee/modules/engine/models/Skill.py
  • cognee/modules/engine/models/SkillImprovementProposal.py
  • cognee/modules/engine/models/SkillRun.py
  • cognee/modules/memify/skill_improvement.py
  • cognee/modules/retrieval/agentic_retriever.py
  • cognee/modules/retrieval/coding_rules_retriever.py
  • cognee/modules/search/methods/get_search_type_retriever_instance.py
  • cognee/modules/search/methods/search.py
  • cognee/modules/search/models/SearchResultPayload.py
模块标签
  • 检索、召回与索引
  • 入库与解析
  • 图谱与关系
  • 智能体运行时
  • 测试、发布与运维

章节正文

Agentic 与 Skill-Based 召回

基于智能体与技能的检索

相关源文件

以下文件为本维基页面的生成提供了上下文:

  • cognee/api/v1/search/search.py
  • cognee/modules/engine/models/Skill.py
  • cognee/modules/engine/models/SkillImprovementProposal.py
  • cognee/modules/engine/models/SkillRun.py
  • cognee/modules/memify/skill_improvement.py
  • cognee/modules/retrieval/agentic_retriever.py
  • cognee/modules/retrieval/coding_rules_retriever.py
  • cognee/modules/search/methods/get_search_type_retriever_instance.py
  • cognee/modules/search/methods/search.py
  • cognee/modules/search/models/SearchResultPayload.py
  • cognee/modules/search/types/SearchType.py
  • cognee/modules/tools/ingest_skills.py
  • cognee/modules/tools/resolve_skills.py
  • cognee/modules/tools/skill_parser.py
  • cognee/modules/tools/skill_runs.py
  • cognee/tests/unit/modules/tools/test_skill_ingest.py
  • examples/demos/skill_feedback_loop/data/bad_pr_comment.txt
  • examples/demos/skill_feedback_loop/data/tiny_diff.patch
  • examples/demos/skill_feedback_loop/skill_feedback_loop_demo.py
  • temp/PR_review_topoteretes_cognee_2705/micro_real_sdk_skills_demo.py

基于智能体与技能的检索代表了从被动数据检索到主动、目标导向推理的转变。通过使用 AgenticRetriever,Cognee 使 AI 智能体不仅能够搜索记忆三元组,还能选择并执行特定的"技能"(以 Markdown 定义的结构化流程),并调用工具来满足复杂查询。

AgenticRetriever

AgenticRetriever 扩展了 GraphCompletionRetriever,将知识图谱检索与 ReAct 风格(推理与行动)的执行循环统一起来。它由 AGENTIC_COMPLETION 搜索类型触发。

实现与数据流

当使用 SearchType.AGENTIC_COMPLETION 发起搜索时,系统会实例化一个 AgenticRetriever cognee/modules/search/methods/get_search_type_retriever_instance.py:32-127

  1. 技能与工具解析:检索器使用 resolve_skills 识别相关技能,并根据数据集范围和技能声明确定允许使用的工具 cognee/modules/retrieval/agentic_retriever.py:124-165
  2. 记忆检索:它执行标准的图三元组搜索,从知识图谱中提供上下文 cognee/modules/retrieval/agentic_retriever.py:131-132
  3. ReAct 循环:为大语言模型(LLM)提供包含工具清单和可用技能的系统提示。它会生成一个 AgentStep,可以是 tool_callfinal_answer cognee/modules/retrieval/agentic_retriever.py:51-67
  4. 工具执行:如果调用了工具,则会调用 execute_tool。该函数会强制执行每个数据集的权限,确保智能体无法访问其授权范围之外的数据或工具 cognee/modules/retrieval/agentic_retriever.py:32-192
智能体检索流程
Cognee · 智能体检索流程 · 图 1
Cognee · 智能体检索流程 · 图 1

来源:cognee/modules/retrieval/agentic_retriever.py:68-172cognee/modules/search/methods/get_search_type_retriever_instance.py:32-127

技能入库与解析

技能被定义为 SKILL.md 文件,其中包含用于元数据(描述、允许的工具)的 YAML 前置元数据和用于流程的 Markdown 正文 cognee/tests/unit/modules/tools/test_skill_ingest.py:13-21

技能解析逻辑

add_skills 函数负责将这些文件入库到系统中 cognee/modules/tools/ingest_skills.py:115-122

  • 规范化:路径会被规范化,并检查是否在 COGNEE_SKILL_SOURCE_ROOTS 定义的允许根目录内 cognee/modules/tools/ingest_skills.py:24-68
  • 数据集范围限定:技能严格限定在数据集范围内。两个不同数据集中同名的技能会获得不同的 UUID cognee/modules/tools/ingest_skills.py:93-102
  • 持久化:解析后的技能会作为 Skill 节点保存到图中,并与 NodeSet(默认为"skills")关联 cognee/modules/tools/ingest_skills.py:148-163
代码实体关联:技能入库
Cognee · 代码实体关联:技能入库 · 图 2
Cognee · 代码实体关联:技能入库 · 图 2

来源:cognee/modules/tools/ingest_skills.py:115-165cognee/modules/engine/models/Skill.py:1-20

技能反馈循环与 SkillRun 追踪

为了随时间提升智能体性能,Cognee 使用 SkillRun 模型追踪每次技能的执行 cognee/modules/engine/models/SkillRun.py:33-67

SkillRun 组件
  • run_id:执行的唯一标识符。
  • success_score:一个浮点数(0.0 到 1.0),表示结果的质量 cognee/modules/engine/models/SkillRun.py:41
  • tool_trace:一个 ToolCall 对象列表,捕获运行期间每次工具调用、输入和输出 cognee/modules/engine/models/SkillRun.py:10-18
  • feedback:人工或系统提供的反馈(-1.0 到 1.0)cognee/modules/engine/models/SkillRun.py:59
改进循环

improve_skill 操作会分析近期得分较低或存在错误的 SkillRun 记录 cognee/modules/memify/skill_improvement.py:85-133

  1. 失败分析:它会检索特定技能的近期失败记录。
  2. 提案生成:大语言模型(LLM)分析失败原因和当前流程,生成一个 SkillImprovementProposal cognee/modules/memify/skill_improvement.py:135-157
  3. 应用:如果向 improve_skill_from_config 传递了 apply=True,则技能的流程会在图中更新为新版本 cognee/modules/memify/skill_improvement.py:162-197
代码实体关联:反馈循环
Cognee · 代码实体关联:反馈循环 · 图 3
Cognee · 代码实体关联:反馈循环 · 图 3

来源:cognee/modules/engine/models/SkillRun.py:10-67cognee/modules/memify/skill_improvement.py:85-197cognee/modules/tools/skill_runs.py:69-133

智能体检索的搜索配置

search API 通过几个特定参数支持智能体检索 cognee/api/v1/search/search.py:28-52

参数描述
query_type必须设置为 SearchType.AGENTIC_COMPLETION 以触发智能体循环。
skills一个技能名称或 Skill 对象的列表,供智能体使用。
tools一个可选的工具名称白名单,在会话期间暴露给智能体。
max_iter允许的最大工具调用迭代次数(默认为 6)。
使用示例
from cognee.modules.search.types import SearchType

results = await cognee.search(
    query_text = "审查认证模块中的最新变更",
    query_type = SearchType.AGENTIC_COMPLETION,
    skills = ["code-review"],
    max_iter = 5
)

来源:cognee/api/v1/search/search.py:28-121cognee/modules/retrieval/agentic_retriever.py:82-106