メモリーの格納および検索

この記事では、エージェント・メモリー・コンポーネントを作成し、スレッド・メッセージと永続メモリーの両方を格納してから、再度検索します。

ヒント:パッケージの設定については、エージェント・メモリーの開始を参照してください。この例にローカルのOracle AI Databaseが必要な場合は、Run Oracle AI Database Locallyを参照してください。

メッセージとメモを保存

Oracle DB接続またはプールを使用してエージェント・メモリー・コンポーネントを作成し、短いスレッドを追加し、メッセージとともに1つの永続ユーザー・メモリーを保持します。

ノート:デフォルトでは、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.