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 Loader from "./Loader"; import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter'; import { TransformersEmbeddingFunction } from '../embed/hf'; import { ChromaClient } from "chromadb"; const client = new ChromaClient(); const embedder = new TransformersEmbeddingFunction({}); function ChatWindow({ stopStrings, maxTokens, }) { const { loadingStatus, send, isGenerating, setOnMessage } = useLLM(); const [fileText, setFileText] = 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 (fileText) { console.log('found file text splitting into chunks') const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 }); const docs = await textSplitter.createDocuments([fileText]); console.log(`split docs: ${docs}`); const collection = await client.createCollection({name: "docs", embeddingFunction: embedder }) console.log(`collection: ${collection}`); let queryResult; try { await collection.add({ ids: [...docs.map((v, k) => k)], metadatas: [...docs.map(doc => doc.metadata)], documents: [...docs.map(doc => doc.pageContent)], }); const queryResult = await collection.query({ nResults: 2, queryTexts: [userPrompt] }); console.log(queryResult); } catch (err) { console.log(err); } 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} ========= ${queryResult} ========= Answer: ` send(qaPrompt, maxTokens, stopStrings); } else { send(userInput, maxTokens, stopStrings); } setUserInput(""); }, [ userInput, send, isGenerating, isReady, maxTokens, stopStrings, fileText ]); 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('file loaded'); } useEffect(() => { loadFile(); }, [fileText]) return (