llm-to-agent/legacy/old/knowledge/llm-to-agent-guide.md
2026-07-24 17:37:00 +08:00

61 lines
2.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# LLM-to-Agent 框架知识库
## Agent 工具调用流程
Agent 的 `run()` 方法执行以下循环:
1. 将用户消息加入上下文
2. 调用 LLM传入可用工具列表
3. 如果 LLM 返回 `tool_calls`,逐个执行工具,将结果加入上下文,回到步骤 2
4. 如果 LLM 直接返回文本,结束循环,返回最终回复
工具定义需包含 `name``description``parameters`JSON Schema`execute` 函数。
## 子 Agent 管理
通过 `spawn_agent` 工具可以创建具有独立人设的子 Agent如销售、顾客
每个子 Agent 拥有独立的上下文和基础工具集(不含 sub-agent 管理工具,防止无限递归)。
通过 `run_conversation` 工具可以在两个子 Agent 之间运行多轮对话。
## 知识库功能
知识库位于 `knowledge/` 目录,支持 `.md` 格式文档。
文档按 `##` 二级标题自动分块,通过 `search_knowledge` 工具进行关键词检索。
检索算法基于关键词命中次数,标题命中加权 ×3。
也提供 `list_knowledge` 工具查看知识库全貌。
## 常用命令
- 启动框架:`pnpm dev`
- 指定提示词模式:`pnpm dev orchestrator`
- 可用模式:`default``toxic``json``agent``reAct``orchestrator`
## 项目结构
```
src/
index.ts -- CLI 入口
llm.ts -- LLM 客户端OpenAI 兼容)
context.ts -- 上下文管理器
types/index.ts -- 类型定义
agents/
agent.ts -- Agent 核心类
manager.ts -- 子 Agent 管理器
tools.ts -- spawn_agent / run_conversation 工具
tools/
registry.ts -- 工具注册表
weather.ts -- 天气查询工具
calculator.ts -- 计算器工具
guess.ts -- 猜数字游戏工具
knowledge/
knowledge-base.ts -- 知识库类
search-tool.ts -- 知识库搜索工具
hooks/
index.ts -- Hook 总线
log.hooks.ts -- 日志 Hook
registry.ts -- Hook 注册
prompts/
system.ts -- 系统提示词
orchestrator.ts -- Orchestrator 提示词
reAct.ts -- ReAct 提示词
```