Skip to content

Commit

Permalink
Merge pull request #12 from appwrite/fix-missing-key
Browse files Browse the repository at this point in the history
  • Loading branch information
TorstenDittmann authored Sep 23, 2023
2 parents 8dab6e4 + 905eac6 commit 3d2d5c7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
31 changes: 18 additions & 13 deletions embeddings.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,25 @@ async function chunk_sources(sources) {
return source_chunks;
}

const sources = globSync("docs/*.json").map((filename) => {
const source = JSON.parse(fs.readFileSync(filename));
return new Document({
pageContent: source.page_content,
metadata: source.metadata,


export const initializeSearchIndex = async () => {
const sources = globSync("docs/*.json").map((filename) => {
const source = JSON.parse(fs.readFileSync(filename));
return new Document({
pageContent: source.page_content,
metadata: source.metadata,
});
});
});

export const search_index = FaissStore.fromDocuments(
await chunk_sources(sources),
new OpenAIEmbeddings({
openAIApiKey: process.env._APP_ASSISTANT_OPENAI_API_KEY,
})
);

return FaissStore.fromDocuments(
await chunk_sources(sources),
new OpenAIEmbeddings({
openAIApiKey: process.env._APP_ASSISTANT_OPENAI_API_KEY,
})
);
}

export const getChain = (res) => {
return loadQAStuffChain(
new OpenAIChat({
Expand Down
19 changes: 16 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import bodyParser from "body-parser";
import cors from "cors";
import express from "express";
import { getChain, search_index } from "./embeddings.js";
import { getChain, initializeSearchIndex } from "./embeddings.js";
import "dotenv/config";

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

let searchIndex = null;

const port = 3003;

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

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

const chain = await getChain(res);
await chain.call({
input_documents: await (await search_index).similaritySearch(templated, 4),
input_documents: await searchIndex.similaritySearch(templated, 4),
question: templated,
});

res.end();
});

app.listen(port, () => {
app.listen(port, async () => {
console.log(`Started server on port: ${port}`);
console.log('Initializing search index...');
try {
searchIndex = await initializeSearchIndex();
console.log('Search index initialized');
} catch (e) {
console.error(e);
}
});

0 comments on commit 3d2d5c7

Please sign in to comment.