Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(memory): fix the memory compression functionality (#1107)
DavdGaob379c0815a9d
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex ba8f4b686..e0fdc9e71 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -185,10 +185,27 @@ examples/### Adding New Examples-We highly encourage contributions of new examples that showcase the capabilities of AgentScope.-Please add them to the `examples/` directory with a clear README explaining the purpose and usage of the example.+We highly encourage contributions of new examples that showcase the capabilities of AgentScope! Your examples help others learn and get inspired.-Now our examples are organized into subdirectories based on their type:+**📝 About the Examples Directory:**++To maintain code quality and keep the repository accessible for everyone, we've designed the `examples/` directory in the main AgentScope repository to focus on **demonstrating AgentScope's functionalities**. Think of these as educational references and feature showcases that help developers quickly understand what AgentScope can do.++**What makes a great example here:**+- Clearly demonstrates specific AgentScope features or capabilities+- Easy to understand and follow along+- Serves as a learning material or reference implementation+- Focused and concise++**For More Complex Applications:**++Have you built something amazing with AgentScope? Perhaps a more sophisticated, production-ready application? That's fantastic! 🎉++We'd love to see your work in our **[agentscope-samples](https://github.com/agentscope-ai/agentscope-samples)** repository. This dedicated space is perfect for showcasing complete, real-world applications and sharing your AgentScope-based projects with the community. It's a great way to inspire others and demonstrate the full potential of the AgentScope ecosystem!++**Example Organization:**++Examples in the main repository are organized into subdirectories based on their type:- `examples/agent/` for specialized agents- `examples/functionality/` for showcasing specific functionalities of AgentScope@@ -208,6 +225,31 @@ examples/└── ... # The other scripts```+### Adding New Memory Databases++The memory module in AgentScope currently supports:++- **In-memory storage**: For lightweight, temporary memory needs+- **Relational databases via SQLAlchemy**: For persistent, structured data storage+- **NoSQL databases**: For flexible schema requirements (e.g., Redis)++**⚠️ Important Notice:**++For **relational databases**, we use **SQLAlchemy** as a unified abstraction layer. SQLAlchemy already supports a wide+range of SQL databases including PostgreSQL, MySQL, SQLite, Oracle, Microsoft SQL Server, and many others.++**Therefore, we do not accept separate implementations for relational databases that are already supported by SQLAlchemy.**+If you need support for a specific relational database, please ensure it works through the existing SQLAlchemy integration.++**If you wish to contribute a new memory database implementation**, please consider:++1. **For relational databases**: Use the existing SQLAlchemy integration.++2. **For NoSQL databases**: If you're adding support for a new NoSQL database (e.g., MongoDB, Cassandra), please:+ - Implement a new memory class that extends the appropriate base class+ - Add comprehensive unit tests+ - Update documentation accordingly+## Do's and Don'tsdiff --git a/CONTRIBUTING_zh.md b/CONTRIBUTING_zh.mdindex 5e52727fd..1b8b32ce2 100644--- a/CONTRIBUTING_zh.md+++ b/CONTRIBUTING_zh.md@@ -182,12 +182,30 @@ examples/### 添加新的示例+欢迎开源社区贡献新的示例来展示 AgentScope 的各种功能!-请将它们添加到 `examples/` 目录,并附上清晰的 README 说明示例的目的和用法。+**📝 关于示例目录:**-目前我们的示例根据类型组织到子目录中:-- `examples/functionality/` 用于展示 AgentScope 的特定基础功能-- `examples/evaluation/` 用于评估+为了避免仓库变得过于臃肿,我们将 AgentScope 仓库中的 `examples/` 目录设计为专注于**展示 AgentScope 的功能性**。可以把这些示例看作是指导性的参考和功能展示,帮助开发者快速理解 AgentScope 能做什么。++**什么样的示例适合放在这里:**+- 清晰地展示 AgentScope 的特定功能或能力+- 易于理解和跟随学习+- 作为学习材料或参考实现+- 专注且简洁++**对于更复杂的应用:**++对于更加复杂,生产就绪的应用,我们非常期待在 **[agentscope-samples](https://github.com/agentscope-ai/agentscope-samples)** 仓库中看到您的作品。这个仓库专门用于展示、分享基于 AgentScope 生态搭建的完整的、真实世界的应用。++**示例组织方式:**++主仓库中的示例根据类型组织到子目录中:++- `examples/agent/` 用于专门的智能体+- `examples/functionality/` 用于展示 AgentScope 的特定功能+- `examples/game/` 用于游戏相关示例+- `examples/evaluation/` 用于评估脚本- `examples/workflows/` 用于工作流演示- `examples/training/` 用于训练相关示例@@ -202,6 +220,30 @@ examples/└── ... # 其他脚本```+### 添加新的记忆数据库++AgentScope 的记忆模块目前支持:++- **内存存储**:用于轻量级的临时记忆需求+- **通过 SQLAlchemy 支持关系型数据库**:用于持久化的结构化数据存储+- **NoSQL 数据库**:用于灵活的模式需求(例如 Redis)++**⚠️ 请注意:**++对于**关系型数据库**,我们使用 **SQLAlchemy** 作为统一的抽象层。SQLAlchemy 已经支持多种 SQL 数据库,包括 PostgreSQL、MySQL、SQLite、Oracle、Microsoft SQL Server 等。++**因此,为了保持 AgentScope 代码的整洁,目前不接受为 SQLAlchemy 已经支持的关系型数据库单独实现新的支持。**+如果您需要支持特定的关系型数据库,请确保通过现有的 SQLAlchemy 集成来实现。++**如果您希望贡献新的记忆数据库实现**,请考虑以下几点:++1. **对于关系型数据库**:使用现有的 SQLAlchemy 集成。++2. **对于 NoSQL 数据库**:如果您要添加对新 NoSQL 数据库的支持(例如 MongoDB、Cassandra),请:+ - 实现一个扩展适当基类的新记忆类+ - 添加全面的单元测试+ - 相应地更新文档+## Do's and Don'tsdiff --git a/src/agentscope/agent/_react_agent.py b/src/agentscope/agent/_react_agent.pyindex c7ac59a70..2752bed39 100644--- a/src/agentscope/agent/_react_agent.py+++ b/src/agentscope/agent/_react_agent.py@@ -401,9 +401,6 @@ class ReActAgent(ReActAgentBase):# Retrieve relevant documents from the knowledge base(s) if anyawait self._retrieve_from_knowledge(msg)- # -------------- Memory compression --------------- await self._compress_memory_if_needed()-# Control if LLM generates tool calls in each reasoning steptool_choice: Literal["auto", "none", "required"] | None = None@@ -433,6 +430,9 @@ class ReActAgent(ReActAgentBase):structured_output = Nonereply_msg = Nonefor _ in range(self.max_iters):+ # -------------- Memory compression --------------+ await self._compress_memory_if_needed()+# -------------- The reasoning process --------------msg_reasoning = await self._reasoning(tool_choice)