存储和搜索内存

在本指南中,您将创建一个 Oracle Agent Memory 组件,存储线程消息和持久内存,然后重新搜索它们。

提示:有关软件包设置,请参见 Get Started with Agent Memory 。如果在此示例中需要本地 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 OracleThread

长消息和记忆会自动分块,以便在需要时进行检索索引。分块不会更改存储的消息或内存文本。搜索在内部使用块,然后返回原始逻辑记录。

注:将 "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 OracleSearchResult

有关可配置的直接结果限制、重新排列、修剪和标记预算,请参阅提高搜索结果相关性。

结论

在本指南中,我们了解了如何创建 Oracle Agent Memory 客户端、将线程消息与持久存储器一起存储以及通过用户范围检索来搜索这些记录。

→了解了核心单用户内存工作流后,现在可以继续执行 Use Oracle Agent Memory Short-Term APIs with 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.