中文 | English
cardbox 是一个以 Card / CardBox 为核心的数据与上下文编排库。
Card是原子信息单元。- 逻辑“修改”通过创建新
Card完成(例如Card.update(...)返回新对象和新card_id)。 - 这样可以保证变换过程可追踪、易审计。
CardBox是有序card_id集合。- 主流程通过线性顺序处理上下文,降低复杂度。
- 多轮上下文和策略变换都围绕
CardBox展开。
CardStore是卡片读写入口,底层委托给存储适配器。CardBox只保存引用,实体内容统一从存储层读取。
- 业务处理封装为独立
Strategy。 ContextEngine.transform(...)负责编排策略链,策略间保持低耦合。- 内置策略示例:
ExtractCodeStrategy、PdfToTextStrategy、InlineTextFileContentStrategy。
ContextEngine可按history_level记录卡片与CardBox变换日志。trace_id贯穿一次完整处理流程,用于关联审计信息。
Card:支持文本、JSON、工具调用、文件引用等内容。CardBox:上下文引用序列。ContextEngine:提供transform、to_api、call_model。AsyncPostgresStorageAdapter:PostgreSQL 异步存储。LLMAdapter:统一 LLM 调用(LiteLLM / 可选 Interactions)。
python -m venv .venv
source .venv/bin/activate
pip install -e .从 PyPI 安装:
pip install cardbox可选依赖:
pip install -e .[test]
pip install -e .[interactions]如果你想通过 uv 安装并获得 demo 命令:
uv tool install cardbox
cardbox设置连接串(二选一):
export POSTGRES_STORAGE_ADAPTER_DSN="postgresql://user:pass@localhost:5432/cardbox_db"
# 或
export CARD_BOX_POSTGRES_DSN="postgresql://user:pass@localhost:5432/cardbox_db"main.py 演示最小端到端流程:
- 初始化
AsyncPostgresStorageAdapter(可自动建表)。 - 创建并保存
Card。 - 保存并加载
CardBox。 - 输出加载结果。
运行:
python main.py或者在安装后直接执行:
cardboxpython -m unittest discover -s tests -p 'test_*.py' -v说明:
tests/test_llm_providers.py依赖外部模型环境变量,未配置时会跳过。- 其余测试优先使用本地替身,不依赖真实外部服务。
可在启动时使用 configure 覆盖默认配置:
from cardbox.config import configure
configure({
"POSTGRES_STORAGE_ADAPTER": {
"dsn": "postgresql://user:pass@localhost:5432/cardbox_db",
"auto_bootstrap": True,
},
"LLM_BACKEND": "litellm",
})