Custom Memory for ChatGPT API. A Gentle Introduction to LangChain… | by Andrea Valenzuela | Aug, 2023


A Gentle Introduction to LangChain Memory Types

Self-made gif.

If you have ever used the OpenAI API, I am sure you have noticed the catch.

Got it?

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

And that is definitely annoying when you need to perform follow-up interactions with the model. A chatbot is the golden example where follow-up interactions are needed.

In this article, we will explore how to give memory to ChatGPT when using the OpenAI API, so that it remembers our previous interactions.

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

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

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

But when asked a follow-up question:

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

# Output: I'm sorry, as an AI language model, I don't have the ability
# to remember specific information about individual users.

Right, so in fact the model does not remember my name even though it was given on the first interaction.

Note: The method chatgpt_call() is just a wrapper around the OpenAI API. We already gave a shot on how easily call GPT models at ChatGPT API Calls: A Gentle Introduction in case you want to check it out!

Some people normally work around this memoryless situation by pre-feeding the previous conversation history to the model every time they do a new API call. Nevertheless, this practice is not cost-optimized and it has certainly a limit for long conversations.

In order to create a memory for ChatGPT so that it is aware of the previous interactions, we will be using the popular langchain framework. This framework allows you to easily manage the ChatGPT conversation history and optimize it by choosing the right memory type for your application.



Source link

Leave a Comment