1 of 38

Mastering Retrieval Augmented Generation (RAG) with LlamaIndex and LLMs

Ravi Theja:

Data Scientist - Glance (Inmobi)

Open Source Contributor at LlamaIndex.

2 of 38

Context

  • LLMs are a phenomenal piece of technology for knowledge generation and reasoning. They are pre-trained on large amounts of publicly available data.

Use Cases

Question-Answering

Text Generation

Summarization

Planning

LLM’s

3 of 38

Context

  • How do we best augment LLMs with our own private data?

Use Cases

Question-Answering

Text Generation

Summarization

Planning

LLM’s

API’s

Raw Files

SQL DB’s

Vector Stores

?

4 of 38

Paradigms for inserting knowledge

Fine-tuning - baking knowledge into the weights of the network

LLM

Before college the two main things I worked on, outside of school, were writing and programming. I didn't write essays. I wrote what beginning writers were supposed to write then, and probably still are: short stories. My stories were awful. They had hardly any plot, just characters with strong feelings, which I imagined made them deep...

RLHF, Adam, SGD, etc.

5 of 38

Paradigms for inserting knowledge

Fine-tuning - baking knowledge into the weights of the network

Downsides:

  • Data preparation effort
  • Lack of transparency
  • Doesn’t work well
  • High upfront cost

6 of 38

Paradigms for inserting knowledge

In-context learning - Fix the model, put context into the prompt

LLM

Before college the two main things I worked on, outside of school, were writing and programming. I didn't write essays. I wrote what beginning writers were supposed to write then, and probably still are: short stories. My stories were awful. They had hardly any plot, just characters with strong feelings, which I imagined made them deep...

Input Prompt

Here is the context:

Before college the two main things…

Given the context, answer the following question:ďż˝{query_str}

7 of 38

Key challenges of in-context learning

  • How to retrieve the right context for the prompt?
  • How to deal with long context?
  • How to deal with source data that is potentially very large? (GB’s, TB’s)
  • How to tradeoff between:
    • Performance
    • Latency
    • Cost

8 of 38

LlamaIndex: A data framework for LLM applications

  • Data Management and Query Engine for your LLM application
  • Offers components across the data lifecycle: ingest, index, and query over data

Data Ingestion (LlamaHub 🦙)

Data Structures

Retrieval and Query Interface

  • Connect your existing data sources and data formats (API’s, PDF’s, docs, SQL, etc.)
  • Store and index your data for different use cases. Integrate with different db’s.
  • Given an input prompt, retrieve relevant context and synthesize a knowledge-augmented output.

9 of 38

LlamaIndex

Knowledge-Intensive LLM Applications

LlamaIndex

Data framework for LLM app development

Foundation Models

Input: rich query description

Output: rich response with references, actions, etc

Sales

Marketing

Recruiting

Dev

Legal

Finance

…

10 of 38

Data Connectors: powered by LlamaHub 🦙

  • Easily ingest any kind of data, from anywhere
    • into unified document containers
  • Powered by community-driven hub
    • rapidly growing (100+ loaders and counting!)
  • Growing support for multimodal documents (e.g. with inline images)

<10 lines of code to ingest from Notion

11 of 38

Data Indices + Query Interface

Your source documents are stored in a data collection

In-memory, MongoDB

Our data indices help to provide a view of your raw data

Vectors, keyword lookups, summaries

A retriever helps to retrieve relevant documents for your query

A query engine manages retrieval and synthesis given the query.

12 of 38

Vector Store Index

Doc

Doc

Doc

Vector Store

Node1

Node2

Node3

Embedding1

Embedding2

Embedding3

Raw Documents

Stored as Nodes in a vector store

Each Node is indexed with an embedding

13 of 38

Vector Store Index

14 of 38

Response Synthesis

Refine

15 of 38

Use Case: Semantic Search

Answer

