# Enabling Long-term Memory in GPT-3 ```toc ``` The idea is to enable a (more) human like conversation between a user and the generative AI (i.e. GPT-3 in this case). In order to make that happen, we need to have a way to make GPT-3 aware of the user, their history, what they are like, and all other context relevant to the user. This document outlines a way in which this can be done using text embedding in a way that uses minimal memory. ## Step 1: Create a Database -- Store Memory - `User Memory Bank` Create a database to store information about the user. This information should include the following: - Who the user is ### Step 1.1 Embed Embed all new information and add it to the `User Memory Bank` Use something like this ```python def gpt3_embedding(content, engine='text-embedding-ada-002'): content = content.encode(encoding='ASCII',errors='ignore').decode() response = openai.Embedding.create(input=content,engine=engine) vector = response['data'][0]['embedding'] # this is a normal list return vector ``` ## Step 2: Load Conversation Load relevant user history in *chronological order* - Fetch memories based on a certain length that is pre-decided (I believe HH is using the past X amount) - Fetch memories based on how similar they are to what the user has just given - Calculate similarity using *cosine similarity index* - Order the top X most similar memories and return those ```python def similarity(v1, v2): # return cosine similarity return np.dot(v1, v2)/(norm(v1)*norm(v2)) ``` ```python def fetch_memories(vector, logs, count): scores = [] # TODO: Implement cossine similarity return most_similar_memories ``` ## Step 3: Summarise Memories and Create Prompt Summarise memories onto one payload. 1. Sort chronologically 2. Reconstitute block ### Step 3.1 Use GPT-3 to create a summary - Take all the most similar memories and then use GPT-3 to summarise the key components ## Step 4: Prompt GPT with Memory Summary + Most Recent Chat Prompt would look like the following ```md I am an AI coach named Eden. I am a personality coach. My goals are to reduce anxiety, increase psycological flexibility, and increase self-understanding. I will read the conversation notes and recent messages, and then I will provide a long, verbose, detailed answer. I will then end my response with a follow-up or leading question. The following are notes from earlier conversations with USER named Nate: <<Here goes the summary of the memories from above>> The following are the most recent messages in the conversation: <<here goes the last X conversations>> I will now provide a long, detailed, verbose response, followed by a question: Eden: ``` Step 1: Record what the user says Step 2: Whenever a user says something, search through memory bank for relevant (similar context) memories Step 3: Select the most relevant memories out of all recorded memories Step 4: Summarise relevant memories using gpt3 Step 5: Use summarised memory + New Conversation and ask gpt3 for new response Step 6 Save interaction