Spaces:
Runtime error
Runtime error
matt HOFFNER
commited on
Commit
Β·
f1b879c
1
Parent(s):
d4cd819
identify if fileId before sending message
Browse files- src/components/ChatWindow.jsx +17 -1
- src/components/FileLoader.jsx +2 -3
- src/embed/hf.ts +3 -2
- src/pages/api/docChat.ts +1 -1
- src/pages/api/docHandle.ts +4 -6
- src/utils/file-handler.ts +2 -1
src/components/ChatWindow.jsx
CHANGED
@@ -10,6 +10,7 @@ function ChatWindow({
|
|
10 |
maxTokens,
|
11 |
}) {
|
12 |
const { loadingStatus, send, isGenerating, setOnMessage } = useLLM();
|
|
|
13 |
const [userInput, setUserInput] = useState("");
|
14 |
|
15 |
const handleChange = (event) => {
|
@@ -23,6 +24,10 @@ function ChatWindow({
|
|
23 |
return;
|
24 |
}
|
25 |
|
|
|
|
|
|
|
|
|
26 |
send(userInput, maxTokens, stopStrings);
|
27 |
setUserInput("");
|
28 |
}, [
|
@@ -48,6 +53,16 @@ function ChatWindow({
|
|
48 |
};
|
49 |
}, [handleSubmit]);
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
return (
|
52 |
<div className="window sm:w-[500px] w-full">
|
53 |
|
@@ -98,7 +113,7 @@ function ChatWindow({
|
|
98 |
height="40"
|
99 |
/>
|
100 |
</button>
|
101 |
-
<FileLoader />
|
102 |
</div>
|
103 |
|
104 |
<div
|
@@ -117,6 +132,7 @@ function ChatWindow({
|
|
117 |
</div>
|
118 |
)}
|
119 |
{!isReady && <Loader />}
|
|
|
120 |
</div>
|
121 |
</div>
|
122 |
</div>
|
|
|
10 |
maxTokens,
|
11 |
}) {
|
12 |
const { loadingStatus, send, isGenerating, setOnMessage } = useLLM();
|
13 |
+
const [fileId, setFileId] = useState();
|
14 |
const [userInput, setUserInput] = useState("");
|
15 |
|
16 |
const handleChange = (event) => {
|
|
|
24 |
return;
|
25 |
}
|
26 |
|
27 |
+
if (fileId) {
|
28 |
+
console.log('we have a fileId, so this should be contextual chat')
|
29 |
+
}
|
30 |
+
|
31 |
send(userInput, maxTokens, stopStrings);
|
32 |
setUserInput("");
|
33 |
}, [
|
|
|
53 |
};
|
54 |
}, [handleSubmit]);
|
55 |
|
56 |
+
const loadFile = async () => {
|
57 |
+
console.log('test');
|
58 |
+
}
|
59 |
+
|
60 |
+
useEffect(() => {
|
61 |
+
console.log('have a fileId, look it up and summarize after uploading for demo');
|
62 |
+
loadFile();
|
63 |
+
|
64 |
+
}, [fileId])
|
65 |
+
|
66 |
return (
|
67 |
<div className="window sm:w-[500px] w-full">
|
68 |
|
|
|
113 |
height="40"
|
114 |
/>
|
115 |
</button>
|
116 |
+
<FileLoader setFileId={setFileId} />
|
117 |
</div>
|
118 |
|
119 |
<div
|
|
|
132 |
</div>
|
133 |
)}
|
134 |
{!isReady && <Loader />}
|
135 |
+
{fileId && <div>loaded {fileId}</div>}
|
136 |
</div>
|
137 |
</div>
|
138 |
</div>
|
src/components/FileLoader.jsx
CHANGED
@@ -26,7 +26,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) => {
|
@@ -58,14 +58,13 @@ export const FileLoader = () => {
|
|
58 |
body: JSON.stringify({ text }),
|
59 |
});
|
60 |
const docChat = await response.json();
|
61 |
-
console.log('handleDocs-getModels: ', docChat);
|
62 |
const id = await db.docs.add({
|
63 |
fileName: file.name || '',
|
64 |
fileSourceData: text,
|
65 |
model: docChat,
|
66 |
});
|
67 |
setUploadStatus("Embedding Completed");
|
68 |
-
|
69 |
}
|
70 |
}}
|
71 |
>
|
|
|
26 |
}
|
27 |
}
|
28 |
|
29 |
+
export const FileLoader = ({ setFileId }) => {
|
30 |
const [files, setFiles] = useState();
|
31 |
const [uploadStatus, setUploadStatus] = useState("Embed");
|
32 |
const handleEmbed = (files) => {
|
|
|
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 |
>
|
src/embed/hf.ts
CHANGED
@@ -5,7 +5,7 @@ import { pipeline } from "@xenova/transformers";
|
|
5 |
model?: string;
|
6 |
}
|
7 |
|
8 |
-
export class XenovaTransformersEmbeddings
|
9 |
extends Embeddings
|
10 |
implements XenovaTransformersEmbeddingsParams
|
11 |
{
|
@@ -22,10 +22,11 @@ import { pipeline } from "@xenova/transformers";
|
|
22 |
if (!this.client) {
|
23 |
this.client = await pipeline("embeddings", this.model);
|
24 |
}
|
|
|
25 |
|
26 |
return this.caller.call(async () => {
|
27 |
return await Promise.all(
|
28 |
-
texts.map(async (t) => (await this.client(t)).data)
|
29 |
);
|
30 |
});
|
31 |
}
|
|
|
5 |
model?: string;
|
6 |
}
|
7 |
|
8 |
+
export default class XenovaTransformersEmbeddings
|
9 |
extends Embeddings
|
10 |
implements XenovaTransformersEmbeddingsParams
|
11 |
{
|
|
|
22 |
if (!this.client) {
|
23 |
this.client = await pipeline("embeddings", this.model);
|
24 |
}
|
25 |
+
console.log(this.client, texts);
|
26 |
|
27 |
return this.caller.call(async () => {
|
28 |
return await Promise.all(
|
29 |
+
texts.map(async (t) => (await this.client(t, { pooling: 'mean', normalize: true })).data)
|
30 |
);
|
31 |
});
|
32 |
}
|
src/pages/api/docChat.ts
CHANGED
@@ -3,7 +3,7 @@ import type { NextApiRequest, NextApiResponse } from 'next';
|
|
3 |
import { storesDir } from '@/utils/file-handler';
|
4 |
|
5 |
import { makeChain } from '@/utils/make-chain';
|
6 |
-
import
|
7 |
import { HNSWLib } from 'langchain/vectorstores/hnswlib';
|
8 |
|
9 |
let vectorStore: HNSWLib;
|
|
|
3 |
import { storesDir } from '@/utils/file-handler';
|
4 |
|
5 |
import { makeChain } from '@/utils/make-chain';
|
6 |
+
import XenovaTransformersEmbeddings from '../../embed/hf';
|
7 |
import { HNSWLib } from 'langchain/vectorstores/hnswlib';
|
8 |
|
9 |
let vectorStore: HNSWLib;
|
src/pages/api/docHandle.ts
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import type { NextApiRequest, NextApiResponse } from 'next';
|
2 |
-
|
3 |
import {
|
4 |
readHNSWLibModelFromLocal,
|
5 |
storesDir,
|
@@ -8,16 +7,12 @@ import {
|
|
8 |
import fs from 'fs-extra';
|
9 |
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter';
|
10 |
import { HNSWLib } from 'langchain/vectorstores/hnswlib';
|
11 |
-
import
|
12 |
|
13 |
async function handleDocs(text: string) {
|
14 |
const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 });
|
15 |
const docs = await textSplitter.createDocuments([text]);
|
16 |
-
console.log(docs);
|
17 |
-
|
18 |
const vectorStore = await HNSWLib.fromDocuments(docs, new XenovaTransformersEmbeddings());
|
19 |
-
console.log(vectorStore);
|
20 |
-
|
21 |
return vectorStore;
|
22 |
}
|
23 |
|
@@ -32,6 +27,8 @@ export default async function handler(
|
|
32 |
return res.status(400).json({ message: 'No question in the request' });
|
33 |
}
|
34 |
|
|
|
|
|
35 |
const exists = await fs.exists(storesDir);
|
36 |
console.log(exists);
|
37 |
|
@@ -42,6 +39,7 @@ export default async function handler(
|
|
42 |
...model,
|
43 |
});
|
44 |
}
|
|
|
45 |
|
46 |
const vectorStore = await handleDocs(text);
|
47 |
const model = await vectorStoreToHNSWLibModel(vectorStore);
|
|
|
1 |
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
|
2 |
import {
|
3 |
readHNSWLibModelFromLocal,
|
4 |
storesDir,
|
|
|
7 |
import fs from 'fs-extra';
|
8 |
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter';
|
9 |
import { HNSWLib } from 'langchain/vectorstores/hnswlib';
|
10 |
+
import XenovaTransformersEmbeddings from '../../embed/hf'
|
11 |
|
12 |
async function handleDocs(text: string) {
|
13 |
const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 });
|
14 |
const docs = await textSplitter.createDocuments([text]);
|
|
|
|
|
15 |
const vectorStore = await HNSWLib.fromDocuments(docs, new XenovaTransformersEmbeddings());
|
|
|
|
|
16 |
return vectorStore;
|
17 |
}
|
18 |
|
|
|
27 |
return res.status(400).json({ message: 'No question in the request' });
|
28 |
}
|
29 |
|
30 |
+
/*
|
31 |
+
use dexie instead to get contents
|
32 |
const exists = await fs.exists(storesDir);
|
33 |
console.log(exists);
|
34 |
|
|
|
39 |
...model,
|
40 |
});
|
41 |
}
|
42 |
+
*/
|
43 |
|
44 |
const vectorStore = await handleDocs(text);
|
45 |
const model = await vectorStoreToHNSWLibModel(vectorStore);
|
src/utils/file-handler.ts
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
-
import
|
|
|
2 |
import fs from 'fs-extra';
|
3 |
import {
|
4 |
HNSWLib,
|
|
|
1 |
+
import type XenovaTransformersEmbeddings from '@/embed/hf';
|
2 |
+
// import { HuggingFaceInferenceEmbeddings } from 'langchain/embeddings/hf';
|
3 |
import fs from 'fs-extra';
|
4 |
import {
|
5 |
HNSWLib,
|