logging & timestamp

master
Sparklight 3 years ago
parent 879eab4f07
commit 4e5fc8fc17

@ -0,0 +1 @@
2023-03-06 12:52:27 {'role': 'system', 'content': 'Tu es un prof de Python'}

@ -1,7 +1,11 @@
import sys import sys
import openai import openai
import datetime
import os
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QPushButton, QLineEdit from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QPushButton, QLineEdit
# TODO remplacer le role system, logpath, username
class MainWindow(QMainWindow): class MainWindow(QMainWindow):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
@ -33,22 +37,34 @@ class MainWindow(QMainWindow):
self.send_button.setGeometry(580, 540, 200, 40) self.send_button.setGeometry(580, 540, 200, 40)
self.send_button.clicked.connect(self.send_message) 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): def send_message(self):
# Get user's message from text input field # Get user's message from text input field
user_message = self.text_input.text() user_message = self.text_input.text()
self.chat_log.append({"role": "user", "content": user_message}) self.chat_log.append({"role": "user", "content": user_message})
self.write_to_file()
"""reply = openai.ChatCompletion.create( """reply = openai.ChatCompletion.create(
model="gpt-3.5-turbo", model="gpt-3.5-turbo",
messages=self.chat_log) messages=self.chat_log)
response=reply['choices'][0]['message']['content'] response=reply['choices'][0]['message']['content']"""
self.chat_log.append({"role": "assistant", "content":response})"""
# Increment message counter # Increment message counter
self.num_messages += 1 self.num_messages += 1
# Send greeting to user # Send greeting to user
response = f"Hello, you have sent {self.num_messages} messages." 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 # Add user's message and bot's response to chat window
self.chat_window.append(f"<p style='color: #0084ff;'><strong>You :</strong> {user_message}</p>") self.chat_window.append(f"<p style='color: #0084ff;'><strong>You :</strong> {user_message}</p>")

Loading…
Cancel
Save