Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 2.8 — Calling Embedding Models
RAG and Vector Search · Example 1 of 1

An embeddings request and response, by hand

The server is a fake that returns three-number vectors. Real vectors have hundreds of numbers, but the request, the response, the reversed rows and the batching are exactly what you code against. Real Java projects use Jackson or Gson for JSON, and the course's Json helper keeps this runnable.

A hosted embedding model is one HTTP call: send a list of texts, get back a list of vectors plus a token count. Batch the texts, never trust the order of the rows, handle rate limits, and know the local alternatives (sentence-transformers, Transformers.js).

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write make_batches(items, size) that splits a list into lists of at most size items, and print the batches for [1, 2, 3, 4, 5] with size 2.

02

Write restore_order(rows) that takes response rows like {"index": 1, "embedding": [0.1]} in any order and returns the embeddings sorted by index. Print the result for rows with indexes 1, 2, 0.

Explain it without notes

01

What does a request to an OpenAI-style embeddings endpoint contain, and what comes back?

02

Why must a client sort the response rows by index?

03

How do batching and rate limits interact, and how should a client react to a 429?

04

When would you run an embedding model locally instead of calling an API?

An embeddings request and response, by hand
Sign in to run this example in your browser.

Expected output

request body: {"model":"fake-embed-1","input":["Can I cancel a ride?","Forgot password"],"encoding_format":"float"}
request 1: 2 in batch, rows came back in order 1,0
request 2: 2 in batch, rows came back in order 1,0
request 3: 1 in batch, rows came back in order 0
5 vectors of 3 numbers, 16 tokens billed
Can I cancel a ride? -> [0.50, 0.60, 0.50]
Forgot password -> [0.20, 0.10, 0.50]
Is a helmet included? -> [0.40, 0.00, 0.50]
Monthly pass price -> [0.30, 0.40, 0.50]
Lost items -> [0.20, 0.30, 0.50]