What is LangChain? 101 Beginner's Guide Explained with Animations

Edrick
5 Jun 202311:01

Summary

TLDRThe video script delves into LangChain, a framework that simplifies the development of applications using large language models. It discusses the evolution from a Python package to TypeScript and Ruby ports, emphasizing LangChain's ability to handle complex tasks involving data integration and model actions. The script explores components like document loaders, text splitters, vector databases, and prompt templates, illustrating how they streamline interactions with language models. It also touches on the importance of semantic search, embedding models, and the use of chains and agents to create dynamic, stateful applications, hinting at the vast potential of this emerging technology.

Takeaways

  • πŸ”₯ LangChain is a popular framework for developing applications on top of large language models, addressing the complexities beyond simple API requests.
  • πŸ”§ It started as a Python package but has TypeScript and Ruby ports, aiming for feature parity and expanding accessibility.
  • πŸ“ˆ LangChain simplifies tasks by chaining components to handle data from various sources and execute actions, making it easier to swap models or databases.
  • πŸ” The framework supports models like language models and text embedding models, which are crucial for semantic search and understanding text in a vector space.
  • πŸ“ Prompts are essential for guiding the input to language models, with LangChain offering prompt templates for flexibility and structured input.
  • πŸ“š Few-shot learning is facilitated by providing examples to prompts, helping to shape the model's output with a given set of data.
  • πŸ”‘ Output parsers are used to automatically structure the language model's output into objects, making it easier to handle and interpret.
  • πŸ—‚ The indexes module allows for the use of personal data with document loaders and text splitters, enabling the integration of diverse data formats.
  • πŸ”Ž Vector databases are integrated with LangChain for storing and retrieving embedded documents, supporting semantic search capabilities.
  • πŸ”— Chains in LangChain abstract model calls to language models, handling prompt formatting, model interaction, and result retrieval in a stateless manner.
  • πŸ€– Agents in LangChain, though not detailed in the script, are mentioned as a higher-level concept that will be explored further, indicating a potential for more complex interactions.

Q & A

  • What is LangChain?

    -LangChain is a framework used to develop applications on top of large language models like GPT, Llama, and Hugging Face models. It started as a Python package and now has a TypeScript port and a Ruby version in development.

  • Why is LangChain needed?

    -LangChain is needed because it simplifies the development of complex applications that involve connecting language models to various data sources and making them perform actions like sending emails, searching the web, or running code. It provides components that handle these tasks, making the process less messy and repetitive.

  • What are some of the components LangChain offers?

    -LangChain offers components such as document loaders, text splitters, vector databases, prompt templates, and tools for actions like sending emails. These components help in loading, processing, and managing data for language models.

  • What are the two types of models in LangChain?

    -The two types of models in LangChain are language models and text embedding models. Language models take text as input and return text, while text embedding models convert text into arrays of numbers for use in vector spaces.

  • What is semantic search and how is it used in LangChain?

    -Semantic search is a technique that finds pieces of text most similar in meaning to a given input within a vector space. LangChain uses text embedding models to create these vector representations, enabling semantic search over the stored data.

  • How does LangChain handle prompts?

    -LangChain provides the prompt template class to construct prompts using multiple values. It supports various use cases like few-shot learning and output parsing, enhancing the flexibility and readability of prompts.

  • What is few-shot learning in the context of LangChain?

    -Few-shot learning in LangChain involves providing the model with a few examples to guide its output. This is done by creating a list of examples, formatting them with a template, and using the few-shot prompt template object.

  • What is the purpose of output parsers in LangChain?

    -Output parsers in LangChain automatically structure the output from language models into defined objects, making the results more useful and organized. For example, they can parse the output into joke objects with specific fields like setup and punchline.

  • What are indexes in LangChain, and how are they used?

    -Indexes in LangChain refer to methods of loading and managing data. The indexes module provides document loaders for various formats, text splitters to chunk large texts, and integrations with vector databases for storing and retrieving embedded text.

  • What is the difference between stateless and stateful operations in LangChain?

    -Stateless operations in LangChain do not remember previous interactions, while stateful operations use memory to maintain context across interactions. For example, conversation chains use memory to handle ongoing dialogues, making them stateful.

