儲存並搜尋記憶體

在本手冊中,您將建立一個 Oracle 代理程式記憶體元件,同時儲存繫線訊息和持久性記憶體,然後再搜尋。

提示:如需設定套裝程式,請參閱開始使用代理程式記憶體。如果此範例需要本機 Oracle AI Database,請遵循在本機執行 Oracle AI Database 。

儲存訊息與記憶

使用 Oracle DB 連線或集區建立「Oracle 代理程式記憶體」元件、新增短繫線,以及將一個持續的使用者記憶體與訊息一起保存。

注意:依照預設, OracleAgentMemory 會預期 LLM,因此 OracleThread 可以從透過 add_messages() 新增的最近繫線訊息中定期擷取持久的記憶體。如果您不希望自動擷取記憶體,請在建構用戶端或呼叫 create_thread() 時設定 memory_extraction_config=MemoryExtractionConfig(extract_memories=False)。

from oracleagentmemory.core import SchemaPolicy
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="localhost:1521/...",
)
memory_store_id = "T_QUICKSTART"
memory = OracleAgentMemory(
    connection=db_pool,
    embedder=embedder,
    llm=llm,
    schema_policy=SchemaPolicy.CREATE_IF_NECESSARY,
    memory_store_id=memory_store_id,
)

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 的證明資料和連線字串。如果此範例需要本機資料庫,請遵循在本機執行 Oracle AI Database 。

搜尋代理程式記憶體

搜尋該使用者的預存記憶體與訊息範圍。

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 OracleSearch 結果

如需可設定的直接結果限制、重新排名、刪除及變數替代字預算,請參閱改善搜尋結果相關性。

結論

在本指南中,我們學會如何建立 Oracle Agent Memory 從屬端、將繫線訊息與持續的記憶體並存,然後以使用者範圍的擷取來回搜尋這些記錄。

→ 瞭解核心單一使用者記憶體工作流程之後,您現在可以繼續使用 Oracle 代理程式記憶體短期 API 搭配 LangGraph 。

完整代碼

複製後續的完整代碼。

#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.

#Oracle Agent Memory Code Example - Quickstart
#---------------------------------------------

##Add memories

from oracleagentmemory.core import SchemaPolicy
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="localhost:1521/...",
)
memory_store_id = "T_QUICKSTART"
memory = OracleAgentMemory(
    connection=db_pool,
    embedder=embedder,
    llm=llm,
    schema_policy=SchemaPolicy.CREATE_IF_NECESSARY,
    memory_store_id=memory_store_id,
)

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.")

##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.