Mise en place de l'API

master
Sparklight 3 years ago
parent 3fe2c32798
commit 8f813f8d90

@ -1,5 +1,6 @@
import sys
import requests
import json
from PyQt5.QtWidgets import QApplication, QMainWindow, QGridLayout, QTextEdit, QPushButton, QLineEdit, QWidget
@ -37,11 +38,37 @@ class MainWindow(QMainWindow):
# 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
# Send greeting to user
bot_response = f"Hello, you have sent {self.num_messages} messages."
# 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": "text-davinci-002",
"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>")

Loading…
Cancel
Save