Start learning free

LESSON 59 OF 129 · AI ENGINEERING: RETRIEVAL (RAG)

Embeddings

Practice

To find the chunks that fit a question, a program has to compare meanings, not letters: Can I get my money back? shares no word with a paragraph about refunds.

An embedding model makes that possible. It is not a chat model: it takes a text and gives back a list of numbers, the text's embedding. A list of numbers like this is called a vector, and texts that mean similar things get similar vectors. Imagine a tiny model that gives every text three numbers:

"cat"      →  [0.9, 0.1, 0.0]
"kitten"   →  [0.8, 0.2, 0.0]
"invoice"  →  [0.0, 0.1, 0.9]

You reach the model over HTTP with requests, the way you called the supplier. The address and the key are settings, so they come from the environment (EMBEDDINGS_URL and EMBEDDINGS_API_KEY), never from the code. The key travels in a header, after the word Bearer, as in this call to another service:

headers = {"Authorization": "Bearer " + os.environ["WEATHER_API_KEY"]}
response = requests.post(url, json={"city": "Paris"}, headers=headers)

The embeddings API wants a JSON body with "input" (a list of texts) and "model" ("course-embed-1"). It answers {"data": [{"embedding": [...], "index": 0}, ...]}: one item for each text, in the order you sent them. Your program must print each text, then its vector's length and its first 16 numbers:

Can I get my money back?
64 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.7071, 0.0]
I want a refund
64 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0]
Do you ship to Canada?
64 [0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0]

Your task: the program already has split_document, the loop that builds chunks, and three sentences in texts. Write a function embed(texts) that sends the list to the embeddings API in one request and returns the list of vectors. Then call it on texts, keep the result in vectors, and print each text followed by its vector's length and first 16 numbers.


Assignment
Follow the Coding Tutorial to write embed, call the embeddings API with three sentences and print their vectors.


Hint
The vectors are inside the answer: response.json()["data"] is a list of items, and each item holds its vector under "embedding". Don't forget the return at the end of embed.


STEP 1 OF 12
    Ctrl + Enter to check