11import bodyParser from "body-parser" ;
22import cors from "cors" ;
33import express from "express" ;
4- import { getChain , search_index } from "./embeddings.js" ;
4+ import { getChain , initializeSearchIndex } from "./embeddings.js" ;
55import "dotenv/config" ;
66
77const app = express ( ) ;
@@ -12,6 +12,8 @@ app.use(
1212) ;
1313app . use ( bodyParser . raw ( { inflate : true , type : "*/*" } ) ) ;
1414
15+ let searchIndex = null ;
16+
1517const port = 3003 ;
1618
1719const template = (
@@ -22,6 +24,10 @@ reference docs. For each question show code examples when applicable.
2224${ prompt } `;
2325
2426app . post ( "/" , async ( req , res ) => {
27+ if ( ! searchIndex ) {
28+ res . status ( 500 ) . send ( "Search index not initialized" ) ;
29+ return ;
30+ }
2531 // raw to text
2632 const decoder = new TextDecoder ( ) ;
2733 const text = decoder . decode ( req . body ) ;
@@ -30,13 +36,20 @@ app.post("/", async (req, res) => {
3036
3137 const chain = await getChain ( res ) ;
3238 await chain . call ( {
33- input_documents : await ( await search_index ) . similaritySearch ( templated , 4 ) ,
39+ input_documents : await searchIndex . similaritySearch ( templated , 4 ) ,
3440 question : templated ,
3541 } ) ;
3642
3743 res . end ( ) ;
3844} ) ;
3945
40- app . listen ( port , ( ) => {
46+ app . listen ( port , async ( ) => {
4147 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+ }
4255} ) ;
0 commit comments