Level 7 · Artificial Intelligence and Intelligent Automation · Course 7.2 AI Application Engineering

Module 85: Embeddings and Vector Search

Goal: search by meaning rather than exact words: turn text into embeddings, measure how similar two pieces of text are, store and search embeddings efficiently in PostgreSQL, build semantic search over your company documents, and check that it finds the right things.

You'll need: your PostgreSQL skills (Level 5), your assistant backend (Module 84), a set of your company's approved documents (policies, product information, FAQs) and an embeddings API or a local embeddings model.

Lessons

  1. 1Embeddings15–25 minutes✓
  2. 2Similarity15–25 minutes✓
  3. 3Vector indexes15–25 minutes✓
  4. 4Semantic search15–25 minutes✓
  5. 5Search quality15–25 minutes✓

🛠️Module practice: Semantic search over approved knowledge

  1. Embeddings for your approved documents, with one model used consistently.
  2. A pgvector table with an HNSW index and metadata (document, department, visibility).
  3. An indexing function that keeps chunks in step with document updates and withdrawals.
  4. A hybrid search endpoint (semantic plus keyword) filtered by the user's permissions.
  5. A retrieval test set with hit rate and MRR for each search method, and one measured improvement.

✅Module check

Explain in a few sentences, in your own words:

How does semantic search work, and how do you know it works well?

Show a good answer

A good answer mentions: embeddings turn text into vectors where similar meanings are close, produced by one consistent embedding model (with data rules respected); similarity is measured by cosine (or dot product) and means relatedness, not correctness; vectors are stored with an ANN index such as HNSW, conveniently in PostgreSQL with pgvector so metadata and permissions filter in SQL; semantic search embeds the question and finds nearest chunks, combined with keyword search for exact terms, over only approved, up-to-date material; and quality is measured on a retrieval test set with hit rate and MRR, improved one change at a time, in the languages users actually write.

📝Module quiz

10 questions · 10 marks each · 100 marks in all · pass mark 70% · retry as often as you like ·

1. What does an embedding represent?

Why each option is right or wrong

  • “The exact words of the text, stored in a shorter form” (Incorrect): An embedding can't be turned back into the exact words; it captures meaning.
  • “A compressed copy of the file, for faster downloads” (Incorrect): It's not compression; it's a numeric summary of meaning.
  • “How often each word appears in the text, counted up” (Incorrect): Word counts are an older method; embeddings capture meaning beyond exact words.
  • “The meaning of a piece of text, as numbers” is correct.

    An embedding model turns text into a long list of numbers, a vector, so that texts with similar meanings get similar vectors. "How do I reset my password?" and "I forgot my login" end up close together, even with no words in common.

    • Typical size: hundreds to a few thousand numbers.
    • Used for: search, grouping and recommendations.

📖 Revise: Lesson 1: Embeddings

Question ID module-85-q1 · Report a mistake in this question
2. Why must documents and questions use the same embedding model?

Why each option is right or wrong

  • “Embedding models only accept documents and questions as a pair” (Incorrect): Each text is embedded separately; the point is that they share one model.
  • “Using one model is cheaper than paying for two” (Incorrect): Cost isn't the reason; mixed vectors give meaningless matches.
  • “It doesn't matter, as long as both have the same number of dimensions” (Incorrect): Same length doesn't mean the same meaning space.
  • “Different models' vectors can't be compared” is correct.

    Each embedding model arranges meaning in its own way, like two maps drawn with different scales and directions. A vector from one model means nothing in another model's space, so search results would be close to random.

    • Switching models: re-embed every document.
    • Record: which model and version made each vector.

📖 Revise: Lesson 1: Embeddings

Question ID module-85-q2 · Report a mistake in this question
3. What does a cosine similarity near 1 mean?

Why each option is right or wrong

  • “The two texts are the same length in words” (Incorrect): Cosine ignores length; it measures direction, which reflects meaning.
  • “The two texts are completely different in meaning” (Incorrect): Unrelated texts score near 0, not near 1.
  • “One of the two texts is empty or missing” (Incorrect): An empty text doesn't produce a meaningful score near 1.
  • “The two texts have very similar meanings” is correct.

    Cosine similarity measures how closely two vectors point in the same direction. Near 1 means very similar meaning, near 0 means unrelated. It ignores the vectors' length, so long and short texts can still match.

    • In practice: compare scores against each other, not against a fixed rule.
    • Thresholds: depend on the model; test with your data.

📖 Revise: Lesson 2: Similarity

Question ID module-85-q3 · Report a mistake in this question
4. Does high similarity mean a passage answers the question?

Why each option is right or wrong

  • “Yes, a high score shows the passage contains the answer” (Incorrect): Similarity measures topic closeness, not whether the answer is there.
  • “Only for short passages of a sentence or two” (Incorrect): Short or long, similarity shows topic, not an answer.
  • “Only when the score is above 0.5” (Incorrect): No threshold turns similarity into a sure sign of an answer.
  • “No, it means it's on the same topic” is correct.

    A passage can be very similar to the question, even repeating its words, without containing the answer. For "What's the refund deadline?", a passage saying "We offer refunds" scores high but doesn't say when.

    • Next step: the model reads the passages and checks.
    • Tell the model: to say when the passages don't contain the answer.

