Integrating
document loaders
D E V E L O P I N G L L M A P P L I C AT I O N S W I T H L A N G C H A I N
Jonathan Bennion
AI Engineer & LangChain Contributor
Retrieval Augmented Generation (RAG)
Use embeddings to retrieve relevant information to integrate into the prompt
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
RAG development steps
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
LangChain document loaders
Classes designed to load and configure
documents for system integration
Document loaders for common file types:
.pdf , .csv
3rd party loaders: S3, .ipynb , .wav
1 [Link]
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
PDF document loader
Requires installation of the pypdf package: pip install pypdf
from langchain_community.document_loaders import PyPDFLoader
loader = PyPDFLoader("path/to/file/attention_is_all_you_need.pdf")
data = [Link]()
print(data[0])
Document(page_content='Provided proper attribution is provided, Google hereby grants
permission to\nreproduce the tables and figures in this paper solely for use in [...]
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
CSV document loader
from langchain_community.document_loaders.csv_loader import CSVLoader
loader = CSVLoader('fifa_countries_audience.csv')
data = [Link]()
print(data[0])
Document(page_content='country: United States\nconfederation: CONCACAF\npopulation_share: [...]
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
HTML document loader
Requires installation of the unstructured package: pip install unstructured
from langchain_community.document_loaders import UnstructuredHTMLLoader
loader = UnstructuredHTMLLoader("white_house_executive_order_nov_2023.html")
data = [Link]()
print(data[0])
print(data[0].metadata)
page_content="To search this site, enter a search term\n\nSearch\n\nExecutive Order on the Safe, Secure,
and Trustworthy Development and Use of Artificial Intelligence\n\nHome\n\nBriefing Room\n\nPresidential
Actions\n\nBy the authority vested in me as President by the Constitution and the laws of the United
States of America, it is hereby ordered as follows: ..."
{'source': 'white_house_executive_order_nov_2023.html'}
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
Let's practice!
D E V E L O P I N G L L M A P P L I C AT I O N S W I T H L A N G C H A I N
Splitting external
data for retrieval
D E V E L O P I N G L L M A P P L I C AT I O N S W I T H L A N G C H A I N
Jonathan Bennion
AI Engineer & LangChain Contributor
RAG development steps
Document splitting: split document into chunks
Break documents up to fit within an LLM's context window
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
Thinking about splitting...
Line 1:
Recurrent neural networks, long short-term memory [13] and gated recurrent [7] neural networks
Line 2:
in particular, have been firmly established as state of the art approaches in sequence modeling and
1 [Link]
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
Chunk overlap
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
What is the best document splitting strategy?
1. CharacterTextSplitter
2. RecursiveCharacterTextSplitter
3. Many others
1 Wikipedia Commons
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
quote = '''One machine can do the work of fifty ordinary humans.\nNo machine can do
the work of one extraordinary human.'''
len(quote) chunk_size = 24
chunk_overlap = 3
103
1 Elbert Hubbard
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
from langchain_text_splitters import CharacterTextSplitter
ct_splitter = CharacterTextSplitter(
separator='.',
chunk_size=chunk_size,
chunk_overlap=chunk_overlap)
docs = ct_splitter.split_text(quote)
print(docs)
print([len(doc) for doc in docs])
['One machine can do the work of fifty ordinary humans',
'No machine can do the work of one extraordinary human']
[52, 53]
Split on separator so < chunk_size , but may not always succeed!
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
from langchain_text_splitters import RecursiveCharacterTextSplitter
rc_splitter = RecursiveCharacterTextSplitter(
separators=["\n\n", "\n", " ", ""],
chunk_size=chunk_size,
chunk_overlap=chunk_overlap)
docs = rc_splitter.split_text(quote)
print(docs)
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
RecursiveCharacterTextSplitter
separators=["\n\n", "\n", " ", ""]
['One machine can do the',
'work of fifty ordinary',
'humans.',
'No machine can do the',
'work of one',
'extraordinary human.']
1. Try splitting by paragraph: "\n\n"
2. Try splitting by sentence: "\n"
3. Try splitting by words: " "
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
RecursiveCharacterTextSplitter with HTML
from langchain_community.document_loaders import UnstructuredHTMLLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
loader = UnstructuredHTMLLoader("white_house_executive_order_nov_2023.html")
data = [Link]()
rc_splitter = RecursiveCharacterTextSplitter(
chunk_size=chunk_size,
chunk_overlap=chunk_overlap,
separators=['.'])
docs = rc_splitter.split_documents(data)
print(docs[0])
Document(page_content="To search this site, enter a search term [...]
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
Let's practice!
D E V E L O P I N G L L M A P P L I C AT I O N S W I T H L A N G C H A I N
RAG storage and
retrieval using
vector databases
D E V E L O P I N G L L M A P P L I C AT I O N S W I T H L A N G C H A I N
Jonathan Bennion
AI Engineer & LangChain Contributor
RAG development steps
Focus of this video: storage and retrieval
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
What is a vector database and why do I need it?
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
Which vector database should I use?
Need to consider:
Open source vs. closed source (license)
Cloud vs. on-premises
Lightweight vs. powerful
1 Image Credit: Yingjun Wu
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
Meet the documents...
docs
[
Document(
page_content="In all marketing copy, TechStack should always be written with the T and S
capitalized. Incorrect: techstack, Techstack, etc.",
metadata={"guideline": "brand-capitalization"}
),
Document(
page_content="Our users should be referred to as techies in both internal and external
communications.",
metadata={"guideline": "referring-to-users"}
)
]
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
Setting up a Chroma vector database
from langchain_openai import OpenAIEmbeddings
from langchain_chroma import Chroma
embedding_function = OpenAIEmbeddings(api_key=openai_api_key, model='text-embedding-3-small')
vectorstore = Chroma.from_documents(
docs,
embedding=embedding_function,
persist_directory="path/to/directory"
)
retriever = vectorstore.as_retriever(
search_type="similarity",
search_kwargs={"k": 2}
)
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
Building a prompt template
from langchain_core.prompts import ChatPromptTemplate
message = """
Review and fix the following TechStack marketing copy with the following guidelines in consideration:
Guidelines:
{guidelines}
Copy:
{copy}
Fixed Copy:
"""
prompt_template = ChatPromptTemplate.from_messages([("human", message)])
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
Chaining it all together!
from langchain_core.runnables import RunnablePassthrough
rag_chain = ({"guidelines": retriever, "copy": RunnablePassthrough()}
| prompt_template
| llm)
response = rag_chain.invoke("Here at techstack, our users are the best in the world!")
print([Link])
Here at TechStack, our techies are the best in the world!
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
Let's practice!
D E V E L O P I N G L L M A P P L I C AT I O N S W I T H L A N G C H A I N
Wrap-up!
D E V E L O P I N G L L M A P P L I C AT I O N S W I T H L A N G C H A I N
Jonathan Bennion
AI Engineer & LangChain Contributor
LangChain's core components
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
Chains and agents
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
Retrieval Augmented Generation (RAG)
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
LangChain Hub
Access the LangChain Hub at: [Link]
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
The LangChain ecosystem
LangSmith: troubleshooting and evaluating applications
LangServe: deploying applications
LangGraph: multi-agent knowledge graphs
DEVELOPING LLM APPLICATIONS WITH LANGCHAIN
Let's practice!
D E V E L O P I N G L L M A P P L I C AT I O N S W I T H L A N G C H A I N