您好,登錄后才能下訂單哦!
這篇“Python怎么調(diào)用ChatGPT的API實(shí)現(xiàn)文章生成”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“Python怎么調(diào)用ChatGPT的API實(shí)現(xiàn)文章生成”文章吧。
獲取API
書(shū)寫(xiě)python調(diào)用框架
封裝到pyqt中,實(shí)現(xiàn)UI化
封裝為exe
話不多說(shuō),直接上代碼
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)建一個(gè)垂直布局,并將所有控件添加到布局中 layout = QVBoxLayout() # 創(chuàng)建一個(gè)標(biāo)簽,并添加到布局中 label = QLabel('Please enter your question:') layout.addWidget(label) # 創(chuàng)建一個(gè)文本框,并添加到布局中 self.text_edit = QLineEdit() layout.addWidget(self.text_edit) # 創(chuàng)建一個(gè)水平布局,并添加一個(gè)按鈕和一個(gè)標(biāo)簽 hbox = QHBoxLayout() self.button = QPushButton('Ask') self.button.clicked.connect(self.on_button_clicked) hbox.addWidget(self.button) # 創(chuàng)建一個(gè)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): # 從文本框中獲取問(wèn)題 prompt = self.text_edit.text() # 獲取選中的線程數(shù)量 num_threads = self.thread_spinbox.value() # 創(chuàng)建并啟動(dò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)建一個(gè)Qt應(yīng)用對(duì)象 app = QApplication(sys.argv) # 創(chuàng)建一個(gè)窗口對(duì)象 window = ChatWindow() # 顯示窗口 window.show() # 運(yùn)行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('小杰巨無(wú)霸gpt自動(dòng)生成器') self.resize(500, 400) # 創(chuàng)建一個(gè)垂直布局,并將所有控件添加到布局中 layout = QVBoxLayout() # 創(chuàng)建一個(gè)標(biāo)簽,并添加到布局中 label = QLabel('請(qǐng)?jiān)谙路捷斎肽膯?wèn)題:') label.setStyleSheet('font-size: 18pt; color: #006699; font-family: SimSun') label.setAlignment(Qt.AlignCenter) layout.addWidget(label) # 創(chuàng)建一個(gè)文本框,并添加到布局中 self.text_edit = QLineEdit() self.text_edit.setStyleSheet('font-size: 14pt; font-family: SimSun') layout.addWidget(self.text_edit) # 創(chuàng)建一個(gè)水平布局,并添加一個(gè)按鈕和一個(gè)標(biāo)簽 hbox = QHBoxLayout() self.button = QPushButton('開(kāi)始生成') 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): # 從文本框中獲取問(wèn)題 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)建一個(gè)Qt應(yīng)用對(duì)象 app = QApplication(sys.argv) # 創(chuàng)建一個(gè)窗口對(duì)象 window = ChatWindow() # 顯示窗口 window.show() # 運(yùn)行Qt應(yīng)用的主循環(huán) sys.exit(app.exec_())
以上就是關(guān)于“Python怎么調(diào)用ChatGPT的API實(shí)現(xiàn)文章生成”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。