Skip to content

AgentKit Memory Quick Start Guide

AI Agents face increasingly complex multi-turn tasks and context window limitations during operation. They require memory capabilities for context retention and personalization. AgentKit Memory provides a unified memory store interface and integrates with mainstream frameworks, offering cross-session, context-aware, and personalized interactions for Agents, enabling continuous learning and evolution. This chapter guides you through installing dependencies and implementing long-term memory functionality.

Usage Examples

The following example demonstrates how to use AgentKit Memory to implement long-term memory functionality.

Steps:

  • Create a memory resource with semantic policies
  • Write events (conversation history) to the memory resource
  • Retrieve memory records from long-term memory

Creating a Memory Store

Create a memory store for your agent through the Volcano Engine Console: Agentkit-Memory

Obtaining Memory Resource Connection Information

On the Memory Details - Integration Code page, obtain the connection information.

  • For Mem0-type memory resources, you will see environment variables similar to:
DATABASE_MEM0_BASE_URL="https://mem0-your-mem-url.mem0.volces.com:8000"
DATABASE_MEM0_API_KEY="xxxxxx-xxxxxxx"
  • For VikingDB-type memory resources, you will see environment variables similar to:
DATABASE_VIKINGMEM_COLLECTION=<collection_name of your vikingdb memory> #index
DATABASE_VIKINGMEM_MEMORY_TYPE=memory_summary_04uyfx # memory type, separate multiple by comma, e.g. event_v1,event_v2

Building an Agent with Long-Term Memory

VeADK is an open-source agent development framework that provides development, execution, and debugging capabilities for agents, maintained by the Volcano Engine team. The following example demonstrates how to use VeADK to build an agent with long-term memory capabilities, using mem0 as the memory store backend. The agent can store user preferences, conversation summaries, and other personalized interaction information using long-term memory based on user input.

You don't need to build a VeADK project from scratch. We recommend using agentkit-cli to create a project from the Basic Agent App template (the template uses the VeADK framework). After the template is created, you need to make the following modifications:

  • Add LongTermMemory to the Agent
  • Configure the environment variables obtained in the previous section to agentkit.yaml
  • Add mem0ai==0.1.118 to requirements.txt
  • Explicitly call runner.save_session_to_long_term_memory after each round of conversation to save the dialogue results to long-term memory

Finally, your code file should look like this:

python
'''
**simple agent demo app**

Before running, the user must set the following environment variables; otherwise, runtime exceptions will inevitably occur:
- MODEL_AGENT_NAME  # model id in Volcano Engine Ark platform
- MODEL_AGENT_API_KEY  # model api key in Volcano Engine Ark platform

MODEL_AGENT_NAME and MODEL_AGENT_API_KEY are used to access the model service of the Volcano Engine Ark platform.
'''
import logging
import os

from veadk import Agent, Runner
from veadk.memory.long_term_memory import LongTermMemory


from agentkit.apps import AgentkitSimpleApp

logger = logging.getLogger(__name__)

index = "simple_app" # required for viking backend, arbitrary for mem0 backend

backend = "mem0" # one of "mem0", "viking_mem"
if backend == "viking_mem":
    collection_name = os.getenv("DATABASE_VIKINGMEM_COLLECTION")
    if not collection_name:
        raise ValueError("DATABASE_VIKINGMEM_COLLECTION environment variable is not set")
    index = collection_name

app = AgentkitSimpleApp()
long_term_memory = LongTermMemory(
  backend=backend,
  index=index
)

agent = Agent(
  name="simple_app_agent",
  instruction="You are a helpful assistant.",
  long_term_memory=long_term_memory
)
runner = Runner(agent=agent)


@app.entrypoint
async def run(payload: dict, headers: dict) -> str:
    prompt = payload["prompt"]
    user_id = headers["user_id"]
    session_id = headers["session_id"]

    logger.info(
        f"Running agent with prompt: {prompt}, user_id: {user_id}, session_id: {session_id}"
    )
    response = await runner.run(messages=prompt, user_id=user_id, session_id=session_id)

    logger.info(f"Run response: {response}")

    # save the teaching prompt and answer in long term memory
    await runner.save_session_to_long_term_memory(session_id=session_id, user_id=user_id)

    return response


@app.ping
def ping() -> str:
    return "pong!"


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000)

Environment Configuration

Please add the following configuration to your agentkit.yaml file:

# When the long-term memory backend is mem0, add the following configuration
launch_types:
  cloud:
    runtime_envs:
      DATABASE_MEM0_BASE_URL: <your mem0 url>
      DATABASE_MEM0_API_KEY: <your mem0 api key>

# When the long-term memory backend is VikingDB, add the following configuration
launch_types:
  cloud:
    runtime_envs:
      DATABASE_VIKINGMEM_COLLECTION: <your vikingdb collection name>
      DATABASE_VIKINGMEM_MEMORY_TYPE: <your vikingdb memory type, defaults to "event_v1", separate multiple by comma, e.g. event_v1,event_v2>

Starting the Service

Run agentkit launch. The Runtime connection information will be automatically saved to the agentkit.yaml file.

How to Invoke

Execute the following command to invoke:

agentkit invoke "My secret is 0xabcd"

This will automatically use user_id="agentkit_user", session_id="agentkit_sample_session" to invoke the agent.

Verifying Memory Store Functionality

In the first invocation, you saved the password. We need to change the session_id to verify whether the Agent can correctly read information from the memory store. We recommend waiting approximately 1 minute for the long-term memory extraction to complete before invoking again.

agentkit invoke \
  --payload '{"prompt": "What is my secret?"}' \
  --headers '{"user_id": "agentkit_user", "session_id": "s123"}'

You will see that the Agent successfully reads information from the memory store and tells you the password is 0xabcd.

Released under the Apache-2.0 License.