Discover LlamaIndex: Ask Complex Queries over Multiple Documents

LlamaIndex · Intermediate ·🧠 Large Language Models ·3y ago

Key Takeaways

LlamaIndex's SubQuestionQueryEngine allows users to ask complex comparison queries over multiple documents, breaking down queries into sub-questions and executing them on specific data sources.

Full Transcript

hey everyone welcome to discover llama Index this is a new tutorial series that we're starting up to tell you guys all about the latest features of llama index and how you can really use it to supercharge your large language model applications especially when dealing with large amounts of data I'm Simon the co-founder and CTO of llama index and today I'm super excited to tell you guys just a little bit more about our latest feature the sub question query engine before we dive down into the details let's recap at a high level what a query engine is in Lama index a query engine really is a high level natural language interface that we build over your data source so that you can very easily ask a question over your data and extract very nuanced insights out of it now the key challenge in building such a unified query interface over your data is that there's often a wide range of complicated questions that you might want to ask over multiple data sources now for example you might have multiple financial documents and you want to compare and contrast the financial statements of multiple companies over multiple years now just naively combining all those data sources and doing topk retrieval of Chunk documents for a final synthesis is simply not good enough to get a very good and nuanced answers for complex questions now how do we do this the sub question query engine is one step in the direction of answering these complex questions the key intuition is that when you have a complex multi-part question the best way to deal with it is to First decompose it into multiple sub questions that are relevant to specific data sources now for example here we have three data sources and we might want to decompose the question that targets a subset of them first and then combines the response over multiple data sources to get the final answer now this approach really leverages the composability aspect of our framework by first defining sub query engines over each of these data sources and then Define a top level sub question query engine on top now how does this work exactly to start when given an initial complex questions we will use the large language model to generate sub questions given the description of our data sources that are available then we will execute the sub questions on the select data sources and then gather all the sub responses and then lastly we will take all the relevant contacts from these sub sources and synthesize the final answer now let's take a look at this in action to help us do some complex analysis over multiple financial documents which are called 10ks okay now that we have our notebook open we can finally go ahead and play around with some code before I dive down into the notebook let me just show you the data that we'll be working with so we have these two PDFs which are the lift 10K document for 2021 and the Uber document for 2021 so these are fairly large financial documents that I think have oh yeah 307 Pages for this one and 238 Pages for this one so quite a lot of data here and it's very heterogeneous there are texts bullet points and I think a whole bunch of tables where a lot of financial terms are found okay going back to the notebook let's just uh go straight into it by doing some imports we're going to first configure our large language model service um here we're just setting the temperature to zero so the output is more deterministic we're using gpt3 and then basically setting the max token to negative one so that we don't limit the amount of outputs for these models we're going to wrap the Ln predictor into our service context which we'll be using to pass into our indexing query engine now for the first step we're going to use this simple directory reader to read these two files separately so I think this might take a little bit of time since these are 300 Page documents okay now we have the lift 10K loaded 238 documents I think that's correct yep and see how long this takes okay there we go now the next step is to build our indices here we're just going to use the vector store index what is happening under the hood here when we call the index style from document is that each of these documents will be chunked up into text chunks we'll be calling the embedding API to get a dense Vector embedding for each of these chunks and save it to R in-memory Vector store so this takes a little bit of time just because there's a lot of data so in total 500 pages of text that needs to kind of pass through the API calls and get back the responses now okay so we finished building the left 10K index was 379 notes you can see that is a little bit higher than the 238 pages I think most of the pages gets broken to one or two nodes given our default chunk size right now okay many time now just waiting for the Uber index to be built okay there we go now we're going to build individual query engines for each of these indexes here when we do s-query engine what we're really what's really happening under the hood is uh we're setting up the configuration so the query engine knows what to do at great time now the next step is to add sort of the metadata to identify these query engines to the top level sub question query engine here we're just specifying the name of this query Engine 2 and also giving a description so the top level large language model can know how to make use of this data source okay now that we're we have that built we're ready to run some queries the first one that we're going to ask is to compare and contrast the customer segments and geographies that grew the fastest over the lift-in Uber documents so as we run these you should be able to see various sub questions being generated and executed on the sub query engines before synthesizing the final answer okay so we generated four sub questions and the first one is what customer segments grew the fastest for Uber being asked on the Uber 10K documents um you can see a sub response generated another question being asked for the Uber 10K about what geography screw the fastest in 2021 the corresponding responses the customer segments for Lyft and then the geography that grew the fastest foreclosed so one thing to know is that I think given the context here the model actually decided that not possible to answer this question was to give in the context um okay now that all the sub questions have been generated all of these question and answer pairs will be used to create a final response and you can see yeah the large language model is pretty good at kind of stitching together all these information for a final synthesis and that really talks about um the various aspects comparing between lift now just to Showcase another example we can compare the revenue growth of uber and Lyft from 2020 to 2021 this should be a pretty straightforward answer that really just depends on querying the right index using the right sub question so in this case we only needed two sub questions one is the revenue girls of uber being as an Uber document and the revenue growth of lift being asked on lift document and as you can see we have a clear comparison between the two as the final answer where Uber had a higher rate of Revenue growth than left cool that's all hopefully you enjoyed this tutorial

