Skip to content

Commit 31eb7e4

Browse files
committed
semantic caching in use for both Google and OpenAI
1 parent 44882f1 commit 31eb7e4

File tree

4 files changed

+65
-52
lines changed

4 files changed

+65
-52
lines changed

app/src/app/page.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default function Home() {
3030

3131
const handleSearch = async (question: string) => {
3232
// Replace with your API call
33+
setResults(undefined);
3334
const response = await fetch(`/api/search?question=${question}`);
3435
const data: VideoSearchResult = await response.json();
3536
setResults(data);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323
},
2424
"author": "",
2525
"license": "ISC"
26-
}
26+
}

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

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ async function getAnswer(question: string, videos: VideoDocument[]) {
3434
location: 'google.search.getAnswer',
3535
});
3636

37+
const answer = (await questionAnswerChain.invoke({
38+
question,
39+
data: JSON.stringify(videos),
40+
})) as string;
41+
42+
return answer;
43+
}
44+
45+
export async function search(question: string) {
46+
log.debug(`Original question: ${question}`, {
47+
location: 'google.search.search',
48+
});
49+
const semanticQuestion = (await summarize.question(question)) as string;
50+
3751
const haveAnswers = await answerVectorStore.checkIndexExists();
3852

3953
if (haveAnswers) {
@@ -52,36 +66,17 @@ async function getAnswer(question: string, videos: VideoDocument[]) {
5266
answer: result[0],
5367
score: result[1],
5468
});
55-
69+
5670
if (Array.isArray(result) && result.length > 0) {
57-
return result[0];
71+
log.debug(`Found answer: ${result[0].metadata.answer}`, {
72+
location: 'google.search.getAnswer',
73+
result: result[0].metadata,
74+
});
75+
76+
return result[0].metadata;
5877
}
5978
}
6079

61-
const answer = (await questionAnswerChain.invoke({
62-
question,
63-
data: JSON.stringify(videos),
64-
})) as string;
65-
66-
await answerVectorStore.addDocuments([
67-
new Document({
68-
metadata: {
69-
videos,
70-
answer,
71-
},
72-
pageContent: question,
73-
}),
74-
]);
75-
76-
return answer;
77-
}
78-
79-
export async function search(question: string) {
80-
log.debug(`Original question: ${question}`, {
81-
location: 'google.search.search',
82-
});
83-
const semanticQuestion = (await summarize.question(question)) as string;
84-
8580
log.debug(`Semantic question: ${semanticQuestion}`, {
8681
location: 'google.search.search',
8782
});
@@ -102,8 +97,20 @@ export async function search(question: string) {
10297
location: 'openai.search.search',
10398
});
10499

100+
const answer = getAnswer(semanticQuestion, videos);
101+
102+
await answerVectorStore.addDocuments([
103+
new Document({
104+
metadata: {
105+
videos,
106+
answer,
107+
},
108+
pageContent: question,
109+
}),
110+
]);
111+
105112
return {
106113
videos,
107-
answer: await getAnswer(semanticQuestion, videos),
114+
answer,
108115
};
109116
}

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

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,6 @@ async function getAnswer(question: string, videos: VideoDocument[]) {
3434
location: 'openai.search.getAnswer',
3535
});
3636

37-
const haveAnswers = await answerVectorStore.checkIndexExists();
38-
39-
if (haveAnswers) {
40-
log.debug(`Searching for closest answer to question: ${question}`, {
41-
location: 'google.search.getAnswer',
42-
question,
43-
});
44-
45-
const [result] = await answerVectorStore.similaritySearchWithScore(
46-
question,
47-
1,
48-
);
49-
50-
log.debug(`Found closest answer with score: ${String(result[1])}`, {
51-
location: 'google.search.getAnswer',
52-
answer: result[0],
53-
score: result[1],
54-
});
55-
56-
if (Array.isArray(result) && result.length > 0) {
57-
return result[0];
58-
}
59-
}
60-
6137
const answer = (await questionAnswerChain.invoke({
6238
question,
6339
data: JSON.stringify(videos),
@@ -82,6 +58,35 @@ export async function search(question: string) {
8258
});
8359
const semanticQuestion = (await summarize.question(question)) as string;
8460

61+
const haveAnswers = await answerVectorStore.checkIndexExists();
62+
63+
if (haveAnswers) {
64+
log.debug(`Searching for closest answer to question: ${question}`, {
65+
location: 'openai.search.getAnswer',
66+
question,
67+
});
68+
69+
const [result] = await answerVectorStore.similaritySearchWithScore(
70+
question,
71+
1,
72+
);
73+
74+
log.debug(`Found closest answer with score: ${String(result[1])}`, {
75+
location: 'openai.search.getAnswer',
76+
answer: result[0],
77+
score: result[1],
78+
});
79+
80+
if (Array.isArray(result) && result.length > 0) {
81+
log.debug(`Found answer: ${result[0].metadata.answer}`, {
82+
location: 'openai.search.getAnswer',
83+
result: result[0].metadata,
84+
});
85+
86+
return result[0].metadata;
87+
}
88+
}
89+
8590
log.debug(`Semantic question: ${semanticQuestion}`, {
8691
location: 'openai.search.search',
8792
});

0 commit comments

Comments
 (0)