Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

feat(chat): self-check contradictory prompt/dataset settings (#5703) (#18274)

peewee9293b38808e605
diff --git a/web/src/locales/en.ts b/web/src/locales/en.ts
index 127da738ba..fb5f07960e 100644
--- a/web/src/locales/en.ts
+++ b/web/src/locales/en.ts
@@ -1060,6 +1060,8 @@ This auto-tagging feature enhances retrieval by adding another layer of domain-s
- **When information is unavailable**: Your response must contain this exact sentence: "The answer you are looking for is not found in the dataset!"
- **Always consider** the entire conversation history.`,
systemMessage: 'Please input!',
+ knowledgePlaceholderMessage:
+ 'Datasets are selected but the {knowledge} placeholder is missing from the system prompt, so retrieved content cannot be injected. Add {knowledge} to the system prompt or remove the datasets.',
systemTip:
'Your prompts or instructions for the LLM, including but not limited to its role, the desired length, tone, and language of its answers. If your model has native support for reasoning, you can add //no_thinking add the prompt to stop reasoning.',
topN: 'Top N',
diff --git a/web/src/locales/zh.ts b/web/src/locales/zh.ts
index 08d2778a14..316e64db60 100644
--- a/web/src/locales/zh.ts
+++ b/web/src/locales/zh.ts
@@ -954,6 +954,8 @@ NER:使用 spaCy NER 和基于规则的关键词提取来抽取 Entities 和 R
{knowledge}
以上是知识库。`,
systemMessage: '请输入',
+ knowledgePlaceholderMessage:
+ '已选择知识库但系统提示词中缺少 {knowledge} 占位符,检索到的内容将无法注入。请在系统提示词中添加 {knowledge},或移除已选择的知识库。',
systemTip:
'当LLM回答问题时,你需要LLM遵循的说明,比如角色设计、答案长度和答案语言等。如果您的模型原生支持在问答中推理,可以通过 //no_thinking 关闭自动推理。',
topN: 'Top N',
diff --git a/web/src/pages/next-chats/chat/app-settings/use-chat-setting-schema.tsx b/web/src/pages/next-chats/chat/app-settings/use-chat-setting-schema.tsx
index b0ec1eeeeb..cfc1c34157 100644
--- a/web/src/pages/next-chats/chat/app-settings/use-chat-setting-schema.tsx
+++ b/web/src/pages/next-chats/chat/app-settings/use-chat-setting-schema.tsx
@@ -11,7 +11,8 @@ import {
import { topnSchema } from '@/components/top-n-item';
import { WebSearchProvider } from '@/constants/chat';
import { useTranslate } from '@/hooks/common-hooks';
-import { z } from 'zod';
+import { z, ZodIssueCode } from 'zod';
+import { chatPromptKbIssues } from './validate-chat-prompt';
export function useChatSettingSchema(staleDatasetIds: Set<string>) {
const { t } = useTranslate('chat');
@@ -47,23 +48,33 @@ export function useChatSettingSchema(staleDatasetIds: Set<string>) {
.optional(),
});
- const formSchema = z.object({
- name: z.string().min(1, { message: t('assistantNameMessage') }),
- icon: z.string(),
- description: z.string().optional(),
- dataset_ids: z.array(z.string()).min(0, {
- message: t('knowledgeBasesMessage'),
- }),
- prompt_config: promptConfigSchema,
- ...rerankFormSchema,
- llm_setting: z.object(LlmSettingFieldSchema),
- ...LlmSettingEnabledSchema,
- llm_id: z.string().optional(),
- ...vectorSimilarityWeightSchema,
- ...similarityThresholdSchema,
- ...topnSchema,
- ...MetadataFilterSchema,
- });
+ const formSchema = z
+ .object({
+ name: z.string().min(1, { message: t('assistantNameMessage') }),
+ icon: z.string(),
+ description: z.string().optional(),
+ dataset_ids: z.array(z.string()).min(0, {
+ message: t('knowledgeBasesMessage'),
+ }),
+ prompt_config: promptConfigSchema,
+ ...rerankFormSchema,
+ llm_setting: z.object(LlmSettingFieldSchema),
+ ...LlmSettingEnabledSchema,
+ llm_id: z.string().optional(),
+ ...vectorSimilarityWeightSchema,
+ ...similarityThresholdSchema,
+ ...topnSchema,
+ ...MetadataFilterSchema,
+ })
+ .superRefine((value, ctx) => {
+ for (const issue of chatPromptKbIssues(value, t)) {
+ ctx.addIssue({
+ code: ZodIssueCode.custom,
+ path: issue.path,
+ message: issue.message,
+ });
+ }
+ });
// A persisted dataset_ids value may reference datasets that have since been
// deleted or emptied of chunks — those stale ids are flagged here.
diff --git a/web/src/pages/next-chats/chat/app-settings/validate-chat-prompt.test.ts b/web/src/pages/next-chats/chat/app-settings/validate-chat-prompt.test.ts
new file mode 100644
index 0000000000..f7c2cf85a4
--- /dev/null
+++ b/web/src/pages/next-chats/chat/app-settings/validate-chat-prompt.test.ts
@@ -0,0 +1,79 @@
+import { chatPromptKbIssues } from './validate-chat-prompt';
+
+const t = (key: string) => key;
+
+describe('chatPromptKbIssues', () => {
+ it('flags a system prompt missing {knowledge} when datasets are selected', () => {
+ const issues = chatPromptKbIssues(
+ { dataset_ids: ['kb1'], prompt_config: { system: 'answer the question' } },
+ t,
+ );
+ expect(issues).toContainEqual({
+ path: ['prompt_config', 'system'],
+ message: 'knowledgePlaceholderMessage',
+ });
+ });
+
+ it('does not flag when {knowledge} is present in the system prompt', () => {
+ const issues = chatPromptKbIssues(
+ {
+ dataset_ids: ['kb1'],
+ prompt_config: { system: 'answer using {knowledge}' },
+ },
+ t,
+ );
+ expect(issues).toEqual([]);
+ });
+
+ it('does not flag an empty system prompt (min(1) covers that instead)', () => {
+ const issues = chatPromptKbIssues(
+ { dataset_ids: ['kb1'], prompt_config: { system: '' } },
+ t,
+ );
+ expect(issues).toEqual([]);
+ });
+
+ it('flags a non-empty empty_response when no dataset is selected', () => {
+ const issues = chatPromptKbIssues(
+ {
+ dataset_ids: [],
+ prompt_config: { system: '{knowledge}', empty_response: 'nothing found' },
+ },
+ t,
+ );
+ expect(issues).toContainEqual({
+ path: ['prompt_config', 'empty_response'],
+ message: 'emptyResponseMessage',
+ });
+ });
+
+ it('does not flag an empty empty_response when no dataset is selected', () => {
+ const issues = chatPromptKbIssues(
+ { dataset_ids: [], prompt_config: { system: 'x', empty_response: '' } },
+ t,
+ );
+ expect(issues).toEqual([]);
+ });
+
+ it('does not flag empty_response when datasets are selected', () => {
+ const issues = chatPromptKbIssues(
+ {
+ dataset_ids: ['kb1'],
+ prompt_config: { system: '{knowledge}', empty_response: 'none' },
+ },
+ t,
+ );
+ expect(issues).toEqual([]);
+ });
+
+ it('ignores a whitespace-only empty_response', () => {
+ const issues = chatPromptKbIssues(
+ {
+ dataset_ids: [],
+ prompt_config: { system: 'x', empty_response: ' ' },
+ },
+ t,
+ );
+ expect(issues).toEqual([]);
+ });
+});
diff --git a/web/src/pages/next-chats/chat/app-settings/validate-chat-prompt.ts b/web/src/pages/next-chats/chat/app-settings/validate-chat-prompt.ts
new file mode 100644
index 0000000000..0bff08c5d7
--- /dev/null
+++ b/web/src/pages/next-chats/chat/app-settings/validate-chat-prompt.ts
@@ -0,0 +1,42 @@
+export type ChatPromptKbIssue = {
+ path: string[];
+ message: string;
+};
+
+export type ChatPromptKbValue = {
+ dataset_ids?: string[];
+ prompt_config?: {
+ system?: string;
+ empty_response?: string;
+ };
+};
+
+export function chatPromptKbIssues(
+ value: ChatPromptKbValue,
+ t: (key: string) => string,
+): ChatPromptKbIssue[] {
+ const system = value?.prompt_config?.system ?? '';
+ const emptyResponse = value?.prompt_config?.empty_response ?? '';
+ const datasetIds = value?.dataset_ids ?? [];
+ const issues: ChatPromptKbIssue[] = [];
+
+ if (
+ datasetIds.length > 0 &&
+ system.trim() !== '' &&
+ !system.includes('{knowledge}')
+ ) {
+ issues.push({
+ path: ['prompt_config', 'system'],
+ message: t('knowledgePlaceholderMessage'),
+ });
+ }
+
+ if (datasetIds.length === 0 && emptyResponse.trim() !== '') {
+ issues.push({
+ path: ['prompt_config', 'empty_response'],
+ message: t('emptyResponseMessage'),
+ });
+ }
+
+ return issues;
+}