代理内存入门

本文将指导您安装代理内存并执行基本的内存操作,包括存储和检索用户上下文。

Prerequisites

确保您具有:

安装 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) 兼容。

平方米

oci

双子

人类

openai

嵌入

已确认以下嵌入模型兼容。

主办机构

oci

双子

openai