Unlock the full power of LangChain and learn how to build real AI applications using open-source LLMs like Llama 2, Mistral 7B, and Hugging Face models. This video is your complete beginner-friendly guide to understanding LangChain fundamentals, building your first chatbot, adding memory, using RAG (Retrieval-Augmented Generation), and working with agents and tools—without using the OpenAI API.
In just over an hour, you will learn how modern AI apps are built using the same frameworks used by professionals. Whether you’re a Python beginner or an AI enthusiast, this tutorial will give you the foundation needed to build scalable, production-ready LLM applications.
🔥 What You’ll Learn (Highlights)
What LangChain is and how it works
How to generate text using open-source LLMs
LangChain Expression Language (LCEL)
Prompting techniques & one-shot prompting
How to build a chatbot with memory
How RAG works and why it’s essential
Creating embeddings & storing them in ChromaDB
Loading Mistral / Gemma / Llama models
Building agents: XML Agents & ReAct Agents
Running your own LLM server
Practical coding walkthroughs & real demos
This course is perfect for developers, AI builders, data science students, and anyone who wants to work with open-source LLMs instead of paid APIs.
Hello and welcome to this course on lang
chain. In this course we will discuss
about LLMs prompts chains memory
retriever augmented
generation argens tools and much more.
I'm going to use mistro and lamatu
models in this course. And the libraries
that I'm going to use are Llama CPP for
loading GGUF models and hugging face
transformers library for loading
original models from hugging face. To
follow along in this course, you can
either use Visual Studio Code or Collab
or Kaggle, whichever you prefer.
Langshine, launched in October
2022 as an open-source project by
Harrison Chase is a framework tailored
for developing applications leveraging
large language models. It provides
Python and JavaScript libraries, serving
as a central hub for large language
models development and integration with
external data sources and workflows.
Launched in October 2022, it rapidly
became the fastest growing open-source
project on GitHub by June
2023, playing a vital role in
popularizing generative AI, especially
alongside the release of Open AI's chat
GPT. Langshine supports various LLM use
cases from chatbots to intelligent
search and summarization services. To
generate text using
LLM, you need an
LM. In this course, I will be
downloading opensource GGF models from
Hugging Face. And to load the model, I
will be using Llama CPP. The reason why
I use GGF models is that you can run the
models on a
CPU. Once we load the model, we pass the
text as a parameter to the LLM. To
stream text as it's being generated, we
use a callback manager. Within this, we
pass the streaming standard output as an
option. All these functions are readily
available for use in the lang chain
library. Welcome back. Now create an
empty folder and then open vcode from
that
folder and then open terminal by
pressing control back
tick. Now type pip install
pipnv. After that type pipion
vshell. This will create a new
environment for our
folder. Now we need to install two
libraries.
Lang
Shane and Llama CP pay
Oh, the model that I am using in this
lecture is the Mistral AI7D model.
I have taken this model from hugging
face. Here you have various types of
quantized models. Quantised models allow
you to run the model on a CPU. You can
see that I have downloaded the 8-bit
GGUF quantized model. Now you need to
create one new file named text gen.py.
After that you need to import llama CPP
from langchain.lms
LMS. Then you need to import callback
manager. And then you need to import
streaming standard out call back handler
to stream the text in terminal.
After that, copy and paste the piece of
code I provided in the text file after
this lecture. Make sure you change the
model path to the location of your own
model in your system.
Finally, type lm bracket and ask your
question.
We have successfully generated text
using an
LLM. See you in the next lecture. A
prompt for a language model is a set of
instructions or input provided by a user
to guide the model's response.
So instead of
directly passing text as input, we
enclose the text inside the prompt. In
langshane, we utilize prompt templates
to generate prompts for inputting text
into the model. But why do we need to
use a prompt? We use prompts because
prompt templates allow for dynamic
generation of prompts by incorporating
variables or placeholders. This enables
the customization of prompts based on
specific requirements or input data. Let
me show you an example. First, we import
prompt template from
langchain.prompts and then we write
question equals to prompt template dot
from
template and then inside template. Let's
say we want to cook a recipe. We can
write tell me how to cook parenthesis
and then
recipe. That's how you create a prompt.
Now, in order to use this prompt, we
need to use a chain. We will look into
chains in the next lecture. A chain in
lang chain is used to chain LLM with
other functions like prompts, output
parser or another LMM.
First we pass the input to the prompt in
dictionary format with recipe
placeholder value. The prompt template
will then replace the placeholder with
recipe
name. After that the updated text will
be passed through the pipe operator. The
pipe operator will convert the output of
prompt template into input to the next
function.
Then the model will generate the text
based on the input. Now inside V code
create a variable named chain. Here we
are storing the prompt inside question
variable. So we type question and then
pipe operator and then llm.
Now instead of passing text into
llm we type
chain.invoke recipe
name. Now let's run it and we can see
that it's generating
text. In the previous lecture we have
used a method called invoke. Invoker is
a method that comes under lang chain
expression language. Invoker is used to
pass a single input to the chain and
this is the syntax for using invoke
method. To pass multiple inputs we use
batch and this is the syntax. And to
stream the output as it gets generated
we use stream. This is the syntax to use
stream. LCAL objects implement runnable
interface which has methods such as
invoke, batch, stream etc. When you use
pipe operator for chaining prompts and
llm, it returns a lceel object which
implements runnable. So you can use
methods like invoke, batch and stream on
your chains. Now let's look at an
example of how to use stream. We no
longer need to use callback manager. So
let's delete
it. After that, let's paste the stream
code. And that's
it. Now let's run
it. And we can see that it's generating
text.
Until now we have passed text directly
to the LLM and it responded with an
output. This is called a zeroshot
prompting. With zeroshot prompting llm
tries to answer without any specific
information or structure. In one shot
prompting we give an example of how to
answer to our model and our model will
know how to structure its output.
So now let's try one short prompting and
see how our model
responds. Here you can see that output
is more structured
now. But our model doesn't stop and
keeps producing other questions and
answers. To stop this, we use stop
sequences. Stop sequences help us to
stop our model whenever a specific text
or character
occurs. Now you can see that we get only
the output we wanted.
Welcome back. In this section, we are
going to build a chatbot with memory.
But before starting that, I want to
clarify some things. There are two types
of models in lench chain. One is LLM
which takes in string as input and
output string as output. Another type is
chat models which take in human messages
as input and output and AI message.
There are three types of messages in
chat models. System message, human
message and AI message. System message
is set by the developer to tell the
model how it should answer the
question. Human message is the question
asked by the user.
An AI message is the answer given by the
LLM. Chat models are fine-tuned for chat
purposes. So they are a good fit for
chat assistants and chat bots. If we
want to build a chatbot with memory,
then we need to use a chat
model. In this course, I will be using
Lamatu 7B chat quantized model.
Now open up this
code and create a new file named chatbot
dot pi. Copy everything from text gen
file and paste it here.
Now change prompt template to chat
prompt
template. After that change model path
to new lamatu chat model
path and then remove this prompt
template and type chat prompt template
dot from
messages human message. This will be the
question entered by human. The stem
message. This is the message that is set
by the developer. For example, answer in
five sentences
only. And here we want to stop the
model when we encounter human string. To
get the input continuously from user, we
use a while loop and we use the input
method in Python to receive input from
terminal. After that we move the for
loop inside while loop so that it can
run until user interrupts the program
and also we need to change the input to
the
question entered by the user. Now let's
run
it. We can see that it generates
text and allows us to enter the
follow-up question. But when we ask how
many eggs model doesn't actually answers
what we
needed, it's not like the model itself
will get a memory. Instead, we will have
a variable that will hold the previous
messages. We will see how to do that in
next lecture. In order to create memory
for our chat, we need to import
conversation buffer memory from
langchain.mmemory. Azo let's change this
question variable to prompt so it better
resembles the
task and then we create an instance of
conversation buffer memory with
parameter memory key equals to chat
history and we set return messages to
true. This code will create a variable
named chat history inside the memory and
stores all the messages inside it.
Return messages make sure that it is
stored in message format like
this. We need to pass these messages
whenever the human enters a new
question. So we import and put a message
placeholder inside our prompt. After our
system message which holds all our
messages. After that from
langchain.shima.runnable
Runnable. We import runnable lambda and
runnable pass
through. And then we write runnable pass
through dot assign chat history equals
to runnable
lambda. And we pass
memory.load memory variables method to
runnable
lambda. And we use pipe operator to pass
the output of runnable lambda to item
getter. Let me explain this code to
you. Runnable lambda is used to run
custom functions and the function we are
running is memory doc load memory
variables. This returns a dictionary
object containing chat history which we
will pass through pipe operator to the
item getter. Item getter is used to
retrieve a value from
dictionary. Here we are retrieving the
value of chat
history. Now we have chat history equals
to some text. Runnable pastro dot assign
is used to convert the data inside it
into dictionary format and concatenate
it with the incoming
input. So finally we will have this
dictionary which we will pass to the
prompt.
Now after the for loop we want to save
the input and output into the memory.
For that we use memory.save save context
function. We pass the question as
input and for the output we create a new
variable named text and set it to empty
string and we take every chunk of text
that gets
generated and concatenate it with our
text variable and pass that as output
for save context
function. Finally, let's run our
program.
You can see that this time it is able to
answer correctly.
Retrieval. Augmented generation is an AI
framework that enhances the performance
of large language models. It does this
by retrieving and incorporating external
data not included in the model's
training set into the generation
process.
This approach ensures that the model's
responses are grounded in the most
current and reliable facts, thereby
improving the quality and accuracy of
its output. First, we load the
document. Then, we split the document
and then we generate
embedding. After that, we store the
embeddings in a vector store and then we
use a similarity search algorithm to
find most relevant documents and
retrieve them. Langshine has several
document loaders like text loader, GSV
loader, PDF loader and much more. For
example, to load a text file, import the
text loader from the lankchain
community. Document loaders module.
Create an instance of text loader with
the path to the text file as a parameter
and then use the load function to load
the file. Langshine has several text
splitters like code splitters, character
splitters. For example, to split a text
file whenever you encounter a new line,
you use the character text splitter
class that splits text into
chunks. Separator is the character used
to split the text. Chunk size is the
maximum size of each chunk. Chunk
overlap is the number of characters that
overlap between chunks. The embedding
algorithm that we are going to use is
all mini
LML6 V2 and it's an open-source
algorithm available on hugging face.
This algorithm maps the text to a
384dimensional vector space. To use this
algorithm, you need to first import
hugging face embeddings from lank chain
community. And then we can pass model
name as a parameter to the instance of
hugging face
embeddings. It is used to compare
similarity of two sentences. For
example, when you compare this sentence
with these three sentences, this is the
result that shows which sentence is most
similar. The database that we are going
to use to store embeddings is Chroma DB.
Chroma DB is an open-source embedding
database. To use Chroma DB in langchain,
we import Chroma from langchain
community. Vector stores and then we can
use chroma. Documents function to load
the splitted text. And we pass our
embedding function and path to storage
directory to store
embeddings. To load data from an
existing Chroma DB, we call Chroma
instance directly with storage path and
embedding function. Now let's create a
new file named DB
init.py. We begin by importing the
necessary library for loading our
document. We'll use the text loader from
langchain.d document loaders. Then we
create an instance of the text loader
class and pass
sample.txt as the file
sample.txt file is attached as a
resource. Make sure you download it and
keep it in current
folder. Next, we need to split the text
into manageable chunks. We import the
character texts splitter from
langchain texts
spplitter. Then we create an instance of
character text splitter with a new line
as the separator. A chunk size of 400
and a chunk overlap of 100. We then use
the text splitter to split the loaded
documents by passing it as an argument
to load and split function of loader
object. Now we move on to
embeddings. We import the hugging face
embeddings from chain.bings.
Then we create an instance of hugging
face embeddings with model name as all
minimum
L6V2. Then we import chroma from
langchain.veector stores dot chroma.
Then we create an instance of chroma
from the documents and embeddings and
persist the data in a directory named
data.
Now we need to install two libraries
sentence transformers and chroma
DB. After that let's run our code to
initialize the database.
And now if we check our data folder, we
can see that it contains some
data. And that concludes our
code. We've successfully loaded our
document, split the text, created
embeddings, and stored everything in a
database. Now, let's create a new file
named rag.py. We start by importing the
necessary libraries for our language
model. We will use the llama CPP from
langchain.lm. Then we copy paste the llm
code from chatbot.py
file. Next we import the hugging face
embeddings from langchain.
Embeddings and create an instance of
hugging face embeddings with all minimum
L6V2 as the model name. We then move on
to the vector
store. We import the chroma from
langchain.vector
stores
chroma and create an instance of chroma
with the specified persist directory and
embedding
function. When we set persist directory
to data, it will make sure that we are
using the same database that we created
in our previous lectures. We then create
a retriever from the vector store with k
equals 1 as the search
parameter. As retriever function
retrieves the similar text from our
database. When we set parameter k equals
1, then it will retrieve only one result
that is most similar to our question.
Next, we create a template and we tell
the model to answer based only on the
given context.
Now we move on to
prompts. We import the chat prompt
template from
langchain.prompts and create an instance
of chat prompt template from the
specified template and then we move on
to
chains. We start with a dictionary that
contains two elements context and
question. Their context is set to our
previously defined retriever which will
retrieve relevant information based on
the
input. We import runnable pass through
from
langchains schemaunnables.
The question is set to an instance of
runnable pass through which is a class
from langchain.s schema dot runnable
that simply passes the input through
without modifying it. Next, we use the
pipe operator to connect our dictionary
to the prompt. This means that the
output of the dictionary which is our
context and question will be passed as
input to the prompt.
And then we connect the prompt to our
language model using another pipe
operator. This means that the output of
the prompt will be passed as input to
the
llm. We then stream the chain with what
is a chat
model. as the input and print each
chunk. Note that the question what is a
chat model will be passed to retriever
to retrieve the top matching result and
the result is stored in
context. Now let's run it.
We can see that now it generates text
based on the text from our text file.
See you in the next lecture. Go to
kaggle.com and create an account and
verify it with your phone number. Then
click on create and create a new
notebook.
First
we install the necessary libraries using
pip. This includes transformers,
langchain, inops,
autogq, optimum, accelerate and
bitsandes. Make sure you select the GPU
T4X2 as the accelerator.
We then import automodel for causal LM
and autotokenizer from the transformers
library. These classes will be used to
load our pre-trained language model and
its corresponding
tokenizer. And then we add our model
inside Kaggle.
After that we copy the path to the model
directory and store in model path
variable. We create an instance of
autotokenizer and load our tokenizer
from the specified model path folder. We
then create an instance of automodel for
causal LM to load our model from the
specified
folder. We set trust remote code to
true. Specify offflow as the offload
folder. Set device map to auto and set
torch data type to torch dot float 16.
We also import text streamer from the
transformers library. This class will be
used to stream text to our language
model. We create an instance of text
streamer using the
tokenizer. This is used to stream the
text to the
output. Next, we import pipeline from
the
transformers library. This function will
be used to create a pipeline for text
generation.
Next, we create a pipeline for text
generation using the model
tokenizer, a maximum of 150 new tokens,
and the
streamer. We then import hugging face
pipeline from the
langchain.lmshugging face pipeline
module. This class will be used to
create an instance of our language model
pipeline. Finally, we create an instance
of hugging face pipeline using the
pipeline.
When we run this code, it will load the
checkpoint shards which takes about 2
minutes.
After that, we can ask a question to the
LM and use a for loop to stream the
output.