所以我正在运行一个半本地聊天 gpt,但有额外的自定义,我想将所有聊天保存到一个文本文件中。它只会用新消息覆盖旧消息,而不是在它们出现时将它们放在新行上。有没有办法将所有终端输入和输出记录到文本文件中?脚本直到我手动停止它才结束它只是在每次响应后循环回到开头。
哦,它的蟒蛇我对这个哈哈真的很陌生。
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