Outlines

00:00

πŸ€– Introduction to LangChain: AI Framework for Complex Applications

LangChain is a cutting-edge framework that facilitates the development of applications on top of large language models. Initially a Python package, it now includes a TypeScript version and a budding Ruby version. The framework is essential for handling complex tasks such as integrating language models with various data sources and performing actions like sending emails or executing code. LangChain simplifies these processes through components like document loaders, text splitters, vector databases, and prompt templates. It also supports easy swapping of language models and vector databases, enhancing flexibility and efficiency in AI application development.

05:01

πŸ” Deep Dive into LangChain Components: Models, Prompts, and Indexes

This paragraph delves into the intricacies of LangChain's components, focusing on models, prompts, and indexes. It explains the distinction between language models and text embedding models, the latter of which convert text into numerical arrays for semantic search. The paragraph also covers prompt templates, which are flexible inputs for models, and output parsers that structure the model's outputs. Additionally, it introduces the concept of few-shot learning for guiding model responses and example selectors for managing costs. The indexes module is highlighted for its ability to load and process data from various formats and services, emphasizing the importance of data in AI applications.

10:02

πŸ”— LangChain in Action: Chains, Agents, and Practical Applications

The final paragraph showcases how LangChain can be practically applied in creating AI applications. It discusses the use of chains to abstract model calls and memory to make interactions stateful. The paragraph also touches on custom chains for specific tasks and high-level chains for solving complex problems. An example of chaining language model calls to generate a company name and a catchphrase is provided. The potential of agents in LangChain is mentioned, with a promise of a future in-depth exploration. The paragraph concludes by emphasizing the practicality and versatility of LangChain in the rapidly evolving field of AI.

Mindmap

Keywords

πŸ’‘LangChain

LangChain is a framework designed to facilitate the development of applications using large language models. It started as a Python package and is expanding to other languages like TypeScript and Ruby. In the video, LangChain is presented as a solution to the complexity that arises when integrating language models with various data sources and actions, such as sending emails or running code.

πŸ’‘Language Models (LMs)

Language models are AI systems trained to generate human-like text. They are a core component of LangChain, allowing developers to interact with and utilize these models within their applications. The script distinguishes between two subtypes: large language models (LLMs) and chat models, with the former taking text as input and the latter handling chat messages.

πŸ’‘Vector Databases

Vector databases are used to store and retrieve text data in a format that represents text as vectors in a multi-dimensional space. They enable semantic search, where the system looks for text that is similar in meaning. In the context of the video, vector databases are used in conjunction with text embedding models to index and retrieve data efficiently.

πŸ’‘Text Embedding Models

Text embedding models convert text into a numerical format, allowing for semantic comparisons and searches within a vector space. The video mentions that these models can process large texts and output vectors of high dimensions, which are crucial for the functionality of vector databases.

πŸ’‘Prompts

Prompts are the inputs given to language models to guide their output. The video explains that prompts can be constructed using prompt templates, which allow for flexibility and can be formatted with multiple values. They are essential for controlling the responses of language models and are used in techniques like few-shot learning.

πŸ’‘Few-shot Learning

Few-shot learning is a technique where a model is given a few examples to guide its output for a specific type of query. The video script uses this concept to illustrate how prompts can be formatted with examples to help language models understand the expected responses, thus improving the quality of their output.

πŸ’‘Output Parsers

Output parsers are used to automatically structure the outputs from language models into a specific format, such as objects. In the script, an example is given where a joke's setup and punchline are parsed into a 'joke' object, demonstrating how output parsers can help in organizing and interpreting the model's responses.

πŸ’‘Document Loaders

Document loaders are components in LangChain that can load data from various sources and formats, such as PDFs, CSVs, and SQL databases. They are essential for integrating external data into the application, allowing developers to work with their own data within the framework.

πŸ’‘Text Splitters

