RAG Technique with the LangChain Library: A Tutorial to Apply Now
In this tutorial, we'll explore how to use the RAG (Retrieval-Augmented Generation) technique with the LangChain library to integrate proprietary data…

In this tutorial, we'll explore how to use the RAG (Retrieval-Augmented Generation) technique with the LangChain library to integrate proprietary data into large language models (LLMs). We'll do this step by step, from initial setup to practical implementation.
Integrating Proprietary Data into LLMs with LangChain
In this tutorial, you'll learn how to use the RAG (Retrieval-Augmented Generation) technique with the LangChain library to integrate proprietary data into large language models (LLMs). This technique lets you use external data, such as web pages or documents, to improve the responses of language models.
Initial Setup
Before we start, make sure you have Python installed on your system. Then, install the LangChain library and its dependencies using the following command:
pip install langchain openai faiss-cpu beautifulsoup4Step 1: Creating the Main Tutorial
Let's start by creating the main file where the tutorial will be run. This file will show the practical application of integrating LangChain with an LLM
from langchain import OpenAI, PromptTemplate, LLMChain
# Initialize the OpenAI API with your API key
openai = OpenAI(api_key="SUA_CHAVE_API")
# Define the prompt template
prompt_template = PromptTemplate(
template="Quantos Oscars o filme {filme} ganhou em {ano}?",
input_variables=["filme", "ano"]
)
# Create the LLM chain
llm_chain = LLMChain(llm=openai, prompt=prompt_template)
# Run the chain with sample parameters
resposta = llm_chain.run(filme="Openheimer", ano="2024")
print(resposta)Step 2: Creating the File for Web Scraping and Vector Creation
Now, let's create a file responsible for scraping the Wikipedia page and turning the content into embeddings for storage in the vector database.
from langchain.document_loaders import WebBaseLoader
from langchain.vectorstores import FAISS
from langchain.embeddings import OpenAIEmbeddings
# Load documents from the Wikipedia page
loader = WebBaseLoader("https://pt.wikipedia.org/wiki/Filme_Openheimer")
documents = loader.load()
# Create embeddings for the documents
embeddings = OpenAIEmbeddings()
vectorstore = FAISS.from_documents(documents, embeddings)
# Create the retriever
retriever = vectorstore.as_retriever()Step 3: Creating a Utility Functions File
Let's encapsulate the scraping, embedding creation, and data retrieval operations into a utility function.
from langchain.document_loaders import WebBaseLoader
from langchain.vectorstores import FAISS
from langchain.embeddings import OpenAIEmbeddings
def url_to_retriever(url):
loader = WebBaseLoader(url)
documents = loader.load()
embeddings = OpenAIEmbeddings()
vectorstore = FAISS.from_documents(documents, embeddings)
return vectorstore.as_retriever()Step 4: Creating the Main File for RAG Integration
Let's integrate all the components we've created to implement the RAG technique with an LLM.
from langchain import OpenAI, ChatPromptTemplate, create_retrieval_chain
from your_module import url_to_retriever
# Initialize the OpenAI API with your API key
openai = OpenAI(api_key="SUA_CHAVE_API")
# Create the retriever from the Wikipedia URL
retriever = url_to_retriever("https://pt.wikipedia.org/wiki/Filme_Openheimer")
# Define the prompt template
prompt_template = ChatPromptTemplate(
template="Quantos Oscars o filme {filme} ganhou em {ano}?",
input_variables=["filme", "ano"]
)
# Create the retrieval chain
chain = create_retrieval_chain(
llm=openai,
retriever=retriever,
prompt_template=prompt_template
)
# Run the chain with sample parameters
resposta = chain.run(filme="Openheimer", ano="2024")
print(resposta)Conclusion
In this tutorial, you learned how to use the RAG technique with the LangChain library to integrate proprietary data into large language models (LLMs). This process involves creating embeddings from external data, storing those embeddings in a vector database, and retrieving that information to improve the responses of language models.
With this knowledge, you can apply this technique to various types of data and build smarter, more informed applications. If you have questions or suggestions, feel free to comment below. See you in the next tutorial!
Note: Replace "SUA_CHAVE_API" with your OpenAI API key.
This tutorial should help you understand and implement the RAG technique with LangChain in your own applications. Good luck!
Translated from the Brazilian Portuguese original · Read the original