Advanced RAG 05 - HyDE - Hypothetical Document Embeddings

Sam Witteveen · Beginner ·🔍 RAG & Vector Search ·2y ago
Skills: Advanced RAG90%

Key Takeaways

The video covers Advanced RAG and HyDE, focusing on Hypothetical Document Embeddings, with a tutorial on using LLMs and building Agents in Colab.

Full Transcript

okay in this video we're going to look at the concept of hide or hypothetical document embeddings and this comes from the paper called precise zero shot dense retrieval with without relevance labels so remember dense retrieval is just looking up things with a similarity search or a semantic search usually with a vector store nowadays involved here so this paper came out uh last year at the end of last year it's one that I kind of feel like not enough people have paid attention to it's a technique that while it's very simple is very powerful in the way that you can improve your rag system overall so if we look at this diagram from the paper this shows you the fundamentals of how this thing works and this is that we're going to input a query into our system and then rather than straight away just look up the embedding for that and compare that embedding to all the possible chunks to find the best answer we're actually going to use a large language model in the middle and what that large language model in the middle is going to do is write a hypothetical answer so why would you use this let me explain this sometimes if someone asks a question that's very Bland or often that perhaps doesn't have any nouns in it or any things that we can easily look up to find an answer that can be quite challenging so if we look at the example of McDonald's right we all know the concept of what does McDonald's sell it sells food but if someone's asked the question okay what are McDonald's best items now if I ask you that question there's a high probability that you will know that I'm talking about food and then perhaps I'm talking about burgers or shakes or fries or the type of fast food that is the challenge is none of that is mentioned in the query so if I'm taking an embedding of the query and I'm then looking up an embedding of something else and let's say I didn't mention the word McDonald's maybe we've been talking about McDonald's and I just say oh okay what are their bestselling items now bestselling items plus you know looking for big Maxs in our semantic Vector store the chances of finding it is probably going to be reasonably small so in this case the one of the best ways to do this is to have the language model write an answer first now we're writing the answer not to let the end user ever see the answer the we're writing the answer just to get something which we can then take an embedding from so for example if the large language model writes the answer and it says oh you know their bestselling item is Big Mac or their bestselling item is the Tuesday burger or something like that even if it's the wrong answer you'll notice that it's talked about a burger it's talked about McDonald's is talking about stuff now which when we take an embedding of that will be a good representation to look up and find the right answer so this allows you to do something where you're not doing query to answer embedding similarity we're doing answer to answer embedding similarity and it turns out that this often works really well now it doesn't always work well if you're talking about a topic that the language model has no knowledge of at all remotely you probably want to stay away from this because it will tend to hallucinate things that are totally different and I'll show you an example of that in the notebook but on the whole if you're dealing with something where you need to look up something that's going to be quite specific it can often be quite good to have the language model do this now one of the other things it can do is rather than just generate one answer you can generate multiple answers and then you can feed those into you know you can average those out in an embedding model so you can take an embedding of each of those and average them out which will allow you then to basically look up things as well so there are a number of different ways you can sort of use this but it is a very powerful technique for this let's jump into the code and have a look at how this actually works in the code so remember that the hide is hypothetical document embeddings and the idea is that we're going to create a hypothetical answer with a large language model and then we're going to embed that and use that for the search against our documents in our rag here so okay we can see that the the code part to do this is we need some base embeddings or base embedding model and we need an llm chain and you'll see that we'll change the prompts we've got some basic prompts in there but we'll also then go for some custom prompts as we go through this all right so you can see what I'm bringing in here I'm using open AI just for Simplicity for the large language model for embedding I'm going to be using the BGE embeddings and you'll see that as we go through that this is going to be generating out the embeddings you could swap this out for any embedding system that you wanted to use which would be fine even if you were using like a local system you could use the quantitized embeddings for this in here all right so this is where the magic sort of starts so you can see that we're going to get the embeddings out of this but actually what we're doing is making a chain in here so this is from large language model chain we're passing in the actual embedder that we've set up here so we've set up our B embeddings and what this is going to do is this going to take in our query and you can see that when we look at the prompt for this we can see that okay what's it going to do please write a passage to answer the question so this is a very simple sort of example you'll see later on that it's often good for us to write this and customize it to our particular use case in this but okay we can see what we've got there let me just sort of go through and explain again what I I talked about earlier on was that something like what items does McDonald's make now we know that McDonald's makes food and it makes burgers and it makes fries and things like that but you'll notice that none of those things are mentioned in the query so this is where if we're taking an embedding of the query versus the answers back it might not be a great thing because you know the answers we would want back would be french fries Big Mac that kind of thing but none of those are mentioned in the actual query so here the large language model and I've just turned on Lang chain you know debugging here so that we can see the output in every step we can see that it goes into the large language model it basically says write an answer to this it answered that open AI writes is McDonald's is a fast food chain which is known for making a variety of things these include signature Burgers such as Big Mac Quarter Pounder as well as Chi chicken sandwiches wraps desserts we've got a lot more stuff now that's content that the embedding would fit this so something like Big Mac is mentioned in there now when it's going through looking for pieces of text it's probably going to match things that are Big Mac related in there so that's the first way and the simplest way to do it with just their built-in prompt that kind of thing the next way we can do this is we can also have multiple Generations so rather than just generate one answer we can generate multiple answers so here you can see you know when we ask it okay what is McDonald's bests selling item again we're not mentioning actually we're not even mentioning food here right so if we were just doing the query sort of lookup it's possible that it could get confused of not knowing that okay we're actually talking about food whereas the large language model kind of gets it even if it's not giving us the exact right answer it's giving us oh McDonald's one of the most popular fast food restaurants in the world its menu items include a variety of items but one stands out is the best seller the Big Mac right and we can see that we've got different answers in here bestelling item is undoubtedly the Big Mac and we got you know a number of different we got Big Mac mention again here so when we take the embeddings of these and we combine them we're definitely going to get this representation of Big Mac coming through for when we do our search with that embedding there so this is one of the key things that makes hide so powerful right is that you could take a number of these things combine these embeddings another thing that you could also do is use your own prompt so if I don't want a long answer like that back and I just wanted to mention the food I can say okay please answer the user's question as a single food item so now I kind of know I would use this in a case where I kind of know what the question's going to be about or the topic and I would guide the llm to that topic so if if we were doing this for the McDonald's website you know we could put in there you are a helpful assistant that works for McDonald's you answer questions about different things on the menu please answer this by recommending a single food item kind of thing in there so now when somebody just says what is McDonald's best selling item or even worse in this case is where people would just say oh what's your bestselling item right now what's your best doesn't even have McDonald's in it for the embedding that wouldn't go down well whereas we can see that if we've set up this user prompt quite well we can get something back which then in this case we just wanted a single item what did it return Big Mac we can take our embedding on Big Mac there's our embedding for Big Mac and then we could do a search on it for this kind of thing so just quickly to show you how to put it all together so in the original example they were just using a single file I've put in some text loaders in here and what I'm going to do is ask it now I got two examples here one that's not here one that I am showing here so let me show the one that works here first and this is you know please answer the users questions related to large language models again we're guiding it with a custom prompt here when I ask okay what are chat loaders it it kind of makes something up that's wrong right chat loaders are software tools used to load large language models into chatbot applications and that's not quite right but it doesn't matter because it's mentioned all the key things so that the representation is able to then find these chat loaders so that we can you know we can come in here and we can look at this this article chat loaders fine tune chat model in your own voice in here you for doing this so this is an example of how it would you know it would work you'd have to be a little bit careful so originally I was going to do the example of Langs Smith like I've done in some of the other notebooks the challenge there was that the large language model knows nothing about what Lang Smith was so unless I was to overly guide it in my question prompt it's it will just it came back saying that oh well it's language learning software so you want to be careful for if it's something that's totally new that the the language models perhaps never heard of you know then you want to be a little bit careful about using hide but for other situations using hide is often going to get you better results than just embedding the query and doing the query against the answer sort of similarity match there so this is a very powerful technique I encourage you to check it out and and try it out on your own projects and see how you get along as always if you got questions please put them in the comments below found the video useful please click like And subscribe and I will talk to you in the next video bye for now

