智能体持久化

将智能体保存到数据库并加载,以便复用。

快速开始#

克隆完整示例#

最简单的开始方式是克隆完整的示例仓库:

bash
git clone https://github.com/astreus-ai/examples
cd examples/agent-persistence
npm install

或仅安装包#

如果你更喜欢从零开始构建:

bash
npm install @astreus-ai/astreus

环境配置#

bash
# .env
OPENAI_API_KEY=sk-your-openai-api-key-here
DB_URL=sqlite://./astreus.db

智能体持久化#

typescript
import { Agent } from '@astreus-ai/astreus';

// Create and save an agent
const agent = await Agent.create({
  name: 'ProjectAssistant',
  model: 'gpt-6-astra',
  memory: true,
  systemPrompt: 'You are a project management assistant.'
});

// Use the agent
await agent.ask("Remember that our project deadline is March 15th");

// Later, load the same agent by name
const loadedAgent = await Agent.findByName('ProjectAssistant');
const response = await loadedAgent?.ask("What is our project deadline?");
console.log(response); // Should remember March 15th

运行示例#

如果你克隆了仓库:

bash
npm run dev

仓库#

完整示例可在 GitHub 上获取:examples/agent-persistence