From 57bbb4d7abb5f0f2ff281c6363f337af43f6715e Mon Sep 17 00:00:00 2001 From: Lester Solbakken Date: Thu, 20 Jun 2024 13:15:34 +0200 Subject: [PATCH 1/2] Move RAG search to Vespa --- src/App/libs/provider/provider.js | 33 ++++++++++++++----------------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/App/libs/provider/provider.js b/src/App/libs/provider/provider.js index c5f48b9..f85152a 100644 --- a/src/App/libs/provider/provider.js +++ b/src/App/libs/provider/provider.js @@ -3,7 +3,6 @@ import { useNavigate, useLocation } from 'react-router-dom'; import { create } from 'zustand'; import { useConsent } from 'App/pages/search/abstract-container/abstract/use-consent.js'; import { UrlBuilder } from 'App/utils'; -import { Get } from 'App/libs/fetcher'; import { createStore } from 'App/libs/provider/reducer'; import { parseUrlParams, createUrlParams } from 'App/libs/provider/url-params'; @@ -56,36 +55,34 @@ export function SearchContext() { if (query.length === 0) return; const filters = namespaces.map((n) => `+namespace:${n}`).join(' '); + let cancelled = false; const streamUrl = new UrlBuilder(endpoint) - .add('stream') + .add('sse') .queryParam('query', query) .queryParam('filters', filters) - .queryParam('queryProfile', 'llmsearch') + .queryParam('queryProfile', 'ragsearch') + .queryParam('llm.includeHits', 'true') .toString(true); const source = abstractConsent ? new EventSource(streamUrl) : dummyEventSource; - const onMessage = (e) => summaryAppend(e.data); + const onToken = (e) => summaryAppend(JSON.parse(e.data).token); + const onHits = (e) => { + if (!cancelled) { + const result = JSON.parse(e.data); + setHits({ hits: result.root.children ?? [] }); + } + }; const onError = () => summaryComplete() || source.close(); - source.addEventListener('message', onMessage); + source.addEventListener('token', onToken); + source.addEventListener('hits', onHits); source.addEventListener('error', onError); - let cancelled = false; - const searchUrl = new UrlBuilder(endpoint) - .add('search') - .queryParam('query', query) - .queryParam('filters', filters) - .queryParam('queryProfile', 'llmsearch') - .toString(true); - Get(searchUrl) - .then( - (result) => !cancelled && setHits({ hits: result.root.children ?? [] }), - ) - .catch((error) => !cancelled && setHits({ error })); return () => { cancelled = true; source.close(); - source.removeEventListener('message', onMessage); + source.removeEventListener('token', onToken); + source.removeEventListener('hits', onHits); source.removeEventListener('error', onError); }; }, [ From 36a28f2b6e959f4f970c12aeab41c6b008e4764a Mon Sep 17 00:00:00 2001 From: Lester Solbakken Date: Fri, 21 Jun 2024 09:49:21 +0200 Subject: [PATCH 2/2] Add fallback to regular search if not consented yet to abstract --- src/App/libs/provider/provider.js | 32 ++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/App/libs/provider/provider.js b/src/App/libs/provider/provider.js index f85152a..376a819 100644 --- a/src/App/libs/provider/provider.js +++ b/src/App/libs/provider/provider.js @@ -3,16 +3,12 @@ import { useNavigate, useLocation } from 'react-router-dom'; import { create } from 'zustand'; import { useConsent } from 'App/pages/search/abstract-container/abstract/use-consent.js'; import { UrlBuilder } from 'App/utils'; +import { Get } from 'App/libs/fetcher'; import { createStore } from 'App/libs/provider/reducer'; import { parseUrlParams, createUrlParams } from 'App/libs/provider/url-params'; export const useSearchContext = create(createStore); const endpoint = import.meta.env.VITE_ENDPOINT; -const dummyEventSource = Object.freeze({ - close: () => {}, - addEventListener: () => {}, - removeEventListener: () => {}, -}); export function SearchContext() { const location = useLocation(); @@ -56,6 +52,27 @@ export function SearchContext() { const filters = namespaces.map((n) => `+namespace:${n}`).join(' '); let cancelled = false; + + // If the user has not consented to the abstract yet, use regular search + if (!abstractConsent) { + const searchUrl = new UrlBuilder(endpoint) + .add('search') + .queryParam('query', query) + .queryParam('filters', filters) + .queryParam('queryProfile', 'llmsearch') + .toString(true); + Get(searchUrl) + .then( + (result) => + !cancelled && setHits({ hits: result.root.children ?? [] }), + ) + .catch((error) => !cancelled && setHits({ error })); + return () => { + cancelled = true; + }; + } + + // However, if the user has consented, use RAG search const streamUrl = new UrlBuilder(endpoint) .add('sse') .queryParam('query', query) @@ -63,9 +80,7 @@ export function SearchContext() { .queryParam('queryProfile', 'ragsearch') .queryParam('llm.includeHits', 'true') .toString(true); - const source = abstractConsent - ? new EventSource(streamUrl) - : dummyEventSource; + const source = new EventSource(streamUrl); const onToken = (e) => summaryAppend(JSON.parse(e.data).token); const onHits = (e) => { if (!cancelled) { @@ -77,7 +92,6 @@ export function SearchContext() { source.addEventListener('token', onToken); source.addEventListener('hits', onHits); source.addEventListener('error', onError); - return () => { cancelled = true; source.close();