Skip to content

Commit 2209919

Browse files
committed
caching original question as answer, limiting acceptable answer score
1 parent 58488dc commit 2209919

File tree

3 files changed

+61
-42
lines changed

3 files changed

+61
-42
lines changed

services/video-search/src/api/search.ts

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

services/video-search/src/api/store.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,31 @@ export default function initialize({
1818
prefix,
1919
embeddings,
2020
}: Pick<ApiConfig, 'prefix' | 'embeddings'>): Store {
21+
/**
22+
* This vector store will match a short question (e.g. "Tell me about streams") with
23+
* a bank of longer transcript summaries + questions.
24+
*/
2125
const vectorStore = new RedisVectorStore(embeddings, {
2226
redisClient: client,
2327
indexName: `${prefix}-${config.redis.VIDEO_INDEX_NAME}`,
2428
keyPrefix: `${prefix}-${config.redis.VIDEO_PREFIX}`,
2529
indexOptions: {
2630
ALGORITHM: VectorAlgorithms.HNSW,
27-
DISTANCE_METRIC: 'COSINE',
31+
DISTANCE_METRIC: 'IP',
2832
},
2933
});
3034

35+
/**
36+
* This vector store will match a short question (e.g. "Tell me about streams") with previously
37+
* answered questions (e.g. "What is a stream?")
38+
*/
3139
const answerVectorStore = new RedisVectorStore(embeddings, {
3240
redisClient: client,
3341
indexName: `${prefix}-${config.redis.ANSWER_INDEX_NAME}`,
3442
keyPrefix: `${prefix}-${config.redis.ANSWER_PREFIX}`,
3543
indexOptions: {
36-
ALGORITHM: VectorAlgorithms.HNSW,
37-
DISTANCE_METRIC: 'COSINE',
44+
ALGORITHM: VectorAlgorithms.FLAT,
45+
DISTANCE_METRIC: 'L2',
3846
},
3947
});
4048

services/video-search/src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export default {
5252
},
5353
searches: {
5454
KNN: 3,
55-
answerCache: false,
55+
answerCache: true,
5656
},
5757
log: {
5858
LEVEL: LOG_LEVEL ?? 'info',

0 commit comments

Comments
 (0)