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.
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
and api_key
after setting up your backend.
Apply the Memobase memory patch to your OpenAI client instance with a single function call.
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:
Once a user’s information is captured, it can be recalled in subsequent, separate conversations.
The openai_memory
function wraps the OpenAI client with two key actions:
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.
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 to 1000
.
additional_memory_prompt
: Provides a meta-prompt to guide the LLM on how to use the memory.
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.