From 907cf61da77c2f686710efc818b791d1dcc6be47 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sun, 24 Nov 2024 18:03:58 -0800 Subject: [PATCH] refac: query generation --- backend/open_webui/config.py | 5 +++-- src/lib/apis/index.ts | 9 +++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/backend/open_webui/config.py b/backend/open_webui/config.py index 3b12b2f94..8c185d146 100644 --- a/backend/open_webui/config.py +++ b/backend/open_webui/config.py @@ -972,7 +972,7 @@ DEFAULT_QUERY_GENERATION_PROMPT_TEMPLATE = """### Task: Based on the chat history, determine whether a search is necessary, and if so, generate a 1-3 broad search queries to retrieve comprehensive and updated information. If no search is required, return an empty list. ### Guidelines: -- Respond exclusively with a JSON object. +- Respond **EXCLUSIVELY** with a JSON object. Any form of extra commentary, explanation, or additional text is prohibited. - If a search query is needed, return an object like: { "queries": ["query1", "query2"] } where each query is distinct and concise. - If no search query is necessary, output should be: { "queries": [] } - Default to suggesting a search query to ensure accurate and updated information, unless it is definitively clear no search is required. @@ -981,7 +981,8 @@ Based on the chat history, determine whether a search is necessary, and if so, g - Today's date is: {{CURRENT_DATE}} ### Output: -JSON format: { +Strictly return in JSON format: +{ "queries": ["query1", "query2"] } diff --git a/src/lib/apis/index.ts b/src/lib/apis/index.ts index 699980a5e..9c726e4d0 100644 --- a/src/lib/apis/index.ts +++ b/src/lib/apis/index.ts @@ -391,16 +391,13 @@ export const generateQueries = async ( // Step 1: Safely extract the response string const response = res?.choices[0]?.message?.content ?? ''; - // Step 2: Attempt to fix common JSON format issues like single quotes - const sanitizedResponse = response.replace(/['‘’`]/g, '"'); // Convert single quotes to double quotes for valid JSON - // Step 3: Find the relevant JSON block within the response - const jsonStartIndex = sanitizedResponse.indexOf('{'); - const jsonEndIndex = sanitizedResponse.lastIndexOf('}'); + const jsonStartIndex = response.indexOf('{'); + const jsonEndIndex = response.lastIndexOf('}'); // Step 4: Check if we found a valid JSON block (with both `{` and `}`) if (jsonStartIndex !== -1 && jsonEndIndex !== -1) { - const jsonResponse = sanitizedResponse.substring(jsonStartIndex, jsonEndIndex + 1); + const jsonResponse = response.substring(jsonStartIndex, jsonEndIndex + 1); // Step 5: Parse the JSON block const parsed = JSON.parse(jsonResponse);