上下文压缩
使用 Astreus 的自动上下文压缩系统,通过对较早的消息进行摘要来自动管理长对话,同时保留重要上下文。
快速开始#
克隆完整示例#
最简单的开始方式是克隆完整的示例仓库:
git clone https://github.com/astreus-ai/examples
cd examples/context-compression
npm install或仅安装包#
如果你更喜欢从零开始构建:
npm install @astreus-ai/astreus环境配置#
# .env
OPENAI_API_KEY=sk-your-openai-api-key-here
DB_URL=sqlite://./astreus.db自动上下文压缩#
当对话变得过长时,autoContextCompression 功能会自动对较早的消息进行摘要,在减少 token 用量的同时保持上下文:
import { Agent } from '@astreus-ai/astreus';
const agent = await Agent.create({
name: 'ContextAgent',
model: 'gpt-6-astra',
memory: true,
autoContextCompression: true,
systemPrompt: 'You can handle very long conversations efficiently.'
});
// Have a long conversation
for (let i = 1; i <= 20; i++) {
await agent.ask(`Tell me an interesting fact about space. This is message #${i}.`);
}
// Test memory - agent should remember early facts despite context compression
const response = await agent.ask("What was the first space fact you told me?");
console.log(response);运行示例#
如果你克隆了仓库:
npm run dev仓库#
完整示例可在 GitHub 上获取:examples/context-compression