Skip to content

Commit

Permalink
AI对话新增多轮对话能力
Browse files Browse the repository at this point in the history
  • Loading branch information
LC044 committed Apr 12, 2024
1 parent 4fed8ce commit c7cf15a
Show file tree
Hide file tree
Showing 20 changed files with 4,790 additions and 2,042 deletions.
6 changes: 2 additions & 4 deletions app/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

version = '2.0.0'
version = '2.0.5'
contact = '701805520'
github = 'https://github.com/LC044/WeChatMsg'
website = 'https://memotrace.cn/'
Expand Down Expand Up @@ -32,6 +32,4 @@
os.makedirs(OUTPUT_DIR, exist_ok=True)
# 全局参数
SEND_LOG_FLAG = True # 是否发送错误日志
SERVER_API_URL = 'http://api.lc044.love' # api接口
# SERVER_API_URL = 'https://api.memotrace.cn/'
# SERVER_API_URL = 'http://127.0.0.1:8000' # api接口
SERVER_API_URL = 'http://api.lc044.love' # api接口
Binary file modified app/resources/icons/weixin.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6,768 changes: 4,741 additions & 2,027 deletions app/resources/resource_rc.py

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions app/ui/Icon.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Icon:
Arrow_left_Icon = QIcon(':/icons/icons/arrow-left.svg')
Arrow_right_Icon = QIcon(':/icons/icons/arrow-right.svg')
Update_Icon = QIcon(':/icons/icons/update.svg')
Clear_Icon = QIcon(':/icons/icons/clear.svg')
# Man_Icon_pixmap = QPixmap(Man_Icon_path)
# Woman_Icon_pixmap = QPixmap(Woman_Icon_path)
# Logo_Icon = QIcon(':/icons/icons/logo.png')
40 changes: 38 additions & 2 deletions app/ui/chat/ai_chat.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import sys
import time
import traceback
Expand All @@ -8,6 +9,7 @@
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QHBoxLayout, QApplication, QTextBrowser, QMessageBox

from app import config
from app.config import SERVER_API_URL
from app.log import logger
from app.ui.Icon import Icon
Expand Down Expand Up @@ -63,6 +65,8 @@ def __init__(self, contact, parent=None):
self.show_chats()
self.pushButton.clicked.connect(self.send_msg)
self.toolButton.clicked.connect(self.tool)
self.btn_clear.clicked.connect(self.clear_dialog)
self.btn_clear.setIcon(Icon.Clear_Icon)

def init_ui(self):
self.textEdit.installEventFilter(self)
Expand Down Expand Up @@ -94,6 +98,16 @@ def send_msg(self):
self.show_chat_thread.start()
self.scrollArea.verticalScrollBar().setValue(self.scrollArea.verticalScrollBar().maximum())

def clear_dialog(self):
self.show_chat_thread.history = []
while self.verticalLayout_message.count():
item = self.verticalLayout_message.takeAt(0)
widget = item.widget()
if widget is not None:
widget.deleteLater()
else:
del item

def show_chats(self):
# return
self.show_chat_thread = AIChatThread()
Expand Down Expand Up @@ -134,13 +148,16 @@ class AIChatThread(QThread):
def __init__(self):
super().__init__()
self.msg = ''
self.history = []

