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.

130 lines
4.1 KiB

import sys
import requests
import json
import os
import openai
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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'
# Load your API key from an environment variable or secret management service
openai.api_key = os.getenv("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")
response = openai.Completion.create(model="gpt-3.5-turbo", prompt="Say this is a test", temperature=0, max_tokens=7)
openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"},
{"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
{"role": "user", "content": "Where was it played?"}
]
)
"""
{
'id': 'chatcmpl-6p9XYPYSTTRi0xEviKjjilqrWU2Ve',
'object': 'chat.completion',
'created': 1677649420,
'model': 'gpt-3.5-turbo',
'usage': {'prompt_tokens': 56, 'completion_tokens': 31, 'total_tokens': 87},
'choices': [
{
'message': {
'role': 'assistant',
'content': 'The 2020 World Series was played in Arlington, Texas at the Globe Life Field, which was the new home stadium for the Texas Rangers.'},
'finish_reason': 'stop',
'index': 0
}
]
}
"""
# 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_())