The author grew up writing short stories, programming on an IBM 1401, and working on microcomputers. He wrote simple games, a program to predict how high his model rockets would fly, and a word processor. He studied philosophy in college, but switched to AI. He reverse-engineered SHRDLU for his undergraduate thesis and wrote a book about Lisp hacking. He visited the Carnegie Institute and realized he could make art that would last.

from llama_index import GPTVectorStoreIndex, SimpleDirectoryReader

documents = SimpleDirectoryReader('data').load_data()

index = VectorStoreIndex.from_documents(documents)

query_engine = index.as_query_engine(response_mode="tree_summarize")

response = query_engine.query(

"What did the author do growing up?"

)

16 of 38

Use Case: Summarization

Answer

  • The author began writing and programming before college, and studied philosophy in college before switching to AI.
  • He realized that AI, as practiced at the time, was a hoax and decided to focus on Lisp hacking instead.
  • He wrote a book about Lisp hacking and graduated with a PhD in computer science.
  • ….

from llama_index import GPTListIndex, SimpleDirectoryReader

documents = SimpleDirectoryReader('data').load_data()

index = ListIndex.from_documents(documents)

query_engine = index.as_query_engine(response_mode="tree_summarize")

response = query_engine.query("Could you give a summary of this article in newline separated bullet points?")

17 of 38

Using Open Source Models - Llama2

18 of 38

Using Open Source Models - Llama2

19 of 38

Use Case: Building a Unified Query Interface

Can use a “Router” abstraction to route to different query engines.

For instance, can do joint semantic search / summarization

20 of 38

Use Case: Document Comparisons

Say you want to compare the 2021 10-K filings for Uber and Lyft

Question: “Compare and contrast the customer segments and geographies that grew the fastest.”

Generate a query plan over your document sources.

21 of 38

Use Case: Exploiting Temporal Relationships

Given a question, what if we would like to retrieve additional context in the past or the future?

Example question: “What did the author do after his time at Y Combinator?”

Requires looking at context in the future!

22 of 38

Use Case: Recency Filtering / Outdated nodes

Imagine you have three timestamped versions of the same data.

If you ask a question over this data, you want to make sure it’s over the latest document.

23 of 38

Use Case: Masking PII information

NER PII Node processor

My name is Ravi Theja and I live in Bangalore. My email address is ravi.theja@gmail.com and my phone number is +91 9550164716.

My name is [NAME] and I live in [PLACE]. My email address is [EMAIL] and my phone number is [CONTACT]

Masking personal information is beneficial prior to inputting it into LLM.

24 of 38

Llama Readers

  • Dhruv and team at MumbaiHacks

25 of 38

Use Case: Joint Text-to-SQL and Semantic Search

Query with SQL over structured data, and “join” it with unstructured context from a vector database!

Combine expressivity of SQL with semantic understanding

26 of 38

Evaluation:

  • What is the need for evaluation?
  • Question Generator.
  • Evaluators.

27 of 38

Response Evaluator

Takes in response source information and output response to evaluate the correctness of the response.

28 of 38

Query Response Evaluator

Takes in Query, response source information and output response to evaluate the correctness of the response.

29 of 38

Source Context Evaluation

Takes in Query, each source information and output response to evaluate the correctness of the response.

30 of 38

AlBus

31 of 38

Workflow:

32 of 38

Data Agents

  1. Powered by LLMs.
  2. Handle all data types.
  3. Call external APIs.
  4. Store conversation history.
  5. Perform diverse data tasks.

33 of 38

34 of 38

35 of 38

36 of 38

Query

Wikipedia

Index

Query Engine

Response

On-Demand Loader Tool

37 of 38

LLamaIndex in production

  1. Guide to build full stack web app with LlamaIndex.
  2. Deploying and monitoring with Vellum
  3. Build and Scale a Powerful Query Engine with LlamaIndex and Ray

38 of 38

Thanks!

Check out our docs for more details