存储和搜索内存

在本文中,您将创建一个代理内存组件,存储线程消息和持久内存,然后重新搜索它们。

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

小结

在本文中,我们学习了如何为单个用户创建代理内存客户端,将线程消息与持久存储器一起存储,并通过用户范围的检索来搜索这些记录。

提示:了解核心单用户内存工作流后,您可能还对将代理内存短期 API 与 LangGraph 一起使用感兴趣。

完整代码

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