Original Description

In this video, we show how to ask complex comparison queries over multiple documents with LlamaIndex. Specifically, we show how to use our SubQuestionQueryEngine object which can break down complex queries into a query plan over subsets of documents. LlamaIndex: https://github.com/jerryjliu/llama_index Docs: https://gpt-index.readthedocs.io/en/latest/ Questions? Hop on our Discord: https://discord.gg/dGcwcsnxhU Twitter: https://twitter.com/gpt_index
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from LlamaIndex · LlamaIndex · 3 of 60

1 LlamaIndex Virtual Meetup (May 4th, 2023)
LlamaIndex Virtual Meetup (May 4th, 2023)
LlamaIndex
2 LlamaIndex + MongoDB Workshop/Fireside Chat
LlamaIndex + MongoDB Workshop/Fireside Chat
LlamaIndex
Discover LlamaIndex: Ask Complex Queries over Multiple Documents
Discover LlamaIndex: Ask Complex Queries over Multiple Documents
LlamaIndex
4 Discover LlamaIndex: Document Management
Discover LlamaIndex: Document Management
LlamaIndex
5 Discover LlamaIndex: Joint Text to SQL and Semantic Search
Discover LlamaIndex: Joint Text to SQL and Semantic Search
LlamaIndex
6 Discover LlamaIndex: JSON Query Engine
Discover LlamaIndex: JSON Query Engine
LlamaIndex
7 LlamaIndex Webinar: Active Retrieval Augmented Generation
LlamaIndex Webinar: Active Retrieval Augmented Generation
LlamaIndex
8 LlamaIndex Webinar: Demonstrate-Search-Predict (DSP) with Omar Khattab
LlamaIndex Webinar: Demonstrate-Search-Predict (DSP) with Omar Khattab
LlamaIndex
9 LlamaIndex Sessions: Practical challenges of building a Legal Chatbot over your PDFs
LlamaIndex Sessions: Practical challenges of building a Legal Chatbot over your PDFs
LlamaIndex
10 LlamaIndex Webinar: Graph Databases, Knowledge Graphs, and RAG with Wey (NebulaGraph)
LlamaIndex Webinar: Graph Databases, Knowledge Graphs, and RAG with Wey (NebulaGraph)
LlamaIndex
11 LlamaIndex Webinar: Community Project Showcase (07/07/2023)
LlamaIndex Webinar: Community Project Showcase (07/07/2023)
LlamaIndex
12 LlamaIndex Webinar: LLMs for Investment Research (with Didier Lopes, co-founder/CEO at OpenBB)
LlamaIndex Webinar: LLMs for Investment Research (with Didier Lopes, co-founder/CEO at OpenBB)
LlamaIndex
13 Discover LlamaIndex: Bottoms-Up Development With LLMs (Part 1, LLMs and Prompts)
Discover LlamaIndex: Bottoms-Up Development With LLMs (Part 1, LLMs and Prompts)
LlamaIndex
14 Discover LlamaIndex: Bottoms-Up Development With LLMs (Part 2, Documents and Metadata)
Discover LlamaIndex: Bottoms-Up Development With LLMs (Part 2, Documents and Metadata)
LlamaIndex
15 Discover LlamaIndex: Key Components to build QA Systems
Discover LlamaIndex: Key Components to build QA Systems
LlamaIndex
16 Discover LlamaIndex: Bottoms-Up Development with LLMs (Part 3, Evaluation)
Discover LlamaIndex: Bottoms-Up Development with LLMs (Part 3, Evaluation)
LlamaIndex
17 LlamaIndex Webinar: From Prompt to Schema Engineering with Pydantic  (with @jxnlco)
LlamaIndex Webinar: From Prompt to Schema Engineering with Pydantic (with @jxnlco)
LlamaIndex
18 Discover LlamaIndex: Bottoms-Up Development with LLMs (Part 4, Embeddings)
Discover LlamaIndex: Bottoms-Up Development with LLMs (Part 4, Embeddings)
LlamaIndex
19 Discover LlamaIndex: Custom Retrievers + Hybrid Search
Discover LlamaIndex: Custom Retrievers + Hybrid Search
LlamaIndex
20 LlamaIndex Webinar: Document Metadata and Local Models for Better, Faster Retrieval
LlamaIndex Webinar: Document Metadata and Local Models for Better, Faster Retrieval
LlamaIndex
21 LlamaIndex Webinar: Build Personalized AI Characters with RealChar
LlamaIndex Webinar: Build Personalized AI Characters with RealChar
LlamaIndex
22 LlamaIndex Webinar: Make RAG Production-Ready
LlamaIndex Webinar: Make RAG Production-Ready
LlamaIndex
23 LlamaIndex Workshop: Building RAG with Knowledge Graphs
LlamaIndex Workshop: Building RAG with Knowledge Graphs
LlamaIndex
24 Discover LlamaIndex: Introduction to Data Agents for Developers
Discover LlamaIndex: Introduction to Data Agents for Developers
LlamaIndex
25 LlamaIndex Webinar: Finetuning + RAG
LlamaIndex Webinar: Finetuning + RAG
LlamaIndex
26 Discover LlamaIndex: SEC Insights, End-to-End Guide
Discover LlamaIndex: SEC Insights, End-to-End Guide
LlamaIndex
27 Discover LlamaIndex: Custom Tools for Data Agents
Discover LlamaIndex: Custom Tools for Data Agents
LlamaIndex
28 LlamaIndex Sessions: Building a Lending Criteria Chatbot in Production
LlamaIndex Sessions: Building a Lending Criteria Chatbot in Production
LlamaIndex
29 Discover LlamaIndex: Bottoms-Up Development with LLMs (Part 5, Retrievers + Node Postprocessors)
Discover LlamaIndex: Bottoms-Up Development with LLMs (Part 5, Retrievers + Node Postprocessors)
LlamaIndex
30 LlamaIndex Webinar: How to Win a LLM Hackathon
LlamaIndex Webinar: How to Win a LLM Hackathon
LlamaIndex
31 LlamaIndex Webinar: LLM Challenges in Production (w/ Mayo Oshin, AI Jason, Dylan from Starmorph)
LlamaIndex Webinar: LLM Challenges in Production (w/ Mayo Oshin, AI Jason, Dylan from Starmorph)
LlamaIndex
32 LlamaIndex Webinar: Agents Showcase!
LlamaIndex Webinar: Agents Showcase!
LlamaIndex
33 LlamaIndex Webinar: Learn about DSPy
LlamaIndex Webinar: Learn about DSPy
LlamaIndex
34 LlamaIndex Webinar: Time-based retrieval for RAG (with Timescale)
LlamaIndex Webinar: Time-based retrieval for RAG (with Timescale)
LlamaIndex
35 LlamaIndex Webinar: Build/Break/Test LLM Apps Showcase (co-hosted with TrueEra, Pinecone)
LlamaIndex Webinar: Build/Break/Test LLM Apps Showcase (co-hosted with TrueEra, Pinecone)
LlamaIndex
36 LlamaIndex Workshop: Evaluation-Driven Development (EDD)
LlamaIndex Workshop: Evaluation-Driven Development (EDD)
LlamaIndex
37 LlamaIndex Webinar: Building LLM Apps for Production, Part 1 (co-hosted with Anyscale)
LlamaIndex Webinar: Building LLM Apps for Production, Part 1 (co-hosted with Anyscale)
LlamaIndex
38 LlamaIndex Webinar: Learn about Fine-tuning + RAG (w/ Victoria Lin, author of RA-DIT)
LlamaIndex Webinar: Learn about Fine-tuning + RAG (w/ Victoria Lin, author of RA-DIT)
LlamaIndex
39 LlamaIndex Webinar: What's next for AI after OpenAI Dev Day?
LlamaIndex Webinar: What's next for AI after OpenAI Dev Day?
LlamaIndex
40 Introducing create-llama
Introducing create-llama
LlamaIndex
41 LlamaIndex Webinar: PrivateGPT - Production RAG with Local Models
LlamaIndex Webinar: PrivateGPT - Production RAG with Local Models
LlamaIndex
42 Multi-modal Retrieval Augmented Generation with LlamaIndex
Multi-modal Retrieval Augmented Generation with LlamaIndex
LlamaIndex
43 LlamaIndex Webinar: LLaVa Deep Dive
LlamaIndex Webinar: LLaVa Deep Dive
LlamaIndex
44 A deep dive into Retrieval-Augmented Generation with Llamaindex
A deep dive into Retrieval-Augmented Generation with Llamaindex
LlamaIndex
45 LlamaIndex Workshop: Multimodal + Advanced RAG Workhop with Gemini
LlamaIndex Workshop: Multimodal + Advanced RAG Workhop with Gemini
LlamaIndex
46 LlamaIndex Webinar: Efficient Parallel Function Calling Agents with LLMCompiler
LlamaIndex Webinar: Efficient Parallel Function Calling Agents with LLMCompiler
LlamaIndex
47 Introduction to Query Pipelines (Building Advanced RAG, Part 1)
Introduction to Query Pipelines (Building Advanced RAG, Part 1)
LlamaIndex
48 LLMs for Advanced Question-Answering over Tabular/CSV/SQL Data (Building Advanced RAG, Part 2)
LLMs for Advanced Question-Answering over Tabular/CSV/SQL Data (Building Advanced RAG, Part 2)
LlamaIndex
49 LlamaIndex Webinar: Advanced Tabular Data Understanding with LLMs
LlamaIndex Webinar: Advanced Tabular Data Understanding with LLMs
LlamaIndex
50 Ollama X LlamaIndex Multi-Modal
Ollama X LlamaIndex Multi-Modal
LlamaIndex
51 Build Agents from Scratch (Building Advanced RAG, Part 3)
Build Agents from Scratch (Building Advanced RAG, Part 3)
LlamaIndex
52 LlamaIndex Webinar: Build No-Code RAG with Flowise
LlamaIndex Webinar: Build No-Code RAG with Flowise
LlamaIndex
53 LlamaIndex Sessions: Practical Tips and Tricks for Productionizing RAG (feat. Sisil @ Jasper)
LlamaIndex Sessions: Practical Tips and Tricks for Productionizing RAG (feat. Sisil @ Jasper)
LlamaIndex
54 Introduction to LlamaIndex v0.10
Introduction to LlamaIndex v0.10
LlamaIndex
55 Build SELF-DISCOVER from Scratch with LlamaIndex
Build SELF-DISCOVER from Scratch with LlamaIndex
LlamaIndex
56 Introducing LlamaCloud (and LlamaParse)
Introducing LlamaCloud (and LlamaParse)
LlamaIndex
57 LlamaIndex Sessions: 12 RAG Pain Points and Solutions
LlamaIndex Sessions: 12 RAG Pain Points and Solutions
LlamaIndex
58 LlamaIndex Webinar: RAG Beyond Basic Chatbots
LlamaIndex Webinar: RAG Beyond Basic Chatbots
LlamaIndex
59 A Comprehensive Cookbook for Claude 3
A Comprehensive Cookbook for Claude 3
LlamaIndex
60 LlamaIndex Webinar: RAPTOR - Tree-Structured Indexing and Retrieval
LlamaIndex Webinar: RAPTOR - Tree-Structured Indexing and Retrieval
LlamaIndex