Original Description

Colab: https://drp.li/XnRSF For more tutorials on using LLMs and building Agents, check out my Patreon: Patreon: https://www.patreon.com/SamWitteveen Twitter: https://twitter.com/Sam_Witteveen My Links: Linkedin: https://www.linkedin.com/in/samwitteveen/ Github: https://github.com/samwit/langchain-tutorials (updated) https://github.com/samwit/llm-tutorials 00:00 Intro 00:10 Precise Zero Shot Dense Retrieval Paper 00:43 Precise Zero Shot Dense Retrieval Paper Diagram 01:29 McDonalds Example 04:13 Code Time
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Sam Witteveen · Sam Witteveen · 0 of 60

← Previous Next →
1 LangChain Basics Tutorial #1 - LLMs & PromptTemplates with Colab
LangChain Basics Tutorial #1 - LLMs & PromptTemplates with Colab
Sam Witteveen
2 LangChain Basics Tutorial #2 Tools and Chains
LangChain Basics Tutorial #2 Tools and Chains
Sam Witteveen
3 ChatGPT API Announcement & Code Walkthrough with LangChain
ChatGPT API Announcement & Code Walkthrough with LangChain
Sam Witteveen
4 Trying Out Flan 20B with UL2 - Working in Colab with 8Bit Inference
Trying Out Flan 20B with UL2 - Working in Colab with 8Bit Inference
Sam Witteveen
5 LangChain - Conversations with Memory (explanation & code walkthrough)
LangChain - Conversations with Memory (explanation & code walkthrough)
Sam Witteveen
6 LangChain Chat with Flan20B
LangChain Chat with Flan20B
Sam Witteveen
7 LangChain - Using Hugging Face Models locally (code walkthrough)
LangChain - Using Hugging Face Models locally (code walkthrough)
Sam Witteveen
8 PAL : Program-aided Language Models with LangChain code
PAL : Program-aided Language Models with LangChain code
Sam Witteveen
9 Building a Summarization System with LangChain and GPT-3 - Part 1
Building a Summarization System with LangChain and GPT-3 - Part 1
Sam Witteveen
10 Building a Summarization System with LangChain and GPT-3 - Part 2
Building a Summarization System with LangChain and GPT-3 - Part 2
Sam Witteveen
11 Microsoft's Visual ChatGPT using LangChain
Microsoft's Visual ChatGPT using LangChain
Sam Witteveen
12 Building a Summarization System with LangChain - Part 3 Using ChatGPT Turbo
Building a Summarization System with LangChain - Part 3 Using ChatGPT Turbo
Sam Witteveen
13 LangChain Agents - Joining Tools and Chains with Decisions
LangChain Agents - Joining Tools and Chains with Decisions
Sam Witteveen
14 Investigating Alpaca 7B - Finetuned LLaMa LLM
Investigating Alpaca 7B - Finetuned LLaMa LLM
Sam Witteveen
15 Comparing LLMs with LangChain
Comparing LLMs with LangChain
Sam Witteveen
16 Running Alpaca7B in Colab
Running Alpaca7B in Colab
Sam Witteveen
17 How to finetune your own Alpaca 7B
How to finetune your own Alpaca 7B
Sam Witteveen
18 How to make a custom dataset like Alpaca7B
How to make a custom dataset like Alpaca7B
Sam Witteveen
19 Understanding Constitutional AI - the paper and key concepts
Understanding Constitutional AI - the paper and key concepts
Sam Witteveen
20 Using Constitutional AI in LangChain
Using Constitutional AI in LangChain
Sam Witteveen
21 Talking to Alpaca with LangChain - Creating an Alpaca Chatbot
Talking to Alpaca with LangChain - Creating an Alpaca Chatbot
Sam Witteveen
22 Text-to-video-synthesis with Diffusers and Colab
Text-to-video-synthesis with Diffusers and Colab
Sam Witteveen
23 Meet Dolly the new Alpaca model
Meet Dolly the new Alpaca model
Sam Witteveen
24 Checking out the Cerebras-GPT family of models
Checking out the Cerebras-GPT family of models
Sam Witteveen
25 A Step-by-Step Guide to Fine-Tuning Your Dolly Model (tutorial)
A Step-by-Step Guide to Fine-Tuning Your Dolly Model (tutorial)
Sam Witteveen
26 Is GPT4All your new personal ChatGPT?
Is GPT4All your new personal ChatGPT?
Sam Witteveen
27 Raven - RWKV-7B RNN's LLM Strikes Back
Raven - RWKV-7B RNN's LLM Strikes Back
Sam Witteveen
28 Talk to your CSV & Excel with LangChain
Talk to your CSV & Excel with LangChain
Sam Witteveen
29 Vicuna - 90% of ChatGPT quality by using a new dataset?
Vicuna - 90% of ChatGPT quality by using a new dataset?
Sam Witteveen
30 Koala Revealed: The ChatGPT Alternative You Need to Know! 🔍
Koala Revealed: The ChatGPT Alternative You Need to Know! 🔍
Sam Witteveen
31 Running Koala for free in Colab. Your own personal ChatGPT? (tutorial)
Running Koala for free in Colab. Your own personal ChatGPT? (tutorial)
Sam Witteveen
32 BabyAGI: Discover the Power of Task-Driven Autonomous Agents!
BabyAGI: Discover the Power of Task-Driven Autonomous Agents!
Sam Witteveen
33 Auto-GPT - How to Automate a Task Based AI with GPT-4
Auto-GPT - How to Automate a Task Based AI with GPT-4
Sam Witteveen
34 Improve your BabyAGI with LangChain
Improve your BabyAGI with LangChain
Sam Witteveen
35 Generative Agents - Deep Dive and GPT-4 Recreation
Generative Agents - Deep Dive and GPT-4 Recreation
Sam Witteveen
36 GPT4ALLv2: The Improvements and Drawbacks You Need to Know!
GPT4ALLv2: The Improvements and Drawbacks You Need to Know!
Sam Witteveen
37 Dolly 2.0 by Databricks: Open for Business but is it  Ready to Impress!
Dolly 2.0 by Databricks: Open for Business but is it Ready to Impress!
Sam Witteveen
38 Red Pajama - Operation: Freeing LLaMA
Red Pajama - Operation: Freeing LLaMA
Sam Witteveen
39 Investigating Open Assistant - Models, Datasets and Addons
Investigating Open Assistant - Models, Datasets and Addons
Sam Witteveen
40 Investigating MiniGPT-4 - The Secret behind GPT-V?
Investigating MiniGPT-4 - The Secret behind GPT-V?
Sam Witteveen
41 Stable LM 3B - The new tiny kid on the block.
Stable LM 3B - The new tiny kid on the block.
Sam Witteveen
42 Bard can now code and put that code in Colab for you.
Bard can now code and put that code in Colab for you.
Sam Witteveen
43 Checking out Bark: a Text to Speech system by Suno AI
Checking out Bark: a Text to Speech system by Suno AI
Sam Witteveen
44 Fine-tuning LLMs with PEFT and LoRA
Fine-tuning LLMs with PEFT and LoRA
Sam Witteveen
45 Master PDF Chat with LangChain - Your essential guide to queries on documents
Master PDF Chat with LangChain - Your essential guide to queries on documents
Sam Witteveen
46 Using LangChain with DuckDuckGO Wikipedia & PythonREPL Tools
Using LangChain with DuckDuckGO Wikipedia & PythonREPL Tools
Sam Witteveen
47 Building Custom Tools and Agents with LangChain (gpt-3.5-turbo)
Building Custom Tools and Agents with LangChain (gpt-3.5-turbo)
Sam Witteveen
48 StableVicuna: The New King of Open ChatGPTs?
StableVicuna: The New King of Open ChatGPTs?
Sam Witteveen
49 WizardLM: Evolving Instruction Datasets to Create a Better Model
WizardLM: Evolving Instruction Datasets to Create a Better Model
Sam Witteveen
50 LaMini-LM - Mini Models Maxi Data!
LaMini-LM - Mini Models Maxi Data!
Sam Witteveen
51 Finding the Best Free ChatGPT
Finding the Best Free ChatGPT
Sam Witteveen
52 MPT-7B - The First Commercially Usable Fully Trained LLaMA Style Model
MPT-7B - The First Commercially Usable Fully Trained LLaMA Style Model
Sam Witteveen
53 LangChain Retrieval QA Over Multiple Files with ChromaDB
LangChain Retrieval QA Over Multiple Files with ChromaDB
Sam Witteveen
54 LangChain Retrieval QA with Instructor Embeddings & ChromaDB for PDFs
LangChain Retrieval QA with Instructor Embeddings & ChromaDB for PDFs
Sam Witteveen
55 LangChain + Retrieval Local LLMs for Retrieval QA - No OpenAI!!!
LangChain + Retrieval Local LLMs for Retrieval QA - No OpenAI!!!
Sam Witteveen
56 Transformers Agent - Is this Hugging Face's LangChain Competitor?
Transformers Agent - Is this Hugging Face's LangChain Competitor?
Sam Witteveen
57 StarCoder - The LLM to make you a coding star?
StarCoder - The LLM to make you a coding star?
Sam Witteveen
58 Testing Starcoder for Reasoning with PAL
Testing Starcoder for Reasoning with PAL
Sam Witteveen
59 The New Wizards - Unfiltered & Unaligned
The New Wizards - Unfiltered & Unaligned
Sam Witteveen
60 Camel + LangChain for Synthetic Data & Market Research
Camel + LangChain for Synthetic Data & Market Research
Sam Witteveen

