1 of 62

Introduction to

🦜🔗LangChain and Retrieval Augmented Generation (RAG)

Sophia Yang, Ph.D.

ANACONDA

2 of 62

ABOUT ME

5+ years at Anaconda

Psychology, Statistics, and Computer Science

DS/ML book club

sophiamyang

Open-source Python package creator and contributor

condastats, cranlogs, PyPowerUp, intake-stripe, intake-salesforce, HoloViz, etc.

Python & Open-source community

NumFocus, PyData, Scipy Conferences

sophiamyang

sophiamyang

SophiaYangDS

3 of 62

AGENDA

1

2

3

LangChain

RAG

Building a Chatbot

4 of 62

What is 🦜🔗 LangChain?

5 of 62

🦜🔗 LangChain is a framework for developing applications powered by language models.

connect a language model to other sources of context

Context-aware

reason about how to answer, what actions to take

Reason

6 of 62

prompt

LLM

Output

user query

7 of 62

prompt

LLM

Output

...

user query

LLMs

8 of 62

9 of 62

10 of 62

11 of 62

12 of 62

13 of 62

prompt

LLM

Output

prompt template

user query

Prompt Template

14 of 62

15 of 62

prompt

LLM

Output

Output parser

user query

Output Parser

16 of 62

17 of 62

prompt

LLM

Output

user query

Memories

18 of 62

19 of 62

Keep the last 1 interaction

20 of 62

creates a summary of the conversation over time

21 of 62

Combine the two

22 of 62

prompt template

23 of 62

LLM 🔗 memory 🔗 prompt

24 of 62

25 of 62

26 of 62

27 of 62

prompt

LLM

🔗

Output

user query

Chains

28 of 62

LLM

🔗

prompt

template

LLMChain

29 of 62

LLM

🔗

prompt

template

LLMChain

LLM

🔗

prompt

template

LLMChain

30 of 62

LLM

🔗

prompt

template

LLMChain

LLM

🔗

prompt

template

LLMChain

🔗

31 of 62

32 of 62

prompt

LLM

Output

tools

user query

Tools

33 of 62

🦜🔗 LangChain

Tools

34 of 62

Tools

35 of 62

Agent

Use an LLM to

choose a sequence

of actions to take

36 of 62

Agent

37 of 62

Agent

38 of 62

Agent

39 of 62

What is RAG

Retrieval Augmented Generation?

40 of 62

Knowledge base

Relevant

text chunks

LLM

Output

user query

41 of 62

42 of 62

43 of 62

44 of 62

45 of 62

Every step can be optimized!

A few examples:

46 of 62

47 of 62

SelfQueryRetriever:

semantic search + extract filters on the metadata

48 of 62

RAG Fine-tuning

49 of 62

How to build a Chatbot using Panel?

50 of 62

What is Panel?

51 of 62

Build a basic chatbot

52 of 62

53 of 62

Build an AI chatbot

54 of 62

55 of 62

Chat with PDF

56 of 62

Step 1: Define Panel widgets

57 of 62

Step 2: Wrap LangChain Logic into a Function

58 of 62

Step 2: Wrap LangChain Logic into a Function

replaced some values with the widgets we just defined

59 of 62

Step 3: Create a chat

interface

60 of 62

Step 4: Customize the look with a template

61 of 62

Chat with PDF

62 of 62

sophiamyang

sophiamyang

sophiamyang

SophiaYangDS