Skip to content

Commit 92bb0ba

Browse files
committed
Refactor semantic search gateway ID references to use embedding gateway ID
Updated the codebase to replace instances of `CLOUDFLARE_AI_GATEWAY_ID` with `CLOUDFLARE_AI_EMBEDDING_GATEWAY_ID` in the semantic search functionality. This change clarifies the distinction between the two gateway IDs and ensures that both runtime queries and indexing jobs utilize the correct embedding gateway. Additionally, updated related documentation and test cases to reflect this change.
1 parent 7b5f9fb commit 92bb0ba

5 files changed

Lines changed: 18 additions & 18 deletions

File tree

.env.example

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ CLOUDFLARE_API_TOKEN=MOCK_CLOUDFLARE_API_TOKEN
105105
CLOUDFLARE_VECTORIZE_INDEX=MOCK_CLOUDFLARE_VECTORIZE_INDEX
106106
# Route Workers AI requests through Cloudflare AI Gateway (gateway name/id)
107107
CLOUDFLARE_AI_GATEWAY_ID=MOCK_CLOUDFLARE_AI_GATEWAY_ID
108-
# Optional: indexing/batch embedding jobs can use a different AI Gateway (for
109-
# example, without guardrails). Runtime user search queries still use
110-
# CLOUDFLARE_AI_GATEWAY_ID. Falls back to CLOUDFLARE_AI_GATEWAY_ID when omitted.
108+
# Optional: embedding requests can use a different AI Gateway (for example,
109+
# without guardrails). Used by semantic-search embeddings for both runtime
110+
# queries and indexing jobs. Falls back to CLOUDFLARE_AI_GATEWAY_ID when omitted.
111111
CLOUDFLARE_AI_EMBEDDING_GATEWAY_ID=MOCK_CLOUDFLARE_AI_EMBEDDING_GATEWAY_ID
112112
# AI Gateway Authenticated Gateway token (sent as `cf-aig-authorization`)
113113
CLOUDFLARE_AI_GATEWAY_AUTH_TOKEN=MOCK_CLOUDFLARE_AI_GATEWAY_AUTH_TOKEN

