@@ -3,47 +3,69 @@ import { getDocsFileContent } from "./content.js";
33import { embed } from "ai" ;
44import { openai } from "@ai-sdk/openai" ;
55
6- const MIN_SCORE = process . env . MIN_SCORE ? parseFloat ( process . env . MIN_SCORE ) : 0.25 ;
6+ const MIN_SCORE = process . env . MIN_SCORE
7+ ? parseFloat ( process . env . MIN_SCORE )
8+ : 0.25 ;
79
810if ( ! process . env . OPENAI_API_KEY ) {
911 throw new Error ( "OPENAI_API_KEY is not set" ) ;
1012}
1113
1214export async function queryVectorStore ( query : string ) {
1315 console . log ( `Querying vector store with query: ${ query } ` ) ;
14- const queryEmbedding = await embed ( {
15- model : openai . embedding ( "text-embedding-3-small" ) ,
16- value : query ,
17- maxRetries : 3 ,
18- } ) ;
19-
20- const results = await vectorStore . query ( {
21- indexName : "docs" as any ,
22- queryVector : queryEmbedding . embedding ,
23- topK : 5 ,
24- minScore : MIN_SCORE ,
25- } as any ) ;
16+
17+ console . log ( "Creating embeddings..." ) ;
18+
19+ let queryEmbedding : Awaited < ReturnType < typeof embed > > [ "embedding" ] = [ ] ;
20+ let results : Awaited < ReturnType < typeof vectorStore . query > > = [ ] ;
21+
22+ try {
23+ const queryEmbeddingResult = await embed ( {
24+ model : openai . embedding ( "text-embedding-3-small" ) ,
25+ value : query ,
26+ maxRetries : 3 ,
27+ } ) ;
28+ queryEmbedding = queryEmbeddingResult . embedding ;
29+ } catch ( error ) {
30+ console . error ( "Error creating embeddings:" , error ) ;
31+ throw error ;
32+ }
33+
34+ try {
35+ results = await vectorStore . query ( {
36+ indexName : "docs" as any ,
37+ queryVector : queryEmbedding ,
38+ topK : 5 ,
39+ minScore : MIN_SCORE ,
40+ } as any ) ;
41+ } catch ( error ) {
42+ console . error ( "Error querying vector store:" , error ) ;
43+ throw error ;
44+ }
2645
2746 console . log ( `Retrieved ${ results . length } results` ) ;
2847
29- const contents = await Promise . all ( results . map ( async ( result ) => {
30- const metadata = result . metadata as VectorStoreMetadata ;
31- // const fullDocPage = await getDocsFileContent(metadata.webPath);
32- // const { body } = fullDocPage;
33- const fullDocPage = metadata . content ;
34-
35- return {
36- path : `${ metadata . webPath } ` ,
37- score : parseFloat ( result . score . toFixed ( 3 ) ) ,
38- title : metadata . title ,
39- description : metadata . description ?? "" ,
40- content : fullDocPage ,
41- } ;
42- } ) ) ;
48+ const contents = await Promise . all (
49+ results . map ( async ( result ) => {
50+ const metadata = result . metadata as VectorStoreMetadata ;
51+ // const fullDocPage = await getDocsFileContent(metadata.webPath);
52+ // const { body } = fullDocPage;
53+ const fullDocPage = metadata . content ;
54+
55+ return {
56+ path : `${ metadata . webPath } ` ,
57+ score : parseFloat ( result . score . toFixed ( 3 ) ) ,
58+ title : metadata . title ,
59+ description : metadata . description ?? "" ,
60+ content : fullDocPage ,
61+ } ;
62+ } )
63+ ) ;
4364
4465 // Remove contents with duplicate paths
45- const uniqueContents = contents . filter ( ( content , index , self ) =>
46- index === self . findIndex ( ( t ) => t . path === content . path )
66+ const uniqueContents = contents . filter (
67+ ( content , index , self ) =>
68+ index === self . findIndex ( ( t ) => t . path === content . path )
4769 ) ;
4870
4971 return uniqueContents ;
0 commit comments