Skip to content

Commit 3d2d5c7

Browse files
Merge pull request #12 from appwrite/fix-missing-key
2 parents 8dab6e4 + 905eac6 commit 3d2d5c7

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

embeddings.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,25 @@ async function chunk_sources(sources) {
3030
return source_chunks;
3131
}
3232

33-
const sources = globSync("docs/*.json").map((filename) => {
34-
const source = JSON.parse(fs.readFileSync(filename));
35-
return new Document({
36-
pageContent: source.page_content,
37-
metadata: source.metadata,
33+
34+
35+
export const initializeSearchIndex = async () => {
36+
const sources = globSync("docs/*.json").map((filename) => {
37+
const source = JSON.parse(fs.readFileSync(filename));
38+
return new Document({
39+
pageContent: source.page_content,
40+
metadata: source.metadata,
41+
});
3842
});
39-
});
40-
41-
export const search_index = FaissStore.fromDocuments(
42-
await chunk_sources(sources),
43-
new OpenAIEmbeddings({
44-
openAIApiKey: process.env._APP_ASSISTANT_OPENAI_API_KEY,
45-
})
46-
);
43+
44+
return FaissStore.fromDocuments(
45+
await chunk_sources(sources),
46+
new OpenAIEmbeddings({
47+
openAIApiKey: process.env._APP_ASSISTANT_OPENAI_API_KEY,
48+
})
49+
);
50+
}
51+
4752
export const getChain = (res) => {
4853
return loadQAStuffChain(
4954
new OpenAIChat({

main.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import bodyParser from "body-parser";
22
import cors from "cors";
33
import express from "express";
4-
import { getChain, search_index } from "./embeddings.js";
4+
import { getChain, initializeSearchIndex } from "./embeddings.js";
55
import "dotenv/config";
66

77
const app = express();
@@ -12,6 +12,8 @@ app.use(
1212
);
1313
app.use(bodyParser.raw({ inflate: true, type: "*/*" }));
1414

15+
let searchIndex = null;
16+
1517
const port = 3003;
1618

1719
const template = (
@@ -22,6 +24,10 @@ reference docs. For each question show code examples when applicable.
2224
${prompt}`;
2325

2426
app.post("/", async (req, res) => {
27+
if (!searchIndex) {
28+
res.status(500).send("Search index not initialized");
29+
return;
30+
}
2531
// raw to text
2632
const decoder = new TextDecoder();
2733
const text = decoder.decode(req.body);
@@ -30,13 +36,20 @@ app.post("/", async (req, res) => {
3036

3137
const chain = await getChain(res);
3238
await chain.call({
33-
input_documents: await (await search_index).similaritySearch(templated, 4),
39+
input_documents: await searchIndex.similaritySearch(templated, 4),
3440
question: templated,
3541
});
3642

3743
res.end();
3844
});
3945

40-
app.listen(port, () => {
46+
app.listen(port, async () => {
4147
console.log(`Started server on port: ${port}`);
48+
console.log('Initializing search index...');
49+
try {
50+
searchIndex = await initializeSearchIndex();
51+
console.log('Search index initialized');
52+
} catch (e) {
53+
console.error(e);
54+
}
4255
});

0 commit comments

Comments
 (0)