📖 Revise: Lesson 2: Similarity

Question ID module-85-q4 · Report a mistake in this question
5. Why use an approximate nearest-neighbour index?

Why each option is right or wrong

  • “It's exact, so results are better than a full search” (Incorrect): It's approximate; it trades a little accuracy for speed.
  • “It stores the text so you don't need a database” (Incorrect): It stores vectors for search; the text usually lives elsewhere.
  • “It encrypts the vectors to protect the documents” (Incorrect): Indexes organise vectors for speed; they don't encrypt them.
  • “It finds close matches much faster with tiny accuracy loss” is correct.

    Comparing a question with every one of millions of vectors is slow. An approximate nearest-neighbour (ANN) index, such as HNSW, organises vectors so it checks only a small part, returning nearly the same top matches in a fraction of the time.

    • Trade-off: may occasionally miss one close match.
    • Small collections: an exact search may be fine.

📖 Revise: Lesson 3: Vector indexes

Question ID module-85-q5 · Report a mistake in this question
6. What's an advantage of pgvector in PostgreSQL?

Why each option is right or wrong

  • “It replaces SQL with a new query language for vectors” (Incorrect): You still use SQL; pgvector adds vector types and operators.
  • “It's a separate cloud service that runs beside your database” (Incorrect): It's an extension inside PostgreSQL itself.
  • “It removes the need for a database at all” (Incorrect): It needs PostgreSQL; it's an extension to it.
  • “Chunks, permissions and vectors live in one database” is correct.

    pgvector adds a vector column type and similarity search to PostgreSQL. You can store each chunk's text, its vector, and who may see it in the same database, and filter by permission in the same SQL query as the search.

    • Fewer systems: one backup, one set of access rules.
    • Large scale: dedicated vector databases may suit better.

📖 Revise: Lesson 3: Vector indexes

Question ID module-85-q6 · Report a mistake in this question
7. When does keyword search beat semantic search?

Why each option is right or wrong

  • “For finding passages that use synonyms of the question's words” (Incorrect): Synonyms are where semantic search shines, not keyword search.
  • “For vague questions that describe a problem loosely” (Incorrect): Loose descriptions suit semantic search, which matches meaning.
  • “For any question, since keyword search is more modern” (Incorrect): Keyword search is older; each suits different questions.
  • “For exact terms like product codes and names” is correct.

    Semantic search matches meaning, which can blur exact strings: "INV-2041" and "INV-2014" look nearly identical to an embedding model. Keyword search finds the exact code or name. Many systems combine both, called hybrid search.

    • Hybrid: run both and merge the results.
    • Examples: order numbers, error codes, people's names.

📖 Revise: Lesson 4: Semantic search

Question ID module-85-q7 · Report a mistake in this question
8. What must happen when an approved document is updated?

Why each option is right or wrong

  • “The language model must be retrained on the new document” (Incorrect): Retrieval doesn't need retraining; the index is updated instead.
  • “The whole index must be deleted and rebuilt from scratch” (Incorrect): Only that document's chunks need replacing.
  • “Nothing, because the index updates itself automatically” (Incorrect): Most indexes only change when you re-embed and store new chunks.
  • “Its chunks must be re-created and re-embedded” is correct.

    The search index holds chunks and vectors made from the old version. Until they're replaced, search returns outdated text. Delete the old chunks, split the new version, embed it, and store the new chunks.

    • Automate: trigger re-indexing on every approved change.
    • Track: which version each chunk came from.

📖 Revise: Lesson 5: Search quality

Question ID module-85-q8 · Report a mistake in this question
9. Why measure retrieval separately from final answers?

Why each option is right or wrong

  • “Because final answers can't be measured in any useful way” (Incorrect): They can be measured; separating the two shows where problems come from.
  • “To save time by measuring only the retrieval step” (Incorrect): Both are measured; separating them is about finding the cause.
  • “Because retrieval is free, so it's cheaper to test” (Incorrect): Embedding queries has a cost; the reason is diagnosis, not price.
  • “If the right passage isn't found, the answer can't be right” is correct.

    Answers depend on what retrieval finds. Measuring retrieval on its own shows whether a bad answer came from missing passages or from the model misusing good ones, so you fix the right part.

    • Retrieval test: questions paired with the chunks that answer them.
    • Answer test: check accuracy and citations.

📖 Revise: Lesson 5: Search quality

Question ID module-85-q9 · Report a mistake in this question
10. What does a hit rate at 5 of 90% mean?

Why each option is right or wrong

  • “90% of the final answers given to users are correct” (Incorrect): It measures retrieval, not the final answers.
  • “The top five results are each correct 90% of the time” (Incorrect): It asks whether at least one correct chunk is in the top five.
  • “The search takes about 5 seconds for 90% of questions” (Incorrect): Hit rate measures relevance, not speed.
  • “For 90% of questions, a correct chunk is in the top five” is correct.

    Hit rate at k checks, for each test question, whether at least one correct chunk appears among the top k results. 90% at 5 means it succeeds for nine questions in ten; for the other one in ten, the model won't get the right material.

    • Improve: better chunking, hybrid search, or reranking.
    • Doesn't measure: whether the final answer is right.

📖 Revise: Lesson 5: Search quality

Question ID module-85-q10 · Report a mistake in this question

ProLearners Assist