Intégration API ChatGPT Done

master
Sparklight 3 years ago
parent a4df943d34
commit 0c0fe8ddac

@ -1,10 +1,6 @@
import sys
import requests
import json
import os
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):
def __init__(self):
@ -23,98 +19,38 @@ class MainWindow(QMainWindow):
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)
# Initialize API needs
openai.api_key = "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"
self.chat_log = [{"role": "system", "content": "Tu es un prof de Python"}]
# 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
# 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)
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(
self.chat_log.append({"role": "user", "content": user_message})
reply = 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}"
messages=self.chat_log)
response=reply['choices'][0]['message']['content']
self.chat_log.append({"role": "assistant", "content":response})
# 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>")
self.chat_window.append(f"<p style='color: #008000;'><strong>Bot:</strong> {response}</p>")
# Clear text input field
self.text_input.clear()
if __name__ == "__main__":
# Create QApplication instance
app = QApplication(sys.argv)

Loading…
Cancel
Save