From 386c976e9a0454f1e50ad9de1aea288a2b2ac102 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Thu, 21 Nov 2024 17:58:29 -0800 Subject: [PATCH] enh: inline citations --- backend/open_webui/config.py | 29 ++++++++++++------- backend/open_webui/main.py | 8 ++++- .../components/chat/Messages/Citations.svelte | 1 + .../chat/Messages/ContentRenderer.svelte | 4 +++ .../components/chat/Messages/Markdown.svelte | 6 +++- .../Markdown/MarkdownInlineTokens.svelte | 4 +++ .../Messages/Markdown/MarkdownTokens.svelte | 19 +++++++++--- .../chat/Messages/Markdown/Source.svelte | 23 +++++++++++++++ .../chat/Messages/ResponseMessage.svelte | 9 ++++++ .../chat/Messages/UserMessage.svelte | 6 +--- src/lib/utils/index.ts | 15 +++++++++- 11 files changed, 102 insertions(+), 22 deletions(-) create mode 100644 src/lib/components/chat/Messages/Markdown/Source.svelte diff --git a/backend/open_webui/config.py b/backend/open_webui/config.py index 4dc73ed89..57b9fbcfd 100644 --- a/backend/open_webui/config.py +++ b/backend/open_webui/config.py @@ -1181,21 +1181,30 @@ CHUNK_OVERLAP = PersistentConfig( int(os.environ.get("CHUNK_OVERLAP", "100")), ) -DEFAULT_RAG_TEMPLATE = """You are given a user query, some textual context and rules, all inside xml tags. You have to answer the query based on the context while respecting the rules. +DEFAULT_RAG_TEMPLATE = """### Task: +Respond to the user query using the provided context, incorporating inline citations in the format [source_id]. + +### 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. +- Include inline citations using [source_id] corresponding to the sources listed in the context. +- Do not use XML tags in your response. +- Ensure citations are concise and directly related to the information provided. + +### Example of Citation: +If the user asks about a specific topic and the information is found in "whitepaper.pdf", the response should include the citation like so: +* "According to the study, the proposed method increases efficiency by 20% [whitepaper.pdf]." + +### Output: +Provide a clear and direct response to the user's query, including inline citations in the format [source_id] where relevant. {{CONTEXT}} - -- If you don't know, just say so. -- If you are not sure, ask for clarification. -- Answer in the same language as the user query. -- If the context appears unreadable or of poor quality, tell the user then answer as best as you can. -- If the answer is not in the context but you think you know the answer, explain that to the user then answer with your own knowledge. -- Answer directly and without using xml tags. - - {{QUERY}} diff --git a/backend/open_webui/main.py b/backend/open_webui/main.py index 538d8519a..0279cee05 100644 --- a/backend/open_webui/main.py +++ b/backend/open_webui/main.py @@ -679,7 +679,13 @@ class ChatCompletionMiddleware(BaseHTTPMiddleware): # If context is not empty, insert it into the messages if len(contexts) > 0: - context_string = "/n".join(contexts).strip() + context_string = "" + for context_idx, context in enumerate(contexts): + print(context) + source_id = citations[context_idx].get("source", {}).get("name", "") + context_string += f"{source_id}{context}\n" + + context_string = context_string.strip() prompt = get_last_user_message(body["messages"]) if prompt is None: diff --git a/src/lib/components/chat/Messages/Citations.svelte b/src/lib/components/chat/Messages/Citations.svelte index 79f8d2af6..69feb4e6c 100644 --- a/src/lib/components/chat/Messages/Citations.svelte +++ b/src/lib/components/chat/Messages/Citations.svelte @@ -94,6 +94,7 @@
{#each _citations as citation, idx}