From 905eac6c4f9c08026f64de5714bbb9ca36ac07e9 Mon Sep 17 00:00:00 2001 From: Steven Nguyen Date: Tue, 19 Sep 2023 11:58:56 -0700 Subject: [PATCH] Update assistant to not crash on startup. If the _APP_ASSISTANT_OPENAI_API_KEY env var was not set, the assistant would crash. This prevents that from happening and returns an error message if the key is not set. --- embeddings.js | 31 ++++++++++++++++++------------- main.js | 19 ++++++++++++++++--- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/embeddings.js b/embeddings.js index 7900e9d..cb475f0 100644 --- a/embeddings.js +++ b/embeddings.js @@ -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({ diff --git a/main.js b/main.js index b8ad6d4..b1e1fbe 100644 --- a/main.js +++ b/main.js @@ -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(); @@ -12,6 +12,8 @@ app.use( ); app.use(bodyParser.raw({ inflate: true, type: "*/*" })); +let searchIndex = null; + const port = 3003; const template = ( @@ -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); @@ -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); + } });