LlamaIndex's SubQuestionQueryEngine enables users to ask complex comparison queries over multiple documents by breaking down queries into sub-questions and executing them on specific data sources. This allows for more nuanced and accurate answers.

Key Takeaways
  1. Import necessary libraries and configure the large language model service
  2. Load and index documents using the vector store index
  3. Build individual query engines for each index
  4. Add metadata to identify query engines to the top-level sub question query engine
  5. Run complex queries using the SubQuestionQueryEngine
💡 The SubQuestionQueryEngine allows for more accurate and nuanced answers to complex queries by breaking down queries into sub-questions and executing them on specific data sources.

Related Reads

📰
Designing a Production-Grade RAG System for PDF Question Answering
Learn to design a production-grade RAG system for PDF question answering and improve your LLM applications
Medium · AI
📰
ChatGPT vs. Gemini 2.5: Which Brain Fits Your 2026 Stack?
Learn how to optimize LLM model usage in your 2026 production stack by comparing ChatGPT and Gemini 2.5, and discover how to reduce token overspending
Dev.to AI
📰
I Scored 10,000 Job Listings a Day With GPT-4. Here's What Broke
Learn how to build and optimize a production-ready RAG pipeline to score 10,000 job listings daily using GPT-4
Dev.to AI
📰
Knowledge Distillation: Fit a Big Model's Smarts Into a Small One
Learn how to use knowledge distillation to transfer the knowledge of a large model to a smaller one, reducing latency and costs while maintaining performance.
Dev.to AI
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →