环境要求
通过调用openai接口实现聊天机器人功能,支持各种参数得调整,文章末尾有各个参数得说明
- python3.9
- pyqt5
- 环境安装
pip install pyqt5
pip install openai
pip install pyinstaller
源码如下:
chat.py
import sys
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui, QtWidgets
import openai
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.lineEdit_prompt = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_prompt.setObjectName("lineEdit_prompt")
self.horizontalLayout.addWidget(self.lineEdit_prompt)
self.pushButtonSend = QtWidgets.QPushButton(self.centralwidget)
self.pushButtonSend.setObjectName("pushButtonSend")
self.horizontalLayout.addWidget(self.pushButtonSend)
self.verticalLayout_4.addLayout(self.horizontalLayout)
self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_2.setObjectName("groupBox_2")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.groupBox_2)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.horizontalLayout_12 = QtWidgets.QHBoxLayout()
self.horizontalLayout_12.setObjectName("horizontalLayout_12")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout_11 = QtWidgets.QHBoxLayout()
self.horizontalLayout_11.setObjectName("horizontalLayout_11")
self.label_10 = QtWidgets.QLabel(self.groupBox_2)
self.label_10.setObjectName("label_10")
self.horizontalLayout_11.addWidget(self.label_10)
self.lineEdit_api_key = QtWidgets.QLineEdit(self.groupBox_2)
self.lineEdit_api_key.setObjectName("lineEdit_api_key")
self.horizontalLayout_11.addWidget(self.lineEdit_api_key)
self.verticalLayout.addLayout(self.horizontalLayout_11)
self.horizontalLayout_7 = QtWidgets.QHBoxLayout()
self.horizontalLayout_7.setObjectName("horizontalLayout_7")
self.label_6 = QtWidgets.QLabel(self.groupBox_2)
self.label_6.setObjectName("label_6")
self.horizontalLayout_7.addWidget(self.label_6)
self.spinBox_max_tokens = QtWidgets.QSpinBox(self.groupBox_2)
self.spinBox_max_tokens.setMinimum(10)
self.spinBox_max_tokens.setMaximum(1024)
self.spinBox_max_tokens.setProperty("value", 100)
self.spinBox_max_tokens.setObjectName("spinBox_max_tokens")
self.horizontalLayout_7.addWidget(self.spinBox_max_tokens)
self.verticalLayout.addLayout(self.horizontalLayout_7)
self.horizontalLayout_6 = QtWidgets.QHBoxLayout()
self.horizontalLayout_6.setObjectName("horizontalLayout_6")
self.label_5 = QtWidgets.QLabel(self.groupBox_2)
self.label_5.setObjectName("label_5")
self.horizontalLayout_6.addWidget(self.label_5)
self.doubleSpinBox_temperature = QtWidgets.QDoubleSpinBox(self.groupBox_2)
self.doubleSpinBox_temperature.setDecimals(0)
self.doubleSpinBox_temperature.setMinimum(0.0)
self.doubleSpinBox_temperature.setMaximum(1.0)
self.doubleSpinBox_temperature.setSingleStep(0.1)
self.doubleSpinBox_temperature.setProperty("value", 0.0)
self.doubleSpinBox_temperature.setObjectName("doubleSpinBox_temperature")
self.horizontalLayout_6.addWidget(self.doubleSpinBox_temperature)
self.verticalLayout.addLayout(self.horizontalLayout_6)
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.label_3 = QtWidgets.QLabel(self.groupBox_2)
self.label_3.setObjectName("label_3")
self.horizontalLayout_4.addWidget(self.label_3)
self.doubleSpinBox_presence_penalty = QtWidgets.QDoubleSpinBox(self.groupBox_2)
self.doubleSpinBox_presence_penalty.setDecimals(1)
self.doubleSpinBox_presence_penalty.setMinimum(-2.0)
self.doubleSpinBox_presence_penalty.setMaximum(2.0)
self.doubleSpinBox_presence_penalty.setSingleStep(0.1)
self.doubleSpinBox_presence_penalty.setProperty("value", 1.0)
self.doubleSpinBox_presence_penalty.setObjectName("doubleSpinBox_presence_penalty")
self.horizontalLayout_4.addWidget(self.doubleSpinBox_presence_penalty)
self.verticalLayout.addLayout(self.horizontalLayout_4)
self.horizontalLayout_12.addLayout(self.verticalLayout)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_12.addItem(spacerItem)
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label_2 = QtWidgets.QLabel(self.groupBox_2)
self.label_2.setObjectName("label_2")
self.horizontalLayout_3.addWidget(self.label_2)
self.comboBox_model = QtWidgets.QComboBox(self.groupBox_2)
self.comboBox_model.setObjectName("comboBox_model")
self.comboBox_model.addItem("")
self.comboBox_model.addItem("")
self.horizontalLayout_3.addWidget(self.comboBox_model)
self.verticalLayout_2.addLayout(self.horizontalLayout_3)
self.horizontalLayout_10 = QtWidgets.QHBoxLayout()
self.horizontalLayout_10.setObjectName("horizontalLayout_10")
self.label_9 = QtWidgets.QLabel(self.groupBox_2)
self.label_9.setObjectName("label_9")
self.horizontalLayout_10.addWidget(self.label_9)
self.lineEdit_stop = QtWidgets.QLineEdit(self.groupBox_2)
self.lineEdit_stop.setObjectName("lineEdit_stop")
self.horizontalLayout_10.addWidget(self.lineEdit_stop)
self.verticalLayout_2.addLayout(self.horizontalLayout_10)
self.horizontalLayout_8 = QtWidgets.QHBoxLayout()
self.horizontalLayout_8.setObjectName("horizontalLayout_8")
self.label_7 = QtWidgets.QLabel(self.groupBox_2)
self.label_7.setObjectName("label_7")
self.horizontalLayout_8.addWidget(self.label_7)
self.doubleSpinBox_top_p = QtWidgets.QDoubleSpinBox(self.groupBox_2)
self.doubleSpinBox_top_p.setDecimals(1)
self.doubleSpinBox_top_p.setMaximum(1.0)
self.doubleSpinBox_top_p.setSingleStep(0.1)
self.doubleSpinBox_top_p.setProperty("value", 0.1)
self.doubleSpinBox_top_p.setObjectName("doubleSpinBox_top_p")
self.horizontalLayout_8.addWidget(self.doubleSpinBox_top_p)
self.verticalLayout_2.addLayout(self.horizontalLayout_8)
self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.label_4 = QtWidgets.QLabel(self.groupBox_2)
self.label_4.setObjectName("label_4")
self.horizontalLayout_5.addWidget(self.label_4)
self.doubleSpinBox_frequency_penalty = QtWidgets.QDoubleSpinBox(self.groupBox_2)
self.doubleSpinBox_frequency_penalty.setDecimals(1)
self.doubleSpinBox_frequency_penalty.setMinimum(-2.0)
self.doubleSpinBox_frequency_penalty.setMaximum(2.0)
self.doubleSpinBox_frequency_penalty.setSingleStep(0.1)
self.doubleSpinBox_frequency_penalty.setProperty("value", 1.0)
self.doubleSpinBox_frequency_penalty.setObjectName("doubleSpinBox_frequency_penalty")
self.horizontalLayout_5.addWidget(self.doubleSpinBox_frequency_penalty)
self.verticalLayout_2.addLayout(self.horizontalLayout_5)
self.horizontalLayout_12.addLayout(self.verticalLayout_2)
spacerItem1 = QtWidgets.QSpacerItem(100, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_12.addItem(spacerItem1)
self.verticalLayout_3.addLayout(self.horizontalLayout_12)
self.verticalLayout_4.addWidget(self.groupBox_2)
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setObjectName("groupBox")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.groupBox)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.textBrowser_result = QtWidgets.QTextBrowser(self.groupBox)
self.textBrowser_result.setObjectName("textBrowser_result")
self.horizontalLayout_2.addWidget(self.textBrowser_result)
self.verticalLayout_4.addWidget(self.groupBox)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "请输入问题:"))
self.pushButtonSend.setText(_translate("MainWindow", "发送"))
self.groupBox_2.setTitle(_translate("MainWindow", "参数设置"))
self.label_10.setText(_translate("MainWindow", "apikey"))
self.label_6.setText(_translate("MainWindow", "max_tokens"))
self.label_5.setText(_translate("MainWindow", "temperature"))
self.label_3.setText(_translate("MainWindow", "presence_penalty"))
self.label_2.setText(_translate("MainWindow", "model"))
self.comboBox_model.setItemText(0, _translate("MainWindow", "code-davinci-002"))
self.comboBox_model.setItemText(1, _translate("MainWindow", "text-davinci-003"))
self.label_9.setText(_translate("MainWindow", "stop"))
self.label_7.setText(_translate("MainWindow", "top_p"))
self.label_4.setText(_translate("MainWindow", "frequency_penalty"))
self.groupBox.setTitle(_translate("MainWindow", "结果"))
class MainWindow(QMainWindow, Ui_MainWindow, ):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
self.pushButtonSend.clicked.connect(self.handle_send)
self.lineEdit_prompt.setText("openai接口stop参数啥意思")
self.setWindowTitle("PyqtForChatgpt")
def handle_send(self):
api_key = self.lineEdit_api_key.text()
if not api_key:
QMessageBox.warning(self, 'error', '请输入有效的api key')
return
openai.api_key = api_key
prompt = self.lineEdit_prompt.text()
model = self.comboBox_model.currentText()
max_tokens = self.spinBox_max_tokens.value()
stop = self.spinBox_max_tokens.text()
temperature = self.doubleSpinBox_temperature.value()
top_p = self.doubleSpinBox_top_p.value()
presence_penalty = self.doubleSpinBox_presence_penalty.value()
frequency_penalty = self.doubleSpinBox_frequency_penalty.value()
text = openai.Completion.create(
prompt=prompt,
model=model,
max_tokens=max_tokens,
# stop=[stop],
temperature=temperature,
top_p=top_p,
presence_penalty=presence_penalty,
frequency_penalty=frequency_penalty,
n=1,
).choices[0].text
self.textBrowser_result.setPlainText(text)
# self.textBrowserResult.setMarkdown(f"""""")
if __name__ == "__main__":
app = QApplication(sys.argv)
myapp = MainWindow()
myapp.show()
sys.exit(app.exec_())
执行
python chat.py
效果如下
打包成exe
pyinstaller -y -F -w -n chat chat.py
openai参数使用说明
参数名 | 类型 | 默认值 | 说明 |
---|---|---|---|
model | string | 模型名称 | |
prompt | string | 输入的提示 | |
max_tokens | int | 16 | 文本生成时要生成的最大token数.提示的token数加上max_tokens不能超过模型的上下文长度。大多数模型的上下文长度为2048个token(最新模型支持4096 tokens) |
stop | string | null | 最多4个序列,遇到stopAPI将停止生成。返回的文本不包含停止序列。 |
temperature | float | 1 | 采样温度。值越高意味着模型承担的风险越大。对于需要创意的场景,可以尝试0.9,对于答案明确的场景,建议用0(argmax采样),建议不要与top_p同时改变。 |
top_p | float | 1 | 核采样(温度采样的另一种方式),其中模型考虑具有top_p概率质量的token的结果。因此,0.1意味着只考虑包含最高10%概率质量的token,建议不要与temperature同时改变。 |
presence_penalty | float | 0 | 数值介于-2.0和2.0之间。正值将根据到目前为止新token是否出现在文本中来惩罚新token,从而增加模型谈论新主题的可能性。 |
frequency_penalty | float | 0 | 数值介于-2.0和2.0之间。正值根据文本中新token已经出现的频率惩罚新token,从而降低模型逐字重复同一行的可能性。 |
使用说明
- 输入问题
- 参数根据情况选填
- 必须填写
api_key