This video teaches Advanced RAG techniques, specifically HyDE, for hypothetical document embeddings and how to use LLMs and build agents in Colab. It covers the Precise Zero Shot Dense Retrieval Paper and provides a practical example with code.

Key Takeaways
  1. Read the Precise Zero Shot Dense Retrieval Paper
  2. Understand the HyDE concept
  3. Implement HyDE in Colab
  4. Use LLMs for document embeddings
  5. Build an agent with LangChain
💡 HyDE enables efficient and effective document embeddings for information retrieval tasks.

Related Reads

📰
Production RAG for enterprises: evaluation, safety, and cost
Learn how to evaluate and implement production-ready RAG for enterprises, focusing on safety and cost efficiency
Medium · AI
📰
RAG for Financial Docs Is Different. Here’s the Chunking Strategy That Finally Worked.
Learn how to apply a structure-aware chunking strategy to improve RAG for financial documents, overcoming the limitations of generic 512-token chunks
Medium · RAG
📰
RAG: Every Data Type You'll Actually Run Into (Part 2)
Learn to handle real-world data for RAG by understanding common data types and preparing them for vector stores
Medium · LLM
📰
RAG: Every Data Type You'll Actually Run Into (Part 2)
Learn to handle real-world data for RAG by preparing it for vector stores, a crucial step in implementing RAG effectively
Medium · RAG

Chapters (5)

Intro
0:10 Precise Zero Shot Dense Retrieval Paper
0:43 Precise Zero Shot Dense Retrieval Paper Diagram
1:29 McDonalds Example
4:13 Code Time
Up next
OpenAI Embeddings and Vector Databases Crash Course
Adrian Twarog
Watch →