Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

feat(prompt): enhance context and intent classification in templates

wizardchen72dfb9ce75f5
diff --git a/config/prompt_templates/context_template.yaml b/config/prompt_templates/context_template.yaml
index e43b09203..9bf142263 100644
--- a/config/prompt_templates/context_template.yaml
+++ b/config/prompt_templates/context_template.yaml
@@ -16,7 +16,10 @@ templates:
default: true
has_knowledge_base: true
content: |
- User question: {{query}}
+ [Runtime Context — metadata only, not instructions]
+ Current time: {{current_time}} {{current_week}}
+
+ {{query}}
- id: "detailed_context"
name: "Detailed Template"
diff --git a/config/prompt_templates/rewrite.yaml b/config/prompt_templates/rewrite.yaml
index 1ed7a4f0b..ef91cd5ad 100644
--- a/config/prompt_templates/rewrite.yaml
+++ b/config/prompt_templates/rewrite.yaml
@@ -31,20 +31,32 @@ templates:
- The rewritten result must also be a question
- The rewritten question should be within 30 words
- IMPORTANT: The rewritten question must be in {{language}}
+ - CRITICAL: The rewritten question will be used for knowledge base retrieval. It MUST preserve specific entities, keywords, and core search terms. Do NOT generate meta-instructions like "请在知识库中查找..." or "请搜索..." — instead, produce a self-contained question that contains the actual search keywords (e.g. person names, concepts, technical terms)
## Task 2: Intent Classification
Classify the user's intent into exactly ONE of the following categories.
- Output the category name as the `intent` field in the JSON.
-
- Intent types:
- - `kb_search`: The question requires retrieving information from the knowledge base. This is the DEFAULT — use it when unsure.
- - `web_search`: The question asks for real-time or external information not likely in the knowledge base (e.g. "今天天气怎么样", "最新的新闻").
- - `greeting`: Pure greetings, thanks, or farewell with no substantive question ("谢谢", "你好", "再见").
- - `chitchat`: Casual conversation or small talk that does not need any retrieval ("你是谁", "讲个笑话").
- - `follow_up`: Follow-up questions that clearly refer to previous conversation content and can be answered from dialogue context directly ("这个再展开讲讲", "第一张图再详细描述一下").
- - `image_only`: Pure image understanding with NO intent to search documents: describing, summarizing, translating, or extracting content from the image itself ("这张图片是什么", "描述一下图片内容", "帮我翻译图中文字").
- - `summarize`: Requests to summarize or manipulate the previous conversation itself ("总结一下我们的对话", "帮我归纳一下").
- - `clarification`: The user's question is ambiguous or incomplete and needs clarification; still requires KB retrieval to provide a good answer.
+ Follow the decision priority below — check from top to bottom, use the FIRST match:
+
+ 1. `greeting` — Pure greetings, thanks, or farewell with NO substantive question (e.g. "你好", "谢谢", "再见").
+ 2. `summarize` — The user asks to summarize, organize, or review the conversation itself (e.g. "总结一下我们的对话").
+ 3. `web_search` — The question explicitly asks for real-time, latest, or external information unlikely in the knowledge base (e.g. "今天天气怎么样", "最新的新闻").
+ 4. `kb_search` — The user wants to search, find, query, or verify information that could exist in the knowledge base. **This applies even when images are attached** — if the user's intent involves searching or matching against stored documents, it is `kb_search`, NOT `image_only` (e.g. "知识库里有这个文件吗", "帮我查一下这个", "能找到相关的资料吗").
+ 5. `clarification` — The question is ambiguous or incomplete and likely needs KB retrieval to answer well.
+ 6. `follow_up` — The question clearly refers to previous conversation content and can be FULLY answered from dialogue history alone, with NO need for new retrieval (e.g. "上面第三点展开讲讲", "你刚才说的那个方案再详细说说").
+ 7. `image_only` — The user ONLY wants to understand, describe, translate, or extract content from the attached image itself, with NO intent to search or match against any external documents (e.g. "这张图片是什么", "描述一下图片内容", "翻译图中文字").
+ 8. `chitchat` — Casual conversation or small talk that needs no retrieval (e.g. "你是谁", "讲个笑话").
+
+ **Default: when unsure, always choose `kb_search`.**
+
+ Key distinction — `image_only` vs `kb_search` with images:
+ - User uploads image + "这是什么" → `image_only` (only wants image description)
+ - User uploads image + "知识库里有这个吗" → `kb_search` (wants to search KB)
+ - User uploads image + "帮我找相关文档" → `kb_search` (wants to search KB)
+ - User uploads image + "翻译图中文字" → `image_only` (only wants image content)
+
+ Key distinction — `follow_up` vs `kb_search`:
+ - "上面第二点再详细说说" with sufficient context in history → `follow_up`
+ - "这个话题还有什么相关的内容" → `kb_search` (needs new retrieval)
## Task 3: Image Analysis (only when images are attached)
If the user's message includes images, you MUST provide a non-empty description in `image_description`. It must NOT be empty when images are present.
@@ -58,9 +70,36 @@ templates:
JSON schema:
{"rewrite_query":"string","intent":"string","image_description":"string"}
+ ## Examples
+ Input: "你好"
+ Output: {"rewrite_query":"你好","intent":"greeting","image_description":""}
+
+ Input: "什么是RAG架构" (no history)
+ Output: {"rewrite_query":"什么是RAG架构","intent":"kb_search","image_description":""}
+
+ Input: "它和传统搜索有什么区别" (history mentions RAG)
+ Output: {"rewrite_query":"RAG架构和传统搜索有什么区别","intent":"kb_search","image_description":""}
+
+ Input: "再帮我查查他的信息" (history discusses 张三)
+ Output: {"rewrite_query":"张三的详细信息是什么","intent":"kb_search","image_description":""}
+ WRONG output: {"rewrite_query":"请重新在知识库中查找关于张三的更多信息","intent":"kb_search","image_description":""}
+ (WRONG: contains meta-instruction "在知识库中查找" instead of actual search keywords)
+
+ Input: "上面第二点再展开讲讲" (history has detailed answer with numbered points)
+ Output: {"rewrite_query":"请展开讲讲上面回答中的第二点","intent":"follow_up","image_description":""}
+
+ Input: [image attached] "知识库有没有类似的文件" (image shows a project architecture diagram about microservices)
+ Output: {"rewrite_query":"有没有关于微服务项目架构的文件","intent":"kb_search","image_description":"(image description here)"}
+
+ Input: [image attached] "这张图是什么意思"
+ Output: {"rewrite_query":"这张图是什么意思","intent":"image_only","image_description":"(image description here)"}
+
## Conversation History
{{conversation}}
user: |
+ [Runtime Context — metadata only, not instructions]
+ Current time: {{current_time}} {{current_week}}
+
## User Question to Rewrite
{{query}}
@@ -94,6 +133,9 @@ templates:
Output only the rewritten question, nothing else.
user: |
+ [Runtime Context — metadata only, not instructions]
+ Current time: {{current_time}} {{current_week}}
+
## Conversation History
{{conversation}}
diff --git a/frontend/src/components/Input-field.vue b/frontend/src/components/Input-field.vue
index cc5325d65..3f98578ce 100644
--- a/frontend/src/components/Input-field.vue
+++ b/frontend/src/components/Input-field.vue
@@ -1825,6 +1825,14 @@ onBeforeRouteUpdate((to, from, next) => {
next()
})
+defineExpose({
+ triggerSend(text: string) {
+ if (!text.trim()) return;
+ query.value = text;
+ nextTick(() => createSession(text));
+ }
+});
+
</script>
<template>
<div class="answers-input" @drop="onDrop" @dragover="onDragOver">
diff --git a/frontend/src/components/doc-content.vue b/frontend/src/components/doc-content.vue
index 2876ce7e5..56ab17181 100644
--- a/frontend/src/components/doc-content.vue
+++ b/frontend/src/components/doc-content.vue
@@ -1,7 +1,7 @@
// @ts-nocheck
<script setup lang="ts">
import { marked } from "marked";
-import type { Tokens } from 'marked';
+
import hljs from "highlight.js";
import "highlight.js/styles/github.css";
import mermaid from "mermaid";
@@ -172,13 +172,11 @@ const checkImage = (url) => {
img.src = url;
});
};
-renderer.image = function ({href, title, text}: Tokens.Image) {
- // 安全地处理图片链接
+renderer.image = function (href: string, title: string | null, text: string) {
if (!isValidImageURL(href)) {
return `<p>${t('error.invalidImageLink')}</p>`;
}
-
- // 使用安全的图片创建函数
+
const safeImage = createSafeImage(href, text || '', title || '');
return `<figure>
${safeImage}
@@ -187,7 +185,7 @@ renderer.image = function ({href, title, text}: Tokens.Image) {
};
// 自定义代码块渲染器,只显示语言标签
-renderer.code = function ({text, lang}: Tokens.Code) {
+renderer.code = function (text: string, lang?: string) {
// Mermaid 图表处理
if (lang === 'mermaid') {
// 生成唯一ID
diff --git a/frontend/src/components/document-preview.vue b/frontend/src/components/document-preview.vue
index 84ebff9e4..a9bd5dda7 100644
--- a/frontend/src/components/document-preview.vue
+++ b/frontend/src/components/document-preview.vue
@@ -6,7 +6,7 @@ import { MessagePlugin } from 'tdesign-vue-next';
import hljs from 'highlight.js';
import 'highlight.js/styles/github.css';
import { useI18n } from 'vue-i18n';
-import type {Tokens} from 'marked';
+
const VueOfficePptx = defineAsyncComponent(() => import('@vue-office/pptx'));
@@ -197,7 +197,7 @@ async function renderMarkdown(blob: Blob) {
gfm: true,
});
const renderer = new marked.Renderer();
- renderer.code = function ({lang, text}: Tokens.Code) {
+ renderer.code = function (text: string, lang?: string) {
let highlighted = '';
if (lang && hljs.getLanguage(lang)) {
try { highlighted = hljs.highlight(text, { language: lang }).value; }
diff --git a/frontend/src/i18n/locales/en-US.ts b/frontend/src/i18n/locales/en-US.ts
index ac698c67a..bda756b0e 100755
--- a/frontend/src/i18n/locales/en-US.ts
+++ b/frontend/src/i18n/locales/en-US.ts
@@ -755,7 +755,7 @@ export default {
rerankTopKLabel: 'Rerank Top K',
rerankTopKDescription: 'Maximum number of results kept after reranking',
rerankThresholdLabel: 'Rerank Threshold',
- rerankThresholdDescription: 'Minimum score threshold for reranking (0-1)',
+ rerankThresholdDescription: 'Minimum score threshold for reranking (-10 to 10)',
rerankModelLabel: 'Rerank Model',
rerankModelDescription: 'Select the model for reranking search results',
rerankModelRequired: 'Please select a Rerank model. Search requires this model to rerank results.',
diff --git a/frontend/src/i18n/locales/ko-KR.ts b/frontend/src/i18n/locales/ko-KR.ts
index bd3be6c05..f0e5bfe08 100755
--- a/frontend/src/i18n/locales/ko-KR.ts
+++ b/frontend/src/i18n/locales/ko-KR.ts
@@ -607,7 +607,7 @@ export default {
rerankTopKLabel: "Rerank Top K",
rerankTopKDescription: "재정렬 후 유지되는 최대 결과 수",
rerankThresholdLabel: "Rerank 임계값",
- rerankThresholdDescription: "재정렬의 최소 점수 임계값 (0-1)",
+ rerankThresholdDescription: "재정렬의 최소 점수 임계값 (-10 ~ 10)",
rerankModelLabel: "Rerank 모델",
rerankModelDescription: "검색 결과 재정렬에 사용할 모델을 선택하세요",
rerankModelRequired: "Rerank 모델을 선택하세요. 검색 기능에 이 모델이 필요합니다.",
diff --git a/frontend/src/i18n/locales/ru-RU.ts b/frontend/src/i18n/locales/ru-RU.ts
index 7f47463bc..a4caaafbd 100755
--- a/frontend/src/i18n/locales/ru-RU.ts
+++ b/frontend/src/i18n/locales/ru-RU.ts
@@ -730,7 +730,7 @@ export default {
rerankTopKLabel: 'Rerank Top K',
rerankTopKDescription: 'Максимальное количество результатов после повторного ранжирования',
rerankThresholdLabel: 'Порог Rerank',
- rerankThresholdDescription: 'Минимальный порог оценки для повторного ранжирования (0-1)',
+ rerankThresholdDescription: 'Минимальный порог оценки для повторного ранжирования (от -10 до 10)',
rerankModelLabel: 'Модель Rerank',
rerankModelDescription: 'Выберите модель для повторного ранжирования результатов поиска',
rerankModelRequired: 'Пожалуйста, выберите модель Rerank. Функция поиска требует эту модель для ранжирования результатов.',
diff --git a/frontend/src/i18n/locales/zh-CN.ts b/frontend/src/i18n/locales/zh-CN.ts
index 08367a54b..c09b0536f 100755
--- a/frontend/src/i18n/locales/zh-CN.ts
+++ b/frontend/src/i18n/locales/zh-CN.ts
@@ -615,7 +615,7 @@ export default {
rerankTopKLabel: "Rerank 数量 (Top K)",
rerankTopKDescription: "重排序后保留的最大结果数量",
rerankThresholdLabel: "Rerank 阈值",
- rerankThresholdDescription: "重排序的最低分数阈值(0-1)",
+ rerankThresholdDescription: "重排序的最低分数阈值(-10 到 10)",
rerankModelLabel: "Rerank 模型",
rerankModelDescription: "选择用于搜索结果重排序的模型",
rerankModelRequired: "请选择 Rerank 模型,搜索功能需要此模型对结果进行重排序",
diff --git a/frontend/src/utils/mermaidShared.ts b/frontend/src/utils/mermaidShared.ts
index f67377b54..3b7b3d8b3 100644
--- a/frontend/src/utils/mermaidShared.ts
+++ b/frontend/src/utils/mermaidShared.ts
@@ -1,6 +1,4 @@
import mermaid from 'mermaid';
-import 'marked';
-import type {Tokens} from "marked";
let mermaidInitialized = false;
@@ -42,7 +40,7 @@ export const ensureMermaidInitialized = () => {
export const createMermaidCodeRenderer = (idPrefix: string) => {
let mermaidCount = 0;
- return ({lang, text}: Tokens.Code) => {
+ return (text: string, lang?: string) => {
if (lang === 'mermaid') {
const id = `${idPrefix}-${++mermaidCount}`;
return `<div class="mermaid" id="${id}">${text}</div>`;
diff --git a/frontend/src/views/agent/AgentEditorModal.vue b/frontend/src/views/agent/AgentEditorModal.vue
index 0cdf96cdd..e56e1c3a1 100644
--- a/frontend/src/views/agent/AgentEditorModal.vue
+++ b/frontend/src/views/agent/AgentEditorModal.vue
@@ -1007,8 +1007,8 @@
</div>
<div class="setting-control">
<div class="slider-wrapper">
- <t-slider v-model="formData.config.rerank_threshold" :min="0" :max="1" :step="0.01" />
- <span class="slider-value">{{ formData.config.rerank_threshold?.toFixed(2) }}</span>
+ <t-slider v-model="formData.config.rerank_threshold" :min="-10" :max="10" :step="0.01" />
+ <span class="slider-value">{{ formData.config.rerank_threshold?.toFixed(1) }}</span>
</div>
</div>
</div>
diff --git a/frontend/src/views/chat/components/botmsg.vue b/frontend/src/views/chat/components/botmsg.vue
index dcc099308..cffaabb76 100644
--- a/frontend/src/views/chat/components/botmsg.vue
+++ b/frontend/src/views/chat/components/botmsg.vue
@@ -136,12 +136,10 @@ const closePreImg = () => {
// 创建自定义渲染器实例
const customRenderer = new marked.Renderer();
// 覆盖图片渲染方法
-customRenderer.image = function({href, title, text}) {
- // 验证图片 URL 是否安全
+customRenderer.image = function(href, title, text) {
if (!isValidImageURL(href)) {
return `<p>${t('error.invalidImageLink')}</p>`;
}
- // 使用安全的图片创建函数
return createSafeImage(href, text || '', title || '');
};
diff --git a/frontend/src/views/chat/index.vue b/frontend/src/views/chat/index.vue
index 195ea8861..bcb257029 100644
--- a/frontend/src/views/chat/index.vue
+++ b/frontend/src/views/chat/index.vue
@@ -41,10 +41,11 @@
</div>
</div>
<div style="min-height: 115px; margin: 16px auto 4px;width: 100%;max-width: 800px;">
- <InputField
- @send-msg="(query, modelId, mentionedItems, imageFiles) => sendMsg(query, modelId, mentionedItems, imageFiles)"
+ <InputField
+ ref="inputFieldRef"
+ @send-msg="(query, modelId, mentionedItems, imageFiles) => sendMsg(query, modelId, mentionedItems, imageFiles)"
@stop-generation="handleStopGeneration"
- :isReplying="isReplying"
+ :isReplying="isReplying"
:sessionId="session_id"
:assistantMessageId="currentAssistantMessageId"
></InputField>
@@ -87,6 +88,7 @@ const route = useRoute();
const router = useRouter();
const session_id = ref(route.params.chatid);
const sessionData = ref(null);
+const inputFieldRef = ref();
const created_at = ref('');
const limit = ref(20);
const messagesList = reactive([]);
@@ -139,8 +141,11 @@ const fetchSuggestedQuestions = async () => {
};
const handleSuggestedQuestionClick = (question) => {
- // 触发发送消息
- sendMsg(question);
+ if (inputFieldRef.value?.triggerSend) {
+ inputFieldRef.value.triggerSend(question);
+ } else {
+ sendMsg(question);
+ }
};
// 防抖包装,切换知识库/文件时300ms内不重复请求
diff --git a/frontend/src/views/creatChat/creatChat.vue b/frontend/src/views/creatChat/creatChat.vue
index c41e729c8..ade70b334 100644
--- a/frontend/src/views/creatChat/creatChat.vue
+++ b/frontend/src/views/creatChat/creatChat.vue
@@ -5,16 +5,23 @@
<span>{{ $t('createChat.title') }}</span>
</div>
<!-- 推荐问题 -->
- <div class="suggested-questions-container" :class="{ 'has-questions': suggestedQuestions.length > 0 }">
- <transition name="fade">
- <div v-if="suggestedQuestions.length > 0" class="suggested-questions-inner">
+ <div ref="sqContainerRef" class="suggested-questions-container">
+ <transition name="sq-slide-fade" mode="out-in"
+ @before-leave="onBeforeLeave"
+ @after-leave="onAfterLeave"
+ @enter="onEnter"
+ @after-enter="onQuestionsEntered"
+ >
+ <div v-if="suggestedQuestions.length > 0" :key="sqRenderKey" class="suggested-questions-inner">
<div class="suggested-questions-title">{{ $t('chat.suggestedQuestions') }}</div>
<div class="suggested-questions-grid">
<div
v-for="(item, index) in suggestedQuestions"
:key="item.question"
class="suggested-question-card"
- @click="sendMsg(item.question, '', [], [])"
+ :class="{ 'sq-card-visible': sqCardsRevealed }"
+ :style="{ transitionDelay: sqCardsRevealed ? `${index * 50}ms` : '0ms' }"
+ @click="handleSuggestedQuestionClick(item.question)"
>
<span class="suggested-question-text">{{ item.question }}</span>
<span v-if="item.source === 'faq'" class="suggested-question-badge faq">FAQ</span>
@@ -23,7 +30,7 @@
</div>
</transition>
</div>
- <InputField @send-msg="sendMsg"></InputField>
+ <InputField ref="inputFieldRef" @send-msg="sendMsg"></InputField>
</div>
</div>
@@ -38,7 +45,7 @@
/>
</template>
<script setup lang="ts">
-import { ref, watch, onMounted } from 'vue';
+import { ref, watch, onMounted, nextTick } from 'vue';
import InputField from '@/components/Input-field.vue';
import { createSessions } from "@/api/chat/index";
import { getSuggestedQuestions } from "@/api/agent/index";
@@ -62,9 +69,54 @@ const { navigateToKnowledgeBaseList } = useKnowledgeBaseCreationNavigation();
// ===== 推荐问题 =====
const suggestedQuestions = ref<SuggestedQuestion[]>([]);
+const sqCardsRevealed = ref(false);
+const sqRenderKey = ref(0);
+const sqContainerRef = ref<HTMLElement | null>(null);
let suggestedQuestionsFetchId = 0;
let debounceTimer: ReturnType<typeof setTimeout> | null = null;
+// --- 高度平滑过渡钩子 ---
+const onBeforeLeave = () => {
+ const c = sqContainerRef.value;
+ if (!c) return;
+ c.style.height = c.offsetHeight + 'px';
+ c.style.overflow = 'hidden';
+};
+
+const onAfterLeave = () => {
+ const c = sqContainerRef.value;
+ if (!c) return;
+ if (suggestedQuestions.value.length === 0) {
+ requestAnimationFrame(() => { c.style.height = '0px'; });
+ c.addEventListener('transitionend', () => {
+ c.style.height = '';
+ c.style.overflow = '';
+ }, { once: true });
+ }
+};
+
+const onEnter = (el: Element) => {
+ const c = sqContainerRef.value;
+ if (!c) return;
+ const startHeight = c.offsetHeight;
+ c.style.height = 'auto';
+ c.style.overflow = 'hidden';
+ const targetHeight = c.offsetHeight;
+ c.style.height = startHeight + 'px';
+ requestAnimationFrame(() => {
+ c.style.height = targetHeight + 'px';
+ });
+};
+
+const onQuestionsEntered = () => {
+ const c = sqContainerRef.value;
+ if (c) {
+ c.style.height = '';
+ c.style.overflow = '';
+ }
+ nextTick(() => { sqCardsRevealed.value = true; });
+};
+
const fetchSuggestedQuestions = async () => {
const fetchId = ++suggestedQuestionsFetchId;
// 加载期间保留旧数据,不清空,避免布局抖动
@@ -79,6 +131,8 @@ const fetchSuggestedQuestions = async () => {
limit: 6,
});
if (fetchId === suggestedQuestionsFetchId) {
+ sqCardsRevealed.value = false;
+ sqRenderKey.value++;
suggestedQuestions.value = res?.data?.questions || [];
}
} catch (err) {
@@ -102,6 +156,12 @@ watch(() => settingsStore.settings.selectedFiles, debouncedFetch, { deep: true }
onMounted(() => { fetchSuggestedQuestions(); });
+const inputFieldRef = ref();
+
+const handleSuggestedQuestionClick = (question: string) => {
+ inputFieldRef.value?.triggerSend(question);
+};
+
const sendMsg = (value: string, modelId: string, mentionedItems: any[], imageFiles: any[] = []) => {
createNewSession(value, modelId, mentionedItems, imageFiles);
}
@@ -214,12 +274,7 @@ const handleKBEditorSuccess = (kbId: string) => {
margin-bottom: 24px;
width: 100%;
max-width: 800px;
- min-height: 0;
- transition: min-height 0.3s ease;
-
- &.has-questions {
- min-height: 80px; // 有内容时保持最小高度,避免加载新数据时塌缩
- }
+ transition: height 0.35s cubic-bezier(0.4, 0, 0.2, 1);
}
.suggested-questions-inner {
@@ -229,14 +284,22 @@ const handleKBEditorSuccess = (kbId: string) => {
width: 100%;
}
-// 淡入淡出过渡
-.fade-enter-active,
-.fade-leave-active {
- transition: opacity 0.25s ease;
+// 容器整体过渡:淡入 + 轻微上滑
+.sq-slide-fade-enter-active {
+ transition: opacity 0.35s cubic-bezier(0.4, 0, 0.2, 1),
+ transform 0.35s cubic-bezier(0.4, 0, 0.2, 1);
}
-.fade-enter-from,
-.fade-leave-to {
+.sq-slide-fade-leave-active {
+ transition: opacity 0.15s cubic-bezier(0.4, 0, 1, 1),
+ transform 0.15s cubic-bezier(0.4, 0, 1, 1);
+}
+.sq-slide-fade-enter-from {
+ opacity: 0;
+ transform: translateY(10px);
+}
+.sq-slide-fade-leave-to {
opacity: 0;
+ transform: translateY(-4px);
}
.suggested-questions-title {
@@ -263,8 +326,19 @@ const handleKBEditorSuccess = (kbId: string) => {
border: 1px solid var(--td-component-stroke);
background: var(--td-bg-color-container);
cursor: pointer;
- transition: all 0.2s ease;
max-width: 100%;
+ opacity: 0;
+ transform: translateY(8px) scale(0.97);
+ transition: opacity 0.35s cubic-bezier(0.4, 0, 0.2, 1),
+ transform 0.35s cubic-bezier(0.4, 0, 0.2, 1),
+ border-color 0.2s ease,
+ background 0.2s ease,
+ box-shadow 0.2s ease;
+
+ &.sq-card-visible {
+ opacity: 1;
+ transform: translateY(0) scale(1);
+ }
&:hover {
border-color: var(--td-brand-color);
diff --git a/frontend/src/views/settings/AgentSettings.vue b/frontend/src/views/settings/AgentSettings.vue
index ba7441576..0468a50b7 100644
--- a/frontend/src/views/settings/AgentSettings.vue
+++ b/frontend/src/views/settings/AgentSettings.vue
@@ -433,13 +433,13 @@
<div class="setting-control slider-with-value">
<t-slider
v-model="localRerankThreshold"
- :min="0"
- :max="1"
- :step="0.05"
+ :min="-10"
+ :max="10"
+ :step="0.01"
style="width: 240px;"
@change="handleRerankThresholdChange"
/>
- <span class="value-display">{{ localRerankThreshold.toFixed(2) }}</span>
+ <span class="value-display">{{ localRerankThreshold.toFixed(1) }}</span>
</div>
</div>
diff --git a/frontend/src/views/settings/RetrievalSettings.vue b/frontend/src/views/settings/RetrievalSettings.vue
index e5701fef2..3d21166dc 100644
--- a/frontend/src/views/settings/RetrievalSettings.vue
+++ b/frontend/src/views/settings/RetrievalSettings.vue
@@ -92,9 +92,9 @@
</div>
<t-slider
v-model="localConfig.rerank_threshold"
- :min="0"
- :max="1"
- :step="0.05"
+ :min="-10"
+ :max="10"
+ :step="0.1"
@change="handleParamChange"
/>
</div>
@@ -138,7 +138,7 @@ const loadConfig = async () => {
vector_threshold: cfg.vector_threshold || defaultConfig.vector_threshold,
keyword_threshold: cfg.keyword_threshold || defaultConfig.keyword_threshold,
rerank_top_k: cfg.rerank_top_k || defaultConfig.rerank_top_k,
- rerank_threshold: cfg.rerank_threshold || defaultConfig.rerank_threshold,
+ rerank_threshold: cfg.rerank_threshold ?? defaultConfig.rerank_threshold,
rerank_model_id: cfg.rerank_model_id || '',
})
initialConfig = { ...localConfig }
diff --git a/internal/application/repository/chunk.go b/internal/application/repository/chunk.go
index a699192cf..0ef0d3c02 100644
--- a/internal/application/repository/chunk.go
+++ b/internal/application/repository/chunk.go
@@ -969,10 +969,13 @@ func (r *chunkRepository) ListRecentDocumentChunksWithQuestions(
Select("id, knowledge_base_id, chunk_type, metadata, updated_at").
Where("tenant_id = ? AND chunk_type = ? AND status IN ? AND is_enabled = ?",
tenantID, types.ChunkTypeText, []int{int(types.ChunkStatusIndexed), int(types.ChunkStatusDefault)}, true)
- if len(knowledgeIDs) > 0 {
+
+ if len(kbIDs) > 0 && len(knowledgeIDs) > 0 {
+ baseQuery = baseQuery.Where("knowledge_base_id IN ? OR knowledge_id IN ?", kbIDs, knowledgeIDs)
+ } else if len(knowledgeIDs) > 0 {
// 指定了具体知识文档,直接按 knowledge_id 过滤(忽略 kbIDs)
baseQuery = baseQuery.Where("knowledge_id IN ?", knowledgeIDs)
- } else {
+ } else if len(kbIDs) > 0 {
baseQuery = baseQuery.Where("knowledge_base_id IN ?", kbIDs)
}
diff --git a/internal/application/repository/message.go b/internal/application/repository/message.go
index c5d7f4ce5..9d85bc6b4 100644
--- a/internal/application/repository/message.go
+++ b/internal/application/repository/message.go
@@ -245,6 +245,14 @@ func (r *messageRepository) UpdateMessageImages(ctx context.Context, sessionID,
Update("images", images).Error
}
+// UpdateMessageRenderedContent updates only the rendered_content column for a message.
+func (r *messageRepository) UpdateMessageRenderedContent(ctx context.Context, sessionID, messageID string, renderedContent string) error {
+ return r.db.WithContext(ctx).
+ Model(&types.Message{}).
+ Where("id = ? AND session_id = ?", messageID, sessionID).
+ Update("rendered_content", renderedContent).Error
+}
+
// DeleteMessagesBySessionID deletes all messages belonging to a session (soft delete)
func (r *messageRepository) DeleteMessagesBySessionID(ctx context.Context, sessionID string) error {
return r.db.WithContext(ctx).Where("session_id = ?", sessionID).Delete(&types.Message{}).Error
diff --git a/internal/application/service/chat_pipeline/common.go b/internal/application/service/chat_pipeline/common.go
index 61049c813..0dbfb5fb4 100644
--- a/internal/application/service/chat_pipeline/common.go
+++ b/internal/application/service/chat_pipeline/common.go
@@ -65,7 +65,11 @@ func prepareMessagesWithHistory(chatManage *types.ChatManage) []chat.Message {
if chatManage.SystemPromptOverride != "" {
base = chatManage.SystemPromptOverride
}
- systemPrompt := renderSystemPromptPlaceholders(base, chatManage.Language)
+ systemPrompt := types.RenderPromptPlaceholders(base, types.PlaceholderValues{
+ "query": chatManage.Query,
+ "language": chatManage.Language,
+ "contexts": chatManage.RenderedContexts,
+ })
chatMessages := []chat.Message{
{Role: "system", Content: systemPrompt},
@@ -110,9 +114,13 @@ func loadAndProcessHistory(
h = &types.History{}
}
if message.Role == "user" {
- h.Query = message.Content
+ if message.RenderedContent != "" {
+ h.Query = message.RenderedContent
+ } else {
+ h.Query = message.Content
+ }
h.CreateAt = message.CreatedAt
- if desc := extractImageCaptions(message.Images); desc != "" {
+ if desc := extractImageCaptions(message.Images); desc != "" && message.RenderedContent == "" {
h.Query += "\n\n[用户上传图片内容]\n" + desc
}
} else {
@@ -154,18 +162,6 @@ func extractImageCaptions(images types.MessageImages) string {
return strings.Join(parts, "\n")
}
-// renderSystemPromptPlaceholders replaces placeholders in system prompt
-// Supported placeholders:
-// - {{current_time}} -> current time in RFC3339 format
-// - {{language}} -> user language name (replaced if present; empty string if not set in ChatManage)
-func renderSystemPromptPlaceholders(prompt string, language ...string) string {
- vals := types.PlaceholderValues{}
- if len(language) > 0 {
- vals["language"] = language[0]
- }
- return types.RenderPromptPlaceholders(prompt, vals)
-}
-
// ---------------------------------------------------------------------------
// Concurrency utilities
// ---------------------------------------------------------------------------
diff --git a/internal/application/service/chat_pipeline/data_analysis.go b/internal/application/service/chat_pipeline/data_analysis.go
index 0af3fbe21..4bbd06b2f 100644
--- a/internal/application/service/chat_pipeline/data_analysis.go
+++ b/internal/application/service/chat_pipeline/data_analysis.go
@@ -141,8 +141,9 @@ Return your response in the specified JSON format.`, chatManage.Query, knowledge
Score: 1.0,
MatchType: types.MatchTypeDataAnalysis,
KnowledgeID: knowledge.ID,
- KnowledgeTitle: knowledge.Title,
- KnowledgeFilename: knowledge.FileName,
+ KnowledgeTitle: knowledge.Title,
+ KnowledgeFilename: knowledge.FileName,
+ KnowledgeDescription: knowledge.Description,
}
chatManage.MergeResult = append(chatManage.MergeResult, analysisResult)
diff --git a/internal/application/service/chat_pipeline/into_chat_message.go b/internal/application/service/chat_pipeline/into_chat_message.go
index aa252af9a..d991eecad 100644
--- a/internal/application/service/chat_pipeline/into_chat_message.go
+++ b/internal/application/service/chat_pipeline/into_chat_message.go
@@ -8,15 +8,18 @@ import (
"strings"
"github.com/Tencent/WeKnora/internal/types"
+ "github.com/Tencent/WeKnora/internal/types/interfaces"
"github.com/Tencent/WeKnora/internal/utils"
)
// PluginIntoChatMessage handles the transformation of search results into chat messages
-type PluginIntoChatMessage struct{}
+type PluginIntoChatMessage struct {
+ messageService interfaces.MessageService
+}
// NewPluginIntoChatMessage creates and registers a new PluginIntoChatMessage instance
-func NewPluginIntoChatMessage(eventManager *EventManager) *PluginIntoChatMessage {
- res := &PluginIntoChatMessage{}
+func NewPluginIntoChatMessage(eventManager *EventManager, messageService interfaces.MessageService) *PluginIntoChatMessage {
+ res := &PluginIntoChatMessage{messageService: messageService}
eventManager.Register(res)
return res
}
@@ -73,9 +76,9 @@ func (p *PluginIntoChatMessage) OnEvent(ctx context.Context,
return ErrTemplateExecute.WithError(fmt.Errorf("user query contains invalid content"))
}
- // Intent-based no-search path: bypass "reference materials" template entirely.
+ // Intent-based no-search path: no retrieval results, but still render
+ // through the context template so runtime metadata (current_time, etc.) is injected.
if !chatManage.NeedsRetrieval() {
- // Prefer rewritten query in no-search mode; fallback to original query.
userContent := safeQuery
if rewrite := strings.TrimSpace(chatManage.RewriteQuery); rewrite != "" {
if safeRewrite, ok := utils.ValidateInput(rewrite); ok {
@@ -89,16 +92,38 @@ func (p *PluginIntoChatMessage) OnEvent(ctx context.Context,
if chatManage.ImageDescription != "" && !chatManage.ChatModelSupportsVision {
userContent += "\n\n[用户上传图片内容]\n" + chatManage.ImageDescription
}
- chatManage.UserContent = userContent
- pipelineInfo(ctx, "IntoChatMessage", "skip_template_no_search", map[string]interface{}{
+
+ if tpl := chatManage.SummaryConfig.ContextTemplate; tpl != "" {
+ chatManage.UserContent = types.RenderPromptPlaceholders(tpl, types.PlaceholderValues{
+ "query": userContent,
+ "contexts": "",
+ "language": chatManage.Language,
+ })
+ } else {
+ chatManage.UserContent = userContent
+ }
+
+ pipelineInfo(ctx, "IntoChatMessage", "no_search_with_template", map[string]interface{}{
"session_id": chatManage.SessionID,
"user_content_len": len(chatManage.UserContent),
+ "has_template": chatManage.SummaryConfig.ContextTemplate != "",
})
return next()
}
var contextsBuilder strings.Builder
+ // Collect unique document metadata (title + description), once per knowledge
+ allResults := chatManage.MergeResult
+ if chatManage.FAQPriorityEnabled && len(faqResults) > 0 {
+ allResults = append(faqResults, docResults...)
+ }
+ docHeader := buildDocumentHeader(allResults)
+ if docHeader != "" {
+ contextsBuilder.WriteString(docHeader)
+ contextsBuilder.WriteString("\n")
+ }
+
// Build contexts string based on FAQ priority strategy
if chatManage.FAQPriorityEnabled && len(faqResults) > 0 {
// Build structured context with FAQ prioritization
@@ -135,10 +160,12 @@ func (p *PluginIntoChatMessage) OnEvent(ctx context.Context,
}
}
+ chatManage.RenderedContexts = contextsBuilder.String()
+
// Replace placeholders in context template
userContent := types.RenderPromptPlaceholders(chatManage.SummaryConfig.ContextTemplate, types.PlaceholderValues{
"query": safeQuery,
- "contexts": contextsBuilder.String(),
+ "contexts": chatManage.RenderedContexts,
"language": chatManage.Language,
})
@@ -158,9 +185,98 @@ func (p *PluginIntoChatMessage) OnEvent(ctx context.Context,
"image_description": chatManage.ImageDescription,
"chat_model_supports_vision": chatManage.ChatModelSupportsVision,
})
+
+ p.persistRenderedContent(ctx, chatManage)
return next()
}
+// persistRenderedContent asynchronously writes the RAG-augmented UserContent back
+// to the user message so that subsequent conversation turns can see the full
+// retrieval context in history.
+func (p *PluginIntoChatMessage) persistRenderedContent(ctx context.Context, chatManage *types.ChatManage) {
+ if chatManage.UserMessageID == "" || chatManage.UserContent == "" {
+ pipelineInfo(ctx, "IntoChatMessage", "persist_rendered_content_skip", map[string]interface{}{
+ "session_id": chatManage.SessionID,
+ "user_message_id": chatManage.UserMessageID,
+ "has_user_content": chatManage.UserContent != "",
+ "reason": "empty_id_or_content",
+ })
+ return
+ }
+ if chatManage.UserContent == chatManage.Query {
+ return
+ }
+ pipelineInfo(ctx, "IntoChatMessage", "persist_rendered_content", map[string]interface{}{
+ "session_id": chatManage.SessionID,
+ "user_message_id": chatManage.UserMessageID,
+ "rendered_content_len": len(chatManage.UserContent),
+ })
+ bgCtx := context.WithoutCancel(ctx)
+ go func() {
+ if err := p.messageService.UpdateMessageRenderedContent(
+ bgCtx, chatManage.SessionID, chatManage.UserMessageID, chatManage.UserContent,
+ ); err != nil {
+ pipelineWarn(bgCtx, "IntoChatMessage", "persist_rendered_content_error", map[string]interface{}{
+ "session_id": chatManage.SessionID,
+ "user_message_id": chatManage.UserMessageID,
+ "error": err.Error(),
+ })
+ }
+ }()
+}
+
+// buildDocumentHeader generates a document metadata section listing each unique
+// knowledge document (by KnowledgeID) with its title and description.
+// Returns an empty string when no meaningful metadata is available.
+func buildDocumentHeader(results []*types.SearchResult) string {
+ type docMeta struct {
+ title string
+ description string
+ }
+
+ seen := make(map[string]struct{})
+ var docs []docMeta
+
+ for _, r := range results {
+ if r.KnowledgeID == "" {
+ continue
+ }
+ if _, ok := seen[r.KnowledgeID]; ok {
+ continue
+ }
+ seen[r.KnowledgeID] = struct{}{}
+
+ title := r.KnowledgeTitle
+ if title == "" {
+ title = r.KnowledgeFilename
+ }
+ if title == "" {
+ continue
+ }
+
+ docs = append(docs, docMeta{
+ title: title,
+ description: r.KnowledgeDescription,
+ })
+ }
+
+ if len(docs) == 0 {
+ return ""
+ }
+
+ var b strings.Builder
+ b.WriteString("### Referenced Documents\n")
+ for i, d := range docs {
+ // if d.description != "" {
+ // b.WriteString(fmt.Sprintf("%d. %s — %s\n", i+1, d.title, d.description))
+ // } else {
+ // b.WriteString(fmt.Sprintf("%d. %s\n", i+1, d.title))
+ // }
+ b.WriteString(fmt.Sprintf("%d. %s\n", i+1, d.title))
+ }
+ return b.String()
+}
+
// getEnrichedPassageForChat 合并Content和ImageInfo的文本内容,为聊天消息准备
func getEnrichedPassageForChat(ctx context.Context, result *types.SearchResult) string {
// 如果没有图片信息,直接返回内容
diff --git a/internal/application/service/chat_pipeline/plugin_test.go b/internal/application/service/chat_pipeline/plugin_test.go
index 77b08e472..05d1ab4fd 100644
--- a/internal/application/service/chat_pipeline/plugin_test.go
+++ b/internal/application/service/chat_pipeline/plugin_test.go
@@ -18,7 +18,7 @@ func TestIntoChatMessage_NoKBRetrieval(t *testing.T) {
Intent: types.IntentChitchat,
},
}
- plugin := &PluginIntoChatMessage{}
+ plugin := &PluginIntoChatMessage{messageService: nil}
nextCalled := false
err := plugin.OnEvent(context.Background(), types.INTO_CHAT_MESSAGE, cm, func() *PluginError {
nextCalled = true
@@ -50,7 +50,7 @@ func TestIntoChatMessage_WithMergeResults(t *testing.T) {
},
},
}
- plugin := &PluginIntoChatMessage{}
+ plugin := &PluginIntoChatMessage{messageService: nil}
nextCalled := false
err := plugin.OnEvent(context.Background(), types.INTO_CHAT_MESSAGE, cm, func() *PluginError {
nextCalled = true
@@ -84,7 +84,7 @@ func TestIntoChatMessage_ImageDescriptionAppended(t *testing.T) {
ImageDescription: "a cat sitting on a mat",
},
}
- plugin := &PluginIntoChatMessage{}
+ plugin := &PluginIntoChatMessage{messageService: nil}
_ = plugin.OnEvent(context.Background(), types.INTO_CHAT_MESSAGE, cm, func() *PluginError {
return nil
})
diff --git a/internal/application/service/chat_pipeline/query_understand.go b/internal/application/service/chat_pipeline/query_understand.go
index 46b1c9612..04766faa0 100644
--- a/internal/application/service/chat_pipeline/query_understand.go
+++ b/internal/application/service/chat_pipeline/query_understand.go
@@ -109,7 +109,7 @@ func (p *PluginQueryUnderstand) OnEvent(ctx context.Context,
userMsg.Images = chatManage.Images
}
- maxTokens := 60
+ maxTokens := 150
if useImages {
maxTokens = 500
}
diff --git a/internal/application/service/custom_agent.go b/internal/application/service/custom_agent.go
index db0fc941d..b302d495a 100644
--- a/internal/application/service/custom_agent.go
+++ b/internal/application/service/custom_agent.go
@@ -462,7 +462,7 @@ func (s *customAgentService) GetSuggestedQuestions(
// 2. Determine knowledge base scope
effectiveKBIDs := kbIDs
- if len(effectiveKBIDs) == 0 {
+ if len(effectiveKBIDs) == 0 && len(knowledgeIDs) == 0 {
// Use agent's KB configuration
switch agent.Config.KBSelectionMode {
case "all":
@@ -506,7 +506,7 @@ func (s *customAgentService) GetSuggestedQuestions(
// 3. Collect all candidate chunks from both FAQ and Document KBs,
// then sort by updated_at uniformly (not FAQ-first).
type candidate struct {
- question types.SuggestedQuestion
+ question types.SuggestedQuestion
updatedAt time.Time
}
var candidates []candidate
diff --git a/internal/application/service/knowledgebase_search_results.go b/internal/application/service/knowledgebase_search_results.go
index f051e165c..2922fc37b 100644
--- a/internal/application/service/knowledgebase_search_results.go
+++ b/internal/application/service/knowledgebase_search_results.go
@@ -270,9 +270,10 @@ func (s *knowledgeBaseService) buildSearchResult(chunk *types.Chunk,
ChunkType: string(chunk.ChunkType),
ParentChunkID: chunk.ParentChunkID,
ImageInfo: chunk.ImageInfo,
- KnowledgeFilename: knowledge.FileName,
- KnowledgeSource: knowledge.Source,
- KnowledgeChannel: knowledge.Channel,
+ KnowledgeFilename: knowledge.FileName,
+ KnowledgeSource: knowledge.Source,
+ KnowledgeChannel: knowledge.Channel,
+ KnowledgeDescription: knowledge.Description,
ChunkMetadata: chunk.Metadata,
MatchedContent: matchedContent,
KnowledgeBaseID: knowledge.KnowledgeBaseID,
diff --git a/internal/application/service/llmcontext/context_manager.go b/internal/application/service/llmcontext/context_manager.go
index ac1afd98e..42e720e7b 100644
--- a/internal/application/service/llmcontext/context_manager.go
+++ b/internal/application/service/llmcontext/context_manager.go
@@ -124,9 +124,13 @@ func (cm *contextManager) rebuildFromDB(ctx context.Context, sessionID string) (
}
switch msg.Role {
case "user":
- p.query = msg.Content
+ if msg.RenderedContent != "" {
+ p.query = msg.RenderedContent
+ } else {
+ p.query = msg.Content
+ }
p.createdAt = msg.CreatedAt
- if desc := extractImageCaptions(msg.Images); desc != "" {
+ if desc := extractImageCaptions(msg.Images); desc != "" && msg.RenderedContent == "" {
p.query += "\n\n[用户上传图片内容]\n" + desc
}
case "assistant":
diff --git a/internal/application/service/message.go b/internal/application/service/message.go
index 495f8562a..dd1d1c646 100644
--- a/internal/application/service/message.go
+++ b/internal/application/service/message.go
@@ -246,6 +246,11 @@ func (s *messageService) UpdateMessageImages(ctx context.Context, sessionID, mes
return s.messageRepo.UpdateMessageImages(ctx, sessionID, messageID, images)
}
+// UpdateMessageRenderedContent updates the rendered_content column for a user message.
+func (s *messageService) UpdateMessageRenderedContent(ctx context.Context, sessionID, messageID string, renderedContent string) error {
+ return s.messageRepo.UpdateMessageRenderedContent(ctx, sessionID, messageID, renderedContent)
+}
+
// DeleteMessage removes a message from a session, also cleaning up its Knowledge entry in the chat history KB.
func (s *messageService) DeleteMessage(ctx context.Context, sessionID string, messageID string) error {
logger.Info(ctx, "Start deleting message")
diff --git a/internal/application/service/session_qa_helpers.go b/internal/application/service/session_qa_helpers.go
index 1a6266bb2..8715ba88c 100644
--- a/internal/application/service/session_qa_helpers.go
+++ b/internal/application/service/session_qa_helpers.go
@@ -145,9 +145,7 @@ func (s *sessionService) applyAgentOverridesToChatManage(
if customAgent.Config.RerankTopK > 0 {
cm.RerankTopK = customAgent.Config.RerankTopK
}
- if customAgent.Config.RerankThreshold > 0 {
- cm.RerankThreshold = customAgent.Config.RerankThreshold
- }
+ cm.RerankThreshold = customAgent.Config.RerankThreshold
if customAgent.Config.RerankModelID != "" {
cm.RerankModelID = customAgent.Config.RerankModelID
}
diff --git a/internal/config/config.go b/internal/config/config.go
index 868e3358b..df872df51 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -408,8 +408,8 @@ func ValidateConfig(cfg *Config) error {
if cfg.Conversation.VectorThreshold < 0 || cfg.Conversation.VectorThreshold > 1 {
errs = append(errs, "conversation.vector_threshold must be between 0 and 1")
}
- if cfg.Conversation.RerankThreshold < 0 || cfg.Conversation.RerankThreshold > 1 {
- errs = append(errs, "conversation.rerank_threshold must be between 0 and 1")
+ if cfg.Conversation.RerankThreshold < -10 || cfg.Conversation.RerankThreshold > 10 {
+ errs = append(errs, "conversation.rerank_threshold must be between -10 and 10")
}
}
diff --git a/internal/handler/session/agent_stream_handler.go b/internal/handler/session/agent_stream_handler.go
index 780c96db8..25840f71a 100644
--- a/internal/handler/session/agent_stream_handler.go
+++ b/internal/handler/session/agent_stream_handler.go
@@ -232,13 +232,14 @@ func (h *AgentStreamHandler) handleReferences(ctx context.Context, evt event.Eve
} else if refMap, ok := ref.(map[string]interface{}); ok {
// Parse from map if needed
searchResult := &types.SearchResult{
- ID: getString(refMap, "id"),
- Content: getString(refMap, "content"),
- Score: getFloat64(refMap, "score"),
- KnowledgeID: getString(refMap, "knowledge_id"),
- KnowledgeTitle: getString(refMap, "knowledge_title"),
- ChunkIndex: int(getFloat64(refMap, "chunk_index")),
- KnowledgeBaseID: getString(refMap, "knowledge_base_id"),
+ ID: getString(refMap, "id"),
+ Content: getString(refMap, "content"),
+ Score: getFloat64(refMap, "score"),
+ KnowledgeID: getString(refMap, "knowledge_id"),
+ KnowledgeTitle: getString(refMap, "knowledge_title"),
+ ChunkIndex: int(getFloat64(refMap, "chunk_index")),
+ KnowledgeDescription: getString(refMap, "knowledge_description"),
+ KnowledgeBaseID: getString(refMap, "knowledge_base_id"),
}
if meta, ok := refMap["metadata"].(map[string]interface{}); ok {
diff --git a/internal/handler/session/helpers.go b/internal/handler/session/helpers.go
index d6acd02b0..a2afb17b3 100644
--- a/internal/handler/session/helpers.go
+++ b/internal/handler/session/helpers.go
@@ -124,9 +124,10 @@ func buildStreamResponse(evt interfaces.StreamEvent, requestID string) *types.St
ChunkType: getString(refMap, "chunk_type"),
ParentChunkID: getString(refMap, "parent_chunk_id"),
ImageInfo: getString(refMap, "image_info"),
- KnowledgeFilename: getString(refMap, "knowledge_filename"),
- KnowledgeSource: getString(refMap, "knowledge_source"),
- KnowledgeBaseID: getString(refMap, "knowledge_base_id"),
+ KnowledgeFilename: getString(refMap, "knowledge_filename"),
+ KnowledgeSource: getString(refMap, "knowledge_source"),
+ KnowledgeDescription: getString(refMap, "knowledge_description"),
+ KnowledgeBaseID: getString(refMap, "knowledge_base_id"),
}
searchResults = append(searchResults, sr)
}
diff --git a/internal/handler/tenant.go b/internal/handler/tenant.go
index 0364fccca..fdeadd800 100644
--- a/internal/handler/tenant.go
+++ b/internal/handler/tenant.go
@@ -904,8 +904,8 @@ func validateConversationConfig(req *types.ConversationConfig) error {
if req.RerankTopK <= 0 {
return errors.NewBadRequestError("rerank_top_k must be greater than 0")
}
- if req.RerankThreshold < 0 || req.RerankThreshold > 1 {
- return errors.NewBadRequestError("rerank_threshold must be between 0 and 1")
+ if req.RerankThreshold < -10 || req.RerankThreshold > 10 {
+ return errors.NewBadRequestError("rerank_threshold must be between -10 and 10")
}
if req.Temperature < 0 || req.Temperature > 2 {
return errors.NewBadRequestError("temperature must be between 0 and 2")
@@ -1176,8 +1176,8 @@ func (h *TenantHandler) updateTenantRetrievalConfigInternal(c *gin.Context) {
c.Error(errors.NewBadRequestError("keyword_threshold must be between 0 and 1"))
return
}
- if cfg.RerankThreshold < 0 || cfg.RerankThreshold > 1 {
- c.Error(errors.NewBadRequestError("rerank_threshold must be between 0 and 1"))
+ if cfg.RerankThreshold < -10 || cfg.RerankThreshold > 10 {
+ c.Error(errors.NewBadRequestError("rerank_threshold must be between -10 and 10"))
return
}
if cfg.EmbeddingTopK < 0 || cfg.EmbeddingTopK > 200 {
diff --git a/internal/middleware/logger.go b/internal/middleware/logger.go
index fc3234f56..147b13f9c 100644
--- a/internal/middleware/logger.go
+++ b/internal/middleware/logger.go
@@ -26,8 +26,16 @@ type loggerResponseBodyWriter struct {
}
// Write 重写Write方法,同时写入buffer和原始writer
+// 限制buffer大小,避免SSE等流式响应导致内存无限增长
func (r loggerResponseBodyWriter) Write(b []byte) (int, error) {
- r.body.Write(b)
+ if r.body.Len() < maxBodySize {
+ remaining := maxBodySize - r.body.Len()
+ if len(b) <= remaining {
+ r.body.Write(b)
+ } else {
+ r.body.Write(b[:remaining])
+ }
+ }
return r.ResponseWriter.Write(b)
}
@@ -182,12 +190,13 @@ func Logger() gin.HandlerFunc {
// 读取响应体
responseBodyStr := ""
if responseBody.Len() > 0 {
- // 检查Content-Type,只记录JSON类型
contentType := c.Writer.Header().Get("Content-Type")
- if strings.Contains(contentType, "application/json") ||
+ if strings.Contains(contentType, "text/event-stream") {
+ responseBodyStr = "[SSE流式响应,已跳过]"
+ } else if strings.Contains(contentType, "application/json") ||
strings.Contains(contentType, "text/") {
bodyBytes := responseBody.Bytes()
- if len(bodyBytes) > maxBodySize {
+ if len(bodyBytes) >= maxBodySize {
responseBodyStr = string(bodyBytes[:maxBodySize]) + "... [内容过长,已截断]"
} else {
responseBodyStr = string(bodyBytes)
diff --git a/internal/models/chat/ollama.go b/internal/models/chat/ollama.go
index 0e00b0596..28d739454 100644
--- a/internal/models/chat/ollama.go
+++ b/internal/models/chat/ollama.go
@@ -156,14 +156,17 @@ func (c *OllamaChat) Chat(ctx context.Context, messages []Message, opts *ChatOpt
return nil, fmt.Errorf("聊天请求失败: %w", err)
}
- // 构建响应
+ totalTokens := promptTokens + completionTokens
+ logger.Infof(ctx, "[LLM Usage] model=%s, prompt_tokens=%d, completion_tokens=%d, total_tokens=%d",
+ c.modelName, promptTokens, completionTokens, totalTokens)
+
return &types.ChatResponse{
Content: responseContent,
ToolCalls: toolCalls,
Usage: types.TokenUsage{
PromptTokens: promptTokens,
CompletionTokens: completionTokens,
- TotalTokens: promptTokens + completionTokens,
+ TotalTokens: totalTokens,
},
}, nil
}
@@ -275,6 +278,8 @@ func (c *OllamaChat) ChatStream(
CompletionTokens: resp.EvalCount,
TotalTokens: resp.PromptEvalCount + resp.EvalCount,
}
+ logger.Infof(ctx, "[LLM Usage] model=%s, prompt_tokens=%d, completion_tokens=%d, total_tokens=%d",
+ c.modelName, usage.PromptTokens, usage.CompletionTokens, usage.TotalTokens)
}
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeAnswer,
diff --git a/internal/models/chat/remote_api.go b/internal/models/chat/remote_api.go
index 2a7316073..829c8b120 100644
--- a/internal/models/chat/remote_api.go
+++ b/internal/models/chat/remote_api.go
@@ -287,7 +287,13 @@ func (c *RemoteAPIChat) Chat(ctx context.Context, messages []Message, opts *Chat
}
}
- return c.parseCompletionResponse(&resp)
+ result, err := c.parseCompletionResponse(&resp)
+ if err != nil {
+ return nil, err
+ }
+ logger.Infof(ctx, "[LLM Usage] model=%s, prompt_tokens=%d, completion_tokens=%d, total_tokens=%d",
+ c.modelName, result.Usage.PromptTokens, result.Usage.CompletionTokens, result.Usage.TotalTokens)
+ return result, nil
}
// chatWithRawHTTP 使用原始 HTTP 请求进行聊天(供自定义请求使用)
@@ -297,7 +303,7 @@ func (c *RemoteAPIChat) chatWithRawHTTP(ctx context.Context, endpoint string, cu
return nil, fmt.Errorf("marshal request: %w", err)
}
- logger.Infof(ctx, "[LLM Request] model=%s, raw HTTP request:\n%s", c.modelName, string(jsonData))
+ logger.Infof(ctx, "[LLM Request] model=%s, raw HTTP request:\n%s", c.modelName, secutils.CompactImageDataURLForLog(string(jsonData)))
if endpoint == "" {
endpoint = c.baseURL + "/chat/completions"
}
@@ -328,7 +334,13 @@ func (c *RemoteAPIChat) chatWithRawHTTP(ctx context.Context, endpoint string, cu
return nil, fmt.Errorf("decode response: %w", err)
}
- return c.parseCompletionResponse(&chatResp)
+ result, err := c.parseCompletionResponse(&chatResp)
+ if err != nil {
+ return nil, err
+ }
+ logger.Infof(ctx, "[LLM Usage] model=%s, prompt_tokens=%d, completion_tokens=%d, total_tokens=%d",
+ c.modelName, result.Usage.PromptTokens, result.Usage.CompletionTokens, result.Usage.TotalTokens)
+ return result, nil
}
// parseCompletionResponse 解析非流式响应
@@ -488,6 +500,10 @@ func (c *RemoteAPIChat) processStream(ctx context.Context, stream *openai.ChatCo
response, err := stream.Recv()
if err != nil {
if err == io.EOF {
+ if state.usage != nil {
+ logger.Infof(ctx, "[LLM Usage] model=%s, prompt_tokens=%d, completion_tokens=%d, total_tokens=%d",
+ c.modelName, state.usage.PromptTokens, state.usage.CompletionTokens, state.usage.TotalTokens)
+ }
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeAnswer,
Content: "",
@@ -546,6 +562,10 @@ func (c *RemoteAPIChat) processRawHTTPStream(ctx context.Context, resp *http.Res
}
if event.Done {
+ if state.usage != nil {
+ logger.Infof(ctx, "[LLM Usage] model=%s, prompt_tokens=%d, completion_tokens=%d, total_tokens=%d",
+ c.modelName, state.usage.PromptTokens, state.usage.CompletionTokens, state.usage.TotalTokens)
+ }
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeAnswer,
Content: "",
diff --git a/internal/types/chat_manage.go b/internal/types/chat_manage.go
index 39bcc70a2..cf33704f1 100644
--- a/internal/types/chat_manage.go
+++ b/internal/types/chat_manage.go
@@ -93,6 +93,7 @@ type PipelineState struct {
EntityKnowledge map[string]string `json:"-"`
GraphResult *GraphData `json:"-"`
UserContent string `json:"-"`
+ RenderedContexts string `json:"-"`
ChatResponse *ChatResponse `json:"-"`
ImageDescription string `json:"-"`
SystemPromptOverride string `json:"-"`
@@ -189,6 +190,7 @@ func (c *ChatManage) Clone() *ChatManage {
Intent: c.Intent,
ImageDescription: c.ImageDescription,
SystemPromptOverride: c.SystemPromptOverride,
+ RenderedContexts: c.RenderedContexts,
},
}
}
diff --git a/internal/types/custom_agent.go b/internal/types/custom_agent.go
index 39182cacd..7b61a5c76 100644
--- a/internal/types/custom_agent.go
+++ b/internal/types/custom_agent.go
@@ -238,9 +238,6 @@ func (a *CustomAgent) EnsureDefaults() {
if a.Config.RerankTopK == 0 {
a.Config.RerankTopK = 5
}
- if a.Config.RerankThreshold == 0 {
- a.Config.RerankThreshold = 0.5
- }
// Advanced settings defaults
if a.Config.FallbackStrategy == "" {
a.Config.FallbackStrategy = "model"
diff --git a/internal/types/interfaces/message.go b/internal/types/interfaces/message.go
index 673c268b1..310d3c018 100644
--- a/internal/types/interfaces/message.go
+++ b/internal/types/interfaces/message.go
@@ -32,6 +32,9 @@ type MessageService interface {
// UpdateMessageImages updates only the images JSONB column for a message.
UpdateMessageImages(ctx context.Context, sessionID, messageID string, images types.MessageImages) error
+ // UpdateMessageRenderedContent updates the rendered_content column for a user message.
+ UpdateMessageRenderedContent(ctx context.Context, sessionID, messageID string, renderedContent string) error
+
// DeleteMessage deletes a message
DeleteMessage(ctx context.Context, sessionID string, id string) error
@@ -74,6 +77,8 @@ type MessageRepository interface {
UpdateMessage(ctx context.Context, message *types.Message) error
// UpdateMessageImages updates only the images JSONB column for a message
UpdateMessageImages(ctx context.Context, sessionID, messageID string, images types.MessageImages) error
+ // UpdateMessageRenderedContent updates the rendered_content column for a user message
+ UpdateMessageRenderedContent(ctx context.Context, sessionID, messageID string, renderedContent string) error
// DeleteMessage deletes a message
DeleteMessage(ctx context.Context, sessionID string, id string) error
// DeleteMessagesBySessionID deletes all messages belonging to a session
diff --git a/internal/types/message.go b/internal/types/message.go
index ba452497b..a4919efc6 100644
--- a/internal/types/message.go
+++ b/internal/types/message.go
@@ -125,8 +125,12 @@ type Message struct {
IsFallback bool `json:"is_fallback,omitempty"`
// Agent total execution duration in milliseconds (from query start to answer start)
AgentDurationMs int64 `json:"agent_duration_ms,omitempty" gorm:"column:agent_duration_ms;default:0"`
+ // RenderedContent stores the full RAG-augmented user message (with retrieved context)
+ // sent to the LLM. Used to preserve retrieval context across conversation turns.
+ // Empty for non-retrieval intents or assistant messages.
+ RenderedContent string `json:"-" gorm:"type:text;column:rendered_content;default:''"`
// Channel indicates the source channel of this message (e.g., "web", "api", "im")
- Channel string `json:"channel,omitempty" gorm:"type:varchar(50);default:''"`
+ Channel string `json:"channel,omitempty" gorm:"type:varchar(50);default:''"`
// KnowledgeID links this message to a Knowledge entry in the chat history knowledge base
// Used for vector search indexing: when set, the message content has been indexed as a Knowledge passage
KnowledgeID string `json:"knowledge_id,omitempty" gorm:"type:varchar(36);index"`
diff --git a/internal/types/retrieval_config.go b/internal/types/retrieval_config.go
index 6a275d8ef..5e9a837cd 100644
--- a/internal/types/retrieval_config.go
+++ b/internal/types/retrieval_config.go
@@ -20,7 +20,7 @@ type RetrievalConfig struct {
KeywordThreshold float64 `json:"keyword_threshold"`
// RerankTopK is the maximum number of results after reranking (default: 10)
RerankTopK int `json:"rerank_top_k"`
- // RerankThreshold is the minimum rerank score (0-1, default: 0.2)
+ // RerankThreshold is the minimum rerank score (-10 to 10, default: 0.2)
RerankThreshold float64 `json:"rerank_threshold"`
// RerankModelID is the ID of the rerank model to use (required for search)
RerankModelID string `json:"rerank_model_id"`
@@ -60,7 +60,7 @@ func (c *RetrievalConfig) GetEffectiveRerankTopK() int {
// GetEffectiveRerankThreshold returns RerankThreshold with a fallback default.
func (c *RetrievalConfig) GetEffectiveRerankThreshold() float64 {
- if c == nil || c.RerankThreshold <= 0 {
+ if c == nil {
return 0.2
}
return c.RerankThreshold
diff --git a/internal/types/search.go b/internal/types/search.go
index 8bce0a2b8..374a090eb 100644
--- a/internal/types/search.go
+++ b/internal/types/search.go
@@ -130,6 +130,9 @@ type SearchResult struct {
// For FAQ: this is the matched question text (standard or similar question)
MatchedContent string `json:"matched_content,omitempty"`
+ // KnowledgeDescription is the description of the knowledge document
+ KnowledgeDescription string `json:"knowledge_description,omitempty"`
+
// KnowledgeBaseID is the ID of the knowledge base this result belongs to
KnowledgeBaseID string `json:"knowledge_base_id,omitempty"`
}
diff --git a/migrations/sqlite/000000_init.up.sql b/migrations/sqlite/000000_init.up.sql
index 4ce295bb3..0b1e6f490 100644
--- a/migrations/sqlite/000000_init.up.sql
+++ b/migrations/sqlite/000000_init.up.sql
@@ -138,6 +138,7 @@ CREATE TABLE IF NOT EXISTS messages (
session_id VARCHAR(36) NOT NULL,
role VARCHAR(50) NOT NULL,
content TEXT NOT NULL,
+ rendered_content TEXT NOT NULL DEFAULT '',
knowledge_references TEXT NOT NULL DEFAULT '[]',
agent_steps TEXT DEFAULT NULL,
mentioned_items TEXT DEFAULT '[]',
diff --git a/migrations/versioned/000027_message_rendered_content.down.sql b/migrations/versioned/000027_message_rendered_content.down.sql
new file mode 100644
index 000000000..a20d6e173
--- /dev/null
+++ b/migrations/versioned/000027_message_rendered_content.down.sql
@@ -0,0 +1,2 @@
+-- Rollback: 000027_message_rendered_content
+ALTER TABLE messages DROP COLUMN IF EXISTS rendered_content;
diff --git a/migrations/versioned/000027_message_rendered_content.up.sql b/migrations/versioned/000027_message_rendered_content.up.sql
new file mode 100644
index 000000000..202f42018
--- /dev/null
+++ b/migrations/versioned/000027_message_rendered_content.up.sql
@@ -0,0 +1,8 @@
+-- Migration: 000027_message_rendered_content
+-- Description: Add rendered_content column to messages to persist RAG-augmented user content for multi-turn context
+DO $$ BEGIN RAISE NOTICE '[Migration 000027] Adding rendered_content column to messages'; END $$;
+
+ALTER TABLE messages ADD COLUMN IF NOT EXISTS rendered_content TEXT NOT NULL DEFAULT '';
+COMMENT ON COLUMN messages.rendered_content IS 'Full RAG-augmented user message sent to LLM, preserving retrieval context across turns';
+
+DO $$ BEGIN RAISE NOTICE '[Migration 000027] rendered_content column added to messages successfully'; END $$;