欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python使用PyQt5實現(xiàn)與DeepSeek聊天的圖形化小軟件

 更新時間:2025年03月11日 10:35:55   作者:老胖閑聊  
在?PyQt5?中,菜單欄(QMenuBar)、工具欄(QToolBar)和狀態(tài)欄(QStatusBar)是?QMainWindow?提供的標準控件,用于幫助用戶更好地與應用程序交互,所以本文給大家介紹了Python使用PyQt5實現(xiàn)與DeepSeek聊天的圖形化小軟件,需要的朋友可以參考下

1. 導入依賴庫

import sys
import requests
import json
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTextEdit, QLineEdit, QPushButton, QLabel, QFileDialog
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtGui import QPixmap
  • sys: 用于處理 Python 的系統(tǒng)相關功能,例如退出程序。
  • requests: 用于發(fā)送 HTTP 請求,與 DeepSeek API 進行通信。
  • json: 用于處理 JSON 格式的數(shù)據(jù)。
  • PyQt5: 用于創(chuàng)建圖形用戶界面(GUI)。
    • QApplication: 管理應用程序的控制流和主要設置。
    • QWidget: 所有用戶界面對象的基類。
    • QVBoxLayout: 垂直布局管理器,用于排列控件。
    • QTextEdit: 多行文本輸入框,用于顯示聊天記錄。
    • QLineEdit: 單行文本輸入框,用于用戶輸入消息。
    • QPushButton: 按鈕控件,用于觸發(fā)事件。
    • QLabel: 標簽控件,用于顯示文本或圖像。
    • QFileDialog: 文件選擇對話框,用于上傳圖像。
  • QThread: 用于創(chuàng)建多線程,避免阻塞主線程。
  • pyqtSignal: 用于在線程和主線程之間傳遞信號。
  • QPixmap: 用于加載和顯示圖像。

2. DeepSeek API 配置

DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat/completions"
DEEPSEEK_API_KEY = "your_deepseek_api_key"  # 替換為自己的 DeepSeek API Key
  • DEEPSEEK_API_URL: DeepSeek API 的端點 URL。
  • DEEPSEEK_API_KEY: DeepSeek API 密鑰,用于身份驗證。

3. ChatThread 類

class ChatThread(QThread):
    response_received = pyqtSignal(str)
    stream_response_received = pyqtSignal(str)

    def __init__(self, messages, stream=False):
        super().__init__()
        self.messages = messages
        self.stream = stream

    def run(self):
        headers = {
            "Authorization": f"Bearer {DEEPSEEK_API_KEY}",
            "Content-Type": "application/json"
        }

        data = {
            "model": "deepseek-chat",
            "messages": self.messages,
            "stream": self.stream
        }

        if self.stream:
            response = requests.post(DEEPSEEK_API_URL, headers=headers, json=data, stream=True)
            for line in response.iter_lines():
                if line:
                    decoded_line = line.decode('utf-8')
                    if decoded_line.startswith("data:"):
                        json_data = json.loads(decoded_line[5:])
                        if "choices" in json_data:
                            content = json_data["choices"][0]["delta"].get("content", "")
                            self.stream_response_received.emit(content)
        else:
            response = requests.post(DEEPSEEK_API_URL, headers=headers, json=data)
            if response.status_code == 200:
                json_data = response.json()
                content = json_data["choices"][0]["message"]["content"]
                self.response_received.emit(content)
            else:
                self.response_received.emit("Error: 無法從DeepSeekAPI獲得響應.")

功能說明

  • ChatThread 是一個繼承自 QThread 的類,用于在后臺與 DeepSeek API 進行通信。
  • response_received 和 stream_response_received: 這兩個信號用于將 API 的響應傳遞回主線程。
  • __init__ 方法:
    • 接受 messages(聊天記錄)和 stream(是否啟用流式輸出)作為參數(shù)。
  • run 方法:
    • 發(fā)送 HTTP POST 請求到 DeepSeek API。
    • 如果啟用流式輸出(stream=True),則逐行讀取響應并發(fā)送信號。
    • 如果禁用流式輸出,則等待完整響應后發(fā)送信號。

4. ChatApp 類

