Skip to content

Instantly share code, notes, and snippets.

@Curtis-64
Created March 28, 2023 06:58
Show Gist options
  • Save Curtis-64/d51becace0d62d54832230aebfcd75ba to your computer and use it in GitHub Desktop.
Save Curtis-64/d51becace0d62d54832230aebfcd75ba to your computer and use it in GitHub Desktop.
ChatGPT Memory
'ChatGPT Streaming Memory by Curtis White+ChatGPT
'This only works for code interpreter alpha
'Not extracted code from conversation-- likely smart enough to extract itself
The ConversationMemory class:
python
Copy code
import re
import json
class ConversationMemory:
def __init__(self):
self.conversation_log = []
def add_to_conversation(self, entry):
self.conversation_log.append(entry)
def search_conversation(self, search_term, limit=3, context=1):
# Escape any special characters in the search term
regex_pattern = re.escape(search_term)
# Compile the regex pattern
pattern = re.compile(regex_pattern, re.IGNORECASE)
# Search for matches in the conversation log and provide additional context
matches_with_context = []
for i, entry in enumerate(self.conversation_log):
if pattern.search(entry):
start = max(i - context, 0)
end = min(i + context + 1, len(self.conversation_log))
matches_with_context.append(self.conversation_log[start:end])
# Limit the number of matches returned
matches_with_context = matches_with_context[:limit]
# Return the matches with context
return matches_with_context
def auto_query_memory(self, input_message):
# Predefined list of query terms
query_terms = ["memory", "Midjourney 5", "amazing"]
results = []
for term in query_terms:
# Search the conversation for matches using the query term
matches = self.search_conversation(term)
if matches:
results.extend(matches)
return results
# Create an instance of the ConversationMemory class
memory = ConversationMemory()
Functions for saving and loading the conversation to/from a file:
python
Copy code
def save_conversation_to_file(conversation_log, filename):
# Serialize the conversation log as JSON
conversation_json =
Curtis White
continue
Certainly! Continuing from where we left off:
python
Copy code
conversation_json = json.dumps(conversation_log)
# Save the JSON to a file
with open(filename, 'w') as file:
file.write(conversation_json)
def load_conversation_from_file(filename):
# Read the JSON from the file
with open(filename, 'r') as file:
conversation_json = file.read()
# Deserialize the conversation log from JSON
conversation_log = json.loads(conversation_json)
return conversation_log
Usage examples of the ConversationMemory class, saving and loading conversation logs, and automatic query:
python
Copy code
# Add entries to the conversation
memory.add_to_conversation("User: How is your memory functioning?")
memory.add_to_conversation("Assistant: My memory is functioning well.")
memory.add_to_conversation("User: Tell me more about your memory capabilities.")
memory.add_to_conversation("User: $d020 is background color")
# Search the conversation log for the term "$d020" and provide additional context
query_results_with_context = memory.search_conversation("$d020", context=1)
# Save the current conversation log to a file
save_conversation_to_file(memory.conversation_log, '/mnt/data/conversation_log.json')
# Load the conversation log from the saved file
entire_conversation = load_conversation_from_file('/mnt/data/conversation_log.json')
# Automatically query memory for the current input message
current_input = "Midjourney 5 is really amazing."
auto_query_results = memory.auto_query_memory(current_input)
This Python code represents the main components we used during the session to implement a memory backing store for the conversation, search for specific terms, save and load conversation logs, and automatically query the memory based on predefined terms. The ConversationMemory class allows us to maintain a log of the conversation, and the search_conversation and auto_query_memory methods enable us to search for relevant information within the conversation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment