1 of 131

RAG

GenAI Enterprise Accelerator Master Class

LAVANYA.AI

2 of 131

Fine-Tuning

​

Updating the weights of some or all layers of the pre-trained model to adapt it to the new task

Prompt Engineering

​

Prompt engineering is temporary learning during inference.

​

It lets us communicate with an LLM to steer its behavior without updating the model weights

RAG

​

For tasks that require latest knowledge after the model pretraining time cutoff or access to private knowledge base

​

LAVANYA.AI

3 of 131

RAG

LAVANYA.AI

4 of 131

RAG

Personalized context to the query.

Overcome the models’ context limitations

Having access to relevant information reduces hallucinations.

5 of 131

RAG Overview

LAVANYA.AI

6 of 131

Components of a RAG System

LAVANYA.AI

7 of 131

LAVANYA.AI

8 of 131

?

embed

embed

1

1

110

1

LAVANYA.AI

9 of 131

Building a RAG Pipeline

LAVANYA.AI

10 of 131

You have a dataset, and you want to build an LLM to do interesting things with it.

LAVANYA.AI

11 of 131

Dataset

12 of 131

Building an

Evaluation Dataset

LAVANYA.AI

13 of 131

13

Comb through the corpus and come up with questions.

Disadvantages

​

  • Super cumbersome and time consuming

​

  • Nearly impossible to come up with questions that comprehensively cover your dataset

Manually

LAVANYA.AI

14 of 131

14

Use ChatGPT

LAVANYA.AI

15 of 131

15

Advantages

​

  • Generate a lot of diverse questions quickly�
  • Much faster than doing it manually

Disadvantages

​

  • Not the most evocative – they’re quite simple�
  • If your corpus is big (e.g. years of research data) then ChatGPT’s context window isn’t big enough to hold them all�
  • Our questions to be comprehensive, generate questions spanning most of the document, be of varying levels of difficulty etc

Use ChatGPT

LAVANYA.AI

16 of 131

16

Lets you systematically generate complex questions and tweak what types of questions you want to optimize for

Use an

LLM Framework

Reasoning

​

Craft the question so the LLM needs to reason to answer it

Multi-Context

​

Create questions that need information from multiple sections for the LLM to answer them

Conditioning

​

Modify the question to introduce a conditional element�

If X, then → Y

LAVANYA.AI

17 of 131

17

18 of 131

Building a RAG Pipeline

LAVANYA.AI

19 of 131

LAVANYA.AI

20 of 131

Load

Dataset

20

21 of 131

21

LAVANYA.AI

22 of 131

Advanced chunking techniques that take the meaning into account

Sentence based

chunking

Advanced chunking techniques that take the meaning into account

Chunking

LAVANYA.AI

23 of 131

23

LAVANYA.AI

24 of 131

LAVANYA.AI

Vector Embedding

25 of 131

LAVANYA.AI

26 of 131

26

Embed your Chunked Documents

LAVANYA.AI

27 of 131

When we get a user question…

LAVANYA.AI

28 of 131

28

LAVANYA.AI

29 of 131

LAVANYA.AI

Vector Embedding

30 of 131

Retrieve Context

30

LAVANYA.AI

31 of 131

31

LAVANYA.AI

32 of 131

Generate Answer

32

LAVANYA.AI

33 of 131

Evaluate Answers

LAVANYA.AI

34 of 131

Evaluation Is Key

34

LAVANYA.AI

35 of 131

35

A

Facts

A

?

LAVANYA.AI

36 of 131

Red Teaming

36

Red Teaming

  • probing, testing, and attacking of AI systems�
  • By a diverse group of people – AI, social sciences, security

​

​

Goal

  • understand the risk surface, and�
  • develop the list of harms… that can inform what needs to be measured and mitigated

LAVANYA.AI

37 of 131

37

LLM As

A Judge

Define Our Own Rubric

LAVANYA.AI

38 of 131

38

LLM as a Judge

LAVANYA.AI

39 of 131

SPADE

39

Automatically write useful evaluation functions for custom LLM chains

Prompt

Assertion

Code

LLM�as a judge

LAVANYA.AI

40 of 131

40

Evaluate

One Answer

LAVANYA.AI

41 of 131

LAVANYA.AI

Evaluate

All Answers

42 of 131

42

Evaluate All Answers

43 of 131

43

Learn from What we got Wrong

LAVANYA.AI

44 of 131

Challenge

LAVANYA.AI

Beat The Leaderboard

45 of 131

Challenge

(30 mins)

45

1. Understand the dataset wandb.me/explore-dataset

2. Get the code wandb.me/class-rag

Beat The Leaderboard

3. Beat the leaderboard

wandb.me/leaderboard

Setup

  1. Get keys
    1. Get OpenAI key
    2. Get Together.ai key
    3. Get W&B key�
  2. In Colab: Runtime > Run All to run code�
  3. Improve performance� look for # 🛠️LEVER�
  4. Tip: Change eval set to 5 to iterate fast

look for # 🔵 NOTE

​

46 of 131

Feedback Survey

46

LAVANYA.AI

47 of 131

47

Community

LAVANYA.AI

48 of 131

Thank You

Learn more at lavanya.ai

3 RAG

49 of 131

50 of 131

​

​

GenAI Enterprise Accelerator Master Class

RAG

51 of 131

ACCELERATOR.CC

Fine-Tuning

​

Updating the weights of some or all layers of the pre-trained model to adapt it to the new task

Prompt Engineering

​

Prompt engineering is temporary learning during inference.

​

It lets us communicate with an LLM to steer its behavior without updating the model weights

RAG

​

For tasks that require latest knowledge after the model pretraining time cutoff or access to private knowledge base

​

52 of 131

ACCELERATOR.CC

RAG

53 of 131

ACCELERATOR.CC

RAG

Personalized context to the query.

Overcome the models’ context limitations

​

Having access to relevant information reduces hallucinations.

54 of 131

ACCELERATOR.CC

RAG Overview

55 of 131

ACCELERATOR.CC

Components of

a RAG system

56 of 131

ACCELERATOR.CC

57 of 131

?

embed

embed

1

1

110

1

ACCELERATOR.CC

58 of 131

ACCELERATOR.CC

Building a

RAG Pipeline

59 of 131

You have a dataset, and you want to build an LLM to do interesting things with it.

ACCELERATOR.CC

60 of 131

Dataset

61 of 131

Building an

Evaluation Dataset

ACCELERATOR.CC

62 of 131

62

Comb through the corpus and come up with questions.

Disadvantages

​

  • Super cumbersome and time consuming

​

  • Nearly impossible to come up with questions that comprehensively cover your dataset

Manually

ACCELERATOR.CC

63 of 131

63

ACCELERATOR.CC

Use ChatGPT

64 of 131

64

Advantages

​

  • Generate a lot of diverse questions quickly�
  • Much faster than doing it manually

Use ChatGPT

Disadvantages

​

  • Not the most evocative – they’re quite simple�
  • If your corpus is big (e.g. years of research data) then ChatGPT’s context window isn’t big enough to hold them all�
  • Our questions to be comprehensive, generate questions spanning most of the document, be of varying levels of difficulty etc

ACCELERATOR.CC

65 of 131

65

Lets you systematically generate complex questions and tweak what types of questions you want to optimize for

Use an LLM Framework

Reasoning

​

Craft the question so the LLM needs to reason to answer it

Multi-Context

​

Create questions that need information from multiple sections for the LLM to answer them

Conditioning

​

Modify the question to introduce a conditional element�

If X, then → Y

ACCELERATOR.CC

66 of 131

66

67 of 131

ACCELERATOR.CC

Build A RAG Pipeline

68 of 131

ACCELERATOR.CC

69 of 131

Load

Dataset

69

70 of 131

70

ACCELERATOR.CC

71 of 131

Advanced chunking techniques that take the meaning into account

Sentence based chunking

Advanced chunking techniques that take the meaning into account

ACCELERATOR.CC

Chunking

72 of 131

72

ACCELERATOR.CC

73 of 131

ACCELERATOR.CC

Vector Embedding

74 of 131

ACCELERATOR.CC

75 of 131

75

Embed your Chunked Documents

ACCELERATOR.CC

76 of 131

ACCELERATOR.CC

When we get a user question…

77 of 131

77

ACCELERATOR.CC

78 of 131

ACCELERATOR.CC

Vector Embedding

79 of 131

Retrieve Context

79

ACCELERATOR.CC

80 of 131

80

ACCELERATOR.CC

81 of 131

Generate Answer

81

ACCELERATOR.CC

82 of 131

ACCELERATOR.CC

Evaluate Answers

83 of 131

Evaluation Is Key

83

ACCELERATOR.CC

84 of 131

84

A

Facts

A

?

ACCELERATOR.CC

85 of 131

Red Teaming

85

Red Teaming

  • probing, testing, and attacking of AI systems�
  • By a diverse group of people – AI, social sciences, security

​

​

​

Goal

  • understand the risk surface, and�
  • develop the list of harms… that can inform what needs to be measured and mitigated

ACCELERATOR.CC

86 of 131

86

LLM As

A Judge

Define Our Own Rubric

87 of 131

87

LLM as a Judge

88 of 131

SPADE

88

Automatically write useful evaluation functions for custom LLM chains

Prompt

Assertion

Code

LLM�as a judge

ACCELERATOR.CC

89 of 131

89

Evaluate

One Answer

ACCELERATOR.CC

90 of 131

ACCELERATOR.CC

Evaluate

All Answers

91 of 131

91

Evaluate All Answers

92 of 131

92

Learn from What we got Wrong

93 of 131

93

​

​

Beat The Leaderboard

CHALLENGE

94 of 131

Challenge

(30 mins)

94

1. Understand the dataset wandb.me/explore-dataset

2. Get the code wandb.me/class-rag

Beat The Leaderboard

3. Beat the leaderboard

wandb.me/leaderboard

Setup

  • Get keys
    • Get OpenAI key
    • Get Together.ai key
    • Get W&B key�
  • In Colab: Runtime > Run All to run code�
  • Improve performance� look for # 🛠️LEVER�
  • Tip: Change eval set to 5 to iterate fast

look for # 🔵 NOTE

​

95 of 131

Feedback Survey

​

wandb.me/feedback

95

ACCELERATOR.CC

96 of 131

Community

96

wandb.me/discord

ACCELERATOR.CC

97 of 131

​

​

Thank You

lavanya@wandb.com

@lavanyai

98 of 131

Challenge

(30 mins)

98

Beat The Leaderboard

Parameter

Good

Default

chunksize

2048

10000

embedding

text-embedding-3-small

text-embedding-3-small

chunking strategy

simple

​

temperature

0.5

1.0

top_k

1

1000

k=2

2

1

prompt

​

​

models

​

​

​

​

​

99 of 131

ACCELERATOR.CC

RAG

RAG is a solution for knowledge-intensive tasks where all the available knowledge can’t be input into the model directly.

​

    • With RAG, only the information most relevant to the query, is retrieved and input into the model.
    • Having access to relevant information in the context can help the model generate more detailed responses while reducing hallucinations.
    • Its main purpose was to overcome the models’ context limitations.

100 of 131

Its main purpose was to overcome the models’ context limitations.

ACCELERATOR.CC

Will a sufficiently long context will be the end of RAG?

Overcoming context limitations

101 of 131

You can think of RAG as a technique to construct context specific to each query, instead of using the same context for all queries.

​

Context construction for foundation models is equivalent to feature engineering for classical ML models.

ACCELERATOR.CC

Personalizing Context

102 of 131

LLM

Information Retriever

LLM

LLM

LLM

BM25

Medical Literature

Question2Query

Query

Relevance

Classifier

Relevant

Articles

Summarization

Synthesis

Question

Answer + Citations

110

?

1

1

1

embed

embed

ACCELERATOR.CC

103 of 131

ACCELERATOR.CC

Retrieval Algorithms

Term-based Retrieval

Embedding Based

104 of 131

ACCELERATOR.CC

Compare retrievers

​

​

Term-based retrieval

Semantic retrieval

Querying speed

Much faster than semantic retrieval

Query embedding generation and vector search can be slow

Performance

Typically strong performance out-of-the-box, but hard to improve

