Orange Book
End-to-end projects

Project three a Workers AI and Vectorize knowledge assistant

Build minimum RAG with multilingual embeddings, traceable metadata, protected ingestion, and an explicit no-context branch.

Edited and verified by Orange Book Editorial Team ·

PROJECT 03ADVANCEDAbout 70 minutesOutcome: minimal sourced RAG

Done means

/index and /ask require separate secrets and Vectorize metadata traces to source text; queries pass only embedding.data[0], while no matches return 404 no_context instead of inviting unsupported generation.

Workers AI and Vectorize knowledge assistant architecture
Ingestion generates vectors for asynchronous upsert; query retrieves metadata before sending constrained context to generation.Open original
Detailed description
An administrator calls the Worker ingestion endpoint with an ingest secret. The Worker uses the multilingual bge-m3 model through Workers AI and writes vectors, text, and source metadata to Vectorize. For a user question, it produces one query embedding and passes only the first data vector to Vectorize. Up to three matches with metadata become constrained generation context. With no match, the Worker returns no_context and never calls the generation model.
  1. 01
    Ingest

    A secret-protected document endpoint.

  2. 02
    Embed

    bge-m3 produces multilingual embeddings.

  3. 03
    Retrieve

    Vectorize returns source-text metadata.

  4. 04
    Answer

    Generation runs only with matched context.

Files and resources

README.md
wrangler.jsonc
index.mjs

Runnable source lives in examples/ai-knowledge-assistant/. INGEST_KEY and QUERY_KEY are secrets and must not be vars values.

Prepare and verify

Create the multilingual vector index

pnpm wrangler vectorize create orange-book-knowledge --preset @cf/baai/bge-m3

This creates a remote resource and requires account authorization; confirm the target account first. --preset lets Wrangler derive dimensions and metric from the current embedding model.

Set the secret and start

cp examples/ai-knowledge-assistant/.dev.vars.example examples/ai-knowledge-assistant/.dev.vars
pnpm wrangler dev --config examples/ai-knowledge-assistant/wrangler.jsonc

Replace both placeholders in .dev.vars and never commit that file. Workers AI and Vectorize use remote service quota and are not a fully offline preview; set production secrets separately only after confirming the target account.

Index one document

curl -i http://127.0.0.1:8787/index \
  -H 'content-type: application/json' \
  -H 'x-ingest-key: <your-local-secret>' \
  --data '{"documents":[{"id":"tls-strict","text":"Prefer Full strict for a new site.","source":"/en/docs/fundamentals/tls-modes"}]}'

After 202 indexing, wait a few seconds before querying because Vectorize upsert becomes visible asynchronously.

Ask and inspect sources

curl -i http://127.0.0.1:8787/ask \
  -H 'content-type: application/json' \
  -H 'x-query-key: <your-local-query-secret>' \
  --data '{"question":"Which TLS mode should a new site use?"}'

Confirm sources came from indexed metadata. With no indexed match, expect no_context, not a fabricated answer.

Rollback and security boundary

  • Remove public routing or roll back the Worker version first; back up or rebuild index data separately.
  • Rotate a leaked INGEST_KEY or QUERY_KEY; deleting code is insufficient.
  • QUERY_KEY is only a server-to-server tutorial guard and must not be embedded in a browser; a public UI also needs user authentication, abuse protection, and cost limits.
  • Model names are time-sensitive configuration. Recheck the model catalog, pricing, and data policy before release.
  • Retrieved content enters the prompt as untrusted reference data. This example returns text and never executes model- or document-proposed actions.

Primary sources

Did this page help you complete your goal?

Beta feedback is generated in this browser and is never uploaded automatically.

On this page