Text splitters are used to divide large text documents into smaller, manageable chunks that can fit within the input token size limits of language models. The video mentions using a recursive character text splitter to create documents from large texts, which is important for constructing prompts that can be effectively processed by the models.

πŸ’‘Chains

Chains in LangChain are a way to abstract model calls to language models, allowing developers to create sequences of operations that can be executed in order. The video describes how chains can be used to format prompts, send them to models, and handle the results, making it easier to manage complex interactions with language models.

πŸ’‘Agents

Agents in the context of LangChain are higher-level abstractions that use language models to perform tasks. While not fully detailed in the script, agents are mentioned as a way to think about what needs to be done using a language model before actually doing it, potentially involving the use of tools and other components within the framework.

Highlights

Langchain is a framework for developing applications on top of large language models, addressing the complexity of integrating models with various data sources and actions.

Langchain started as a Python package with a TypeScript port catching up in feature parity and a newly started Ruby version.

It simplifies tasks like connecting language models to data from Google Analytics, Stripe, SQL, PDFs, and CSVs, and performing actions like sending emails and running code.

Langchain uses components like document loaders, text splitters, and vector databases to manage data and inject it into prompt templates for model input.

The framework allows for easy swapping of language models, vector databases, and data sources with minimal code changes.

Langchain's modules include models, prompts, indexes, memory, chains, and agents, each serving a specific purpose in the development process.

Models can be language models or text embedding models, with the latter converting text into numerical arrays for vector space representation.

Prompt templates in Langchain offer flexibility in constructing prompts using multiple values and support few-shot learning for guiding model output.

Output parsers automatically structure the outputs from language models into objects, making it easier to handle complex data.

Example selectors help manage the cost and complexity of prompts by selecting relevant examples for few-shot learning.

Chat prompt templates are designed for chat models, which take a list of chat messages as input, each with a role (AI, human, system).

Indexes module in Langchain provides document loaders for various formats and services, allowing easy integration of diverse data sources.

Text splitters are used to break down large texts into manageable chunks for embedding and retrieval in vector databases.

Vector stores in the indexes module offer integrations with various databases like Pinecone, Redis, and ChromaDB for semantic search capabilities.

Chains in Langchain abstract model calls to language models, handling prompt formatting, model interaction, and result retrieval.

Memory can be used with chains to make interactions stateful, allowing for more context-aware responses in applications.

Custom chains or high-level chains can be created for complex tasks, such as solving math problems or letting the language model write and execute code.

Agents in Langchain, to be covered in more depth in future videos, represent a higher level of abstraction for accomplishing tasks using language models.

Transcripts

play00:00

langjin probably one of the hottest

play00:02

things in AI right now after Vector

play00:04

databases it's a framework used to

play00:06

develop applications on top of large

play00:07

language models like GPT llama hugging

play00:10

face models and much more it first

play00:12

started as a python package but there is

play00:14

also a typescript port catching up in

play00:15

feature parity and a ruby one that just

play00:17

got started but why is it needed in the

play00:19

first place could we simply send a

play00:21

request an API or model and call it a

play00:23

day and you're right that would work for

play00:25

simple applications but as soon as you

play00:27

start increasing the complexity like

play00:28

connecting the language model to your

play00:30

own data from Google analytics stripe

play00:32

SQL PDF csvs or making the language

play00:35

models take actions such as sending

play00:37

emails searching the web or running code

play00:39

in your terminal things start to get

play00:41

messy and repetitive Lane chain offers

play00:43

to fix this issue with components we can

play00:45

use document loaders to load data from

play00:47

sources like PDF stripe and then

play00:49

optionally use a text splitter to chunk

play00:51

it down before storing it in a vector

play00:53

database at runtime the data can be

play00:55

injected in a prompt template to be then

play00:57

sent as input to the models we might

play00:59

also use tools to take actions for

play01:01

example sending an email with the

play01:03

output's content in practice these

play01:05

abstractions mean you can easily swap to

play01:07

another llm because of cost or features

play01:09

test another Vector database

play01:11

capabilities or ingest another data

play01:13

source in a few lines of code chains are

