메모리 저장 및 검색
이 문서에서는 에이전트 메모리 구성 요소를 만들고 스레드 메시지와 영구 메모리를 모두 저장한 다음 다시 검색합니다.
참고: 패키지 설정은 에이전트 메모리 시작하기를 참조하십시오. 이 예에 대한 로컬 Oracle AI Database가 필요한 경우 Run Oracle AI Database Locally를 참조하십시오.
메시지 및 메모리 저장
Oracle DB 연결 또는 풀을 사용하여 에이전트 메모리 구성 요소를 생성하고, 짧은 스레드를 추가하고, 메시지와 함께 지속 가능한 하나의 사용자 메모리를 유지합니다.
주: 기본적으로 OracleAgentMemory는 LLM이 필요하므로 OracleThread는 add_messages()를 통해 추가된 최근 스레드 메시지에서 영구 메모리를 주기적으로 추출할 수 있습니다. 자동 메모리 추출을 원하지 않는 경우 클라이언트를 생성할 때나 create_thread()를 호출할 때 memory_extraction_config=MemoryExtractionConfig(extract_memories=False)를 설정합니다.
import oracledb
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 = oracledb.SessionPool(
user="YOUR DB USER",
password="YOUR DB PASSWORD",
dsn="YOUR DB CONNECT STRING",
)
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.")
| API 참조: OracleAgentMemory | 오라클 스레드 |
필요한 경우 검색 인덱싱을 위해 긴 메시지와 메모리가 자동으로 조각화됩니다. 조각화는 저장된 메시지 또는 메모리 텍스트를 변경하지 않습니다. 검색은 청크를 내부적으로 사용한 다음 원래 논리적 레코드를 반환합니다.
주: "YOUR DB USER", "YOUR DB PASSWORD" 및 "YOUR DB CONNECT STRING"를 Oracle AI Database에 대한 인증서 및 접속 문자열로 바꿉니다. 이 예에 대한 로컬 데이터베이스가 필요한 경우 Run Oracle AI Database Locally를 따릅니다.
에이전트 메모리 검색
해당 사용자에게 범위가 지정된 저장된 메모리와 메시지를 검색합니다.
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.
주: 앞에 표시된 출력은 그림입니다. 이후 버전은 추가 결과 유형, 필드 또는 순서 지정 패턴을 반환할 수 있습니다.
| API 참조: SearchScope | 오라클 검색 결과 |
결론
이 문서에서는 단일 사용자에 대한 에이전트 메모리 클라이언트를 만들고, 지속 가능한 메모리와 함께 스레드 메시지를 저장하고, 사용자 범위 검색을 통해 해당 레코드를 다시 검색하는 방법을 배웠습니다.
참고: 핵심 단일 사용자 메모리 워크플로우를 학습한 후 LangGraph에서 에이전트 메모리 단기 API 사용에도 관심이 있을 수 있습니다.
전체 코드
#Copyright © 2026 Oracle and/or its affiliates.
#This software is under the Apache License 2.0
#(LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) or Universal Permissive License
#(UPL) 1.0 (LICENSE-UPL or https://oss.oracle.com/licenses/upl), at your option.
#Agent Memory Code Example - Quickstart
#---------------------------------------
##Add memories
import oracledb
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 = oracledb.SessionPool(
user="YOUR DB USER",
password="YOUR DB PASSWORD",
dsn="YOUR DB CONNECT STRING",
)
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.