메모리 저장 및 검색
이 문서에서는 에이전트 메모리 구성 요소를 만들고 스레드 메시지와 영구 메모리를 모두 저장한 다음 다시 검색합니다.
참고: 패키지 설정은 에이전트 메모리 시작하기를 참조하십시오. 이 예에 대한 로컬 Oracle AI Database가 필요한 경우 Run Oracle AI Database Locally를 참조하십시오.
메시지 및 메모리 저장
Oracle DB 연결 또는 풀을 사용하여 에이전트 메모리 구성 요소를 생성하고, 짧은 스레드를 추가하고, 메시지와 함께 지속 가능한 하나의 사용자 메모리를 유지합니다.
주: 기본적으로 OracleAgentMemory는 LLM이 필요하므로 OracleThread는 add_messages()를 통해 추가된 최근 스레드 메시지에서 영구 메모리를 주기적으로 추출할 수 있습니다. 자동 메모리 추출을 원하지 않는 경우 클라이언트를 생성할 때나 create_thread()를 호출할 때 extract_memories=False를 설정합니다.
from oracleagentmemory.core.oracleagentmemory import OracleAgentMemory
from oracleagentmemory.apis.searchscope import SearchScope
from oracleagentmemory.core.embedders.embedder import Embedder
from oracleagentmemory.core.llms.llm import Llm
embedder = Embedder(model="YOUR_EMBEDDING_MODEL")
llm = Llm(model="YOUR_LLM")
db_pool = ... #an oracledb connection or connection pool
memory = OracleAgentMemory(connection=db_pool, embedder=embedder, llm=llm)
messages = [
{
"role": "user",
"content": (
"Orange juice has become my favorite breakfast drink lately, "
"what can I pair it with?"
),
},
{
"role": "assistant",
"content": (
"Nice! Orange juice goes great with something savory. "
"Try eggs and toast, avocado toast, or a breakfast sandwich."
),
},
]
thread = memory.create_thread(user_id="user_123")
#add_messages will add messages to the DB and extract memories automatically
thread.add_messages(messages)
#add_memory adds memory to the DB
thread.add_memory("The user likes orange juice with breakfast.")
에이전트 메모리 검색
해당 사용자에게 범위가 지정된 저장된 메모리와 메시지를 검색합니다.
results = memory.search(query="orange juice", scope=SearchScope(user_id="user_123"))
for result in results:
print(f"- [{result.record.record_type}] {result.content}")
출력:
-[memory] The user likes orange juice with breakfast.
-[message] Orange juice has become my favorite breakfast drink lately, what can I pair it with?
-[message] Nice! Orange juice goes great with something savory.
Try eggs and toast, avocado toast, or a breakfast sandwich.
결론
이 문서에서는 단일 사용자에 대한 에이전트 메모리 클라이언트를 만들고, 지속 가능한 메모리와 함께 스레드 메시지를 저장하고, 사용자 범위 검색을 통해 해당 레코드를 다시 검색하는 방법을 배웠습니다.
참고: 핵심 단일 사용자 메모리 워크플로우를 학습한 후 LangGraph에서 에이전트 메모리 단기 API 사용에도 관심이 있을 수 있습니다.
전체 코드
#Copyright © 2026 Oracle and/or its affiliates.
#isort:skip_file
#fmt: off
#Agent Memory Code Example - Quickstart
#_--------------------------------------
#How to use:
#Create a new Python virtual environment and install the latest oracleagentmemory version.
#You can now run the script
#1. As a Python file:
#```bash
#python quickstart.py
#```
#2. As a Notebook (in VSCode):
#When viewing the file,
#- press the keys Ctrl + Enter to run the selected cell
#- or Shift + Enter to run the selected cell and move to the cell below
##Add memories
#%%
from oracleagentmemory.core.oracleagentmemory import OracleAgentMemory
from oracleagentmemory.apis.searchscope import SearchScope
from oracleagentmemory.core.embedders.embedder import Embedder
from oracleagentmemory.core.llms.llm import Llm
embedder = Embedder(model="YOUR_EMBEDDING_MODEL")
llm = Llm(model="YOUR_LLM")
db_pool = ... #an oracledb connection or connection pool
memory = OracleAgentMemory(connection=db_pool, embedder=embedder, llm=llm)
messages = [
{
"role": "user",
"content": (
"Orange juice has become my favorite breakfast drink lately, "
"what can I pair it with?"
),
},
{
"role": "assistant",
"content": (
"Nice! Orange juice goes great with something savory. "
"Try eggs and toast, avocado toast, or a breakfast sandwich."
),
},
]
thread = memory.create_thread(user_id="user_123")
thread.add_messages(messages)
thread.add_memory("The user likes orange juice with breakfast.")
##Search memories
#%%
results = memory.search(query="orange juice", scope=SearchScope(user_id="user_123"))
for result in results:
print(f"- [{result.record.record_type}] {result.content}")
#- [memory] The user likes orange juice with breakfast.
#- [message] Orange juice has become my favorite breakfast drink lately, what can I pair it with?
#- [message] Nice! Orange juice goes great with something savory.
#Try eggs and toast, avocado toast, or a breakfast sandwich.