transcribe

Embeddings & Semantic Search in Python (OpenAI) | AI Engineer Course #8

AI Dev Playbook · 3m · transcribed 28d ago
More from AI Dev Playbook Business
𝕏 Share ▶ YouTube 📥 PDF 🤖 .md

Section Insights

# 0:00

Introduction to Semantic Search

What is the problem with traditional keyword search?

Traditional keyword search fails when users use different words, while semantic search addresses this by focusing on meaning.

  • Keyword search is limited by vocabulary differences.
  • Semantic search enhances search capabilities by understanding meaning.
  • The lesson will involve creating embeddings and building a semantic search engine.
# 0:42

Creating Embeddings

How do you create embeddings using the text-embedding-3-small model?

You can create embeddings by calling client.embeddings.create with the model and your text, resulting in a vector of 1,536 numbers.

  • Embeddings are numerical representations of text.
  • The model outputs a fixed-length vector regardless of text length.
  • Batch processing of texts is more efficient than individual calls.
# 1:24

Comparing Vectors

How do you compare two vectors?

Vectors are compared using cosine similarity, which measures the angle between them, indicating how similar their meanings are.

  • Cosine similarity helps determine the relationship between vectors.
  • OpenAI embeddings are normalized, simplifying the comparison process.
  • The dot product of vectors can be used directly for similarity calculations.
# 2:07

Building the Search Engine

How does the semantic search engine retrieve relevant documents?

The engine embeds the user's query, scores it against document vectors, and returns the top results based on similarity.

  • Semantic search matches documents by meaning, not just keywords.
  • Storing embeddings efficiently is crucial for performance.
  • For larger projects, consider using a vector database for scalability.
# 2:49

Applications of Embeddings

What are the broader applications of embeddings beyond search?

Embeddings can be used for various AI features like recommendations, duplicate detection, and thematic grouping of feedback.

  • Embeddings enable a wide range of AI functionalities.
  • The next lesson will integrate the search engine with a language model.
  • Understanding embeddings opens up new possibilities in AI development.

Transcript

0:00 Keyword search fails the moment people use different words. Search for how do I get my money back and a page titled refund policy might never appear. Semantic search fixes that by searching by meaning. In this lesson, you will create embeddings, compare them with a little maths, and build a working semantic search engine in about 40 lines of Python. In lesson one, we saw that models turn tokens into embeddings, lists of numbers that capture meaning. OpenAI lets you create embeddings for whole sentences or paragraphs. Texts with similar meanings get vectors that point in similar directions, even if they share no words at all. The model we will use, text-embedding-3-small, turns any text into a list of 1,536 numbers.

0:51 Creating an embedding is one call, client.embeddings.create, with the embedding model and your text. The vector is in response.data.embedding. Print its length and you get 1,536. The numbers themselves mean nothing to a human. What matters is how they compare with other vectors. You can pass a whole list of texts in one request, which is much faster than one call per text. Here are four short help center documents. We embed them all at once and collect the vectors with a list comprehension. In a real app, you would do this once, save the vectors, and reuse them, rather than paying to embed the same documents again every time.

1:38 How do we compare two vectors? With cosine similarity, which measures the angle between them. Close to one means they point the same way, so the meanings are similar. Close to zero means unrelated. With NumPy, it is one line, the dot product divided by the lengths of the two vectors. Openai embeddings are already normalized to length one, so in practice the dot product alone gives the same answer. Now the search engine. Embed the user's query, score it against every document vector, sort from highest to lowest, and return the top results. Search for how do I get my money back, and the refund document comes out on top, even though it never uses the words money or back.

2:23 That is semantic search, matching by meaning. Embedding documents costs time and money, so save the results. For a small project, a JSON file is perfectly fine. Store the texts and their vectors, and load them when your app starts. When you have many thousands of documents, you move to a vector database such as Chroma, PGVector, or Pinecone, which store vectors and search them very fast. The idea is exactly the same as our 40 lines of code.

2:54 Embeddings power far more than search. They are the retrieval step in rag, which we build next lesson. They also drive recommendations, more like this features, finding duplicate tickets, and grouping feedback into themes. Once you can turn text into vectors and compare them, a whole category of AI features opens up. You have built a semantic search engine from scratch. Next lesson, we connect it to a language model and build a full rag app that answers questions from your own documents with sources. Don't miss it.

Summary

This lesson focuses on building a semantic search engine using embeddings to improve search accuracy by understanding meaning rather than relying solely on keywords. By creating and comparing vector representations of text, the engine can return relevant results even when the exact words are not present.

- Keyword searches often fail with different wording; semantic search addresses this by focusing on meaning.
- Embeddings are numerical representations of text that capture semantic meaning, created using OpenAI's models.
- The model text-embedding-3-small generates embeddings as lists of 1,536 numbers.
- Cosine similarity is used to compare vectors, indicating how closely related their meanings are.
- The search engine embeds user queries, scores them against document vectors, and returns the most relevant results.
- For efficiency, it's recommended to save embeddings to avoid repeated costs; options include JSON files or vector databases like Chroma or Pinecone.
- Beyond search, embeddings can enhance features such as recommendations, duplicate detection, and thematic grouping.
- The next lesson will integrate this semantic search engine with a language model to create a comprehensive retrieval-augmented generation (RAG) application.

Questions Answered

What is the problem with traditional keyword search?

Traditional keyword search fails when users use different words, while semantic search addresses this by focusing on meaning.

How do you create embeddings using the text-embedding-3-small model?

You can create embeddings by calling client.embeddings.create with the model and your text, resulting in a vector of 1,536 numbers.

How do you compare two vectors?

Vectors are compared using cosine similarity, which measures the angle between them, indicating how similar their meanings are.

How does the semantic search engine retrieve relevant documents?

The engine embeds the user's query, scores it against document vectors, and returns the top results based on similarity.

What are the broader applications of embeddings beyond search?

Embeddings can be used for various AI features like recommendations, duplicate detection, and thematic grouping of feedback.

© transcribe · For agents Built with care and craft by Gokul Rajaram