pyqt实现聊天机器人

news2024/9/30 13:31:23

环境要求

通过调用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参数使用说明

参数名类型默认值说明
modelstring模型名称
promptstring输入的提示
max_tokensint16文本生成时要生成的最大token数.提示的token数加上max_tokens不能超过模型的上下文长度。大多数模型的上下文长度为2048个token(最新模型支持4096 tokens)
stopstringnull最多4个序列,遇到stopAPI将停止生成。返回的文本不包含停止序列。
temperaturefloat1采样温度。值越高意味着模型承担的风险越大。对于需要创意的场景,可以尝试0.9,对于答案明确的场景,建议用0(argmax采样),建议不要与top_p同时改变。
top_pfloat1核采样(温度采样的另一种方式),其中模型考虑具有top_p概率质量的token的结果。因此,0.1意味着只考虑包含最高10%概率质量的token,建议不要与temperature同时改变。
presence_penaltyfloat0数值介于-2.0和2.0之间。正值将根据到目前为止新token是否出现在文本中来惩罚新token,从而增加模型谈论新主题的可能性。
frequency_penaltyfloat0数值介于-2.0和2.0之间。正值根据文本中新token已经出现的频率惩罚新token,从而降低模型逐字重复同一行的可能性。

使用说明

  • 输入问题
  • 参数根据情况选填
  • 必须填写api_key

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/380985.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Cortex-A7中断控制器GIC

