Diagram of OpenAI API with Memory

Memobase supports OpenAI API integration. This allows you to “patch” Memobase’s memory capabilities to OpenAI chat completion (or any LLM provider that is compatible to OpenAI SDK) without changing the original code.

Setup

  1. Make sure you install Memobase python SDK and OpenAI python SDK
pip install memobase openai
  1. Initialize OpenAI and MemoBaseClient
from openai import OpenAI
from memobase import MemoBaseClient

client = OpenAI()
mb_client = MemoBaseClient(
    project_url=ENDPOINT,
    api_key=TOKEN,
)

Make sure you have the Memobase Endpoint and Token ready, check here

Patch Memory

from memobase.patch.openai import openai_memory

client = openai_memory(client, mb_client)

You’re all set!

How to use OpenAI with Memory?

  1. You can use OpenAI API as you normally would but simply add user_id to the request.
client.chat.completions.create(
    messages=[
        {"role": "user", "content": "I'm Gus"},
    ],
    model="gpt-4o"
)
  1. If no user_id is passed, the client will act just like the original OpenAI client.
  2. If user_id is passed, the client will use Memobase automatically.
  3. The memory processing of the user won’t be trigger immediately, there is a buffer zone to collect the recent messages. However, you can manually trigger the process by
client.flush("test")

Make sure the memory is retained

client.chat.completions.create(
    messages=[
        {"role": "user", "content": "Who'm I?"},
    ],
    model="gpt-4o"
)

# I'm sorry, I can't determine your identity...

How it works?

  1. The openai_memory function patches the OpenAI client to call Memobase SDK before and after the chat completion.
  2. Only the latest user query and assistant response will be inserted into the memory.
    • If your messages are:
    [
        {"role": "user", "content": "I'm Gus"},
        {"role": "assistant", "content": "Hello Gus! How can I help you?"},
        {"role": "user", "content": "Who'm I?"},
    ]
    
    And the response is You'r Gus!.
    • Then the only the latest query and response will be inserted, equivalent to:
    u.insert(
        ChatBlob(messages=[
            {"role": "user", "content": "Who'm I?"},
            {"role": "assistant", "content": "You're Gus!"},
        ]
    ))
    
  3. So you don’t really change the way you’re currently using OpenAI API, you can still keep the recent messages when you call the chat completion API. And Memobase won’t repeatedly insert the same messages into the memory.

The full script is here

Advanced Usage

Params

When you use the openai_memory function, you can pass additional arguments to customize the memory behavior:

client = openai_memory(client, mb_client, max_context_size=500)

max_context_size will control the maximum token size of Memory Context will take, default to 1000.

client = openai_memory(client, mb_client, additional_memory_prompt="Make sure the user's query needs the memory, otherwise just return the answer directly.")

additional_memory_prompt will control how LLM will use the memory, some examples:

additional_memory_prompt="Always use user memory to give a personalized answer"
additional_memory_prompt="Use memory to dig deeper into the user's query and rewrite the query..."

Patched Methods

Once you patch the OpenAI client, you can use the following methods in it:

client.get_memory_prompt("userid")

This method will return the current memory section(including memory, memory_prompt) of the user.

client.flush("userid")

This method will flush the memory of the user immediately, Memobase won’t update the memory right away, if you like to see the updated memory once your message is processed, call this method.