app/utils/__tests__/semantic-search.server.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ vi.mock('#app/utils/semantic-search-presentation.server.ts', () => ({
2121

2222
import { semanticSearchKCD } from '../semantic-search.server.ts'
2323

24-
test('semanticSearchKCD routes user query embeddings through CLOUDFLARE_AI_GATEWAY_ID', async () => {
24+
test('semanticSearchKCD routes user query embeddings through CLOUDFLARE_AI_EMBEDDING_GATEWAY_ID', async () => {
2525
using ignoredEnv = setEnv({
2626
CLOUDFLARE_ACCOUNT_ID: 'cf-account',
2727
CLOUDFLARE_API_TOKEN: 'cf-token',
@@ -70,8 +70,8 @@ test('semanticSearchKCD routes user query embeddings through CLOUDFLARE_AI_GATEW
7070
.find((url) => url.includes('/workers-ai/'))
7171

7272
expect(embeddingRequestUrl).toBeDefined()
73-
expect(embeddingRequestUrl).toContain('/runtime-search-gateway/')
74-
expect(embeddingRequestUrl).not.toContain('/indexing-only-gateway/')
73+
expect(embeddingRequestUrl).toContain('/indexing-only-gateway/')
74+
expect(embeddingRequestUrl).not.toContain('/runtime-search-gateway/')
7575
} finally {
7676
fetchSpy.mockRestore()
7777
}

app/utils/env.server.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ const schemaBase = z.object({
6868
/** AI Gateway "id" is the gateway name you create in Cloudflare. */
6969
CLOUDFLARE_AI_GATEWAY_ID: nonEmptyString,
7070
/**
71-
* Optional indexing/batch embedding AI Gateway id.
72-
* Runtime user search queries continue to use `CLOUDFLARE_AI_GATEWAY_ID`.
71+
* Optional embedding AI Gateway id.
72+
* Used by semantic-search embeddings (runtime queries + indexing jobs).
7373
* Falls back to `CLOUDFLARE_AI_GATEWAY_ID` when omitted.
7474
*/
7575
CLOUDFLARE_AI_EMBEDDING_GATEWAY_ID: z.string().trim().optional(),
@@ -184,9 +184,9 @@ export type Env = Omit<
184184
*/
185185
CLOUDFLARE_AI_CALL_KENT_TRANSCRIPT_FORMAT_MODEL: string
186186
/**
187-
* Indexing/batch embedding jobs can be routed through a separate gateway
188-
* (for example, with guardrails disabled). Runtime user search queries
189-
* should use `CLOUDFLARE_AI_GATEWAY_ID`.
187+
* Embedding calls can be routed through a separate gateway (for example, with
188+
* guardrails disabled). This is used by semantic-search embeddings for both
189+
* runtime user queries and indexing jobs.
190190
*/
191191
CLOUDFLARE_AI_EMBEDDING_GATEWAY_ID: string
192192
/** Derived from CLOUDFLARE_ACCOUNT_ID when not explicitly set. */

app/utils/semantic-search.server.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ function getRequiredSemanticSearchEnv() {
184184
return {
185185
accountId: env.CLOUDFLARE_ACCOUNT_ID,
186186
apiToken: env.CLOUDFLARE_API_TOKEN,
187-
gatewayId: env.CLOUDFLARE_AI_GATEWAY_ID,
187+
embeddingGatewayId: env.CLOUDFLARE_AI_EMBEDDING_GATEWAY_ID,
188188
gatewayAuthToken: env.CLOUDFLARE_AI_GATEWAY_AUTH_TOKEN,
189189
indexName: env.CLOUDFLARE_VECTORIZE_INDEX,
190190
embeddingModel: env.CLOUDFLARE_AI_EMBEDDING_MODEL,
@@ -227,21 +227,21 @@ async function cloudflareFetch(
227227
async function getEmbedding({
228228
accountId,
229229
apiToken,
230-
gatewayId,
230+
embeddingGatewayId,
231231
gatewayAuthToken,
232232
model,
233233
text,
234234
}: {
235235
accountId: string
236236
apiToken: string
237-
gatewayId: string
237+
embeddingGatewayId: string
238238
gatewayAuthToken: string
239239
model: string
240240
text: string
241241
}) {
242242
const url = getWorkersAiRunUrl({
243243
accountId,
244-
gatewayId,
244+
gatewayId: embeddingGatewayId,
245245
model,
246246
})
247247
const res = await fetch(url, {
@@ -502,7 +502,7 @@ export async function semanticSearchKCD({
502502
const {
503503
accountId,
504504
apiToken,
505-
gatewayId,
505+
embeddingGatewayId,
506506
gatewayAuthToken,
507507
indexName,
508508
embeddingModel,
@@ -539,7 +539,7 @@ export async function semanticSearchKCD({
539539
const vector = await getEmbedding({
540540
accountId,
541541
apiToken,
542-
gatewayId,
542+
embeddingGatewayId,
543543
gatewayAuthToken,
544544
model: embeddingModel,
545545
text: cleanedQuery,

other/semantic-search/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ and shared utilities.
2424
- `CLOUDFLARE_API_TOKEN`
2525
- `CLOUDFLARE_VECTORIZE_INDEX`
2626
- `CLOUDFLARE_AI_EMBEDDING_MODEL` (optional; defaults in code)
27-
- `CLOUDFLARE_AI_EMBEDDING_GATEWAY_ID` (optional; indexers only; defaults to `CLOUDFLARE_AI_GATEWAY_ID`)
27+
- `CLOUDFLARE_AI_EMBEDDING_GATEWAY_ID` (optional; used by semantic-search embeddings for runtime queries + indexers; defaults to `CLOUDFLARE_AI_GATEWAY_ID`)
2828

2929
- `R2_BUCKET`
3030

0 commit comments

Comments
 (0)