class ChatApp(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.messages = []

    def initUI(self):
        self.setWindowTitle('DeepSeek Chat')
        self.setGeometry(100, 100, 600, 400)

        layout = QVBoxLayout()

        self.chat_display = QTextEdit()
        self.chat_display.setReadOnly(True)
        layout.addWidget(self.chat_display)

        self.input_box = QLineEdit()
        self.input_box.setPlaceholderText("在此留下你的千言萬語...")
        layout.addWidget(self.input_box)

        self.send_button = QPushButton('發(fā)送')
        self.send_button.clicked.connect(self.send_message)
        layout.addWidget(self.send_button)

        self.image_label = QLabel()
        layout.addWidget(self.image_label)

        self.upload_image_button = QPushButton('上傳圖片')
        self.upload_image_button.clicked.connect(self.upload_image)
        layout.addWidget(self.upload_image_button)

        self.setLayout(layout)

功能說明

  • ChatApp 是主應用程序類,繼承自 QWidget。
  • __init__ 方法:
    • 初始化界面并創(chuàng)建一個空的消息列表 self.messages。
  • initUI 方法:
    • 設置窗口標題和大小。
    • 使用 QVBoxLayout 垂直排列控件。
    • chat_display: 用于顯示聊天記錄的多行文本框。
    • input_box: 用于用戶輸入消息的單行文本框。
    • send_button: 發(fā)送消息的按鈕,點擊后觸發(fā) send_message 方法。
    • image_label: 用于顯示上傳的圖像。
    • upload_image_button: 上傳圖像的按鈕,點擊后觸發(fā) upload_image 方法。

5. 核心功能方法

5.1 send_message 方法

def send_message(self):
    user_input = self.input_box.text()
    if user_input:
        self.messages.append({"role": "user", "content": user_input})
        self.chat_display.append(f"You: {user_input}")
        self.input_box.clear()

        self.chat_thread = ChatThread(self.messages, stream=True)
        self.chat_thread.stream_response_received.connect(self.update_chat_display_stream)
        self.chat_thread.start()
  • 獲取用戶輸入的消息。
  • 將消息添加到 self.messages 列表中。
  • 在聊天顯示區(qū)域顯示用戶的消息。
  • 清空輸入框。
  • 啟動 ChatThread 線程與 DeepSeek API 通信,并啟用流式輸出。

5.2 update_chat_display_stream 方法

def update_chat_display_stream(self, content):
    self.chat_display.moveCursor(self.chat_display.textCursor().End)
    self.chat_display.insertPlainText(content)
    self.chat_display.moveCursor(self.chat_display.textCursor().End)
  • 將 DeepSeek API 的流式響應逐字添加到聊天顯示區(qū)域。
  • 確保光標始終在文本末尾,以便用戶可以看到最新的內容。

5.3 upload_image 方法

def upload_image(self):
    options = QFileDialog.Options()
    file_name, _ = QFileDialog.getOpenFileName(self, "上傳圖片", "", "Images (*.png *.jpg *.jpeg)", options=options)
    if file_name:
        pixmap = QPixmap(file_name)
        self.image_label.setPixmap(pixmap.scaled(200, 200))
        self.messages.append({"role": "user", "content": f"Image: {file_name}"})
  • 打開文件選擇對話框,允許用戶選擇圖像文件。
  • 加載圖像并顯示在 image_label 中。
  • 將圖像路徑添加到 self.messages 列表中。

6. 主程序入口

if __name__ == '__main__':
    app = QApplication(sys.argv)
    chat_app = ChatApp()
    chat_app.show()
    sys.exit(app.exec_())
  • 創(chuàng)建 QApplication 實例。
  • 創(chuàng)建 ChatApp 實例并顯示窗口。
  • 進入主事件循環(huán),等待用戶交互。

7、完整代碼如下:

需要安裝PyQt5和requests

pip install PyQt5 requests
import sys
import requests
import json
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTextEdit, QLineEdit, QPushButton, QLabel, QFileDialog
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtGui import QPixmap

# DeepSeek API 配置
DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat/completions"
DEEPSEEK_API_KEY = "your_deepseek_api_key"  # 替換為自己的 DeepSeek API Key,去DeepSeek注冊獲取

class ChatThread(QThread):
    response_received = pyqtSignal(str)
    stream_response_received = pyqtSignal(str)

    def __init__(self, messages, stream=False):
        super().__init__()
        self.messages = messages
        self.stream = stream

    def run(self):
        headers = {
            "Authorization": f"Bearer {DEEPSEEK_API_KEY}",
            "Content-Type": "application/json"
        }

        data = {
            "model": "deepseek-chat",
            "messages": self.messages,
            "stream": self.stream
        }

        if self.stream:
            response = requests.post(DEEPSEEK_API_URL, headers=headers, json=data, stream=True)
            for line in response.iter_lines():
                if line:
                    decoded_line = line.decode('utf-8')
                    if decoded_line.startswith("data:"):
                        json_data = json.loads(decoded_line[5:])
                        if "choices" in json_data:
                            content = json_data["choices"][0]["delta"].get("content", "")
                            self.stream_response_received.emit(content)
        else:
            response = requests.post(DEEPSEEK_API_URL, headers=headers, json=data)
            if response.status_code == 200:
                json_data = response.json()
                content = json_data["choices"][0]["message"]["content"]
                self.response_received.emit(content)
            else:
                self.response_received.emit("Error: 無法從DeepSeekAPI獲得響應.")

class ChatApp(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.messages = []

    def initUI(self):
        self.setWindowTitle('DeepSeek Chat')
        self.setGeometry(100, 100, 600, 400)

        layout = QVBoxLayout()

        self.chat_display = QTextEdit()
        self.chat_display.setReadOnly(True)
        layout.addWidget(self.chat_display)

        self.input_box = QLineEdit()
        self.input_box.setPlaceholderText("在此留下你的千言萬語...")
        layout.addWidget(self.input_box)

        self.send_button = QPushButton('發(fā)送')
        self.send_button.clicked.connect(self.send_message)
        layout.addWidget(self.send_button)

        self.image_label = QLabel()
        layout.addWidget(self.image_label)

        self.upload_image_button = QPushButton('上傳圖片')
        self.upload_image_button.clicked.connect(self.upload_image)
        layout.addWidget(self.upload_image_button)

        self.setLayout(layout)

    def send_message(self):
        user_input = self.input_box.text()
        if user_input:
            self.messages.append({"role": "user", "content": user_input})
            self.chat_display.append(f"You: {user_input}")
            self.input_box.clear()

            self.chat_thread = ChatThread(self.messages, stream=True)
            self.chat_thread.stream_response_received.connect(self.update_chat_display_stream)
            self.chat_thread.start()

    def update_chat_display_stream(self, content):
        self.chat_display.moveCursor(self.chat_display.textCursor().End)
        self.chat_display.insertPlainText(content)
        self.chat_display.moveCursor(self.chat_display.textCursor().End)

    def upload_image(self):
        options = QFileDialog.Options()
        file_name, _ = QFileDialog.getOpenFileName(self, "上傳圖片", "", "Images (*.png *.jpg *.jpeg)", options=options)
        if file_name:
            pixmap = QPixmap(file_name)
            self.image_label.setPixmap(pixmap.scaled(200, 200))
            self.messages.append({"role": "user", "content": f"Image: {file_name}"})

if __name__ == '__main__':
    app = QApplication(sys.argv)
    chat_app = ChatApp()
    chat_app.show()
    sys.exit(app.exec_())

8. 總結

  • 使用 PyQt5 創(chuàng)建了一個簡單的圖形化聊天界面。
  • 通過 ChatThread 實現(xiàn)了與 DeepSeek API 的異步通信,支持流式和非流式輸出。
  • 支持多輪對話和多模態(tài)輸入(如圖像上傳)。
  • 代碼結構清晰,易于擴展和優(yōu)化。

以上就是Python使用PyQt5實現(xiàn)與DeepSeek聊天的圖形化小軟件的詳細內容,更多關于Python PyQt5與DeepSeek聊天軟件的資料請關注腳本之家其它相關文章!

相關文章

  • 詳解Python3注釋知識點

    詳解Python3注釋知識點

    在本篇文章里小編給大家分享了關于Python3注釋的相關知識點以及用法,需要的朋友們學習下。
    2019-02-02
  • 用python實現(xiàn)PDF解密打印文件

    用python實現(xiàn)PDF解密打印文件

    大家好,本篇文章主要講的是用python實現(xiàn)PDF解密打印文件,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02
  • 詳解Python設計模式之策略模式

    詳解Python設計模式之策略模式

    這篇文章主要介紹了Python設計模式之策略模式的相關知識,文中講解非常詳細,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-06-06
  • python中os.path.exits()的坑

    python中os.path.exits()的坑

    本文主要介紹了python中os.path.exits()的坑,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-04-04
  • Python繪制多因子柱狀圖的實現(xiàn)示例

    Python繪制多因子柱狀圖的實現(xiàn)示例

    本文主要介紹了Python繪制多因子柱狀圖的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-05-05
  • Python 實現(xiàn)把列表中的偶數(shù)變成他的平方

    Python 實現(xiàn)把列表中的偶數(shù)變成他的平方

    這篇文章主要介紹了Python 實現(xiàn)把列表中的偶數(shù)變成他的平方,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Python接口自動化測試的實現(xiàn)

    Python接口自動化測試的實現(xiàn)

    這篇文章主要介紹了Python接口自動化測試的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08
  • Python兩臺電腦實現(xiàn)TCP通信的方法示例

    Python兩臺電腦實現(xiàn)TCP通信的方法示例

    這篇文章主要介紹了Python兩臺電腦實現(xiàn)TCP通信的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-05-05
  • 詳解python中*號的用法

    詳解python中*號的用法

    這篇文章主要介紹了python中*號的用法,文中通過代碼給大家介紹了雙星號(**)的用法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-10-10
  • 老生常談Python startswith()函數(shù)與endswith函數(shù)

    老生常談Python startswith()函數(shù)與endswith函數(shù)

    下面小編就為大家?guī)硪黄仙U凱ython startswith()函數(shù)與endswith函數(shù)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09

最新評論