play01:15

how the magic happens we chain together

play01:17

components in a way to accomplish a

play01:19

specific tasks agents are more abstract

play01:22

the first think about what they need to

play01:24

do using a language model before doing

play01:26

it using things like tools if you're

play01:28

curious about research papers showing

play01:30

the power of connecting language models

play01:32

to your own data and the outside world

play01:34

consider looking at self ask with search

play01:37

and react which were published around

play01:39

the same time Lang chain was launched

play01:40

now let's take a look at what's really

play01:42

going on behind the scenes there are

play01:44

currently seven modules LinkedIn offers

play01:45

that newcomers to the space should know

play01:47

about models prompts indexes memory

play01:50

chains and agents models at a high level

play01:53

there are two different types of models

play01:55

language models and text embedding

play01:57

models the embedding models turn text

play01:59

into an array of numbers we can then

play02:01

think of text in a vector space in this

play02:04

image we can see how in a 2d space man

play02:06

is the king what woman is the queen they

play02:08

represent different things but we can

play02:10

see a pattern this allows for semantic

play02:12

search where we look for pieces of text

play02:14

that are most similar in meaning for a

play02:16

given thesis text within the vector

play02:17

space for example open ai's text

play02:19

embedding model can embed large pieces

play02:21

of text precisely 8 100 and 91 tokens

play02:25

which given their word to token ratio of

play02:27

0.75 should roughly work for up to

play02:30

6143 words it outputs vectors of 1536

play02:35

Dimensions we can use LINE chain to

play02:36

interface with multiple embedding

play02:38

providers some apis like open Ai and

play02:40

cohere are paid but we can do it for

play02:43

free and enter privacy on our data by

play02:45

using open source models run locally

play02:47

with hugging faces open source embedding

play02:49

you can now create your own embeddings

play02:50

on your own machine using only four

play02:52

lines of code however the number of

play02:54

Dimensions might be different and the

play02:56

quality of embeddings might be lower

play02:57

which could make retrieval less accurate

play02:59

moving on to language models there are

play03:01

two different subtypes llms and chat

play03:04

models llms wrap apis which take text in

play03:07

and return text chat models wrap models

play03:09

which take chat messages in and return a

play03:12

chat message it's a subtle difference

play03:14

but the interface to use them is the

play03:16

same we can import both classes

play03:18

instantiate them and then use the

play03:20

predict function on both of them and see

play03:21

the difference however you probably

play03:23

won't be passing text directly to the

play03:25

models and instead use prompts a prompt

play03:28

refers to the input of the model we

play03:30

often want more flexibility than in

play03:32

hard-coded string link chain provides

play03:34

the prompt template class to

play03:35

constructive prompt using multiple

play03:37

values the important Concepts to know of

play03:39

prompts are prompt templates output

play03:41

parsers example selectors and chat

play03:43

prompt templates prompt templates here's

play03:45

an example first you'll want to create a

play03:47

prompt template object there are two

play03:48

ways to do this import prompt template

play03:51

then instant sheet 1 using the

play03:52

Constructor specify your input variables

play03:55

in an array and in your template string

play03:57

by putting them inside of curly braces

play03:59

if you're feeling lazy you can also use

play04:00

the helper method from template so you

play04:03

don't have to specify the input

play04:04

variables explicitly in both cases you

play04:06

can then format The Prompt by telling it

play04:09

what values to replace the placeholders

play04:10

with under the hood it uses F strings to

play04:13

format your prompt by default but you

play04:15

could also use ginger 2 but then why not

play04:16

just use f strings directly the prompts

play04:19

help with readability play well with the

play04:21

rest of the ecosystem and support common

play04:22

use cases like Q shot learning or output

play04:25

parsing few shot learning means we give

play04:27

our prompts a few examples to guide its

play04:29

output let's see how this is done first

play04:31

create a list with a few examples next

play04:33

we specify the template to format the

play04:36

examples we have provided every example

play04:38

will be formatted using this prompt

play04:40

template finally we create the few shot

play04:42

prompt template object pass in the

