-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (53 loc) · 1.43 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
require("dotenv").config()
const { createClient } = require("redis")
const express = require("express")
const { randomUUID } = require("crypto")
const { addEmbedding, chat, createRedisStackIndex } = require("./core")
const {
PORT
} = process.env
void (async () => {
const app = express()
const redisClient = createClient()
redisClient.on('error', err => {
if (err.code === "ECONNREFUSED") {
console.log("waiting for redis to start...")
} else {
console.log('Redis Client Error', err)
}
})
await redisClient.connect()
try {
await createRedisStackIndex(redisClient)
} catch(err) {
if (!err.message.includes("Index already exists")) {
throw err
}
}
app.use(express.json())
app.post("/createEmbedding", async (req, res) => {
const text = req.body.text
const id = randomUUID()
await addEmbedding(redisClient, id, text)
console.log("Embedding created:", id, text)
res.json({ success: true })
})
app.post("/chat", async (req, res) => {
try {
const output = await chat(req.body.prompt, redisClient)
res.json({
response: output
})
} catch(err) {
console.log("Error in /chat", err)
res.status(500).send(err)
}
})
app.use(express.static("public"))
app.get("/embeddings", async (req, res) => {
res.send()
})
app.listen(PORT, () => console.log(`Server listening on port ${PORT}`))
})().catch(err => {
console.error(err)
})