import sys import openai import datetime import os from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QPushButton, QLineEdit # TODO remplacer le role system, logpath, username class MainWindow(QMainWindow): def __init__(self): super().__init__() # Set window title self.setWindowTitle("Chatbot") # Set window size self.setGeometry(100, 100, 800, 600) # Create text input field for user's messages self.text_input = QLineEdit(self) self.text_input.setGeometry(20, 540, 550, 40) # Initialize API needs openai.api_key = "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" self.chat_log = [{"role": "system", "content": "Tu es un prof de Python"}] # Initialize message counter self.num_messages = 0 # Create chat window to display conversation self.chat_window = QTextEdit(self) self.chat_window.setGeometry(20, 20, 760, 500) self.chat_window.setReadOnly(True) # Create send button to send user's messages self.send_button = QPushButton("Send", self) self.send_button.setGeometry(580, 540, 200, 40) self.send_button.clicked.connect(self.send_message) self.username = "JimmyB" self.filename = os.path.join(r"C:\Workspace\Appli\Logs", f"{datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S')}_{self.username}_chatlog.txt") # set the filename self.write_to_file() # call the function to write the first line to the file def write_to_file(self): with open(self.filename, "a") as file: timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") # get the current time in the desired format file.write(f"{timestamp} {self.chat_log[-1]}\n") # write the timestamp and the latest chat log entry to the file def send_message(self): # Get user's message from text input field user_message = self.text_input.text() self.chat_log.append({"role": "user", "content": user_message}) self.write_to_file() """reply = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=self.chat_log) response=reply['choices'][0]['message']['content']""" # Increment message counter self.num_messages += 1 # Send greeting to user response = f"Hello, you have sent {self.num_messages} messages." self.chat_log.append({"role": "assistant", "content":response}) self.write_to_file() # Add user's message and bot's response to chat window self.chat_window.append(f"
You : {user_message}
") self.chat_window.append(f"Bot : {response}
") # Clear text input field self.text_input.clear() if __name__ == "__main__": # Create QApplication instance app = QApplication(sys.argv) # Create MainWindow instance main_window = MainWindow() # Show MainWindow main_window.show() # Execute QApplication event loop sys.exit(app.exec_())