Mem0 Memory Tools
Mem0 Memory Tools
Mem0 통합은 Agent 메모리 워크플로우용으로 두 개의 바로 사용 가능한 Tool을 제공해요.
retrieve_memories(Mem0MemoryRetrieverTool)는 장기 기억을 검색하거나, 쿼리가 제공되지 않으면 범위 안의 모든 기억을 반환해요.store_memory(Mem0MemoryWriterTool)는 지속적인 사실, 선호도, 컨텍스트를 장기 기억으로 저장해요.
본문
개요
이 도구들은 Agent가 대화 간에 지속적인 메모리가 필요할 때 사용해요. 검색 도구는 Agent가 Mem0에 저장된 기억에 접근하게 해주고, 작성 도구는 Agent가 향후 실행에 유용한 새 정보를 저장하게 해줘요.
두 도구 모두 공유 Mem0MemoryStore를 사용해요. 기본적으로 Agent State에서 user_id를 inputs_from_state를 통해 주입하므로, 한 Agent 인스턴스가 사용자 ID를 LLM에게 도구 호출 파라미터로 노출하지 않고도 여러 사용자를 서빙할 수 있어요.
Mem0MemoryRetrieverTool은 query와 top_k를 LLM에 노출해요. Agent가 query를 생략하거나 null을 전달하면, 도구는 주입된 범위 안의 모든 기억을 반환해요. 이는 Agent가 더 구체적인 기억 검색이 필요한지 판단하기 전에 알려진 컨텍스트를 조사할 때 유용해요.
Mem0MemoryWriterTool은 text와 infer를 LLM에 노출해요. 작성 도구는 기본적으로 infer=False를 사용해서 Agent가 선택한 기억 텍스트를 정확히 저장해요. 대화 기록 같은 긴 텍스트에서 Mem0이 기억을 추출하길 원할 때는 infer=True를 사용해요.
파라미터
Mem0MemoryRetrieverTool:
memory_store는 필수예요. 조회할Mem0MemoryStore인스턴스예요.top_k는 선택이고 기본값은5예요. 쿼리 검색에서 반환되는 기본 최대 기억 수를 설정해요.name은 선택이고 기본값은"retrieve_memories"예요.description은 선택이며 LLM에게 도구를 설명해요.parameters는 선택이며 LLM에 노출되는 JSON 스키마를 오버라이드하게 해줘요.inputs_from_state는 선택이고 기본값은{"user_id": "user_id"}예요.
Mem0MemoryWriterTool:
memory_store는 필수예요. 작성할Mem0MemoryStore인스턴스예요.name은 선택이고 기본값은"store_memory"예요.description은 선택이며 LLM에게 도구를 설명해요.parameters는 선택이며 LLM에 노출되는 JSON 스키마를 오버라이드하게 해줘요.inputs_from_state는 선택이고 기본값은{"user_id": "user_id"}예요.
런타임에 Mem0 엔티티 ID를 더 전달하려면, Agent의 state_schema에 필드를 추가하고 inputs_from_state로 그 State 키들을 도구 파라미터에 매핑하면 돼요. 예를 들어 {"user_id": "user_id", "session_id": "run_id"}는 state["session_id"]를 도구의 run_id 파라미터로 전달해요.
기억을 검색하거나 저장할 때 Mem0 범위가 적어도 하나는 있어야 해요. 일반적인 사용자별 경우 user_id를 쓰고, 애플리케이션이 더 좁은 범위를 필요로 하면 run_id, agent_id, app_id를 추가해요.
사용법
Mem0 통합을 설치해요.
pip install mem0-haystack
Mem0 API 키를 설정해요.
export MEM0_API_KEY="your-mem0-api-key"
Agent와 함께
두 도구를 Agent와 함께 써서 턴 시작에 기억을 읽고, 최종 답변 전에 새 지속 기억을 쓸 수 있어요.
from haystack.components.agents import Agent
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.components.generators.utils import print_streaming_chunk
from haystack.dataclasses import ChatMessage
from haystack_integrations.memory_stores.mem0 import Mem0MemoryStore
from haystack_integrations.tools.mem0 import (
Mem0MemoryRetrieverTool,
Mem0MemoryWriterTool,
)
store = Mem0MemoryStore()
retrieve_memories = Mem0MemoryRetrieverTool(memory_store=store, top_k=10)
store_memory = Mem0MemoryWriterTool(memory_store=store)
agent = Agent(
chat_generator=OpenAIChatGenerator(model="gpt-5.4"),
tools=[retrieve_memories, store_memory],
system_prompt="""You are a helpful assistant with long-term memory.
At the beginning of each turn, call retrieve_memories without a query to inspect known memories.
Use store_memory only for new durable user-specific facts, preferences, or project context.
Before storing, compare the proposed memory with retrieved memories and avoid duplicates.
Do not store transient requests that are only useful in the current conversation.
""",
streaming_callback=print_streaming_chunk,
state_schema={"user_id": {"type": str}},
)
result = agent.run(
messages=[
ChatMessage.from_user(
"My name is Alice. Please remember that I prefer concise Python examples.",
),
],
user_id="alice",
)
State를 통해 더 많은 ID 전달
Mem0은 user_id, run_id, agent_id, app_id로 기억 범위를 지정할 수 있어요. 도구들은 기본적으로 user_id만 노출하지만, LLM 노출용 파라미터 스키마에 추가하지 않고 Agent State를 통해 더 많은 ID를 주입할 수 있어요.
from haystack.components.agents import Agent
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.components.generators.utils import print_streaming_chunk
from haystack_integrations.memory_stores.mem0 import Mem0MemoryStore
from haystack_integrations.tools.mem0 import (
Mem0MemoryRetrieverTool,
Mem0MemoryWriterTool,
)
store = Mem0MemoryStore()
inputs_from_state = {
"user_id": "user_id",
# Map the Agent State key "conversation_id" to the tool's "run_id" parameter.
"conversation_id": "run_id",
}
retrieve_memories = Mem0MemoryRetrieverTool(
memory_store=store,
inputs_from_state=inputs_from_state,
)
store_memory = Mem0MemoryWriterTool(
memory_store=store,
inputs_from_state=inputs_from_state,
)
agent = Agent(
chat_generator=OpenAIChatGenerator(model="gpt-5.4"),
tools=[retrieve_memories, store_memory],
state_schema={
"user_id": {"type": str},
"conversation_id": {"type": str},
},
streaming_callback=print_streaming_chunk,
)
result = agent.run(
messages=[
ChatMessage.from_user(
"Remember that this conversation is about the docs assistant prototype.",
),
],
user_id="alice",
conversation_id="docs-assistant-prototype",
)