代理内存入门
本文将指导您安装代理内存并执行基本的内存操作,包括存储和检索用户上下文。
Prerequisites
确保您具有:
- 访问 Oracle AI Database(请参阅本地运行 Oracle AI Database )
- Python 3。10 或更高版本
安装 SDK
要安装代理内存,请运行:
pip install "oracleagentmemory==26.4.0"
使用 pip 进行安装时,将在支持的平台上提取预构建的二进制轮。
初始化内存实例
通过配置嵌入器、LLM 和数据库连接来创建 OracleAgentMemory 实例。
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.
注:显示的输出具有说明性。将来的版本可能会返回其他结果类型、字段或排序。
模型兼容性
自 2026 年 4 月起,以下大型语言模型 (Large Language Models,LLM) 和嵌入模型与 oracleagentmemory 兼容。
LLM
已确认以下大型语言模型 (LLM) 兼容。
平方米
openai/google/gemma-4-26B-A4B-itopenai/openai/gpt-oss-120b
oci
oci/google.gemini-2.5-flashoci/google.gemini-2.5-flash-liteoci/google.gemini-2.5-prooci/xai.grok-3oci/xai.grok-3-fastoci/xai.grok-3-minioci/xai.grok-3-mini-fastoci/xai.grok-4oci/xai.grok-4-1-fast-non-reasoningoci/xai.grok-4-1-fast-reasoningoci/xai.grok-4-fast-non-reasoningoci/xai.grok-4-fast-reasoningoci/xai.grok-4.20-non-reasoningoci/xai.grok-4.20-reasoningoci/xai.grok-code-fast-1oci/cohere.command-latestoci/cohere.command-a-03-2025oci/cohere.command-a-reasoning-08-2025oci/cohere.command-a-vision-07-2025oci/cohere.command-a-translate-08-2025oci/cohere.command-plus-latestoci/cohere.cohere.command-r-08-2024oci/cohere.command-r-plus-08-2024
双子
gemini/gemini-3.1-flash-lite-previewgemini/gemini-3-flash-previewgemini/gemini-3.1-pro-preview
人类
anthropic/claude-opus-4-7anthropic/claude-opus-4-6anthropic/claude-sonnet-4-6anthropic/claude-haiku-4-5
openai
openai/gpt-4.1openai/gpt-4.1-miniopenai/gpt-4oopenai/gpt-5openai/gpt-5-miniopenai/gpt-5.1openai/gpt-5.2openai/gpt-5.4openai/gpt-5.4-mini
嵌入
已确认以下嵌入模型兼容。
主办机构
hosted_vllm/Snowflake/snowflake-arctic-embed-l-v2.0
oci
oci/cohere.embed-english-v3.0oci/cohere.embed-english-light-v3.0oci/cohere.embed-multilingual-v3.0oci/cohere.embed-v4.0
双子
gemini/gemini-embedding-001gemini/gemini-embedding-2-preview
openai
openai/text-embedding-3-largeopenai/text-embedding-3-small