Python調(diào)用ChatGPT的API實現(xiàn)文章生成
實操目標(biāo)
最近ChatGPT大火,在3.5版本后開放了接口API,所以很多人開始進(jìn)行實操,這里我就用python來為大家實現(xiàn)一下,如何調(diào)用API并提問返回文章的說明
實操內(nèi)容
獲取API
書寫python調(diào)用框架
封裝到pyqt中,實現(xiàn)UI化
封裝為exe
具體操作
話不多說,直接上代碼
import sys
import openai
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QSpinBox
from PyQt5.QtCore import QThread, pyqtSignal
class ChatThread(QThread):
response_ready = pyqtSignal(str)
def __init__(self, prompt, num_threads):
super().__init__()
self.prompt = prompt
self.num_threads = num_threads
def run(self):
openai.api_key = "這里輸入你的API"
response = openai.Completion.create(
engine="text-davinci-003",
prompt=self.prompt,
max_tokens=1024,
temperature=0.5,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0,
n=self.num_threads
)
self.response_ready.emit(response.choices[0].text.strip())
class ChatWindow(QWidget):
def __init__(self):
super().__init__()
# 設(shè)置窗口標(biāo)題和大小
self.setWindowTitle('Chat with GPT-3')
self.resize(500, 400)
# 創(chuàng)建一個垂直布局,并將所有控件添加到布局中
layout = QVBoxLayout()
# 創(chuàng)建一個標(biāo)簽,并添加到布局中
label = QLabel('Please enter your question:')
layout.addWidget(label)
# 創(chuàng)建一個文本框,并添加到布局中
self.text_edit = QLineEdit()
layout.addWidget(self.text_edit)
# 創(chuàng)建一個水平布局,并添加一個按鈕和一個標(biāo)簽
hbox = QHBoxLayout()
self.button = QPushButton('Ask')
self.button.clicked.connect(self.on_button_clicked)
hbox.addWidget(self.button)
# 創(chuàng)建一個SpinBox控件,用于選擇線程數(shù)量
self.thread_spinbox = QSpinBox()
self.thread_spinbox.setMinimum(1)
self.thread_spinbox.setMaximum(10)
self.thread_spinbox.setValue(1)
hbox.addWidget(self.thread_spinbox)
self.answer_label = QLabel()
hbox.addWidget(self.answer_label)
layout.addLayout(hbox)
# 設(shè)置窗口的主布局
self.setLayout(layout)
def on_button_clicked(self):
# 從文本框中獲取問題
prompt = self.text_edit.text()
# 獲取選中的線程數(shù)量
num_threads = self.thread_spinbox.value()
# 創(chuàng)建并啟動線程
thread = ChatThread(prompt, num_threads)
thread.response_ready.connect(self.on_response_ready)
thread.start()
def on_response_ready(self, response):
# 將答案顯示在標(biāo)簽中
self.answer_label.setText(response)
if __name__ == '__main__':
# 創(chuàng)建一個Qt應(yīng)用對象
app = QApplication(sys.argv)
# 創(chuàng)建一個窗口對象
window = ChatWindow()
# 顯示窗口
window.show()
# 運行Qt應(yīng)用的主循環(huán)
sys.exit(app.exec_())
'''
import sys
import openai
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton
from PyQt5.QtCore import Qt
class ChatWindow(QWidget):
def __init__(self):
super().__init__()
# 設(shè)置窗口標(biāo)題和大小
self.setWindowTitle('小杰巨無霸gpt自動生成器')
self.resize(500, 400)
# 創(chuàng)建一個垂直布局,并將所有控件添加到布局中
layout = QVBoxLayout()
# 創(chuàng)建一個標(biāo)簽,并添加到布局中
label = QLabel('請在下方輸入您的問題:')
label.setStyleSheet('font-size: 18pt; color: #006699; font-family: SimSun')
label.setAlignment(Qt.AlignCenter)
layout.addWidget(label)
# 創(chuàng)建一個文本框,并添加到布局中
self.text_edit = QLineEdit()
self.text_edit.setStyleSheet('font-size: 14pt; font-family: SimSun')
layout.addWidget(self.text_edit)
# 創(chuàng)建一個水平布局,并添加一個按鈕和一個標(biāo)簽
hbox = QHBoxLayout()
self.button = QPushButton('開始生成')
self.button.setStyleSheet('font-size: 16pt; font-family: SimSun; color: white; background-color: #006699')
self.button.clicked.connect(self.on_button_clicked)
hbox.addWidget(self.button)
self.answer_label = QLabel()
self.answer_label.setStyleSheet('font-size: 14pt; color: #006699; font-family: SimSun')
self.answer_label.setAlignment(Qt.AlignCenter)
hbox.addWidget(self.answer_label)
layout.addLayout(hbox)
hbox.setAlignment(Qt.AlignCenter)
# 設(shè)置窗口的主布局
self.setLayout(layout)
# 初始化OpenAI API
openai.api_key = "這里輸入你獲取的KEY"
def on_button_clicked(self):
# 從文本框中獲取問題
prompt = self.text_edit.text()
# 使用OpenAI API獲取答案
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=1024,
temperature=0.5,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0
)
# 將答案顯示在標(biāo)簽中
self.answer_label.setText(response.choices[0].text.strip())
if __name__ == '__main__':
# 創(chuàng)建一個Qt應(yīng)用對象
app = QApplication(sys.argv)
# 創(chuàng)建一個窗口對象
window = ChatWindow()
# 顯示窗口
window.show()
# 運行Qt應(yīng)用的主循環(huán)
sys.exit(app.exec_())
成品展示
UI界面比較簡單,有興趣的伙伴可以深化美化一番。
直接輸入問題,就可以生成答案!

到此這篇關(guān)于Python調(diào)用ChatGPT的API實現(xiàn)文章生成的文章就介紹到這了,更多相關(guān)Python ChatGPT生成文章內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實現(xiàn)將絕對URL替換成相對URL的方法
這篇文章主要介紹了Python實現(xiàn)將絕對URL替換成相對URL的方法,涉及Python字符串操作及正則匹配的相關(guān)技巧,需要的朋友可以參考下2015-06-06
pandas創(chuàng)建DataFrame的方式小結(jié)
今天給大家整理了pandas創(chuàng)建DataFrame的方式小結(jié),現(xiàn)在我們就來看看這三種生成Dataframe的方式,每種方式通過實例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-09-09
Python實現(xiàn)將藍(lán)底照片轉(zhuǎn)化為白底照片功能完整實例
這篇文章主要介紹了Python實現(xiàn)將藍(lán)底照片轉(zhuǎn)化為白底照片功能,結(jié)合完整實例形式分析了Python基于cv2庫進(jìn)行圖形轉(zhuǎn)換操作的相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2019-12-12
python獲取當(dāng)前運行函數(shù)名稱的方法實例代碼
這篇文章主要介紹了python獲取當(dāng)前運行函數(shù)名稱的方法實例代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04
使用Python腳本來獲取Cisco設(shè)備信息的示例
這篇文章主要介紹了編寫Python腳本來獲取Python腳本來獲取Cisco設(shè)備信息的教程,文中的示例是獲取一臺思科交換機的腳本,需要的朋友可以參考下2015-05-05