play04:44

examples the example formatter a prefix

play04:46

given the command and suffix that aims

play04:48

to guide the output of the llm let's not

play04:51

forget the input variables and a

play04:52

separator that will be used to separate

play04:54

the examples from the prefix and the

play04:56

suffix we can now generate a prompt and

play04:58

this is what it looks like this is an

play05:00

extremely useful Paradigm to control the

play05:02

output of the llm and guide its response

play05:04

similarly we might want to use output

play05:07

parsers we will be automatically parsing

play05:09

the output of the language model into an

play05:11

object it's a little more complex but

play05:13

very useful in structuring the random

play05:14

outputs from llms let's say we want to

play05:16

use openai to create joke objects we can

play05:19

Define our joke class to be more

play05:21

specific we'll need the jokes setup and

play05:23

punchline We'll add descriptions to help

play05:25

the language model understand what those

play05:26

mean then we can set up a parser and

play05:29

tell it to parse using our joke class we

play05:31

use the pedantic output parser which is

play05:33

the strongest and most recommended now

play05:35

we create our prompt template let's pass

play05:37

the template string input variable and

play05:39

we can use the partial variables field

play05:41

to inject or parsing instructions into

play05:43

the prompt template we can then ask the

play05:45

llm to tell us a joke now that we have

play05:47

our prompt ready this is what we need to

play05:49

do to send it to openai we'll first load

play05:51

our openai API key from our DOT and file

play05:53

then instantiate the model run its

play05:55

called under method and parse the

play05:57

model's output using the parser that

play05:59

we've instantiated it and there we go we

play06:00

now have our joke object with its setup

play06:02

and punchline defined the generated

play06:04

prompt is pretty complex consider

play06:06

checking out the GitHub repo to have a

play06:08

better look at it and to really

play06:09

understand what's going on we've

play06:10

previously covered few shot learning

play06:12

where we pass along examples to show the

play06:14

model expected answers for a certain

play06:16

type of query it might be possible that

play06:18

we have many such examples and we can't

play06:20

possibly fit all of them also that could

play06:22

become pretty expensive pretty fast

play06:23

that's where example selectors come into

play06:26

play to keep the cost of a prompt

play06:28

relatively constant we'll use the

play06:29

length-based example selector just like

play06:31

before we specify an example prompt this

play06:34

defines how each example will be

play06:36

formatted we curator selector pass in

play06:38

the examples and then the max length by

play06:40

default the length refers to the number

play06:42

of words and new lines used by the

play06:43

formatter examples section of the prompt

play06:45

and we can take the previous View shot

play06:47

template we defined and simply add the

play06:49

selector we just created replacing the

play06:51

example keyword argument but what about

play06:52

interacting with chat models this brings

play06:55

us to chat prompt template as we've

play06:57

previously mentioned chat models take a

play06:59

list of chat messages as input the list

play07:01

is referred to as a prompt what's

play07:03

different about them is that each

play07:05

message is prepended by a role either AI

play07:07

human or system the model is supposed to

play07:09

follow instructions from the system

play07:11

message closely there's only one system

play07:13

message at the beginning and it can at

play07:15

times sound pretty hypnotic you are a

play07:17

kind customer service agent responds

play07:19

allegedly to the customer's question dot

play07:21

dot dot something like that to tell the

play07:23

chatbot how to behave AI messages are

play07:26

messages that come from the model human

play07:28

message is what we've typed in the roles

play07:30

give the LM better context on the

play07:32

ongoing conversation models and prompts

play07:34

are cool and standardized but how can we

play07:36

use our own data this is where the

play07:38

indexes module comes in handy data is

play07:40

the new oil and you can definitely dig

play07:42

anywhere and find tons of it Lange

play07:44

delivers the rigs by providing document

play07:46

loaders document is their fancy way of

play07:48

saying text there are a ton of supported

play07:50

formats and services like CSV email SQL

play07:53

Discord AWS S3 PDF the list goes on and

play07:57

on it can take only three lines of code

play07:58

to import your here's how easy it is

play08:01

