import useLLM from "@react-llm/headless"; import Image from "next/image"; import { useCallback, useEffect, useState } from "react"; import MessageList from './MessageList'; import {FileLoader} from './FileLoader'; import { db } from '@/utils/db-client'; import Loader from "./Loader"; function ChatWindow({ stopStrings, maxTokens, }) { const { loadingStatus, send, isGenerating, setOnMessage } = useLLM(); const [fileId, setFileId] = useState(); const [userInput, setUserInput] = useState(""); const handleChange = (event) => { setUserInput(event.target.value); }; const isReady = loadingStatus.progress === 1; const handleSubmit = useCallback(async () => { if (isGenerating || !isReady) { return; } if (fileId) { const similarityMatches = 2; const fileContents = await db.docs.get(fileId); const vectorStore = await HNSWLib.fromDocuments(fileContents, new XenovaTransformersEmbeddings()); const result = await vectorStore.similaritySearch(userInput, similarityMatches); const qaPrompt = `You are an AI assistant providing helpful advice. You are given the following extracted parts of a long document and a question. Provide a conversational answer based on the context provided. You should only provide hyperlinks that reference the context below. Do NOT make up hyperlinks. If you can't find the answer in the context below, just say "Hmm, I'm not sure." Don't try to make up an answer. If the question is not related to the context, politely respond that you are tuned to only answer questions that are related to the context. Question: ${userInput} ========= ${result} ========= Answer: ` send(qaPrompt, maxTokens, stopStrings); } else { send(userInput, maxTokens, stopStrings); } setUserInput(""); }, [ userInput, send, isGenerating, isReady, maxTokens, stopStrings ]); useEffect(() => { const handleKeyPress = (event) => { if (event.key === "Enter") { event.preventDefault(); handleSubmit(); } }; window.addEventListener("keydown", handleKeyPress); return () => { window.removeEventListener("keydown", handleKeyPress); }; }, [handleSubmit]); const loadFile = async () => { console.log('test'); } useEffect(() => { console.log('have a fileId, look it up and summarize after uploading for demo'); loadFile(); }, [fileId]) return (
{/* */}
{isReady && (
{isGenerating && ( is typing... )}
)} {!isReady && } {fileId &&
loaded {fileId}
}
); } export default ChatWindow;