From 3872642eda9d1165d085193ec3c34f61aaf8c4ba Mon Sep 17 00:00:00 2001 From: mssssss123 <824186479@qq.com> Date: Mon, 15 Dec 2025 02:28:57 +0000 Subject: [PATCH] update: support select kb when chat for demo --- ui/backend/app.py | 25 +++++++++++++++++++ ui/frontend/index.html | 10 ++++++++ ui/frontend/main.js | 54 ++++++++++++++++++++++++++++++++++++++++-- ui/frontend/style.css | 24 ++++++++++++++++++- 4 files changed, 110 insertions(+), 3 deletions(-) diff --git a/ui/backend/app.py b/ui/backend/app.py index 0777889..f6c01c9 100644 --- a/ui/backend/app.py +++ b/ui/backend/app.py @@ -145,6 +145,31 @@ def chat_pipeline(name: str): session_id = payload.get("session_id") dynamic_params = payload.get("dynamic_params", {}) + selected_collection = dynamic_params.get("collection_name") + + try: + kb_config = pm.load_kb_config() + milvus_global_config = kb_config.get("milvus", {}) + + retriever_params = { + "index_backend": "milvus", + "index_backend_configs": { + "milvus": milvus_global_config + } + } + + if selected_collection: + retriever_params["collection_name"] = selected_collection + print(f"debug: Chat using collection override: {selected_collection}") + + dynamic_params["retriever"] = retriever_params + + if "collection_name" in dynamic_params: + del dynamic_params["collection_name"] + + except Exception as e: + print(f"Warning: Failed to construct retriever config: {e}") + if not session_id: return jsonify({"error": "session_id missing. Please start engine first."}), 400 diff --git a/ui/frontend/index.html b/ui/frontend/index.html index dc469cc..da24db1 100644 --- a/ui/frontend/index.html +++ b/ui/frontend/index.html @@ -174,6 +174,16 @@

Parameter Configuration

+
+
+ + +
+ +
+
diff --git a/ui/frontend/main.js b/ui/frontend/main.js index d0318eb..3c5b2df 100644 --- a/ui/frontend/main.js +++ b/ui/frontend/main.js @@ -77,6 +77,7 @@ const els = { chatNewBtn: document.getElementById("chat-new-btn"), chatSessionList: document.getElementById("chat-session-list"), demoToggleBtn: document.getElementById("demo-toggle-btn"), // 引擎开关 + chatCollectionSelect: document.getElementById("chat-collection-select"), // [新增] 视图容器 chatMainView: document.getElementById("chat-main-view"), @@ -800,6 +801,41 @@ function updateDemoControls() { // --- Chat Logic (Updated with Streaming) --- +// [新增] 渲染聊天界面的 Collection 下拉选项 +async function renderChatCollectionOptions() { + if (!els.chatCollectionSelect) return; + + // 保存当前选中的值,以免刷新后重置 + const currentVal = els.chatCollectionSelect.value; + + try { + // 复用后端的列表接口 + const data = await fetchJSON('/api/kb/files'); + const collections = data.index || []; // data.index 存放的是 collection 列表 + + els.chatCollectionSelect.innerHTML = ''; + + collections.forEach(c => { + const opt = document.createElement("option"); + opt.value = c.name; + // 显示名字和数据量 + opt.textContent = `${c.name} (${c.count || 0})`; + els.chatCollectionSelect.appendChild(opt); + }); + + // 尝试恢复选中状态 + if (currentVal) { + // 检查新列表里是否还有这个值 + const exists = collections.find(c => c.name === currentVal); + if (exists) els.chatCollectionSelect.value = currentVal; + } + + } catch (e) { + console.error("Failed to load collections for chat:", e); + // 出错时不覆盖默认选项,或者显示错误 + } +} + // [新增] 切换到知识库视图 function openKBView() { if (!els.chatMainView || !els.kbMainView) return; @@ -1211,6 +1247,9 @@ function openChatView() { if (els.chatPipelineName) els.chatPipelineName.textContent = state.selectedPipeline || "—"; renderChatPipelineMenu(); + + // [新增] 进入聊天时,加载最新的 Collection 列表 + renderChatCollectionOptions(); if (!state.chat.currentSessionId) createNewChatSession(); @@ -1456,10 +1495,21 @@ async function handleChatSubmit(event) { try { if (!state.parametersReady) await persistParameterData({ silent: true }); + + const selectedCollection = els.chatCollectionSelect ? els.chatCollectionSelect.value : ""; + + const dynamicParams = {}; + if (selectedCollection) { + dynamicParams["collection_name"] = selectedCollection; + } + const endpoint = `/api/pipelines/${encodeURIComponent(state.selectedPipeline)}/chat`; const body = JSON.stringify({ - question, history: state.chat.history, is_demo: true, - session_id: state.chat.engineSessionId, dynamic_params: {} + question, + history: state.chat.history, + is_demo: true, + session_id: state.chat.engineSessionId, + dynamic_params: dynamicParams }); const response = await fetch(endpoint, { diff --git a/ui/frontend/style.css b/ui/frontend/style.css index 68ae3c6..a322f9e 100644 --- a/ui/frontend/style.css +++ b/ui/frontend/style.css @@ -1199,4 +1199,26 @@ body { display: flex; justify-content: space-between; } -.folder-file-row:last-child { border-bottom: none; } \ No newline at end of file +.folder-file-row:last-child { border-bottom: none; } + +/* Chat Context Selector */ +#chat-collection-select { + border-color: var(--border-subtle); + cursor: pointer; + padding-left: 12px; + padding-right: 32px; /* 留出箭头空间 */ + font-weight: 500; + color: var(--text-primary); + box-shadow: none; + transition: all 0.2s; +} + +#chat-collection-select:hover { + background-color: #fff; + border-color: var(--accent-blue); +} + +#chat-collection-select:focus { + border-color: var(--accent-blue); + box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.1); +} \ No newline at end of file