def run(self) -> None:
url = urljoin(SERVER_API_URL, 'chat')
data = {
'username': Me().wxid,
'token': Me().token,
'version': config.version,
'messages': [
*self.history,
{
'role': 'user',
"content": self.msg
Expand All @@ -156,17 +173,36 @@ def run(self) -> None:
# time.sleep(0.05)
# return
resp = requests.post(url, json=data, stream=True)
message = {
'role': 'user',
'content': self.msg
}
resp_message = {
'role': 'assistant',
'content': ''
}
if resp.status_code == 200:
for line in resp.iter_lines():
if line:
trunk = line.decode('utf-8')
print(trunk)
self.msgSignal.emit(trunk)
try:
data = json.loads(trunk.strip('data: '))
answer = data.get('answer')
print(answer)
if isinstance(answer, str):
resp_message['content'] += answer
self.msgSignal.emit(answer)
except:
print(trunk)
resp_message['content'] += trunk
self.msgSignal.emit(trunk)
else:
print(resp.text)
error = resp.json().get('error')
logger.error(f'ai请求错误:{error}')
self.msgSignal.emit(error)
self.history.append(message)
self.history.append(resp_message)
except Exception as e:
error = str(e)
logger.error(f'ai请求错误:{error}{traceback.format_exc()}')
Expand Down
6 changes: 5 additions & 1 deletion app/ui/chat/chatInfoUi.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def setupUi(self, Form):
self.horizontalLayout_2.addWidget(self.label_reamrk)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_2.addItem(spacerItem)
self.btn_clear = QtWidgets.QPushButton(self.frame)
self.btn_clear.setObjectName("btn_clear")
self.horizontalLayout_2.addWidget(self.btn_clear)
self.toolButton = QtWidgets.QToolButton(self.frame)
self.toolButton.setObjectName("toolButton")
self.horizontalLayout_2.addWidget(self.toolButton)
Expand All @@ -41,7 +44,7 @@ def setupUi(self, Form):
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 795, 555))
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 799, 564))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents)
self.verticalLayout_3.setObjectName("verticalLayout_3")
Expand All @@ -66,5 +69,6 @@ def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label_reamrk.setText(_translate("Form", "EmoAi"))
self.btn_clear.setText(_translate("Form", "清除对话"))
self.toolButton.setText(_translate("Form", "..."))
self.pushButton.setText(_translate("Form", "发送"))
3 changes: 1 addition & 2 deletions app/ui/mainview.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,11 @@ def init_ui(self):
self.startBusy()
self.action_update.triggered.connect(self.update)
self.action_help_faq.triggered.connect(
lambda: QDesktopServices.openUrl(QUrl("https://blog.lc044.love/post/7")))
lambda: QDesktopServices.openUrl(QUrl("https://memotrace.cn/doc/posts/error/")))
self.about_view = AboutDialog(main_window=self, parent=self)
self.update_thread = UpdateThread(check_time=True)
self.update_thread.updateSignal.connect(self.show_update)
self.update_thread.start()
# self.statusbar.set

def setCurrentIndex(self, row):
self.stackedWidget.setCurrentIndex(row)
Expand Down
Binary file modified app/ui/menu/about_dialog.cp310-win_amd64.pyd
Binary file not shown.
Binary file modified app/ui/menu/about_dialog.cp311-win_amd64.pyd
Binary file not shown.
Binary file modified app/ui/menu/about_dialog.cp312-win_amd64.pyd
Binary file not shown.
Binary file removed doc/images/chat_window1.png
Binary file not shown.
Binary file removed doc/images/chat_window2.png
Binary file not shown.
Binary file removed doc/images/contact_window.png
Binary file not shown.
Binary file removed doc/images/decrypt_wx.png
Binary file not shown.
Binary file removed doc/images/exe_file.png
Binary file not shown.
Binary file removed doc/images/pc_contact.png
Binary file not shown.
Binary file removed doc/images/pc_decrypt_info.png
Binary file not shown.
Binary file modified doc/images/weixin.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed doc/images/with_wxid_name.png
Binary file not shown.
8 changes: 2 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@

## PC端使用过程中部分问题解决(可参考)

<details>

#### 🤔如果您在pc端使用的时候出现问题,可以先参考以下方面,如果仍未解决,可以在群里交流~

* 不支持Win7
Expand All @@ -131,9 +129,7 @@
* 换电脑
如果您在运行可执行程序的时候出现闪退的现象,请右击软件使用管理员权限运行。

[查看详细教程](https://blog.lc044.love/post/5)

</details>
[查看详细教程](https://memotrace.cn/doc/)

# 🏆致谢

Expand Down Expand Up @@ -216,7 +212,7 @@

后续更新将会在公众号同步发布
<div>
<img src="https://blog.lc044.love/static/img/3fd32f1732a2c8f53a7eb923472b8f19.clipboard-2023-12-18.webp">
<img src="https://blog.lc044.love/static/img/b8df8c594a4cabaa0a62025767a3cfd9.weixin.webp">
<img src="./doc/images/qq2.jpg" height="200">
</div>

Expand Down

0 comments on commit c7cf15a

Please sign in to comment.