OpenAI Client with User Memory
One of the major use case of Memobase is to remember user preferences from the conversation history. In this tutorial, we will demonstrate how to build memory feature around OpenAI client. Memobase offers an easy way already for OpenAI client to remember users. This document is a detailed code breakdown of how to implement this feature.
Setup
- Go to Memobase for your Memobase API Key or launch a local server
- Setup the environment variables:
- Install the dependencies:
Code Breakdown
Diagram of OpenAI API with Memory
To implement the memory feature around OpenAI client, we need to:
- Add wrappers around the OpenAI client, so we can obtain the chat messages and modify the prompts to inject memory
- Integrate Memobase APIs in wrappers to memorize the chat history and retrieve user memory
- Test if the memory feature works as expected
Basic Setup
Add wrappers around the OpenAI client
We use duck typing to add wrappers around the OpenAI client.
Above code is a simplified version of the actual implementation:
- We first check if the OpenAI client is already patched, if so, we return the original client
- We then replace the
chat.completions.create
method, which is the main method to generate the chat completion
We use a function called _sync_chat
to replace the chat.completions.create
method.
New chat.completions.create
method
We hope the new chat.completions.create
method can:
- receive
user_id
in the arguments, so that we can memoize for the specific user - receive every possible arguments that the original
create
method receives, so the new one won’t break any existing code - return the same type as the original
create
method, and I want to support streaming too. - almost the same running time as the original
create
method
We first make sure the orginal arguments can be passed to the original create
method:
As we can see, the new chat.completions.create
method is almost the same as the original one, except that it can receive user_id
in the arguments.
Since Memobase use uuid to identify users, we need to convert the user_id
to uuid. By doing so, you can pass any user name.
If the user_id
is provided, we need to:
- Get or create the user in Memobase
- Insert the user’s memory context into the messages
- Call the original
create
method - Save the conversation to Memobase
Here’s how it’s implemented:
Enhancing messages with user context
The user_context_insert
function adds the user’s memory to the messages before sending to OpenAI:
Saving conversations
After getting a response, we save the conversation to Memobase:
For non-streaming responses, it’s straightforward:
For streaming responses, we accumulate the chunks and save after all chunks are received:
Utility Functions
The wrapper also includes helper functions:
Usage Example
Now that we understand how the client works, let’s use it:
The first time, the AI might not know the user’s name. But after telling it:
Conclusion
This implementation demonstrates a powerful way to add user memory to the OpenAI client. The patched client:
- Works identically to the original OpenAI client
- Adds memory capabilities when
user_id
is provided - Handles both streaming and non-streaming responses
- Automatically saves conversations to Memobase
- Retrieves and injects user context into prompts
For applications requiring personalized AI interactions, this approach provides a clean, non-intrusive way to add memory capabilities to your existing OpenAI-based applications.