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.
Diagram of OpenAI API with Memory
To implement the memory feature around OpenAI client, we need to:
We use duck typing to add wrappers around the OpenAI client.
Above code is a simplified version of the actual implementation:
chat.completions.create
method, which is the main method to generate the chat completionWe use a function called _sync_chat
to replace the chat.completions.create
method.
chat.completions.create
methodWe hope the new chat.completions.create
method can:
user_id
in the arguments, so that we can memoize for the specific usercreate
method receives, so the new one won’t break any existing codecreate
method, and I want to support streaming too.create
methodWe 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:
create
methodHere’s how it’s implemented:
The user_context_insert
function adds the user’s memory to the messages before sending to OpenAI:
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:
The wrapper also includes helper functions:
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:
This implementation demonstrates a powerful way to add user memory to the OpenAI client. The patched client:
user_id
is providedFor applications requiring personalized AI interactions, this approach provides a clean, non-intrusive way to add memory capabilities to your existing OpenAI-based applications.
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.
Diagram of OpenAI API with Memory
To implement the memory feature around OpenAI client, we need to:
We use duck typing to add wrappers around the OpenAI client.
Above code is a simplified version of the actual implementation:
chat.completions.create
method, which is the main method to generate the chat completionWe use a function called _sync_chat
to replace the chat.completions.create
method.
chat.completions.create
methodWe hope the new chat.completions.create
method can:
user_id
in the arguments, so that we can memoize for the specific usercreate
method receives, so the new one won’t break any existing codecreate
method, and I want to support streaming too.create
methodWe 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:
create
methodHere’s how it’s implemented:
The user_context_insert
function adds the user’s memory to the messages before sending to OpenAI:
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:
The wrapper also includes helper functions:
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:
This implementation demonstrates a powerful way to add user memory to the OpenAI client. The patched client:
user_id
is providedFor applications requiring personalized AI interactions, this approach provides a clean, non-intrusive way to add memory capabilities to your existing OpenAI-based applications.