-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatWindow.cpp
More file actions
156 lines (126 loc) · 4.73 KB
/
ChatWindow.cpp
File metadata and controls
156 lines (126 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "stdafx.h"
#include "ChatWindow.h"
#include "LoadingTipWidget.h"
#include <QFileDialog.h>
ChatWindow::ChatWindow(const QString& modelFilePath, QWidget* parent)
: QWidget(parent) {
// 顶部模式条
m_modeLbl = new QLabel("模式:", this);
m_modeBox = new QComboBox(this);
m_modeBox->addItem("稳妥:贪心 + 短答", QVariant(0)); // LLMRunner::Preset::SafeGreedy
m_modeBox->addItem("平衡:top-p 0.7 / temp 0.2", QVariant(1)); // LLMRunner::Preset::Balanced
m_modeBox->setCurrentIndex(1); // 默认“平衡”
// 聊天区与输入区
m_chatView = new QTextEdit(this);
m_chatView->setReadOnly(true);
m_input = new QLineEdit(this);
m_sendBtn = new QPushButton("发送", this);
QVBoxLayout* mainLayout = new QVBoxLayout(this);
m_rag = new SimpleRAG(this);
m_loadDocBtn = new QPushButton(tr("加载文档"), this);
connect(m_loadDocBtn, &QPushButton::clicked, this, [this]() {
QString docPath = QFileDialog::getOpenFileName(this, tr("选择文档"), QString(), tr("文本文件 (*.txt);;All Files (*)"));
if (docPath.isEmpty()) return;
if (m_rag->loadDocument(docPath))
m_chatView->append("<span style='color:gray;'>[RAG] 文档加载成功</span>");
else
m_chatView->append("<span style='color:red;'>[RAG] 文档加载失败</span>");
});
// 顶头位置布局
QHBoxLayout* topBar = new QHBoxLayout();
topBar->addWidget(m_modeLbl);
topBar->addWidget(m_modeBox);
topBar->addWidget(m_loadDocBtn);
topBar->addStretch(1);
mainLayout->addLayout(topBar); // ← 顶头加在最上面
mainLayout->addWidget(m_chatView); // 聊天显示
QHBoxLayout* inputLayout = new QHBoxLayout();
inputLayout->addWidget(m_input);
inputLayout->addWidget(m_sendBtn);
mainLayout->addLayout(inputLayout);
setLayout(mainLayout);
m_loadingTip = new LoadingTipWidget;
m_llm = new LLMRunner(modelFilePath, this);
if (!m_llm) {
QMessageBox::warning(nullptr, "Error", "LLM is NULL");
}
connect(m_llm, &LLMRunner::signalInitLLMFinished, m_loadingTip, &LoadingTipWidget::stopAndClose);
m_futureInit = QtConcurrent::run([this]() { m_llm->init(); });
connect(m_llm, &LLMRunner::chatResult, this, &ChatWindow::onChatResult);
connect(m_llm, &LLMRunner::chatStreamResult, this, &ChatWindow::onChatStreamResult);
// 绑定下拉框到采样预设
connect(m_modeBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, [this](int idx) {
m_llm->setPreset(static_cast<LLMRunner::Preset>(idx));
});
// 初始化一次
m_llm->setPreset(static_cast<LLMRunner::Preset>(m_modeBox->currentIndex()));
connect(m_sendBtn, &QPushButton::clicked, this, &ChatWindow::onSendClicked);
connect(m_input, &QLineEdit::returnPressed, this, &ChatWindow::onSendClicked);
m_processingTimer.setInterval(1000);
connect(&m_processingTimer, &QTimer::timeout, this, [=]() { m_secondsThinking++; });
m_llm->setRag(m_rag);
m_llm->setEnableAgent(true);
m_input->setFocus();
m_loadingTip->exec();
}
ChatWindow::~ChatWindow() {
if (m_llm) {
disconnect(m_llm, nullptr, this, nullptr);
m_llm->requestAbort();
}
if (m_futureInit.isRunning()) m_futureInit.waitForFinished();
if (m_futureChat.isRunning()) m_futureChat.waitForFinished();
delete m_loadingTip;
}
void ChatWindow::appendAssistantMessage(const QString& text) {
if (text.contains("[工具: calculator]") || text.contains("[工具返回]")) {
m_chatView->append("<span style='color:blue;'>[Agent 调用计算器]</span>");
}
m_chatView->append(text);
}
void ChatWindow::closeEvent(QCloseEvent* e) {
QWidget::closeEvent(e);
if (m_llm) m_llm->requestAbort();
m_loadingTip->close();
}
void ChatWindow::onSendClicked() {
const QString inputText = m_input->text().trimmed();
if (inputText.isEmpty()) return;
if (m_llm) m_llm->requestAbort();
if (m_futureChat.isRunning()) m_futureChat.waitForFinished();
m_chatView->append("<b>我:</b> " + inputText);
m_input->clear();
m_lastUserInput = inputText;
m_chatView->append(tr("AI 思考中..."));
m_aiThinking = true;
m_secondsThinking = 0;
m_processingTimer.start();
m_chatView->append("<b>LLM:</b> ");
QTextCursor c = m_chatView->textCursor();
c.movePosition(QTextCursor::End);
m_chatView->setTextCursor(c);
m_futureChat = QtConcurrent::run([this, inputText]() {
(void)m_llm->chat(inputText);
});
}
void ChatWindow::appendInline(const QString& text) {
QTextCursor cursor = m_chatView->textCursor();
cursor.movePosition(QTextCursor::End);
cursor.insertText(text);
m_chatView->setTextCursor(cursor);
m_chatView->ensureCursorVisible();
}
void ChatWindow::onChatStreamResult(const QString& partial) {
if (m_aiThinking) {
m_aiThinking = false;
m_processingTimer.stop();
// 不起新行,改成状态写在标题行末尾的括号里
appendInline(QString("(已思考%1s) ").arg(m_secondsThinking));
m_secondsThinking = 0;
}
appendInline(partial); // 继续在同一行追写
}
void ChatWindow::onChatResult(const QString& reply) {
// 如需最后整体回显,可在此追加
}