Cortex-A7中断控制器GIC 中断号 芯片内部的中断都会引起IRQ InterruptGIC将所有的中断源(最多1020个中断ID)分为三类: SPI(SharedPeripheralInterrupt)共享中断,外部中断都属于SPI中断 [ID32-1019]PPI(PrivatePeripheralInterrupt)私有中断 [ID16-31]SGI(Software-…

【MySQL】基础操作:登录、访问、退出和卸载

一、MySQL简介 MySQL数据库最初是由瑞典MySQL AB公司开发,2008年1月16号被Sun公司收购。2009年,SUN又被Oracle收购。MySQL是目前IT行业最流行的开放源代码的数据库管理系统,同时它也是一个支持多线程、高并发、多用户的关系型数据库管理系统。…

亚马逊物流仓库配送延误,美国暴风雪造成损失严重!

美国拉响暴风雪警报,物流配送大面积延误 去年年底,美国的暴风雪给卖家带来的阴影还没有散去。在这本该春暖百花开的季节,暴风雪天气又回到了美国,这次的破坏力比去年年底的有过之而无不及。 当地2月22日,暴风雪在美国…

环境搭建01-Ubuntu16.04如何查看显卡信息及安装NVDIA显卡驱动

1. 查看显卡型号、驱动 ubuntu-drivers devices2. 安装NVIDIA显卡驱动 (1)验证是否禁用nouveau lsmod | grep nouveau若有输出,没有禁用,进行以下操作禁用。 sudo gedit /etc/modprobe.d/blacklist.conf在文件末尾中添加两条&…

从一个实例配置引入Prometheus的PromQL语法

1. PromQL介绍 PromQL提供对时间序列数据进行逻辑运算、过滤、聚合的支持。应用于数据查询、可视化、告警处理 2. 基本用法 2.1 查询时间序列 点击Prometheus图标,进行查询页面。可以点击地图图标查看有哪些metrics name。输入要查询的metrics name和过滤条件,然后点击执行…

数字逻辑基础:原码、反码、补码

时间紧、不理解可以只看这里的结论 正数的原码、反码、补码相同。等于真值对应的机器码。 负数的原码等于机器码,反码为原码的符号位不变,其余各位按位取反。补码为反码1。 三种码的出现是为了解决计算问题并简化电路结构。 在原码和反码中,存…

C语言 入门教程||C语言 指针||C语言 字符串

C语言 指针 学习 C 语言的指针既简单又有趣。通过指针,可以简化一些 C 编程任务的执行,还有一些任务,如动态内存分配,没有指针是无法执行的。所以,想要成为一名优秀的 C 程序员,学习指针是很有必要的。 …

湖北某灌区量测水监测系统-案例分享

应用背景湖北某灌区是当地主要的粮棉油产区,由于运行多年,渠系工程老化,阻水现象严重,造成了大量水资源浪费。该灌区2019年对渠道进行疏挖、清淤、衬砌,解决输水不畅等问题,2022年启动灌区续建配套与节水改…

Netty核心功能以及线程模型

目录 Netty核心功能以及线程模型 Netty初探 Netty的使用场景: Netty通讯示例 Netty线程模型 Netty模块组件 Netty核心功能以及线程模型 Netty初探 NIO 的类库和 API 繁杂, 使用麻烦: 需要熟练掌握Selector、 ServerSocketChannel、 So…

wepack4配置入门

一、 webpack 简介 1.1 webpack 是什么 webpack是一种前端资源构建工具,一个静态模块打包器(module bundler)。 在 webpack 看来, 前端的所有资源文件(js/json/css/img/less/...)都会作为模块处理。 它将根据模块的依赖关系进行静态分析,打包生成对应的…

机器学习100天(三十二):032 KD树的构造和搜索

机器学习100天,今天讲的是:KD树的构造和搜索! 《机器学习100天》完整目录:目录 在 K 近邻算法中,我们计算测试样本与所有训练样本的距离,类似于穷举法。如果数据量少的时候,算法运行时间没有大的影响,但是如果数据量很大,那么算法运行的时间就会很长。这在实际的应用…

GFD563A101 3BHE046836R0101

GFD563A101 3BHE046836R0101关于高端涂布机张力控制系统方案的介绍高端涂布机张力控制系统方案涂布机是将具有某种功效的胶,或者油墨类物质均匀粘连在塑料薄膜、铝箔、纺织品等表面的机械设备。本系统从放卷到收卷共采用七台变频器,其中收放卷采用闭环张…

Databend 开源周报第 81 期

Databend 是一款现代云数仓。专为弹性和高效设计,为您的大规模分析需求保驾护航。自由且开源。即刻体验云服务:https://app.databend.com 。Whats New探索 Databend 本周新进展,遇到更贴近你心意的 Databend 。Accepted RFCsrfc: 查询结果缓存…

Cartesi 举办的2023 黑客马拉松

Cartesi 是具有 Linux 运行时的特定于应用程序的Rollups执行层。Cartesi 的特定应用程序 Optimistic Rollup 框架使区块链堆栈足够强大,开发人员可以构建计算密集型和以前不可能的去中心化实例。Cartesi 的 RISC-V 虚拟机支持 Linux 运行时环境,允许像你…

DevOps 学习笔记(二)| 使用 Harbor

文章目录1. 上传镜像到 Harbor2. 拉取 Harbor 镜像3. 使用 Jenkins 操作 Harbor1. 上传镜像到 Harbor 首先在CI/CD 服务器中配置 Docker mkdir -p /etc/docker/ cd /etc/docker/ vim daemon.json其中的 IP 地址为Harbor 服务器的 IP 地址 {"xxx": "xxxx"…

IGH主站通信测试csp模式(DC同步 preemrt)连通一从站并实现控制

IGH主站通信测试 linuxcnc配置基础机器人控制LinuxCNC与EtherCAT介绍&&PDO&SDO,搭建环境步骤 需要配置IGH主站的查看这篇文章 linux系统学习笔记7——一次性安装igh-ethercat主站 CSP模式 DC同步方式 preemrt实时补丁 直接上代码,这…

YOLOV7模型调试记录

先前的YOLOv7模型是pytorch重构的,并非官方提供的源码,而在博主使用自己的数据集进行实验时发现效果并不理想,因此生怕是由于源码重构导致该问题,此外还需进行对比实验,因此便从官网上下载了源码,进行调试运…

二叉树——二叉搜索树中的插入操作

二叉搜索树中的插入操作 链接 给定二叉搜索树(BST)的根节点 root 和要插入树中的值 value ,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据 保证 ,新值和原始二叉搜索树中的任意节点值都不同。 注意&#xff0c…

配置二层远程端口镜像案例

实验拓扑: 实验需求: 如图1所示,某公司行政部通过SwitchA与外部Internet通信,监控设备Server通过SwitchB与SwitchA相连。 现在希望Server能够远程对行政部访问Internet的流量进行监控。 操作步骤: 配置观察端口 # 在…

C/C++开发,无可避免的多线程(篇一).跨平台并行编程姗姗来迟

一、编译环境准备 在正式进入c/c多线程编程系列之前,先来搭建支持多线程编译的编译环境。 1.1 MinGW(win) 进入Downloads - MinGW-w64下载页面,选择MinGW-w64-builds跳转下载, 再次进行跳转: 然后进入下载页…