-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (57 loc) · 1.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import dotenv from "dotenv";
dotenv.config();
import express from "express";
import cors from "cors";
import * as fs from "fs";
import { OpenAI } from "langchain/llms/openai";
import { ConversationalRetrievalQAChain } from "langchain/chains";
import { HNSWLib } from "langchain/vectorstores/hnswlib";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
const app = express();
const port = process.env.PORT || 4000;
app.use(express.json());
app.use(cors());
/* Initialize the LLM to use to answer the question */
const model = new OpenAI({ openAIApiKey: process.env.OPENAI_API_KEY });
/* Load in the file we want to do question answering over */
const text = fs.readFileSync("./Handbook1.txt", "utf8");
/* Split the text into chunks */
const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 });
const docs = await textSplitter.createDocuments([text]);
/* Create the vectorstore */
const vectorStore = await HNSWLib.fromDocuments(docs, new OpenAIEmbeddings());
/* Create the chain */
const chain = ConversationalRetrievalQAChain.fromLLM(
model,
vectorStore.asRetriever()
);
app.post("/chatbot", async (req, res) => {
try {
const { prompt } = req.body;
const result = await chain.call({ question: prompt, chat_history: [] });
const data = {
question: prompt,
response: result.text,
};
const filePath = "responses.txt";
const textData = Object.entries(data)
.map(([key, value]) => `\n${key}: ${value}`)
.join("");
fs.appendFile(filePath, textData, "utf8", (err) => {
if (err) {
console.error("An error occurred while writing the file:", err);
} else {
console.log("File has been successfully written!");
}
});
res.json({
message: result,
});
} catch (error) {
res.json({ error });
}
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});