diff --git a/backend/open_webui/config.py b/backend/open_webui/config.py index d016652feb..3045e4e619 100644 --- a/backend/open_webui/config.py +++ b/backend/open_webui/config.py @@ -1075,26 +1075,11 @@ CHUNK_MIN_SIZE_TARGET = int(os.getenv('CHUNK_MIN_SIZE_TARGET', '0')) CHUNK_OVERLAP = int(os.getenv('CHUNK_OVERLAP', '100')) -DEFAULT_RAG_TEMPLATE = """### Task: -Respond to the user query using the provided context, incorporating inline citations in the format [id] **only when the tag includes an explicit id attribute** (e.g., ). +DEFAULT_RAG_TEMPLATE = """Answer the user's message using the sources inside below. They were retrieved automatically for this conversation and may be incomplete or irrelevant. -### Guidelines: -- If you don't know the answer, clearly state that. -- If uncertain, ask the user for clarification. -- Respond in the same language as the user's query. -- If the context is unreadable or of poor quality, inform the user and provide the best possible answer. -- If the answer isn't present in the context but you possess the knowledge, explain this to the user and provide the answer using your own understanding. -- **Only include inline citations using [id] (e.g., [1], [2]) when the tag includes an id attribute.** -- Do not cite if the tag does not contain an id attribute. -- Do not use XML tags in your response. -- Ensure citations are concise and directly related to the information provided. +Cite a source directly after each statement it supports, using its id in square brackets: "The warranty covers parts for two years [1]." For several sources, write [1][2]. Use only ids that appear in the context. A tag with nothing inside it labels the material just above it. -### Example of Citation: -If the user asks about a specific topic and the information is found in a source with a provided id attribute, the response should include the citation like in the following example: -* "According to the study, the proposed method increases efficiency by 20% [1]." - -### Output: -Provide a clear and direct response to the user's query, including inline citations in the format [id] only when the tag with id attribute is present in the context. +If the sources don't contain the answer, say so, then answer from your own knowledge if you can. If the context is unrelated to the message, ignore it. Reply in the user's language, and don't reproduce the or tags. {{CONTEXT}} diff --git a/src/lib/components/chat/Messages/Citations.svelte b/src/lib/components/chat/Messages/Citations.svelte index 137a0ffb7d..59ce3e917d 100644 --- a/src/lib/components/chat/Messages/Citations.svelte +++ b/src/lib/components/chat/Messages/Citations.svelte @@ -42,6 +42,32 @@ if (citations[index]) { console.log('Showing citation modal for:', citations[index]); + // A cited document whose layout the retrieved text loses opens beside the chat, + // at the cited page when one is known. Plain text files read the same in the popup. + const metadata = citations[index].metadata?.[0]; + const fileName = String(metadata?.name ?? citations[index].source?.name ?? ''); + if (!readOnly && metadata?.file_id && /\.(pdf|docx|pptx|xlsx|xls)$/i.test(fileName)) { + showTabs.set(true); + showEmbeds.set(true); + embed.set({ + file: { + id: metadata.file_id, + page: Number.isInteger(metadata.page) ? metadata.page + 1 : null, + // The cited passages of this file, each with its page + passages: (citations[index].document ?? []) + .map((text, i) => ({ text, metadata: citations[index].metadata?.[i] })) + .filter((passage) => passage.metadata?.file_id === metadata.file_id) + .map(({ text, metadata }) => ({ + text, + page: Number.isInteger(metadata?.page) ? metadata.page + 1 : null + })) + }, + title: fileName, + chatId: chatId + }); + return; + } + if (citations[index]?.source?.embed_url) { const embedUrl = citations[index].source.embed_url; // The embed panel renders anything it cannot read as a URL as raw HTML diff --git a/src/lib/components/chat/Messages/Citations/CitationModal.svelte b/src/lib/components/chat/Messages/Citations/CitationModal.svelte index 0785c4a348..4c89dd874e 100644 --- a/src/lib/components/chat/Messages/Citations/CitationModal.svelte +++ b/src/lib/components/chat/Messages/Citations/CitationModal.svelte @@ -10,7 +10,6 @@ import XMark from '$lib/components/icons/XMark.svelte'; import Textarea from '$lib/components/common/Textarea.svelte'; - import ToolOutputFile from '$lib/components/chat/Messages/ToolOutputFile.svelte'; const i18n = getContext('i18n'); @@ -58,21 +57,6 @@ } } - // The cited file as a display_file card, opened at the cited page. - const getFilePreviewItem = (doc: any) => { - const { file_id, name, page } = doc?.metadata ?? {}; - const fileName = String(name ?? doc?.source?.name ?? ''); - if (!file_id || !fileName.toLowerCase().endsWith('.pdf')) return null; - return { - type: 'file', - source: 'open_webui', - path: `/api/v1/files/${file_id}/content`, - name: fileName, - content_type: 'application/pdf', - ...(Number.isInteger(page) ? { page: page + 1 } : {}) - }; - }; - const decodeString = (str: string) => { try { return decodeURIComponent(str); @@ -159,7 +143,7 @@
{#each mergedDocuments as document, documentIdx}
@@ -229,11 +213,6 @@ {/if}
- {#if getFilePreviewItem(document)} - - - {/if} - {#if document.metadata?.html}