Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
});