Skip to main content
December 1, 202612:07

Your AI App Probably Needs an Embedding Model: EmbeddingGemma 2

By Samuel Gregory

About this video

Stop wasting your tokens on basic search tasks when you can run superior models locally. This video explores the release of Google Embedding Gemma 2, a 740m parameter model that brings multimodal search to your local machine. We break down the transition from keyword matching to vector-based semantic search and show you how to build a private retrieval system from scratch. Key takeaways: - Understand how embedding models create a 'map of meaning' using vectors. - Learn why Embedding Gemma 2 is a game changer for 16gb RAM devices. - The difference between models, sentence transformers, and vector databases. - Why similarity does not always mean truth in search results. - A practical Python demo for building your own search application.

The Hidden Map of Meaning: Why Your Search Is About to Change

Most developers are wasting money by forcing massive language models to read every document they own. It is slow, it is expensive, and frankly, it is unnecessary. The secret to efficient retrieval is not a bigger model; it is a smarter map.

What is an Embedding?

Think of an embedding model as a way to turn words into coordinates on a map of meaning. When you give the model a sentence, it produces a list of numbers called a vector. Related concepts sit closer together on this map. If you search for 'holiday', the model knows that 'annual leave' is just a few steps away, even if the words do not match.

Enter Embedding Gemma 2

Google recently released Embedding Gemma 2. It is a tiny powerhouse with only 740m parameters. Unlike the massive models that require server farms, this one runs locally on a 16gb RAM MacBook Pro. It features an 8.19k context window and multimodal capabilities, meaning it can eventually help you search through images and audio just as easily as text.

The Local Advantage

Running these models locally solves the two biggest hurdles in AI: privacy and cost. By using a sentence transformer library and a vector database like Qdrant, you can build a search system that stays on your hardware.

  1. Chunking: Split your documents into manageable pieces.
  2. Embedding: Run those pieces through Gemma 2 to get your vectors.
  3. Storage: Save those vectors in a database.
  4. Retrieval: When a user asks a question, turn that question into a vector and find the nearest matches.

The Reality Check

Similarity does not equal truth. A sentence saying 'refund issued' and 'refund not issued' will look very similar to the model because they share the same topic. Always remember that these systems are meant to shortlist information, not replace human judgment. Stop over-complicating your tech stack and start mapping your data.

Transcript▾

Sorry if you can hear the fans. I am actually doing some testing on a new coding model that apparently can run on 16gb RAM MacBook Pros. If you want that video, subscribe for that one or if it is already done, I will link it above. But today we are going to be talking about embedding models because Gemma, Embedding Gemma 2 has just been released and it got me thinking: what the hell is an embedding model and of course can we run it locally? So that is what we are going to discuss today.

So here is the comparison between Embedding Gemma and Embedding Gemma 2. We are basically adding multimodal capabilities to the model. We lost a little bit on the English here, but ultimately this is the latest and greatest from Google when it comes to embedding models. It is only 740m parameters. So when we talk about billion parameters, it is a much smaller model. If we look at the model card here, you can see yeah, 740m parameters and we are talking about multimodal with an 8.19k context window.

Now you are probably using embedding models already. Things like the search inside the Photos app where you are able to type like dog and there will be pictures of dogs and things like that or indeed Spotlight search when you can make a broad vague searching of a file or whatever and it relates that to files and folders on your system. These are probably all embedding models happening under the hood.

In this video, we are going to build an understanding from the beginning. What does the model produce? How does that help us search? And where do tools like sentence transformer and a vector database fit? This was all questions that I had at the beginning of this. So by the end, you should be able to explain the whole system without needing to understand the maths.

Imagine you are searching your company documents and you ask: how much holiday do I get? The policy says employees receive 25 days of paid annual leave. Those sentences share hardly any important words. A search that only matches the wording may miss that connection. An embedding model helps us represent the relationships between them. It gives us a way to find relevance even when someone describes the same subject differently. That is the problem we are trying to solve.

An embedding model takes some content and produces a list of numbers. We call that list a vector, and the representation is its embedding. A good analogy for all this is maps. Think of it as coordinates on a map of meaning. Related content tends to sit closer together according to what the model learned during its training. A normal map has two dimensions. These vectors can have hundreds or even thousands. The numbers on this slide are just an illustration. Individual positions usually do not have neat labels like happiness or business. The useful representation lives in the span across the numbers.

And so here is the distinction that kind of made it click for me. The embedding model creates the vectors. In our example, that model is embedding Gemma 2. A sentence transformer is a separate software library, and it loads Gemma and gives our Python code convenient ways to use it. It also includes tools for comparing the resulting vectors. A vector database such as Qdrant stores those vectors and searches them. Gemma does not go looking around inside the database. Our application asks the model for a representation, then asks the database for nearby representations.

So you cannot create the vectors with one model and search with another model. It has to be the same model so it returns the same vector points. But before we do the search, we have to prepare the collection. We read our files and split long documents into manageable passages. Each passage goes through the embedding model. Then we save its vector alongside the passage text and details such as the file name. That way, when we find a matching vector, we can return the actual passage and show them where it came from.

We do this ahead of time and reuse the saved results. If the document changes, we update its passages. Adding documents to a search collection does not retrain Gemma or permanently teach an answering model those documents. Now someone asks a question. We use the same compatible model setup to create a vector for that question. The database compares it with the passage vectors and returns the closest matches.

