|
|
|
@ -1,10 +1,6 @@
|
|
|
|
import sys
|
|
|
|
import sys
|
|
|
|
import requests
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
import openai
|
|
|
|
import openai
|
|
|
|
from PyQt5.QtWidgets import QApplication, QMainWindow, QGridLayout, QTextEdit, QPushButton, QLineEdit, QWidget
|
|
|
|
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QPushButton, QLineEdit, QWidget
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MainWindow(QMainWindow):
|
|
|
|
class MainWindow(QMainWindow):
|
|
|
|
def __init__(self):
|
|
|
|
def __init__(self):
|
|
|
|
@ -23,98 +19,38 @@ class MainWindow(QMainWindow):
|
|
|
|
self.text_input = QLineEdit(self)
|
|
|
|
self.text_input = QLineEdit(self)
|
|
|
|
self.text_input.setGeometry(20, 540, 550, 40)
|
|
|
|
self.text_input.setGeometry(20, 540, 550, 40)
|
|
|
|
|
|
|
|
|
|
|
|
# Create send button to send user's messages
|
|
|
|
# Initialize API needs
|
|
|
|
self.send_button = QPushButton("Send", self)
|
|
|
|
openai.api_key = "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"
|
|
|
|
self.send_button.setGeometry(580, 540, 200, 40)
|
|
|
|
self.chat_log = [{"role": "system", "content": "Tu es un prof de Python"}]
|
|
|
|
self.send_button.clicked.connect(self.send_message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Create chat window to display conversation
|
|
|
|
# Create chat window to display conversation
|
|
|
|
self.chat_window = QTextEdit(self)
|
|
|
|
self.chat_window = QTextEdit(self)
|
|
|
|
self.chat_window.setGeometry(20, 20, 760, 500)
|
|
|
|
self.chat_window.setGeometry(20, 20, 760, 500)
|
|
|
|
self.chat_window.setReadOnly(True)
|
|
|
|
self.chat_window.setReadOnly(True)
|
|
|
|
|
|
|
|
|
|
|
|
# Initialize message counter
|
|
|
|
# Create send button to send user's messages
|
|
|
|
self.num_messages = 0
|
|
|
|
self.send_button = QPushButton("Send", self)
|
|
|
|
|
|
|
|
self.send_button.setGeometry(580, 540, 200, 40)
|
|
|
|
|
|
|
|
self.send_button.clicked.connect(self.send_message)
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
# TEMPORAIRE
|
|
|
|
self.chat_log.append({"role": "user", "content": user_message})
|
|
|
|
tokens = 100
|
|
|
|
reply = openai.ChatCompletion.create(
|
|
|
|
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",
|
|
|
|
model="gpt-3.5-turbo",
|
|
|
|
messages=[
|
|
|
|
messages=self.chat_log)
|
|
|
|
{"role": "system", "content": "You are a helpful assistant."},
|
|
|
|
response=reply['choices'][0]['message']['content']
|
|
|
|
{"role": "user", "content": "Who won the world series in 2020?"},
|
|
|
|
self.chat_log.append({"role": "assistant", "content":response})
|
|
|
|
{"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
|
|
|
|
# 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>")
|
|
|
|
self.chat_window.append(f"<p style='color: #008000;'><strong>Bot:</strong> {bot_response}</p>")
|
|
|
|
self.chat_window.append(f"<p style='color: #008000;'><strong>Bot:</strong> {response}</p>")
|
|
|
|
|
|
|
|
|
|
|
|
# Clear text input field
|
|
|
|
# Clear text input field
|
|
|
|
self.text_input.clear()
|
|
|
|
self.text_input.clear()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# Create QApplication instance
|
|
|
|
# Create QApplication instance
|
|
|
|
app = QApplication(sys.argv)
|
|
|
|
app = QApplication(sys.argv)
|
|
|
|
|