|
@@ -281,50 +281,54 @@ export default {
|
|
const chatId = this.getUuid();
|
|
const chatId = this.getUuid();
|
|
const wsUrl = `${process.env.VUE_APP_BASE_API}/chatbot/chat`; //this.wsUrl//'http://58.246.234.210:7860/api/v1/run/3ef7369e-b617-40d2-9e2e-54f3ee76ed2e?stream=false'
|
|
const wsUrl = `${process.env.VUE_APP_BASE_API}/chatbot/chat`; //this.wsUrl//'http://58.246.234.210:7860/api/v1/run/3ef7369e-b617-40d2-9e2e-54f3ee76ed2e?stream=false'
|
|
let message = this.userInput.trim();
|
|
let message = this.userInput.trim();
|
|
- if (message) {
|
|
|
|
- // Markdown换行:在每个换行符之前添加两个空格
|
|
|
|
- message = message.replace(/(\r\n|\r|\n)/g, " \n");
|
|
|
|
-
|
|
|
|
- this.messages.push({
|
|
|
|
- time: Date.now(),
|
|
|
|
- message: message,
|
|
|
|
- messageType: "TEXT",
|
|
|
|
- user: "user",
|
|
|
|
- done: true,
|
|
|
|
- });
|
|
|
|
- this.userInput = "";
|
|
|
|
- // 添加机器人的响应消息(初始为空)
|
|
|
|
- const botMessage = {
|
|
|
|
- user: "bot",
|
|
|
|
- messageType: "TEXT",
|
|
|
|
- message: "",
|
|
|
|
- html: "",
|
|
|
|
- time: Date.now(),
|
|
|
|
- done: false,
|
|
|
|
- };
|
|
|
|
- this.messages.push(botMessage);
|
|
|
|
|
|
+ if (!message) return;
|
|
|
|
+
|
|
|
|
+ // 添加用户消息
|
|
|
|
+ this.messages.push({
|
|
|
|
+ time: Date.now(),
|
|
|
|
+ message: message.replace(/(\r\n|\r|\n)/g, " \n"),
|
|
|
|
+ messageType: "TEXT",
|
|
|
|
+ user: "user",
|
|
|
|
+ done: true,
|
|
|
|
+ });
|
|
|
|
+ this.userInput = "";
|
|
|
|
+
|
|
|
|
+ // 添加机器人的响应消息
|
|
|
|
+ const botMessage = {
|
|
|
|
+ user: "bot",
|
|
|
|
+ messageType: "TEXT",
|
|
|
|
+ message: "",
|
|
|
|
+ html: "",
|
|
|
|
+ time: Date.now(),
|
|
|
|
+ done: false,
|
|
|
|
+ };
|
|
|
|
+ this.messages.push(botMessage);
|
|
|
|
|
|
- // 开始模拟思考
|
|
|
|
- const thinkingPromise = this.simulateThinking(botMessage);
|
|
|
|
- // 通过 HTTP 发送消息
|
|
|
|
- try {
|
|
|
|
- ///send-message
|
|
|
|
- const response = await fetch(`${wsUrl}`, {
|
|
|
|
- method: "POST",
|
|
|
|
- headers: {
|
|
|
|
- "Content-Type": "application/json",
|
|
|
|
- },
|
|
|
|
- /* mode: 'no-cors', */
|
|
|
|
- /* credentials: "include", */
|
|
|
|
- body: JSON.stringify({
|
|
|
|
- /* chatId: chatId, */
|
|
|
|
- message: message,
|
|
|
|
- /* output_type: "chat",
|
|
|
|
|
|
+ // 开始思考动画
|
|
|
|
+ const thinkingController = new AbortController();
|
|
|
|
+ const thinkingPromise = this.simulateThinking(
|
|
|
|
+ botMessage,
|
|
|
|
+ thinkingController.signal
|
|
|
|
+ );
|
|
|
|
+ // 通过 HTTP 发送消息
|
|
|
|
+ try {
|
|
|
|
+ ///send-message
|
|
|
|
+ const response = await fetch(`${wsUrl}`, {
|
|
|
|
+ method: "POST",
|
|
|
|
+ headers: {
|
|
|
|
+ "Content-Type": "application/json",
|
|
|
|
+ },
|
|
|
|
+ /* mode: 'no-cors', */
|
|
|
|
+ /* credentials: "include", */
|
|
|
|
+ body: JSON.stringify({
|
|
|
|
+ /* chatId: chatId, */
|
|
|
|
+ message: message,
|
|
|
|
+ /* output_type: "chat",
|
|
input_type: "chat", */
|
|
input_type: "chat", */
|
|
- chat_config_id: this.currentChat_id,
|
|
|
|
- user_id: this.$store.state.user.id,
|
|
|
|
- session_id: this.session_id || "",
|
|
|
|
- /* tweaks:this.tweaks {
|
|
|
|
|
|
+ chat_config_id: this.currentChat_id,
|
|
|
|
+ user_id: this.$store.state.user.id,
|
|
|
|
+ session_id: this.session_id || "",
|
|
|
|
+ /* tweaks:this.tweaks {
|
|
"ChatInput-U82Vu": {},
|
|
"ChatInput-U82Vu": {},
|
|
"Prompt-b8Z1E": {},
|
|
"Prompt-b8Z1E": {},
|
|
"OllamaModel-z9SVx": {},
|
|
"OllamaModel-z9SVx": {},
|
|
@@ -336,46 +340,48 @@ export default {
|
|
"ParseData-fH1vY": {},
|
|
"ParseData-fH1vY": {},
|
|
"ChatOutput-ybzb9": {}
|
|
"ChatOutput-ybzb9": {}
|
|
} */
|
|
} */
|
|
- }),
|
|
|
|
- });
|
|
|
|
- const result = await response.json();
|
|
|
|
- this.session_id = result.data.session_id;
|
|
|
|
- console.log(result);
|
|
|
|
- if (result.code !== 200) {
|
|
|
|
- const errorText = await response.text(); // 获取错误文本
|
|
|
|
- throw new Error(
|
|
|
|
- `HTTP error! status: ${response.status}, message: ${errorText}`
|
|
|
|
- );
|
|
|
|
- }
|
|
|
|
|
|
+ }),
|
|
|
|
+ });
|
|
|
|
+ // 停止思考动画
|
|
|
|
+ thinkingController.abort();
|
|
|
|
+ await thinkingPromise;
|
|
|
|
+ const result = await response.json();
|
|
|
|
+ this.session_id = result.data.session_id;
|
|
|
|
+ if (result.code !== 200) {
|
|
|
|
+ const errorText = await response.text(); // 获取错误文本
|
|
|
|
+ throw new Error(
|
|
|
|
+ `HTTP error! status: ${response.status}, message: ${errorText}`
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
|
|
- // 等待思考动画完成
|
|
|
|
- await thinkingPromise;
|
|
|
|
- if (this.$route.name !== "ai") {
|
|
|
|
- // 提取 additional_input 数据
|
|
|
|
- const additionalInput = result.data.milvus_ids; //outputs?.[0]?.outputs?.[0]?.results?.message?.data?.additional_input;
|
|
|
|
|
|
+ // 等待思考动画完成
|
|
|
|
+ await thinkingPromise;
|
|
|
|
+ if (this.$route.name !== "ai") {
|
|
|
|
+ // 提取 additional_input 数据
|
|
|
|
+ const additionalInput = result.data.milvus_ids; //outputs?.[0]?.outputs?.[0]?.results?.message?.data?.additional_input;
|
|
|
|
|
|
- // 处理字符串,提取 ID 值
|
|
|
|
- this.idArray = additionalInput; /* .split('\n') // 按换行符分割
|
|
|
|
|
|
+ // 处理字符串,提取 ID 值
|
|
|
|
+ this.idArray = additionalInput; /* .split('\n') // 按换行符分割
|
|
.map(line => line.trim()) // 去除每行首尾空白 */
|
|
.map(line => line.trim()) // 去除每行首尾空白 */
|
|
- /* .filter(line => line.startsWith('ID:')) // 只保留以 'ID:' 开头的行
|
|
|
|
|
|
+ /* .filter(line => line.startsWith('ID:')) // 只保留以 'ID:' 开头的行
|
|
.map(line => line.substring(3).trim()); // 提取 'ID:' 后面的内容并去除空白 */
|
|
.map(line => line.substring(3).trim()); // 提取 'ID:' 后面的内容并去除空白 */
|
|
|
|
|
|
- // 创建符合要求格式的对象
|
|
|
|
- const idObject = { ids: this.idArray };
|
|
|
|
|
|
+ // 创建符合要求格式的对象
|
|
|
|
+ const idObject = { ids: this.idArray };
|
|
|
|
|
|
- console.log("ID Object:", idObject);
|
|
|
|
|
|
+ console.log("ID Object:", idObject);
|
|
|
|
|
|
- // 调用方法来获取 Minio URL,传递新的 idObject
|
|
|
|
- await this.getMinioUrls(idObject);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- this.handleResponse(result, botMessage);
|
|
|
|
- } catch (error) {
|
|
|
|
- console.error("Error sending message:", error);
|
|
|
|
- // 如果发生错误,停止思考动画
|
|
|
|
- botMessage.done = true;
|
|
|
|
- botMessage.message = "抱歉,发生了错误,请稍后再试。";
|
|
|
|
|
|
+ // 调用方法来获取 Minio URL,传递新的 idObject
|
|
|
|
+ await this.getMinioUrls(idObject);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ this.handleResponse(result, botMessage);
|
|
|
|
+ } catch (error) {
|
|
|
|
+ console.error("Error sending message:", error);
|
|
|
|
+ thinkingController.abort();
|
|
|
|
+ await thinkingPromise;
|
|
|
|
+ botMessage.done = true;
|
|
|
|
+ botMessage.message = "抱歉,发生了错误,请稍后再试。";
|
|
}
|
|
}
|
|
},
|
|
},
|
|
async getMinioUrls(idObject) {
|
|
async getMinioUrls(idObject) {
|
|
@@ -413,27 +419,37 @@ export default {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
},
|
|
},
|
|
- async simulateThinking(message) {
|
|
|
|
|
|
+ async simulateThinking(message, signal) {
|
|
this.isThinking = true;
|
|
this.isThinking = true;
|
|
- const thinkingTime = Math.random() * 1000 + 500; // 思考时间为 0.5-1.5 秒
|
|
|
|
const dotInterval = 200; // 每 200ms 更新一次点
|
|
const dotInterval = 200; // 每 200ms 更新一次点
|
|
|
|
|
|
- const updateDots = () => {
|
|
|
|
- this.thinkingDots += ".";
|
|
|
|
- if (this.thinkingDots.length > 3) {
|
|
|
|
- this.thinkingDots = "";
|
|
|
|
- }
|
|
|
|
- message.message = this.thinkingDots;
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- const intervalId = setInterval(updateDots, dotInterval);
|
|
|
|
|
|
+ return new Promise((resolve) => {
|
|
|
|
+ const updateDots = () => {
|
|
|
|
+ if (signal.aborted) {
|
|
|
|
+ clearInterval(intervalId);
|
|
|
|
+ this.isThinking = false;
|
|
|
|
+ this.thinkingDots = "";
|
|
|
|
+ message.message = "";
|
|
|
|
+ resolve();
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
|
|
- await new Promise((resolve) => setTimeout(resolve, thinkingTime));
|
|
|
|
|
|
+ this.thinkingDots += ".";
|
|
|
|
+ if (this.thinkingDots.length > 3) {
|
|
|
|
+ this.thinkingDots = "";
|
|
|
|
+ }
|
|
|
|
+ message.message = "思考中" + this.thinkingDots;
|
|
|
|
+ };
|
|
|
|
|
|
- clearInterval(intervalId);
|
|
|
|
- this.isThinking = false;
|
|
|
|
- this.thinkingDots = "";
|
|
|
|
- message.message = "";
|
|
|
|
|
|
+ const intervalId = setInterval(updateDots, dotInterval);
|
|
|
|
+ signal.addEventListener("abort", () => {
|
|
|
|
+ clearInterval(intervalId);
|
|
|
|
+ this.isThinking = false;
|
|
|
|
+ this.thinkingDots = "";
|
|
|
|
+ message.message = "";
|
|
|
|
+ resolve();
|
|
|
|
+ });
|
|
|
|
+ });
|
|
},
|
|
},
|
|
|
|
|
|
async handleResponse(value, existingMessage) {
|
|
async handleResponse(value, existingMessage) {
|