@@ -41,47 +41,57 @@ export default function initialize({
4141 > ) ;
4242 }
4343
44- async function searchVideos ( question : string ) {
45- log . debug ( `Original question: ${ question } ` , {
46- location : `${ prefix } .search.search` ,
44+ async function checkAnswerCache ( question : string ) {
45+ const haveAnswers = await answerVectorStore . checkIndexExists ( ) ;
46+
47+ if ( ! ( haveAnswers && config . searches . answerCache ) ) {
48+ return ;
49+ }
50+
51+ log . debug ( `Searching for closest answer to question: ${ question } ` , {
52+ location : `${ prefix } .search.getAnswer` ,
53+ question,
4754 } ) ;
48- const semanticQuestion = await prompt . getSemanticQuestion ( question ) ;
4955
50- const haveAnswers = await answerVectorStore . checkIndexExists ( ) ;
56+ const [ result ] = await answerVectorStore . similaritySearchWithScore (
57+ question ,
58+ 1 ,
59+ ) ;
5160
52- if ( haveAnswers && config . searches . answerCache ) {
53- log . debug (
54- `Searching for closest answer to question: ${ semanticQuestion } ` ,
55- {
56- location : `${ prefix } .search.getAnswer` ,
57- question,
58- } ,
59- ) ;
61+ if ( Array . isArray ( result ) && result . length > 0 ) {
62+ log . debug ( `Found closest answer with score: ${ String ( result [ 1 ] ) } ` , {
63+ location : `${ prefix } .search.getAnswer` ,
64+ score : result [ 1 ] ,
65+ } ) ;
6066
61- const [ result ] = await answerVectorStore . similaritySearchWithScore (
62- semanticQuestion ,
63- 1 ,
64- ) ;
67+ if ( result [ 1 ] < 0.2 ) {
68+ log . debug ( `Found answer to question ${ question } ` , {
69+ location : ` ${ prefix } .search.getAnswer` ,
70+ } ) ;
6571
66- if ( Array . isArray ( result ) && result . length > 0 ) {
67- log . debug (
68- `Found closest answer with score: ${ String ( result [ 1 ] ) } ` ,
69- {
70- location : `${ prefix } .search.getAnswer` ,
71- score : result [ 1 ] ,
72- } ,
73- ) ;
74-
75- if ( Array . isArray ( result ) && result . length > 0 ) {
76- log . debug ( `Found answer to question ${ semanticQuestion } ` , {
77- location : `${ prefix } .search.getAnswer` ,
78- } ) ;
79-
80- return result [ 0 ] . metadata ;
81- }
72+ return result [ 0 ] . metadata ;
8273 }
74+
75+ log . debug ( `Score too low for question ${ question } ` , {
76+ location : `${ prefix } .search.getAnswer` ,
77+ score : result [ 1 ] ,
78+ } ) ;
79+ }
80+ }
81+
82+ async function searchVideos ( question : string ) {
83+ log . debug ( `Original question: ${ question } ` , {
84+ location : `${ prefix } .search.search` ,
85+ } ) ;
86+
87+ const existingAnswer = await checkAnswerCache ( question ) ;
88+
89+ if ( typeof existingAnswer !== 'undefined' ) {
90+ return existingAnswer ;
8391 }
8492
93+ const semanticQuestion = await prompt . getSemanticQuestion ( question ) ;
94+
8595 log . debug ( `Semantic question: ${ semanticQuestion } ` , {
8696 location : `${ prefix } .search.search` ,
8797 } ) ;
@@ -102,11 +112,12 @@ export default function initialize({
102112 location : `${ prefix } .search.search` ,
103113 } ) ;
104114
105- const answerDocument = await prompt . answerQuestion (
106- semanticQuestion ,
107- videos ,
108- ) ;
109- await answerVectorStore . addDocuments ( [ answerDocument ] ) ;
115+ // TODO: modify the prompt to ask both questions
116+ const answerDocument = await prompt . answerQuestion ( question , videos ) ;
117+
118+ if ( config . searches . answerCache ) {
119+ await answerVectorStore . addDocuments ( [ answerDocument ] ) ;
120+ }
110121
111122 return answerDocument . metadata ;
112123 }
0 commit comments