Spaces:
Runtime error
Runtime error
matt HOFFNER
commited on
Commit
Β·
5d239ba
1
Parent(s):
8c64b1d
remove server route
Browse files- src/components/ChatWindow.jsx +17 -9
- src/components/FileLoader.jsx +3 -14
- src/pages/api/docHandle.ts +0 -41
src/components/ChatWindow.jsx
CHANGED
@@ -4,7 +4,9 @@ import { useCallback, useEffect, useState } from "react";
|
|
4 |
import MessageList from './MessageList';
|
5 |
import {FileLoader} from './FileLoader';
|
6 |
import Loader from "./Loader";
|
7 |
-
|
|
|
|
|
8 |
const client = new ChromaClient();
|
9 |
|
10 |
function ChatWindow({
|
@@ -12,7 +14,7 @@ function ChatWindow({
|
|
12 |
maxTokens,
|
13 |
}) {
|
14 |
const { loadingStatus, send, isGenerating, setOnMessage } = useLLM();
|
15 |
-
const [
|
16 |
const [userInput, setUserInput] = useState("");
|
17 |
|
18 |
const handleChange = (event) => {
|
@@ -26,9 +28,15 @@ function ChatWindow({
|
|
26 |
return;
|
27 |
}
|
28 |
|
29 |
-
if (
|
30 |
-
|
31 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
const result = await collection.query({
|
33 |
nResults: 2,
|
34 |
queryTexts: [userPrompt]
|
@@ -58,7 +66,8 @@ function ChatWindow({
|
|
58 |
isGenerating,
|
59 |
isReady,
|
60 |
maxTokens,
|
61 |
-
stopStrings
|
|
|
62 |
]);
|
63 |
|
64 |
useEffect(() => {
|
@@ -82,7 +91,7 @@ function ChatWindow({
|
|
82 |
useEffect(() => {
|
83 |
loadFile();
|
84 |
|
85 |
-
}, [
|
86 |
|
87 |
return (
|
88 |
<div className="window sm:w-[500px] w-full">
|
@@ -134,7 +143,7 @@ function ChatWindow({
|
|
134 |
height="40"
|
135 |
/>
|
136 |
</button>
|
137 |
-
<FileLoader
|
138 |
</div>
|
139 |
|
140 |
<div
|
@@ -153,7 +162,6 @@ function ChatWindow({
|
|
153 |
</div>
|
154 |
)}
|
155 |
{!isReady && <Loader />}
|
156 |
-
{fileId && <div>loaded {fileId}</div>}
|
157 |
</div>
|
158 |
</div>
|
159 |
</div>
|
|
|
4 |
import MessageList from './MessageList';
|
5 |
import {FileLoader} from './FileLoader';
|
6 |
import Loader from "./Loader";
|
7 |
+
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter';
|
8 |
+
import { ChromaClient } from "chromadb";
|
9 |
+
|
10 |
const client = new ChromaClient();
|
11 |
|
12 |
function ChatWindow({
|
|
|
14 |
maxTokens,
|
15 |
}) {
|
16 |
const { loadingStatus, send, isGenerating, setOnMessage } = useLLM();
|
17 |
+
const [fileText, setFileText] = useState();
|
18 |
const [userInput, setUserInput] = useState("");
|
19 |
|
20 |
const handleChange = (event) => {
|
|
|
28 |
return;
|
29 |
}
|
30 |
|
31 |
+
if (fileText) {
|
32 |
+
const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 });
|
33 |
+
const docs = await textSplitter.createDocuments([text]);
|
34 |
+
const collection = await client.createCollection({name: "docs", embeddingFunction: TransformersEmbeddingFunction})
|
35 |
+
await collection.add({
|
36 |
+
ids: [...docs.map((v, k) => k)],
|
37 |
+
metadatas: [...docs.map(doc => doc.metadata)],
|
38 |
+
documents: [...docs.map(doc => doc.pageContent)],
|
39 |
+
});
|
40 |
const result = await collection.query({
|
41 |
nResults: 2,
|
42 |
queryTexts: [userPrompt]
|
|
|
66 |
isGenerating,
|
67 |
isReady,
|
68 |
maxTokens,
|
69 |
+
stopStrings,
|
70 |
+
fileText
|
71 |
]);
|
72 |
|
73 |
useEffect(() => {
|
|
|
91 |
useEffect(() => {
|
92 |
loadFile();
|
93 |
|
94 |
+
}, [fileText])
|
95 |
|
96 |
return (
|
97 |
<div className="window sm:w-[500px] w-full">
|
|
|
143 |
height="40"
|
144 |
/>
|
145 |
</button>
|
146 |
+
<FileLoader setFileText={setFileText} />
|
147 |
</div>
|
148 |
|
149 |
<div
|
|
|
162 |
</div>
|
163 |
)}
|
164 |
{!isReady && <Loader />}
|
|
|
165 |
</div>
|
166 |
</div>
|
167 |
</div>
|
src/components/FileLoader.jsx
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import { useState } from 'react';
|
2 |
-
import { db } from '@/utils/db-client';
|
3 |
import { DashButton } from './DashButton'
|
4 |
import { FileEmbedder } from './FileEmbedder';
|
5 |
import * as PDFJS from 'pdfjs-dist/build/pdf';
|
@@ -26,7 +25,7 @@ export default class Pdf {
|
|
26 |
}
|
27 |
}
|
28 |
|
29 |
-
export const FileLoader = ({
|
30 |
const [files, setFiles] = useState();
|
31 |
const [uploadStatus, setUploadStatus] = useState("Embed");
|
32 |
const handleEmbed = (files) => {
|
@@ -53,18 +52,8 @@ export const FileLoader = ({ setFileId }) => {
|
|
53 |
|
54 |
reader.readAsBinaryString(file);
|
55 |
}
|
56 |
-
|
57 |
-
|
58 |
-
body: JSON.stringify({ text }),
|
59 |
-
});
|
60 |
-
const docChat = await response.json();
|
61 |
-
const id = await db.docs.add({
|
62 |
-
fileName: file.name || '',
|
63 |
-
fileSourceData: text,
|
64 |
-
model: docChat,
|
65 |
-
});
|
66 |
-
setUploadStatus("Embedding Completed");
|
67 |
-
setFileId(id)
|
68 |
}
|
69 |
}}
|
70 |
>
|
|
|
1 |
import { useState } from 'react';
|
|
|
2 |
import { DashButton } from './DashButton'
|
3 |
import { FileEmbedder } from './FileEmbedder';
|
4 |
import * as PDFJS from 'pdfjs-dist/build/pdf';
|
|
|
25 |
}
|
26 |
}
|
27 |
|
28 |
+
export const FileLoader = ({ setFileText }) => {
|
29 |
const [files, setFiles] = useState();
|
30 |
const [uploadStatus, setUploadStatus] = useState("Embed");
|
31 |
const handleEmbed = (files) => {
|
|
|
52 |
|
53 |
reader.readAsBinaryString(file);
|
54 |
}
|
55 |
+
setFileText(text)
|
56 |
+
setUploadStatus("Embed Complete");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
}
|
58 |
}}
|
59 |
>
|
src/pages/api/docHandle.ts
DELETED
@@ -1,41 +0,0 @@
|
|
1 |
-
import type { NextApiRequest, NextApiResponse } from 'next';
|
2 |
-
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter';
|
3 |
-
import { TransformersEmbeddingFunction } from '../../embed/hf';
|
4 |
-
const {ChromaClient} = require('chromadb');
|
5 |
-
const client = new ChromaClient();
|
6 |
-
|
7 |
-
async function handleDocs(text: string) {
|
8 |
-
const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 });
|
9 |
-
const docs = await textSplitter.createDocuments([text]);
|
10 |
-
const collection = await client.createCollection({name: "docs", embeddingFunction: TransformersEmbeddingFunction})
|
11 |
-
await collection.add({
|
12 |
-
ids: [...docs.map((v, k) => k)],
|
13 |
-
metadatas: [...docs.map(doc => doc.metadata)],
|
14 |
-
documents: [...docs.map(doc => doc.pageContent)],
|
15 |
-
});
|
16 |
-
|
17 |
-
return collection;
|
18 |
-
}
|
19 |
-
|
20 |
-
export default async function handler(
|
21 |
-
req: NextApiRequest,
|
22 |
-
res: NextApiResponse,
|
23 |
-
) {
|
24 |
-
const { text } = JSON.parse(req.body);
|
25 |
-
// console.log(text);
|
26 |
-
|
27 |
-
if (!text) {
|
28 |
-
return res.status(400).json({ message: 'No question in the request' });
|
29 |
-
}
|
30 |
-
|
31 |
-
const vectorStore = await handleDocs(text);
|
32 |
-
res.status(200).send({
|
33 |
-
model: vectorStore,
|
34 |
-
});
|
35 |
-
}
|
36 |
-
|
37 |
-
export const config = {
|
38 |
-
api: {
|
39 |
-
bodyParser: true, // Disallow body parsing, consume as stream
|
40 |
-
},
|
41 |
-
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|