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
。
- 初始化界面并創(chuàng)建一個空的消息列表
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聊天軟件的資料請關注腳本之家其它相關文章!
相關文章
Python 實現(xiàn)把列表中的偶數(shù)變成他的平方
這篇文章主要介紹了Python 實現(xiàn)把列表中的偶數(shù)變成他的平方,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03老生常談Python startswith()函數(shù)與endswith函數(shù)
下面小編就為大家?guī)硪黄仙U凱ython startswith()函數(shù)與endswith函數(shù)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09