diff --git a/backend/apps/rag/main.py b/backend/apps/rag/main.py index 15dbcc5cb..cc6cf81a3 100644 --- a/backend/apps/rag/main.py +++ b/backend/apps/rag/main.py @@ -655,7 +655,7 @@ def store_web_search(form_data: SearchForm, user=Depends(get_current_user)): ) urls = [result.link for result in web_results] loader = get_web_loader(urls) - data = loader.aload() + data = loader.load() collection_name = form_data.collection_name if collection_name == "": diff --git a/backend/apps/rag/utils.py b/backend/apps/rag/utils.py index 62191481f..d5a826acc 100644 --- a/backend/apps/rag/utils.py +++ b/backend/apps/rag/utils.py @@ -550,6 +550,8 @@ def search_web(query: str) -> list[SearchResult]: Args: query (str): The query to search for """ + + # TODO: add playwright to search the web if SEARXNG_QUERY_URL: return search_searxng(SEARXNG_QUERY_URL, query) elif GOOGLE_PSE_API_KEY and GOOGLE_PSE_ENGINE_ID: diff --git a/src/lib/apis/openai/index.ts b/src/lib/apis/openai/index.ts index 7802e5591..05b9162da 100644 --- a/src/lib/apis/openai/index.ts +++ b/src/lib/apis/openai/index.ts @@ -431,59 +431,25 @@ export const generateSearchQuery = async ( role: 'assistant', content: `You are tasked with generating web search queries. Give me an appropriate query to answer my question for google search. Answer with only the query. Today is ${currentDate}.` }, - { - role: 'user', - content: `Previous Questions: -- Who is the president of France? - -Current Question: What about Mexico?` - }, - { - role: 'assistant', - content: 'President of Mexico' - }, - { - role: 'user', - content: `Previous questions: -- When is the next formula 1 grand prix? - -Current Question: Where is it being hosted?` - }, - { - role: 'assistant', - content: 'location of next formula 1 grand prix' - }, - { - role: 'user', - content: 'Current Question: What type of printhead does the Epson F2270 DTG printer use?' - }, - { - role: 'assistant', - content: 'Epson F2270 DTG printer printhead' - }, - { - role: 'user', - content: 'What were the news yesterday?' - }, - { - role: 'assistant', - content: `news ${yesterdayDate}` - }, { role: 'user', content: 'What is the current weather in Paris?' }, { role: 'assistant', - content: `weather in Paris ${currentDate}` + content: `Weather in Paris ${currentDate}` }, { role: 'user', - content: - (previousMessages.length > 0 - ? `Previous Questions:\n${previousMessages.join('\n')}\n\n` - : '') + `Current Question: ${prompt}` + content: prompt } + // { + // role: 'user', + // content: + // (previousMessages.length > 0 + // ? `Previous Questions:\n${previousMessages.join('\n')}\n\n` + // : '') + `Current Question: ${prompt}` + // } ], stream: false, // Restricting the max tokens to 30 to avoid long search queries diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index fd6673ea4..3115ae8ae 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -407,7 +407,7 @@ responseMessage.userContext = userContext; if (webSearchEnabled) { - await getWebSearchResultsAsFiles(model.id, parentId, responseMessageId); + await getWebSearchResults(model.id, parentId, responseMessageId); } if (model?.owned_by === 'openai') { @@ -424,47 +424,65 @@ await chats.set(await getChatList(localStorage.token)); }; - const getWebSearchResultsAsFiles = async ( - model: string, - parentId: string, - responseId: string - ) => { + const getWebSearchResults = async (model: string, parentId: string, responseId: string) => { const responseMessage = history.messages[responseId]; - responseMessage.progress = $i18n.t('Generating search query'); + + responseMessage.status = { + done: false, + action: 'web_search', + description: $i18n.t('Generating search query') + }; messages = messages; const searchQuery = await generateChatSearchQuery(model, parentId); if (!searchQuery) { toast.warning($i18n.t('No search query generated')); - responseMessage.progress = undefined; + responseMessage.status = { + ...responseMessage.status, + done: true, + error: true, + description: 'No search query generated' + }; messages = messages; return; } - responseMessage.progress = $i18n.t("Searching the web for '{{searchQuery}}'", { searchQuery }); + responseMessage.status = { + ...responseMessage.status, + description: $i18n.t("Searching the web for '{{searchQuery}}'", { searchQuery }) + }; messages = messages; - const searchDocument = await runWebSearch(localStorage.token, searchQuery); - if (searchDocument === undefined) { + const results = await runWebSearch(localStorage.token, searchQuery); + if (results === undefined) { toast.warning($i18n.t('No search results found')); - responseMessage.progress = undefined; + responseMessage.status = { + ...responseMessage.status, + done: true, + error: true, + description: 'No search results found' + }; messages = messages; return; } - if (!responseMessage.files) { + responseMessage.status = { + ...responseMessage.status, + done: true, + description: $i18n.t('Searched {{count}} sites', { count: results.filenames.length }) + }; + + if (responseMessage?.files ?? undefined === undefined) { responseMessage.files = []; } responseMessage.files.push({ - collection_name: searchDocument.collection_name, + collection_name: results.collection_name, name: searchQuery, - type: 'websearch', - upload_status: true, - error: '', - urls: searchDocument.filenames + type: 'web_search_results', + urls: results.filenames }); - responseMessage.progress = undefined; + messages = messages; }; @@ -530,7 +548,9 @@ const docs = messages .filter((message) => message?.files ?? null) .map((message) => - message.files.filter((item) => ['doc', 'collection', 'websearch'].includes(item.type)) + message.files.filter((item) => + ['doc', 'collection', 'web_search_results'].includes(item.type) + ) ) .flat(1); @@ -726,7 +746,9 @@ const docs = messages .filter((message) => message?.files ?? null) .map((message) => - message.files.filter((item) => ['doc', 'collection', 'websearch'].includes(item.type)) + message.files.filter((item) => + ['doc', 'collection', 'web_search_results'].includes(item.type) + ) ) .flat(1); @@ -962,7 +984,7 @@ const model = $models.filter((m) => m.id === responseMessage.model).at(0); if (model) { - if (model?.external) { + if (model?.owned_by === 'openai') { await sendPromptOpenAI( model, history.messages[responseMessage.parentId].content, @@ -987,7 +1009,7 @@ const model = $models.find((model) => model.id === selectedModels[0]); const titleModelId = - model?.external ?? false + model?.owned_by === 'openai' ?? false ? $settings?.title?.modelExternal ?? selectedModels[0] : $settings?.title?.model ?? selectedModels[0]; const titleModel = $models.find((model) => model.id === titleModelId); @@ -1015,7 +1037,7 @@ const generateChatSearchQuery = async (modelId: string, messageId: string) => { const model = $models.find((model) => model.id === modelId); const taskModelId = - model?.external ?? false + model?.owned_by === 'openai' ?? false ? $settings?.title?.modelExternal ?? modelId : $settings?.title?.model ?? modelId; const taskModel = $models.find((model) => model.id === taskModelId); @@ -1024,6 +1046,7 @@ const previousMessages = messages .filter((message) => message.role === 'user') .map((message) => message.content); + return await generateSearchQuery( localStorage.token, taskModelId, diff --git a/src/lib/components/chat/MessageInput.svelte b/src/lib/components/chat/MessageInput.svelte index 443923ea2..cb4e5f160 100644 --- a/src/lib/components/chat/MessageInput.svelte +++ b/src/lib/components/chat/MessageInput.svelte @@ -975,75 +975,6 @@