Memoria di archiviazione e ricerca

In questa guida è possibile creare un componente Oracle Agent Memory, memorizzare i messaggi thread e le memorie permanenti, quindi cercarli di nuovo.

Suggerimento: per l'impostazione dei package, vedere Introduzione alla memoria dell'agente. Se hai bisogno di un Oracle AI Database locale per questo esempio, segui Esegui Oracle AI Database localmente.

Memorie e messaggi in archivio

Creare il componente memoria agente Oracle con una connessione o un pool Oracle DB, aggiungere un thread breve e rendere persistente una memoria utente permanente insieme ai messaggi.

Nota: per impostazione predefinita, OracleAgentMemory prevede un LLM in modo che OracleThread possa estrarre periodicamente memorie permanenti dai messaggi thread recenti aggiunti tramite add_messages(). Se non si desidera eseguire l'estrazione automatica della memoria, impostare memory_extraction_config=MemoryExtractionConfig(extract_memories=False) quando si crea il client o quando si chiama create_thread().

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.")
Riferimento API: OracleAgentMemory OracleThread

I messaggi lunghi e le memorie vengono chunk automaticamente per l'indicizzazione del recupero quando necessario. Il chunk non modifica il testo del messaggio o della memoria memorizzata. La ricerca utilizza i blocchi internamente, quindi restituisce i record logici originali.

Nota: sostituire "YOUR DB USER", "YOUR DB PASSWORD" e "YOUR DB CONNECT STRING" con le credenziali e la stringa di connessione per Oracle AI Database. Se hai bisogno di un database locale per questo esempio, segui Esegui Oracle AI Database localmente.

Ricerca della memoria dell'agente

Cercare le memorie e i messaggi memorizzati, definiti per l'utente.

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.

Nota: l'output mostrato in precedenza è illustrativo. Le versioni future possono restituire ulteriori tipi di risultati, campi o comportamento degli ordini.

Riferimento API: SearchScope Risultati ricerca Oracle

Per i limiti configurabili relativi a risultati diretti, alla riclassificazione, alla potatura e ai budget di token, vedere Migliorare la pertinenza dei risultati della ricerca.

Conclusione

In questa guida è stato illustrato come creare un client Oracle Agent Memory, memorizzare i messaggi thread insieme alle memorie permanenti e cercare di nuovo tali record con il recupero con ambito utente.

→ Dopo aver appreso il workflow di memoria di base per utente singolo, ora è possibile passare a Utilizzare le API a breve termine della memoria di Oracle Agent con LangGraph.

Codice completo

Copiare il codice completo che segue.

#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.