Can retrieve wrong documents due to term ambiguity

Can outperform term-based retrieval with fine tuning

Allows using more natural queries, as it focuses on semantics instead of terms

Cost

Much cheaper than semantic retrieval

Embedding, vector storage, and vector search solutions can be expensive

105 of 131

ACCELERATOR.CC

Retrieval Optimization

106 of 131

ACCELERATOR.CC

107 of 131

ACCELERATOR.CC

    • add graphic

Query by

related content

108 of 131

ACCELERATOR.CC

    • Key factors to keep in mind when evaluating a retrieval solution.
    • What retrieval mechanisms does it support? Does it support hybrid search?
    • If it’s a vector database, what embedding models and vector search algorithms does it support?
    • How scalable is it, both in terms of data storage and query traffic? Does it work for your traffic patterns?
    • How long does it take to index your data? How much data can you process (such as add/delete) in bulk at once?
    • What’s its query latency for different retrieval algorithms?
    • If it’s a managed solution, what’s its pricing structure? Is it based on the document/vector volume or on the query volume?

Evaluation

109 of 131

ACCELERATOR.CC

RAG++

110 of 131

ACCELERATOR.CC

Multi-modal RAG

111 of 131

ACCELERATOR.CC

RAGs with tabular data

Order ID

Timestamp

Product ID

Product

Unit price ($)

Units

Total

1

…

2044

Meow Mix Seasoning

10.99

1

10.99

2

…

3492

Purr & Shake

25

2

50

3

…

2045

Fruity Fedora

18

1

18

…

…

…

…

…

…

…

112 of 131

SpaceRAG Demo

​

Code + Weave

112

ACCELERATOR.CC

113 of 131

ACCELERATOR.CC

RAG From Scratch

114 of 131

ACCELERATOR.CC

Level 1

The Basics

    • Add visuals to these slides

​

115 of 131

ACCELERATOR.CC

Level 2

More Structured Processing

116 of 131

ACCELERATOR.CC

Level 3

Observability

117 of 131

ACCELERATOR.CC

Level 4

Evaluations

118 of 131

ACCELERATOR.CC

Level 5

Understanding Short Comings

119 of 131

ACCELERATOR.CC

Level 6

Level 7

Level 8

Level 9

Finding Segments and Routing: Techniques for identifying and directing data segments efficiently.

​

Processing Tables: Strategies for interpreting and manipulating tabular data.

​

Processing Images: Methods for incorporating image data into RAG applications.

Advanced Data Handling

Building Up Timeline Queries: Crafting queries that span across different timeframes for dynamic data analysis.

​

Adding Additional Metadata: Leveraging metadata to enrich query context and improve response accuracy.

Query Enhancement

Summarization and Summary Indices: Developing concise summaries from extensive datasets to aid quick insights.

Summarization Techniques

Modeling Business Outcomes: Applying RAG techniques to predict and model business outcomes, facilitating strategic decision-making.

Outcome

Modeling

More Levels

120 of 131

ACCELERATOR.CC

Embedding based Retrieval

121 of 131

ACCELERATOR.CC

Chunking

Sentence based chunking

Advanced chunking techniques that take the meaning into account

122 of 131

Add graphic

ACCELERATOR.CC

Reranking

123 of 131

ACCELERATOR.CC

Query Rewriting

124 of 131

ACCELERATOR.CC

Start with Synthetic Data

125 of 131

ACCELERATOR.CC

Continuous RAG Improvement

126 of 131

ACCELERATOR.CC

  1. Initial Implementation
  2. Synthetic Data Generation
  3. Fast Evaluations
  4. Real-World Data Collection
  5. Classification and Analysis
  6. System Improvements
  7. Production Monitoring
  8. User Feedback Integration
  9. Iteration

127 of 131

ACCELERATOR.CC

Start with Synthetic Data

128 of 131

ACCELERATOR.CC

Focus on Leading Metrics

129 of 131

ACCELERATOR.CC

Fast, Unit Test-Like Evaluations

130 of 131

ACCELERATOR.CC

Real-World Data and Clustering

131 of 131

ACCELERATOR.CC

Continuous Improvement Loop