You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.7 KiB

import sys
import requests
import json
from PyQt5.QtWidgets import QApplication, QMainWindow, QGridLayout, QTextEdit, QPushButton, QLineEdit, QWidget
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# Set window title
self.setWindowTitle("Chatbot")
# Set window size
self.setGeometry(100, 100, 800, 600)
# Create central widget
central_widget = QWidget()
# Create text input field for user's messages
self.text_input = QLineEdit(self)
self.text_input.setGeometry(20, 540, 550, 40)
# 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)
# Create chat window to display conversation
self.chat_window = QTextEdit(self)
self.chat_window.setGeometry(20, 20, 760, 500)
self.chat_window.setReadOnly(True)
# Initialize message counter
self.num_messages = 0
def send_message(self):
# Get user's message from text input field
user_message = self.text_input.text()
# TEMPORAIRE
tokens = 100
temperature = 0.1
# Increment message counter
self.num_messages += 1
# Define the API endpoint URL
endpoint = 'https://api.openai.com/v1/completions'
apiKey = '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'
# Define the request headers (if needed)
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {apiKey}"
}
json_data = {
"model": "gpt-3.5-turbo",
"prompt": user_message,
"max_tokens": tokens,
"temperature": temperature
}
# Send the POST request and capture the response
response = requests.post(endpoint, headers=headers, data=json.dumps(json_data))
# Retrieve the response from the API
response_dict = response.json()
ext = response_dict["choices"][0]["text"]
bot_response = f"{ext}"
# 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: #008000;'><strong>Bot:</strong> {bot_response}</p>")
# 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_())