-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
169 lines (144 loc) · 5.48 KB
/
Copy pathapp.js
File metadata and controls
169 lines (144 loc) · 5.48 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import express from 'express'
import http from 'http'
import crypto from 'crypto'
import { HNSWLib } from 'langchain/vectorstores/hnswlib'
import { WebBrowser } from 'langchain/tools/webbrowser'
import { OpenAIEmbeddings } from 'langchain/embeddings/openai'
import { PromptTemplate } from 'langchain/prompts'
import { OpenAI } from 'langchain/llms/openai'
import { LLMChain, VectorDBQAChain } from 'langchain/chains'
import { CheerioWebBaseLoader } from 'langchain/document_loaders/web/cheerio'
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter'
import { Document } from 'langchain/document'
import * as fs from 'fs/promises'
// Load environment variables from .env file
import * as dotenv from 'dotenv'
dotenv.config()
// Setup the secret phrase and IV
const key = crypto.createHash('sha256').update(process.env.SECRET).digest()
const iv = Buffer.alloc(16)
// Set up Express app
const app = express()
// Middleware to parse JSON request bodies
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
// Question endpoint
app.post('/api/question', async (req, res) => {
// Get the search query and APIKey from the request body
const { question, apikey } = req.body
// Create an AES-256-CBC cipher using the secret phrase and IV
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv)
// Encrypt the APIKey using the cipher
let encrypted = cipher.update(apikey, 'utf8', 'base64')
encrypted += cipher.final('base64')
// Instantiate the OpenAI model
const llm = new OpenAI({
modelName: 'gpt-3.5-turbo',
//modelName: 'gpt-4',
concurrency: 5,
cache: true,
temperature: 0,
})
// Use the encrypted APIKey as the directory name
const directory = `./${cleanFilePath(encrypted)}/`
// Load the vector store from the same directory
let vectorStore
try {
vectorStore = await HNSWLib.load(directory, new OpenAIEmbeddings())
} catch (err) {
// If the vector store doesn't exist yet, create a default one
vectorStore = null
}
try {
if (vectorStore) {
// Load the Q&A map reduce chain
const chain = VectorDBQAChain.fromLLM(llm, vectorStore)
const response = await chain.call({
query: question,
})
// Return the response to the user
res.json({ response: response.text })
} else {
// We don't have a vector store yet, so we'll just use a template
const template =
"Your are a kind AI Assistant. Try to answer the following question: {question} If you don't know the answer, just say \"Hmm, I'm not sure.\" Don't try to make up an answer."
const prompt = new PromptTemplate({
template: template,
inputVariables: ['question'],
})
const chain = new LLMChain({ llm: llm, prompt: prompt })
const response = await chain.call({ question: question })
// Return the response to the user
res.json({ response: cleanText(response.text) })
}
} catch (err) {
console.error(err)
res.status(500).json({ message: 'Error processing the request' })
}
})
app.post('/api/parser', async (req, res) => {
const { url, apikey } = req.body
const loader = new CheerioWebBaseLoader(url)
const docs = await loader.load()
const textSplitter = new RecursiveCharacterTextSplitter({
chunkSize: 2500,
chunkOverlap: 200,
})
const docOutput = await textSplitter.splitDocuments(docs)
// Create an AES-256-CBC cipher using the secret phrase and IV
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv)
let encrypted = cipher.update(apikey, 'utf8', 'base64')
encrypted += cipher.final('base64')
const directory = `./${cleanFilePath(encrypted)}/`
// Create a new document for the URL
let vectorStore
let already = false
try {
// Load the vector store from the exisiting directory
vectorStore = await HNSWLib.load(directory, new OpenAIEmbeddings())
// Load the JSON file
const data = await fs.readFile(`${directory}docstore.json`)
const db = JSON.parse(data)
// Check if metadata with the same source already exists
const source = url
const exists = db.some(
([id, { metadata }]) => metadata && metadata.source === source
)
// Check if the source already exists
if (exists) {
already = true
console.log(`Source "${source}" already exists`)
} else {
console.log(`Source "${source}" added to vector store`)
await vectorStore.addDocuments(docOutput)
}
} catch (err) {
// If the vector store doesn't exist yet, create a new one
vectorStore = await HNSWLib.fromDocuments(docOutput, new OpenAIEmbeddings())
}
// Save the vector store to a directory
await vectorStore.save(directory)
try {
// Return the response to the user
res.json({ response: 'success', already: already })
} catch (err) {
console.error(err)
res.status(500).json({ message: 'Error processing conversation request' })
}
})
function cleanFilePath(filePath) {
// Define a regular expression that matches all non-alphanumeric characters and hyphens
const regex = /[^a-zA-Z0-9\-]/g
// Replace all non-matching characters with an empty string
const cleanedFilePath = filePath.replace(regex, '')
return cleanedFilePath
}
function cleanText(text) {
// Define a regular expression that matches all newlines in the beginning of the string
const regex = /^[\n]+/
const cleanedText = text.replace(regex, '')
return cleanedText
}
// Create HTTP server
http.createServer(app).listen(process.env.PORT)
console.info('KB API is listening on port ' + process.env.PORT)