LangChain and LangGraph · Chapter 8 of 25

Embeddings and vector stores

By the end of this chapter you can say what an embedding is and why texts with the same meaning get nearby numbers, make embeddings with LangChain's OpenAIEmbeddings (and avoid the trap that breaks it on any server that is not OpenAI's), compare two texts with cosine similarity, put RoomRaja's rules in an InMemoryVectorStore, search it with scores, and search only part of it with a metadata filter. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: RAG with LangChain
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of LangChain and LangGraph are free. This one comes with the all labs plan.

Why you need this

Chapter 7 cut RoomRaja's documents into pieces. Now the assistant must find the piece that answers a tenant's question. The first idea is to search for the question's words. Mrunal tries it with a real message:

Can I have a cigarette upstairs?

The rule that answers it is 8.2: "Smoking and alcohol are not allowed anywhere in the property." Not one word is shared: no "cigarette", no "upstairs", no "have". A word search finds nothing, or finds the wrong rule because of "can" and "I". Tenants write in their own words, in English and Hinglish; the policy is written in Faisal's. The search has to compare meanings, not spellings. That is what embeddings do.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    embed_compare.py: two rules, one question

    Make an OpenAIEmbeddings for the model lab-embed that works against the lab model (it must send text, not token ids). Embed the tenant's question, Are guests allowed at night?, with embed_query, and the two lines of /workspace/data/policy.md that start with 4.2 and 5.1 (whole lines, numbers included) with one call to embed_documents.

    Print three lines: Dimensions: and the length of the question's vector, then 4.2: and the cosine similarity between the question and rule 4.2, then 5.1: and the same for rule 5.1. Write each similarity with two decimals, like 0.57.

  2. Task 2

    policy_store.py: the policy in a vector store

    Make one Document per rule of /workspace/data/policy.md: every line that starts with a digit, whole, as its page_content, and metadata with rule (the rule's number, like 2.2) and section (the text of the ## heading above it after the number and the dot, like Security deposit). Add them all to an InMemoryVectorStore that uses lab-embed (as in task 1).

    Search it with similarity_search_with_score for the tenant's question, Can my parents stay the night?, with k=3, and print one line per result, best first: the rule's number, a space, and its score with two decimals.

  3. Task 3

    filtered.py: search one section only

    Build the same store as in task 2 (one Document per rule, with rule and section in the metadata). Faisal only cares about money: search for Is there a fee? with k=2 twice, first in the whole store, then only in the section Rent, using the store's filter.

    Print two lines: All: and the two rules' numbers, joined with a comma and a space, then Rent: and the two rules' numbers from the filtered search, the same way.

  4. Task 4 · interview question

    Interview: money_back.py, no words in common

    Build the store of task 2 and search it for the tenant's question Will I get my money back? with k=1.

    Print three lines: the closest rule's text (its whole page_content), then Score: and its score with two decimals, then Words in common: and the words the question and the rule's text share, in alphabetical order joined with a comma and a space, or none when they share none. A word is a run of letters, lowercased.

    The interviewer will ask why a question that shares no words with a rule still finds it, and when that can go wrong.

What you will learn

  • Embeddings: a text as a list of numbers that stands for its meaning.
  • OpenAIEmbeddings against the lab model, and check_embedding_ctx_length.
  • Cosine similarity: how close two meanings are, as one number.
  • InMemoryVectorStore: Documents with their embeddings, searched by meaning.
  • Scores and filters: how close each result is, and searching part of the store.

More in RAG with LangChain

Build "Embeddings and vector stores" yourself

RoomRaja, a Pune co-living company, answers tenants on WhatsApp by hand. You build its assistant. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of LangChain and LangGraph are free. This one comes with the all labs plan.