diff --git a/clients/search-component/example/index.html b/clients/search-component/example/index.html index e4b78eae12..5cfcd6ddf5 100644 --- a/clients/search-component/example/index.html +++ b/clients/search-component/example/index.html @@ -4,7 +4,17 @@ - Vite + React + TS + + + + + + + + + + + Trieve Drop-In RAG and Search Component
diff --git a/clients/search-component/src/TrieveModal/ChatMode.tsx b/clients/search-component/src/TrieveModal/ChatMode.tsx index 9c119f3924..14d896e921 100644 --- a/clients/search-component/src/TrieveModal/ChatMode.tsx +++ b/clients/search-component/src/TrieveModal/ChatMode.tsx @@ -44,7 +44,7 @@ export const ChatMode = ({ setIsLoading(true); const fingerprint = await getFingerprint(); const topic = await trieve.createTopic({ - first_user_message: currentQuestion, + name: currentQuestion, owner_id: fingerprint.toString(), }); setCurrentQuestion(""); @@ -53,25 +53,56 @@ export const ChatMode = ({ } }; + const handleReader = async ( + reader: ReadableStreamDefaultReader + ) => { + setIsLoading(true); + let done = false; + let textInStream = ""; + + while (!done) { + const { value, done: doneReading } = await reader.read(); + if (doneReading) { + done = doneReading; + setIsLoading(false); + } else if (value) { + const decoder = new TextDecoder(); + const newText = decoder.decode(value); + textInStream += newText; + const [text, jsonData] = textInStream.split("||"); + let json; + try { + json = JSON.parse(jsonData); + // eslint-disable-next-line @typescript-eslint/no-unused-vars + } catch (_) { + json = null; + } + + setMessages((m) => [ + [ + m[0][0], + { + type: "system", + text: text, + additional: json ? json : null, + }, + ], + ...m.slice(1), + ]); + } + } + }; + const createQuestion = async (id?: string) => { setIsLoading(true); - const answer = await trieve.createMessage({ + const reader = await trieve.createMessageReader({ topic_id: id || currentTopic, new_message_content: currentQuestion, + llm_options: { + completion_first: true, + }, }); - const [json, text] = answer.split("||"); - setMessages((m) => [ - [ - m[0][0], - { - type: "system", - text: text, - additional: JSON.parse(json), - }, - ], - ...m.slice(1), - ]); - setIsLoading(false); + handleReader(reader); }; useEffect(() => { @@ -105,17 +136,17 @@ export const ChatMode = ({
{messages.map((chat, i) => ( -