儲存並搜尋記憶體
在本文中,您將建立一個「代理程式記憶體」元件,同時儲存繫線訊息和持久的記憶體,然後再搜尋。
秘訣:如需設定套裝程式,請參閱開始使用代理程式記憶體。如果此範例需要本機 Oracle AI Database,請參閱在本機執行 Oracle AI Database 。
儲存訊息與記憶
使用 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.