first import the loader then specify the

play08:03

file path and then call the load method

play08:05

this will load the PDF as text in memory

play08:07

as an array where each index represents

play08:10

a page that's nice but when we want to

play08:12

construct a prompt and include text from

play08:13

these Pages they might be too big to fit

play08:15

within the input token size we've talked

play08:17

about earlier which is why we want to

play08:19

use text Splitters to chop them into

play08:20

chunks after reading the text we can

play08:23

instantiate a recursive character text

play08:24

splitter and specify a chunk size and a

play08:26

chunk overlap we call the create

play08:28

documents method and place our text as

play08:30

argument we then get an array of

play08:32

documents now that we have text chunks

play08:34

we'll want to embed and store them to

play08:36

eventually retrieve them using semantic

play08:37

search which is why we have Vector

play08:39

stores this part of the indexes module

play08:41

offers multiple Integrations with Vector

play08:43

databases like pine cone redis Super

play08:45

Bass chroma DB and much more once you

play08:48

have your documents ready you'll want to

play08:50

choose your embedding provider and store

play08:51

the documents using a vector database

play08:53

helper method now we can write a

play08:55

question and do a similarity search over

play08:57

the vector space moving on to the

play08:58

retriever part of the module the

play09:00

retriever Lane chain has the most

play09:01

support for is the vector store

play09:03

retriever which you can get by calling

play09:05

as retriever on a vector store note that

play09:07

the retriever interface is generic and

play09:09

doesn't have to represent a vector store

play09:11

it could also be querying services like

play09:12

Wikipedia a chat GPT Plugin or a couple

play09:15

other Integrations it only has to

play09:17

implement the get relevant documents

play09:19

which takes in a string and return

play09:20

documents as an illustrative example one

play09:23

thing you could do is self querying with

play09:25

chroma say you have films stored in

play09:27

chroma DB the movies have as metadata

play09:29

score and genre then you could run the

play09:31

following line of code and get the

play09:33

movies that you wanted now that we have

play09:35

models prompts and indexes we can use

play09:37

chains to abstract a model call to an

play09:39

llm we can instantiate our model writer

play09:41

prompt and then pass our model and

play09:43

prompt to the llm change we can then run

play09:45

the chain on the input variables of our

play09:46

prompt the chain takes care of

play09:48

formatting The Prompt sending it to the

play09:50

model and returning the results this is

play09:52

a stateless operation meaning the chain

play09:54

does not remember previous interactions

play09:56

we could use memory to make it stateful

play09:58

let's instantiate a chat model and now

play10:00

use a conversation chain we'll pass the

play10:02

chat model along with a memory object

play10:03

now we'll ask it to respond partially to

play10:05

a question and then to complete its

play10:07

answer you could also create your own

play10:09

custom chains or use high level chains

play10:11

to do things like chaining chains

play10:12

together solve math problems or even

play10:14

things like letting the llm write code

play10:16

and then execute it let's say we want to

play10:18

create a company name and write a good

play10:20

catchphrase for it we can chain both LM

play10:22

calls and a chain after instantiating

play10:24

our model we create our first prompt and

play10:26

our first chain then we write our second

play10:28

prompt asking for the catchphrase and

play10:30

create our second chain now we can chain

play10:32

them using the simple sequential chain

play10:33

and specifying the chains in order we

play10:35

run the chain on the input variables of

play10:37

the first prompt and its output will be

play10:39

used as input variable for the second

play10:40

prompt there are also a couple of

play10:42

pre-made chains that you could use like

play10:44

the LM math chain moving on to agents as

play10:46

this video is already getting pretty

play10:48

long we will cover agents more in depth

play10:50

with real life examples Concepts and

play10:52

working code in a following video this

play10:54

is a hot space and has tons of practical

play10:56

applications if you want to be notified

play10:57

hit the Subscribe button and as always

play10:59

thank you for watching

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
AI FrameworkLangChainLanguage ModelsData IntegrationPrompt TemplatesVector DatabasesSemantic SearchChatbotsEmbedding ModelsFew-shot Learning