Home Artificial Intelligence Custom Memory for ChatGPT API

Custom Memory for ChatGPT API

1
Custom Memory for ChatGPT API

A Gentle Introduction to LangChain Memory Types

Self-made gif.

If you might have ever used the OpenAI API, I’m sure you might have noticed the catch.

Got it?

Right! Each time you call the ChatGPT API, the model has no memory of the previous requests you might have made. In other words: each API call is a standalone interaction.

And that is unquestionably annoying when it’s worthwhile to perform follow-up interactions with the model. A chatbot is the golden example where follow-up interactions are needed.

In this text, we are going to explore give memory to ChatGPT when using the OpenAI API, in order that it remembers our previous interactions.

Let’s perform some interactions with the model in order that we experience this default no-memory phenomenon:

prompt = "My name is Andrea"
response = chatgpt_call(prompt)
print(response)

# Output: Nice to fulfill you, Andrea! How can I assist you today?

But when asked a follow-up query:

prompt = "Do you remember my name?"
response = chatgpt_call(prompt)
print(response)

# Output: I'm sorry, as an AI language model, I do not have the flexibility
# to recollect specific details about individual users.

Right, so actually the model doesn’t remember my name regardless that it was given on the primary interaction.

Note: The tactic chatgpt_call() is only a wrapper across the OpenAI API. We already gave a shot on how easily call GPT models at ChatGPT API Calls: A Gentle Introduction in case you would like to test it out!

Some people normally work around this memoryless situation by pre-feeding the previous conversation history to the model each time they do a latest API call. Nevertheless, this practice shouldn’t be cost-optimized and it has actually a limit for long conversations.

In an effort to create a memory for ChatGPT in order that it’s aware of the previous interactions, we will likely be using the favored langchain framework. This framework lets you easily manage the ChatGPT conversation history and optimize it by selecting the correct memory type in your application.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here