Using Memobase with the OpenAI API
Diagram of OpenAI API with Memory Integration
Memobase integrates with the OpenAI API, allowing you to add long-term memory to chat completions without altering your existing code. This patch works with the official OpenAI SDK and any other OpenAI-compatible provider.
Setup
-
Install SDKs: Ensure both the Memobase and OpenAI Python SDKs are installed.
-
Initialize Clients: Create instances of both the OpenAI and Memobase clients.
You can find your
project_url
andapi_key
after setting up your backend.
Patch Memory
Apply the Memobase memory patch to your OpenAI client instance with a single function call.
Usage
-
To enable memory, simply add a
user_id
to your standard API call. The client will automatically handle the memory context. -
If no
user_id
is passed, the client functions exactly like the original OpenAI client. -
By default, memory processing is not immediate. User interactions are collected in a buffer to optimize performance. You can manually trigger processing using the
flush
method:
Verifying Memory Retention
Once a user’s information is captured, it can be recalled in subsequent, separate conversations.
How It Works
The openai_memory
function wraps the OpenAI client with two key actions:
- Before Request: It retrieves the user’s memory context from Memobase and injects it into the prompt.
- After Response: It saves only the latest user query and assistant response to the memory buffer.
For example, if your message history is:
And the final response is Your name is Gus.
, Memobase will only store the last exchange. This is equivalent to:
This design ensures you can manage short-term conversation history within your API calls as usual, while Memobase prevents duplicate entries in the long-term memory.
The full implementation script is available here.
Advanced Usage
Custom Parameters
You can pass additional arguments to openai_memory
to customize its behavior:
max_context_size
: Controls the maximum token size of the injected memory context. Defaults to1000
.additional_memory_prompt
: Provides a meta-prompt to guide the LLM on how to use the memory.
Patched Methods
The patched client includes new helper methods:
client.get_memory_prompt("user_id")
: Returns the current memory prompt that will be injected for a given user.client.flush("user_id")
: Immediately processes the memory buffer for a user. Call this if you need to see memory updates reflected instantly.