最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python調用ChatGPT的API實現(xiàn)文章生成

 更新時間:2023年03月22日 11:10:24   作者:徐浪老師  
最近ChatGPT大火,在3.5版本后開放了接口API,所以很多人開始進行實操,這里我就用python來為大家實現(xiàn)一下,如何調用API并提問返回文章的說明

實操目標

最近ChatGPT大火,在3.5版本后開放了接口API,所以很多人開始進行實操,這里我就用python來為大家實現(xiàn)一下,如何調用API并提問返回文章的說明

實操內容

獲取API

書寫python調用框架

封裝到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__()

        # 設置窗口標題和大小
        self.setWindowTitle('Chat with GPT-3')
        self.resize(500, 400)

        # 創(chuàng)建一個垂直布局,并將所有控件添加到布局中
        layout = QVBoxLayout()

        # 創(chuàng)建一個標簽,并添加到布局中
        label = QLabel('Please enter your question:')
        layout.addWidget(label)

        # 創(chuàng)建一個文本框,并添加到布局中
        self.text_edit = QLineEdit()
        layout.addWidget(self.text_edit)

        # 創(chuàng)建一個水平布局,并添加一個按鈕和一個標簽
        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)

        # 設置窗口的主布局
        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):
        # 將答案顯示在標簽中
        self.answer_label.setText(response)


if __name__ == '__main__':
    # 創(chuàng)建一個Qt應用對象
    app = QApplication(sys.argv)

    # 創(chuàng)建一個窗口對象
    window = ChatWindow()

    # 顯示窗口
    window.show()

    # 運行Qt應用的主循環(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__()

        # 設置窗口標題和大小
        self.setWindowTitle('小杰巨無霸gpt自動生成器')
        self.resize(500, 400)

        # 創(chuàng)建一個垂直布局,并將所有控件添加到布局中
        layout = QVBoxLayout()

        # 創(chuàng)建一個標簽,并添加到布局中
        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)建一個水平布局,并添加一個按鈕和一個標簽
        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)

        # 設置窗口的主布局
        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
        )

        # 將答案顯示在標簽中
        self.answer_label.setText(response.choices[0].text.strip())


if __name__ == '__main__':
    # 創(chuàng)建一個Qt應用對象
    app = QApplication(sys.argv)

    # 創(chuàng)建一個窗口對象
    window = ChatWindow()

    # 顯示窗口
    window.show()

    # 運行Qt應用的主循環(huán)
    sys.exit(app.exec_())

成品展示

UI界面比較簡單,有興趣的伙伴可以深化美化一番。

直接輸入問題,就可以生成答案!

到此這篇關于Python調用ChatGPT的API實現(xiàn)文章生成的文章就介紹到這了,更多相關Python ChatGPT生成文章內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

怀仁县| 北票市| 上杭县| 长汀县| 阜阳市| 荔波县| 南汇区| 河南省| 鄂伦春自治旗| 扶绥县| 浦江县| 榆中县| 双江| 新绛县| 长白| 淄博市| 吉首市| 聊城市| 邹城市| 青铜峡市| 高州市| 剑河县| 雅江县| 广东省| 鞍山市| 当阳市| 南和县| 章丘市| 剑川县| 琼海市| 融水| 游戏| 五指山市| 囊谦县| 江阴市| 白玉县| 财经| 辽中县| 新巴尔虎右旗| 城固县| 商丘市|