メモリーの格納および検索
このガイドでは、Oracle Agent Memoryコンポーネントを作成し、スレッド・メッセージと永続メモリーの両方を格納してから、再度検索します。
ヒント:パッケージの設定については、エージェント・メモリーのスタート・ガイドを参照してください。この例にローカルのOracle AI Databaseが必要な場合は、「Oracle AI Databaseをローカルで実行」に従います。
メッセージとメモを保存
Oracle DB接続またはプールを使用してOracle Agent Memoryコンポーネントを作成し、短いスレッドを追加し、メッセージとともに1つの永続ユーザー・メモリーを保持します。
ノート:デフォルトでは、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 | OracleSearch結果 |
構成可能な直接結果の制限、再ランク付け、プルーニングおよびトークン予算については、検索結果の関連性の向上を参照してください。
まとめ
このガイドでは、Oracle Agent Memoryクライアントの作成方法、永続メモリーとともにスレッド・メッセージを格納する方法、およびユーザー・スコープ取得を使用してそれらのレコードを検索する方法を学習しました。
→ シングルユーザー・メモリーのコア・ワークフローを学習した後、「LangGraphでOracle Agent Memory Short-Term APIを使用」に進むことができます。
完全コード
次の完全なコードをコピーします。
#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.