Skip to content

StreamMemory module cannot retrieve added memories #59

@februarysea

Description

@februarysea

版本 Version
操作系统 OS: Linux Ubuntu 24.04.3 LTS
Python: 3.12.9
AgentSociety: 1.5.2

描述错误 Describe the bug
StreamMemory的search方法无法返回任何搜索结果,导致智能体无法检索已存储的记忆。问题出现在memory模块中,search方法硬编码了一个过滤条件{"type": "stream"}(line 449),但在add方法(line 352)中添加文档时并未包含此标签,导致过滤条件永远无法匹配任何文档。

复现 To Reproduce
复现错误的步骤:

  1. 创建一个StreamMemory实例
  2. 使用add方法添加一些记忆(await stream_memory.add("Event", "I met a friend")
  3. 使用search方法搜索相关记忆(await stream_memory.search("friend", topic="Event")
  4. 使用get_all方法查看所有记忆(await self.memory.stream.get_all())
  5. 观察到search方法返回结果为"Nothing",get_all方法返回添加记忆
2025-08-30 20:15:14,186 - Agent - INFO - agent1 search memories: Nothing
2025-08-30 20:15:14,186 - Agent - INFO - agent1 all memories: [{'id': 0, 'cognition_id': None, 'topic': 'Event', 'location': 500030808, 'description': 'I ate with my friend', 'day': 0, 't': 28801}, {'id': 1, 'cognition_id': None, 'topic': 'Event', 'location': 500030808, 'description': 'I met my friend', 'day': 0, 't': 28801}, {'id': 2, 'cognition_id': None, 'topic': 'Event', 'location': 500030808, 'description': 'I talked with my friend', 'day': 0, 't': 28801}, {'id': 3, 'cognition_id': None, 'topic': 'Event', 'location': 500030808, 'description': 'I met another friend', 'day': 0, 't': 28801}, {'id': 4, 'cognition_id': None, 'topic': 'Event', 'location': 500030808, 'description': 'I ate with another friend', 'day': 0, 't': 28801}]

复现代码:

    async def record_experience(self, topic: str, thought: str):
        # Record a new experience
        await self.memory.stream.add(
            topic=f"{topic}",
            description=f"{thought}"
        )

    async def recall_related_memories(self, query: str, limit: int = 5):
        # Search for memories related to the query
        memories = await self.memory.stream.search(
            query=query,
            topic='Event',
            top_k=limit
        )
        return memories


    async def forward(self):
        # add random 5 memory items 
        await self.record_experience(topic="Event", thought="I ate with my friend")
        await self.record_experience(topic="Event", thought="I met my friend")
        await self.record_experience(topic="Event", thought="I talked with my friend")
        await self.record_experience(topic="Event", thought="I met another friend")
        await self.record_experience(topic="Event", thought="I ate with another friend")
        
        search_memories = await self.recall_related_memories(query="friend")
        logger.info(f"agent{self.agent_id} search memories: {search_memories}")
        
        all_memories = await self.memory.stream.get_all()
        logger.info(f"agent{self.agent_id} all memories: {all_memories}")

预期结果 Expected behavior
search方法应该能够返回与查询相关的记忆内容,而不是总是返回"Nothing"。当存在匹配的记忆时,应该返回格式化的记忆列表。

关联Issue
#50

技术细节 Technical Details
问题位置:/packages/agentsociety/agentsociety/memory/memory.py

根本原因:

  • search方法(line 449)中硬编码了过滤条件:filter_dict: dict[str, Any] = {"type": "stream"}
  • add方法(line 352)中的extra_tags没有包含"type": "stream"标签
  • 导致VectorStore的similarity_search永远找不到匹配的文档

修复方案:
在add方法中添加type标签:

extra_tags={
    "type": "stream",  # Add this line
    "topic": topic,
    "day": day,
    "time": t,
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions