You can add long-term memory to your OpenAI chat completions by patching the official OpenAI SDK using the openai_memory function. This allows you to inject user context into prompts and save interactions to Memobase automatically by simply providing a user_id in your API calls.
Setup
- Install the required SDKs:
pip install memobase openai
- Initialize both clients and apply the patch:
from openai import OpenAI
from memobase import MemoBaseClient
from memobase.patch.openai import openai_memory
client = OpenAI()
mb_client = MemoBaseClient(
project_url=YOUR_PROJECT_URL,
api_key=YOUR_API_KEY,
)
# Apply the memory patch
client = openai_memory(client, mb_client)
Usage
To enable memory, pass a user_id to the chat.completions.create method. If user_id is omitted, the client behaves like the standard OpenAI client.
client.chat.completions.create(
messages=[
{"role": "user", "content": "My name is Gus"},
],
model="gpt-4o",
user_id="test_user_123",
)
from openai import OpenAI
from memobase import MemoBaseClient
from memobase.patch.openai import openai_memory
client = OpenAI()
mb_client = MemoBaseClient(
project_url=YOUR_PROJECT_URL,
api_key=YOUR_API_KEY,
)
client = openai_memory(client, mb_client)
client.chat.completions.create(
messages=[
{"role": "user", "content": "My name is Gus"},
],
model="gpt-4o",
user_id="test_user_123",
)