自定义自动内存提取
自动内存提取可以捕获有用的详细信息,而无需其他配置。自定义说明允许应用程序在需要针对特定用例定制默认行为时向提取流程添加指导。
本文介绍了如何通过自定义说明指导 Oracle AI Agent Memory 的自动内存提取。
定制说明为内存提取提供了特定于应用程序的附加指导。当您的应用需要提取数据以反映领域知识、产品策略或特定于对话的优先级时,它们非常有用。
注:当内存提取应遵循特定于应用程序的指导时,请使用定制说明。当不需要其他指导时,默认提取是合适的。
提示:有关如何安装 oracleagentmemory,请参见 Get Started with Agent Memory 。如果此示例需要本地 Oracle 数据库,请按照 Run Oracle AI Database Locally 操作。
配置客户端级提取说明
使用 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 | OracleThread |
从线程消息中提取支持记忆
创建支持主题并添加用户和助手消息。该示例设置了 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 | OracleSearchResult |
覆盖一个线程的说明
当一个线程需要比客户机缺省值更窄的提取策略时,将 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 | OracleThread |
更新或清除线程说明
使用 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 | OracleThread |
小结
在本指南中,我们学习了如何配置客户端级别的定制提取指令,为特定线程覆盖它们,以及更新或清除线程级别的提取指令。
在了解如何自定义自动提取后,您现在可以继续将代理内存短期 API 与 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 - 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
),
)