data science/AI Agents

memory - mem0 ("mem-zero")

꼰대코더 2025. 10. 11. 21:35

Mem0는 현대적인 AI 에이전트를 위해 설계된 메모리 레이어입니다.
이 레이어는 에이전트가 다음과 같은 작업을 수행할 수 있도록 하는 지속적인(memory persistent) 기억 저장소 역할을 합니다:

  • 관련 있는 과거 상호작용을 기억
  • 중요한 사용자 선호도와 사실적 맥락을 저장
  • 성공과 실패로부터 학습

즉, Mem0는 AI 에이전트에게 기억하고, 학습하며, 진화할 수 있는 능력을 부여합니다.
또한 Mem0는 에이전트 스택에 손쉽게 통합되며, 프로토타입 단계부터 실제 운영 환경까지 유연하게 확장할 수 있습니다.

 

pip install mem0ai

 

mem0 는 상업용인 클라우드 Mem0 Platform 과 로켈에서 사용할 수 있는 오픈소스 버젼이 있다.

여기서는 오픈소스 버젼과 Vector 저장이 가능한 Qdrant와의 연동을 소개한다.

 

Qdrant docker 를 실행

docker run -p 6333:6333 -p 6334:6334 \
    -v "$(pwd)/qdrant_storage:/qdrant/storage:z" \
    qdrant/qdrant
 

Qdrant의 dashboard 는 localhost:6334/dashboard 로 접속할 수 있다.

from mem0 import Memory
# 초기화
config = {
    "vector_store": {
        "provider": "qdrant",
        "config": {
            "host": "localhost",
            "port": 6333,
        }
    },

memory = Memory.from_config(config)

 

# vector 검색
relevant_memories = memory.search(query=query, user_id=user_id, top_k=5, threshold=0.7)
 

 

# 검색 결과로 LLM Query를 구성하고 OpenAI API로 실행하기
context = "Relevant past information:\n"
if relevant_memories and "results" in relevant_memories:
    for memory in relevant_memories["results"]:
        if "memory" in memory:
            context += f"- {memory['memory']}\n"

full_prompt = f"{context}\nCustomer: {query}\nSupport Agent:"
response = self.client.chat.completions.create(
    model="gpt-4",
    messages=[
        {"role": "system", "content": "You are a customer support AI agent for abc.com, an online electronics store."},
        {"role": "user", "content": full_prompt}
    ]
)

answer = response.choices[0].message.content
 

 

# Query와 LLM의 Answer 를 메모리에 저장
memory.add(query, user_id=user_id, metadata={"app_id": "customer-support", "role": "user"})
memory.add(answer, user_id=user_id, metadata={"app_id": "customer-support", "role": "assistant"})
 
# expiration_date = YYYY-MM-DD 를 파라미터에 추가하면 자동으로 지정된 날짜에 삭제

 

# user_id 와 연결된 모든 메모리를 출력
memories = memory.get_all(user_id=user_id)
if memories:
    if memories and "results" in memories:
        for memory in memories["results"]:
            if "memory" in memory:
                print(f"- {memory['memory']}")