1
1
import bodyParser from "body-parser" ;
2
2
import cors from "cors" ;
3
3
import express from "express" ;
4
- import { getChain , search_index } from "./embeddings.js" ;
4
+ import { getChain , initializeSearchIndex } from "./embeddings.js" ;
5
5
import "dotenv/config" ;
6
6
7
7
const app = express ( ) ;
@@ -12,6 +12,8 @@ app.use(
12
12
) ;
13
13
app . use ( bodyParser . raw ( { inflate : true , type : "*/*" } ) ) ;
14
14
15
+ let searchIndex = null ;
16
+
15
17
const port = 3003 ;
16
18
17
19
const template = (
@@ -22,6 +24,10 @@ reference docs. For each question show code examples when applicable.
22
24
${ prompt } `;
23
25
24
26
app . post ( "/" , async ( req , res ) => {
27
+ if ( ! searchIndex ) {
28
+ res . status ( 500 ) . send ( "Search index not initialized" ) ;
29
+ return ;
30
+ }
25
31
// raw to text
26
32
const decoder = new TextDecoder ( ) ;
27
33
const text = decoder . decode ( req . body ) ;
@@ -30,13 +36,20 @@ app.post("/", async (req, res) => {
30
36
31
37
const chain = await getChain ( res ) ;
32
38
await chain . call ( {
33
- input_documents : await ( await search_index ) . similaritySearch ( templated , 4 ) ,
39
+ input_documents : await searchIndex . similaritySearch ( templated , 4 ) ,
34
40
question : templated ,
35
41
} ) ;
36
42
37
43
res . end ( ) ;
38
44
} ) ;
39
45
40
- app . listen ( port , ( ) => {
46
+ app . listen ( port , async ( ) => {
41
47
console . log ( `Started server on port: ${ port } ` ) ;
48
+ console . log ( 'Initializing search index...' ) ;
49
+ try {
50
+ searchIndex = await initializeSearchIndex ( ) ;
51
+ console . log ( 'Search index initialized' ) ;
52
+ } catch ( e ) {
53
+ console . error ( e ) ;
54
+ }
42
55
} ) ;
0 commit comments