自訂自動記憶體擷取
自動擷取記憶體可擷取有用的詳細資訊,無需進行其他配置。自訂指示可讓應用程式在需要針對特定使用案例量身打造預設行為時,為擷取處理新增指引。
本文說明如何使用自訂指示,引導 Oracle AI Agent Memory 的自動化記憶體擷取。
自訂指示提供其他的應用程式特定記憶體擷取指南。當您的應用程式需要擷取以反映網域知識、產品原則或對話特定優先順序時,這些功能十分有用。
注意:記憶體擷取應遵循應用程式特定指引時,請使用自訂指示。當不需要其他指引時,預設擷取是適當的。
提示:請參閱開始使用代理程式記憶體,瞭解如何安裝 oracleagentmemory。如果此範例需要本機 Oracle 資料庫,請遵循在本機執行 Oracle AI Database 。
設定從屬端層級擷取指示
使用 Oracle DB 連線或集區、內嵌程式、LLM 以及以支援為中心的擷取指示建立 Oracle Agent Memory 元件。這些指示適用於透過此從屬端建立或載入的繫線,除非繫線提供自己的擷取指示。
import oracledb
from oracleagentmemory.core import MemoryExtractionConfig
from oracleagentmemory.core.embedders.embedder import Embedder
from oracleagentmemory.core.llms.llm import Llm
from oracleagentmemory.core.oracleagentmemory import OracleAgentMemory
embedder = Embedder(model="YOUR_EMBEDDING_MODEL")
memory_llm = Llm(model="YOUR_MEMORY_LLM")
db_pool = oracledb.SessionPool(
user="YOUR DB USER",
password="YOUR DB PASSWORD",
dsn="YOUR DB CONNECT STRING",
)
support_extraction_instructions = """
Only extract customer support facts:
- order ids
- return requests
- delivery problems
Ignore greetings, small talk, and one-off troubleshooting text.
""".strip()
memory = OracleAgentMemory(
connection=db_pool,
embedder=embedder,
llm=memory_llm,
memory_extraction_config=MemoryExtractionConfig(
memory_extraction_custom_instructions=support_extraction_instructions
),
)
| API 參考: OracleAgentMemory | 繁體中文 |
從執行緒訊息擷取支援記憶
建立支援討論串並新增使用者和助理訊息。此範例會設定 memory_extraction_config=MemoryExtractionConfig(memory_extraction_frequency=1),因此 add_messages() 會立即執行插入之翻轉的自動擷取。自訂指示會將擷取的記憶體集中在訂單 ID、退貨要求及交貨問題上。
support_thread = memory.create_thread(
thread_id="support_ticket_7421",
user_id="customer_123",
memory_extraction_config=MemoryExtractionConfig(memory_extraction_frequency=1),
)
#add_messages persists the turn and runs automated memory extraction because
#MemoryExtractionConfig(memory_extraction_frequency=1) is set for this thread.
support_thread.add_messages(
[
{
"role": "user",
"content": (
"Hi, order 7421 arrived damaged. I need a return request "
"and a replacement delivery."
),
},
{
"role": "assistant",
"content": "I can help start the return request for order 7421.",
},
]
)
results = support_thread.search(
"return request order 7421",
max_results=5,
record_types=["fact"],
)
for result in results:
print(f"- [{result.record.record_type}] {result.content}")
輸出:
- [fact] Customer reported damaged order 7421 and requested a return and replacement delivery.
以上顯示的輸出為圖解式。確切的記憶體文字取決於設定的擷取 LLM,但擷取的記憶體應遵循提供給用戶端的自訂指示。
比較擷取器被要求保留的記憶體種類,最容易看到的差異如下:
使用自訂指示
擷取器會保留客戶支援的持久事實,並忽略問候語和對話用語。
- [fact] Customer reported damaged order 7421 and requested a return and replacement delivery.
不需要自訂指示
擷取器可能會保留更廣泛的對話記憶體,因為預設擷取提示的範圍不是支援作業。
- [memory] The user contacted support about order 7421 and discussed a damaged delivery.
| API 參照:OracleThread | OracleSearch 結果 |
修訂一個執行緒的指示
當一個繫線需要比從屬端預設值更窄的擷取原則時,請將 memory_extraction_config=MemoryExtractionConfig(memory_extraction_custom_instructions=...) 傳送至 create_thread()。執行緒層次值會優先於該執行緒,並會保存在執行緒組態中。
billing_thread = memory.create_thread(
thread_id="billing_ticket_9310",
user_id="customer_123",
memory_extraction_config=MemoryExtractionConfig(
memory_extraction_frequency=1,
memory_extraction_custom_instructions=(
"Only extract billing facts, invoice identifiers, and payment issues."
),
),
)
billing_thread.add_messages(
[
{
"role": "user",
"content": "Invoice INV-9310 was paid twice and needs a refund.",
}
]
)
| API 參考: OracleAgentMemory | 繁體中文 |
更新或清除執行緒指示
使用 update_thread() 來變更現有繫線的持續擷取指示。傳送 None 以清除繫線層次指示,讓未來的繫線處理使用從屬端層次指示,或者若未設定,則為 SDK 的一般擷取行為。
memory.update_thread(
"support_ticket_7421",
memory_extraction_config=MemoryExtractionConfig(
memory_extraction_custom_instructions=(
"Only extract product defects and replacement requests."
)
),
)
memory.update_thread(
"support_ticket_7421",
memory_extraction_config=MemoryExtractionConfig(
memory_extraction_custom_instructions=None
),
)
注意:get_thread(..., memory_extraction_config=MemoryExtractionConfig(memory_extraction_custom_instructions=...)) 可以將指示套用至傳回的即時繫線處理,而不需更新持續的繫線組態。
| API 參考: OracleAgentMemory | 繁體中文 |
結論
在本指南中,我們學習如何設定用戶端層級的自訂擷取指示、覆寫特定執行緒的擷取指示,以及更新或清除執行緒層級的擷取指示。
瞭解如何自訂自動擷取之後,您現在可以繼續使用 LangGraph 的代理程式記憶體短期 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 - Customize Automatic Memory Extraction
#------------------------------------------------------------------------
##Configure custom memory extraction instructions
import oracledb
from oracleagentmemory.core import MemoryExtractionConfig
from oracleagentmemory.core.embedders.embedder import Embedder
from oracleagentmemory.core.llms.llm import Llm
from oracleagentmemory.core.oracleagentmemory import OracleAgentMemory
embedder = Embedder(model="YOUR_EMBEDDING_MODEL")
memory_llm = Llm(model="YOUR_MEMORY_LLM")
db_pool = oracledb.SessionPool(
user="YOUR DB USER",
password="YOUR DB PASSWORD",
dsn="YOUR DB CONNECT STRING",
)
support_extraction_instructions = """
Only extract customer support facts:
- order ids
- return requests
- delivery problems
Ignore greetings, small talk, and one-off troubleshooting text.
""".strip()
memory = OracleAgentMemory(
connection=db_pool,
embedder=embedder,
llm=memory_llm,
memory_extraction_config=MemoryExtractionConfig(
memory_extraction_custom_instructions=support_extraction_instructions
),
)
##Extract support memories from thread messages
support_thread = memory.create_thread(
thread_id="support_ticket_7421",
user_id="customer_123",
memory_extraction_config=MemoryExtractionConfig(memory_extraction_frequency=1),
)
#add_messages persists the turn and runs automated memory extraction because
#MemoryExtractionConfig(memory_extraction_frequency=1) is set for this thread.
support_thread.add_messages(
[
{
"role": "user",
"content": (
"Hi, order 7421 arrived damaged. I need a return request "
"and a replacement delivery."
),
},
{
"role": "assistant",
"content": "I can help start the return request for order 7421.",
},
]
)
results = support_thread.search(
"return request order 7421",
max_results=5,
record_types=["fact"],
)
for result in results:
print(f"- [{result.record.record_type}] {result.content}")
#- [fact] Customer reported damaged order 7421 and requested a return and replacement delivery.
##Override custom instructions for one thread
billing_thread = memory.create_thread(
thread_id="billing_ticket_9310",
user_id="customer_123",
memory_extraction_config=MemoryExtractionConfig(
memory_extraction_frequency=1,
memory_extraction_custom_instructions=(
"Only extract billing facts, invoice identifiers, and payment issues."
),
),
)
billing_thread.add_messages(
[
{
"role": "user",
"content": "Invoice INV-9310 was paid twice and needs a refund.",
}
]
)
##Update or clear thread custom instructions
memory.update_thread(
"support_ticket_7421",
memory_extraction_config=MemoryExtractionConfig(
memory_extraction_custom_instructions=(
"Only extract product defects and replacement requests."
)
),
)
memory.update_thread(
"support_ticket_7421",
memory_extraction_config=MemoryExtractionConfig(
memory_extraction_custom_instructions=None
),
)