so im runnung a semi-local chat gpt but with extra customization and i want to save all the chats to a text file. it only overwrites the old message with the new one instead of putting them on new lines as they come. is there a way to record all of the terminal input and outputs to a text file? the script doesnt end till i manually stop it it just loops back to the beginning after each response.
oh and its python im really new to this lol.
from contextlib import redirect_stdout
import sys
import openai,pyautogui
import tkinter as tk
from tkinter import simpledialog
import datetime
from datetime import datetime
# Set the API key
openai.api_key = "THATS MY PRIVATE API KEY"
# Choose a model
MODEL_ENGINE = "text-davinci-002"
def get_response(prompt):
"""Returns the response for the given prompt using the OpenAI API."""
completions = openai.Completion.create(
engine = MODEL_ENGINE,
prompt = prompt,
max_tokens = 1024,
temperature = 0.7,
)
return completions.choices[0].text
def handle_input(
input_str : str,
conversation_history : str,
USERNAME : str,
AI_NAME : str,
):
"""Updates the conversation history and generates a response using GPT-3."""
# Update the conversation history
conversation_history += f"{USERNAME}: {input_str}\n"
# Generate a response using GPT-3
message = get_response(conversation_history)
# Update the conversation history
conversation_history += f"{AI_NAME}: {message}\n"
# Print the response
print(f'{AI_NAME}: {message}')
pyautogui.alert('You Said: ' + USER_INP + '''
''' + f'{AI_NAME}: {message}')
now = datetime.now()
dt_string2 = now.strftime("%d/%m/%Y %H:%M:%S")
return conversation_history
# Set the initial prompt to include a personality and habits
ROOT = tk.Tk()
ROOT.withdraw()
# the input dialog and prompt
#use "USER_INP" as the tag
USER_INP_USERNAME = simpledialog.askstring(title="MattGPT ~ Local",
prompt="What is your name?",
initialvalue="Matt")
ROOT = tk.Tk()
ROOT.withdraw()
# the input dialog and prompt
#use "USER_INP" as the tag
USER_INP_NAME = simpledialog.askstring(title="MattGPT ~ Local",
prompt="Would you like to change the AI Name?",
initialvalue="MattGPT")
ROOT = tk.Tk()
ROOT.withdraw()
# the input dialog and prompt
#use "USER_INP" as the tag
USER_INP_PERSONALITY = simpledialog.askstring(title="MattGPT ~ Local",
prompt="Would you like to change the AI personality?",
initialvalue='''private stuff ;)''')
INITIAL_PROMPT = (USER_INP_PERSONALITY)
conversation_history = INITIAL_PROMPT + "\n"
print('Current Prompt: ' + INITIAL_PROMPT)
USERNAME = USER_INP_USERNAME
AI_NAME = USER_INP_NAME
while True:
# Get the user's input
ROOT = tk.Tk()
ROOT.withdraw()
# the input dialog and prompt
#use "USER_INP" as the tag
USER_INP = simpledialog.askstring(title="MattGPT ~ Local",
prompt="What would you like to say?")
user_input = USER_INP
if(user_input=="None"):
pyautogui.hotkey('command', 'q')
else:
# Handle the input
print(USER_INP_USERNAME + ': ' + user_input)
handle_input(user_input, conversation_history, USERNAME, AI_NAME)
now = datetime.now()
dt_string1 = now.strftime("%d/%m/%Y %H:%M:%S")
class Logger(object):
def __init__(self):
self.terminal = sys.stdout
self.log = open("/Users/matthew/Desktop/MattGPT_Log", "a")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
#this flush method is needed for python 3 compatibility.
#this handles the flush command by doing nothing.
#you might want to specify some extra behavior here.
pass
sys.stdout = Logger()
# f = open("/Users/matthew/Desktop/MattGPT_Log", 'w')
# sys.stdout = f