For 5 passages, our script can compare everything directly in memory. We do not need a database at all. With a larger collection, a dedicated search system helps store the vectors and find matches efficiently. Notice that the model is not reading every document again for every question. The saved representations are what make repeated searches useful.

Now again, it is just comparing numbers to numbers and making sure that there is relevance there and showing you basically giving you the closest match. We are going to go through all this a practical example in a bit. I am just kind of giving you the spiel of everything. But hopefully this makes sense and if it is, then give us a like and subscribe. Otherwise, let us keep going with it.

So I honestly ask the questions: why use embedding models at all if a normal AI can read documents too? It is because having a language model inspect every document for every question can be expensive and slow. Embeddings let us prepare reusable representations and quickly shortlist useful material. A generative language model can then read the shortlist and write a response. That combination is a common form of retrieval augmented generation or as you might have heard about it, RAG.

Retrieval quality still matters. If we supply the wrong passages, the answer can go wrong and finding a passage does not guarantee the language model will interpret it correctly. I also had the question: does this work best when you have a vector database of similar material or like related material like you have got your company documents here or can you mix a match?

But I learned that usually a model itself can cover a broad range of subjects. You can still supply it to a specific collection such as your own notes or company manuals. You could also mix several subjects in the same collection. What matters is whether the model represents the relationships your task needs. Similarity of topic is different from similarity of opinion, style, or purpose. There is no single perfect map for every single task, and a search system can only retrieve material that is actually available in its collection. There is like no hallucination here.

Here is the catch. A passage saying a refund issued and one saying a refund not issued concern almost exactly the same topic. Their representations may be close even though the crucial fact is in fact the complete opposite. The nearest result can also be irrelevant. Ask a collection of heating manuals about sourdough, and it may still return the closest heating passage. Similarity does not establish truth or prove that something answered the question.

Real systems may combine keyword search with embeddings, judge shortlisted results more carefully, and provide a way to say that there is insufficient relevant information. A good way to understand this is to try a small collection, add a few notes, then add questions using words that do not appear in the original text. Check the matching passages and their sources. Then ask it something your collection cannot answer. Seeing the irrelevant matches is just as useful as seeing the successful ones.

And the CPU can actually handle a small experiment. A GPU can help when you create larger batches of embeddings. Some models also represent images or audio, so search can cross different formats. The idea to take away is straightforward. The model makes the coordinates, and the search system finds nearby content.

Let us actually look at a practical example of what is going on here. This is all in one file. Obviously, if you want a bigger system like a production-ready system, you will separate all of these individual components. I am happy to do that, and I actually have a project compared where we are separating all this thing, but I just kind of want to treat this video as a kind of basics getting started thing, and I do not want to go too overboard if you guys are not interested in it.

So, here is just a single Python file called search. Now we load that sentence transformer, which is the thing that loads embedding Gemma 2, and we are choosing to run it on the CPU. Now I do not understand this stuff, but we are just I guess we are setting some configurations and stuff like that. So here are the passages, and they are all kind of different, but they could represent loads of documents or images or whatever. But in this case, we have just got so many like passages that are completely irrelevant to each other just to demo what is going on here.

Now here is where we are creating those that vector database. It is all this is all in memory. It is not an actual database at this point, but we actually encode the passages and make that into a document. Now we have a vectorized representation of all of these passages, basically. And here is just a for loop that will run, which essentially takes a question, encodes that question, and then does a score of similarity.

So we are just again just matching those similarity and then returns the result in a list. So all we have got to do to actually run this is go Python search. Okay, we just got to activate it and run our little file here. So if embedding Gemma 2 isn't downloaded, it will just download it, I think through hugging face. We have already got it downloaded, so it is all good.

So the model is in memory now, and we can say things like if we go here, there is some password reset. Let us go: I forgot my password. Now it is going to give me similar to a decision model a kind of confidence level on what it thinks our question means once it is actually broken that down. So in fact, it is returned the result that we intended on. Now it is 0.6 confident that we are thinking we are talking about turning off the heating. Not really sure about that, but we are going to take that result.

Now again, you might pass this through another model to judge which one it thinks it is, but you saw how quickly that was. Let us take another example to return an item request. My jumper is defected. So here would be an example. So if things I am talking about heating, I guess because of jumpers, I do not know, but this is the passage that I expected: return item or whatever.

So now I can think of like tons of my apps that should have probably been using embedding models to be honest where I am using traditional LLMs. So tell me, is there any applications that you have built that you could probably swap or should swap for an embedding model that you are using generative language models for?

And just because I did not want to leave you hanging, sorry, it got a bit cold, so I am wearing the dressing gown again. Here is just a basic web app I have built extending off of that file we have just gone through. I have basically given it the ability to upload a bunch of text files or MD files, and what the app then does is pass through all of those passages or lines inside that document and create a vector database for them.

I am then able to just search on those documents. So one is like a script here. I do not even know what that one is. I just uploaded a bunch of files, but podcast intro. Let us say: are we allowed to talk about AI? I am guessing that might retrieve something in the podcast intro. We search collection here. There we go. It is podcast intro, so it is already talking about AI.

So this is a basic example. In a production app, you would have these completely separately. You would run all the collections behind the scenes, and then you would expose the ability to search to the users. Anyway, back to previous me before I decided to add this bit onto the end of the video. Anyway, I will wrap it up there. I hope you enjoyed this one. Like, subscribe if you haven't already. Until next time, keep on vibing.