From 1bb5d0139ffd23c940fb76cd43f405163a842004 Mon Sep 17 00:00:00 2001 From: Tat Dat Duong Date: Thu, 19 Oct 2023 20:31:07 +0200 Subject: [PATCH] Handle streamed message chunks in playground --- nextjs/app/components/ChatWindow.tsx | 77 +++++++++++++--------------- nextjs/package.json | 1 + nextjs/yarn.lock | 3 ++ 3 files changed, 41 insertions(+), 40 deletions(-) diff --git a/nextjs/app/components/ChatWindow.tsx b/nextjs/app/components/ChatWindow.tsx index b2ac6bf..4d79204 100644 --- a/nextjs/app/components/ChatWindow.tsx +++ b/nextjs/app/components/ChatWindow.tsx @@ -25,6 +25,7 @@ import { import { ArrowUpIcon } from "@chakra-ui/icons"; import { Source } from "./SourceBubble"; import { DefaultQuestion } from "./DefaultQuestion"; +import { LangServePlayground } from "langserve-playground"; type RetrieverName = "tavily" | "kay" | "you" | "google" | "kay_press_release"; @@ -38,14 +39,13 @@ export function ChatWindow(props: { const [messages, setMessages] = useState>([]); const [input, setInput] = useState(""); const [isLoading, setIsLoading] = useState(false); - const [retriever, setRetriever] = useState("tavily"); - const [llm, setLlm] = useState("openai"); const [chatHistory, setChatHistory] = useState< { human: string; ai: string }[] >([]); const { apiBaseUrl, titleText } = props; + const [config, setConfig] = useState>({}); const sendMessage = async (message?: string) => { if (messageContainerRef.current) { @@ -84,7 +84,7 @@ export function ChatWindow(props: { : "plaintext"; const highlightedCode = hljs.highlight( validLanguage || "plaintext", - code, + code ).value; return `
${highlightedCode}
`; }; @@ -105,10 +105,7 @@ export function ChatWindow(props: { chat_history: chatHistory, }, config: { - configurable: { - retriever, - llm, - }, + ...config, metadata: { conversation_id: conversationId, }, @@ -133,19 +130,22 @@ export function ChatWindow(props: { const chunk = JSON.parse(msg.data); streamedResponse = applyPatch( streamedResponse, - chunk.ops, + chunk.ops ).newDocument; if ( Array.isArray( streamedResponse?.logs?.[sourceStepName]?.final_output - ?.documents, + ?.documents ) ) { sources = streamedResponse.logs[ sourceStepName ].final_output.documents.map((doc: Record) => ({ url: doc.metadata.source ?? doc.metadata.data_source_link, - defaultSourceUrl: retriever === "you" ? "https://you.com" : "", + defaultSourceUrl: + config?.configurable?.retriever === "you" + ? "https://you.com" + : "", title: doc.metadata.title, images: doc.metadata.images, })); @@ -248,23 +248,6 @@ export function ChatWindow(props: { ? "We appreciate feedback!" : "Ask me anything about anything!"} -
- Powered by - - and - -
- {DEFAULT_QUESTIONS[retriever] + {DEFAULT_QUESTIONS[ + (config?.configurable?.retriever ?? "tavily") as RetrieverName + ] .slice(0, 4) .map((defaultQuestion, i) => { return ( @@ -332,7 +317,7 @@ export function ChatWindow(props: { question={defaultQuestion} onMouseUp={(e) => sendInitialQuestion( - (e.target as HTMLDivElement).innerText, + (e.target as HTMLDivElement).innerText ) } > @@ -340,23 +325,35 @@ export function ChatWindow(props: { })}
- {DEFAULT_QUESTIONS[retriever].slice(4).map((defaultQuestion, i) => { - return ( - - sendInitialQuestion((e.target as HTMLDivElement).innerText) - } - > - ); - })} + {DEFAULT_QUESTIONS[ + (config?.configurable?.retriever ?? "tavily") as RetrieverName + ] + .slice(4) + .map((defaultQuestion, i) => { + return ( + + sendInitialQuestion( + (e.target as HTMLDivElement).innerText + ) + } + > + ); + })}
) : ( "" )} + setConfig(config)} + /> + {messages.length === 0 ? (