teddymax/context-engineering
GitHub: teddymax/context-engineering
一个面向 AI 智能体的轻量级上下文管理系统,提供基于语义检索和动态 prompt 注入的能力,解决智能体对话中上下文组织和知识管理问题。
Stars: 0 | Forks: 0
# 上下文工程
一个面向 AI 智能体的轻量级上下文管理系统,具备基于 embedding 的检索和动态 prompt 注入功能。
## 功能特性
- 📚 **文档摄取** — 将文件、目录或原始文本加载到上下文存储中
- 🔍 **语义搜索** — 基于 TF-IDF 或 API 的 embedding 检索(支持 DashScope)
- 🏗️ **Prompt 构建** — 使用模板将检索到的上下文注入到 LLM prompt 中
- 🔄 **生命周期管理** — 基于 TTL 的过期机制、版本控制、压缩、导出/导入
- 🌐 **中文支持** — 使用 Jieba 分词以实现精准的中文文本检索
- 🏷️ **命名空间** — 支持共享命名空间的多智能体隔离
- ⚡ **零依赖** — 纯 Python 实现,无需 sklearn/FAISS(可选 numpy 以提升速度)
## 快速开始
```
# 初始化 store
python3.9 cli.py init my_context
# Ingest documents
python3.9 cli.py ingest my_context ./data/
python3.9 cli.py ingest my_context "Some important context text"
# Query
python3.9 cli.py query my_context "Your question here" --top-k 5
# View stats
python3.9 cli.py stats my_context
```
## 架构
```
src/
├── embeddings.py # TF-IDF vectorizer + cosine similarity + API embedding
├── context_store.py # In-memory document store with JSON persistence
├── retriever.py # Semantic search against context store
├── prompt_builder.py # Build prompts with injected context
├── document_ingestor.py # Load files, chunk text, ingest into store
└── context_manager.py # TTL cleanup, version tracking, export/import
```
## 配置说明
编辑 `configs/default.json`:
| 键 | 默认值 | 描述 |
|-----|---------|-------------|
| `tokenize.method` | `jieba` | `jieba`(中文)或 `regex`(英文) |
| `embedding.method` | `tfidf` | `tfidf` 或 `api`(DashScope) |
| `embedding.max_features` | `10000` | TF-IDF 词汇表大小 |
| `retrieval.top_k` | `5` | 每次查询的结果数 |
| `retrieval.similarity_threshold` | `0.1` | 最低相似度得分 |
| `context_store.ttl_days` | `30` | 文档过期时间 |
| `context_store.max_documents` | `1000` | 存储上限 |
| `prompt.max_context_tokens` | `4000` | 上下文截断限制 |
### 使用 DashScope Embeddings
将 `embedding.method` 设置为 `"api"`,并在 `configs/default.json` 或 `DASHSCOPE_API_KEY` 环境变量中配置您的 API key。
## 智能上下文加载器
用于智能体会话启动时上下文管理的脚本:
```
# 在 session 启动时智能仅加载相关上下文
python3.9 scripts/smart_context_loader.py smart "topic"
# 跨 session 摘要搜索
python3.9 scripts/session_summary.py search "agent team"
# 生成 session 摘要
python3.9 scripts/session_summary.py generate
```
## 许可证
MIT
标签:AI开发框架, AI智能体, CLI命令行工具, DLL 劫持, Homebrew安装, Jieba分词, JSON持久化, LangChain, LLM应用开发, NLP, Prompt注入, Python, RAG, TF-IDF, 上下文工程, 上下文模板, 上下文管理, 上下文缓存, 中文分词, 主机安全, 动态提示词, 向量检索, 命名空间, 多智能体隔离, 大语言模型, 文本嵌入, 文本挖掘, 文档向量化, 文档解析, 无后门, 智能问答系统, 检索增强生成, 生命周期管理, 知识库构建, 纯Python开发, 词向量, 语义搜索, 语义检索, 轻量级, 逆向工具, 零依赖