Skip to content

Commit 386666c

Browse files
committed
Update docs-vectorize to the embeddinggemma model
1 parent 577eacf commit 386666c

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

apps/docs-vectorize/wrangler.jsonc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"vectorize": [
3232
{
3333
"binding": "VECTORIZE",
34-
"index_name": "docs-bge-base"
34+
"index_name": "docs-embeddinggemma-v1"
3535
}
3636
],
3737
"vars": {

packages/mcp-common/src/tools/docs-vectorize.tools.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export function registerDocsTools(agent: CloudflareMcpAgentNoAccount, env: Requi
4242
.map((result) => {
4343
return `<result>
4444
<url>${result.url}</url>
45+
<title>${result.title}</title>
4546
<text>
4647
${result.text}
4748
</text>
@@ -93,10 +94,8 @@ ${result.text}
9394
}
9495

9596
async function queryVectorize(ai: Ai, vectorizeIndex: VectorizeIndex, query: string, topK: number) {
96-
// Recommendation from: https://huggingface.co/BAAI/bge-base-en-v1.5#model-list
97-
const [queryEmbedding] = await getEmbeddings(ai, [
98-
'Represent this sentence for searching relevant passages: ' + query,
99-
])
97+
// Recommendation from: https://ai.google.dev/gemma/docs/embeddinggemma/model_card#prompt_instructions
98+
const [queryEmbedding] = await getEmbeddings(ai, ['task: search result | query: ' + query])
10099

101100
const { matches } = await vectorizeIndex.query(queryEmbedding, {
102101
topK,
@@ -108,6 +107,7 @@ async function queryVectorize(ai: Ai, vectorizeIndex: VectorizeIndex, query: str
108107
similarity: Math.min(match.score, 1),
109108
id: match.id,
110109
url: sourceToUrl(String(match.metadata?.filePath ?? '')),
110+
title: String(match.metadata?.title ?? ''),
111111
text: String(match.metadata?.text ?? ''),
112112
}))
113113
}
@@ -123,15 +123,15 @@ function sourceToUrl(path: string) {
123123
)
124124
}
125125

126-
async function getEmbeddings(ai: Ai, strings: string[]) {
126+
async function getEmbeddings(ai: Ai, strings: string[]): Promise<number[][]> {
127127
const response = await doWithRetries(() =>
128-
ai.run('@cf/baai/bge-base-en-v1.5', {
128+
// @ts-expect-error embeddinggemma not in types yet
129+
ai.run('@cf/google/embeddinggemma-300m', {
129130
text: strings,
130-
// @ts-expect-error pooling not in types yet
131-
pooling: 'cls',
132131
})
133132
)
134133

134+
// @ts-expect-error embeddinggemma not in types yet
135135
return response.data
136136
}
137137

0 commit comments

Comments
 (0)