From b4fb0d1da2cc31160f57f49e745957bcaf74e28c Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Fri, 29 Nov 2024 01:10:46 -0800 Subject: [PATCH] refac --- backend/open_webui/env.py | 4 ++-- src/lib/apis/index.ts | 29 ++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/backend/open_webui/env.py b/backend/open_webui/env.py index 6e591311d..28b5a10a5 100644 --- a/backend/open_webui/env.py +++ b/backend/open_webui/env.py @@ -373,7 +373,7 @@ else: AIOHTTP_CLIENT_TIMEOUT = 300 AIOHTTP_CLIENT_TIMEOUT_OPENAI_MODEL_LIST = os.environ.get( - "AIOHTTP_CLIENT_TIMEOUT_OPENAI_MODEL_LIST", "3" + "AIOHTTP_CLIENT_TIMEOUT_OPENAI_MODEL_LIST", "5" ) if AIOHTTP_CLIENT_TIMEOUT_OPENAI_MODEL_LIST == "": @@ -384,7 +384,7 @@ else: AIOHTTP_CLIENT_TIMEOUT_OPENAI_MODEL_LIST ) except Exception: - AIOHTTP_CLIENT_TIMEOUT_OPENAI_MODEL_LIST = 3 + AIOHTTP_CLIENT_TIMEOUT_OPENAI_MODEL_LIST = 5 #################################### # OFFLINE_MODE diff --git a/src/lib/apis/index.ts b/src/lib/apis/index.ts index f3d21cdab..12716cece 100644 --- a/src/lib/apis/index.ts +++ b/src/lib/apis/index.ts @@ -440,7 +440,34 @@ export const generateAutoCompletion = async ( } const response = res?.choices[0]?.message?.content ?? ''; - return response; + + + try { + const jsonStartIndex = response.indexOf('{'); + const jsonEndIndex = response.lastIndexOf('}'); + + if (jsonStartIndex !== -1 && jsonEndIndex !== -1) { + const jsonResponse = response.substring(jsonStartIndex, jsonEndIndex + 1); + + // Step 5: Parse the JSON block + const parsed = JSON.parse(jsonResponse); + + // Step 6: If there's a "queries" key, return the queries array; otherwise, return an empty array + if (parsed && parsed.text) { + return parsed.text; + } else { + return ""; + } + } + + // If no valid JSON block found, return response as is + return response; + } catch (e) { + // Catch and safely return empty array on any parsing errors + console.error('Failed to parse response: ', e); + return response; + } + };