LangChain is an open-source framework that simplifies building applications using large language models. It helps developers connect LLMs with external data, tools and workflows and is available in both Python and JavaScript.
Simplifies chaining LLMs together for reusable and efficient workflows.
Offers tools for effective prompt engineering and memory handling.
Streamlines the process of building LLM-powered applications.
Chains: Define a sequence of steps where each step can use an LLM, process data or call tools. Simple chains use one step, while multi step chains combine multiple actions.
Prompt Management: Helps design and manage prompts using templates, making it easier to control input, output format and model behavior.
Agents: Agents are LLM driven components that decide which actions to take, such as calling tools or APIs, based on input and predefined capabilities.
Vector Database: Stores data as vectors to enable similarity search, helping retrieve relevant information for tasks like document search and RAG.
Models: Supports multiple LLMs like OpenAI, Hugging Face and others, allowing flexibility in choosing the best model.
Memory Management: Maintains context from past interactions, enabling better responses in conversations and multi step tasks.
Working of LangChain
LangChain enables Retrieval-Augmented Generation (RAG) by combining document processing, vector storage and LLMs to generate accurate, context aware responses. It connects embeddings, vector databases and models into a smooth workflow.
1. Document Processing: Documents (e.g., PDFs) are split into smaller chunks so they can be processed efficiently.
2. Embeddings Creation: Each chunk is converted into embeddings that capture its semantic meaning.
3. Vector Store: These embeddings are stored in a vector database, creating a searchable knowledge base.
4. User Query: The process starts when a user submits a question or request as input.
For example, a user might ask, โWhatโs the weather like today?โ This query serves as the input to the LangChain pipeline.
5. Vector Representation: Once the query is received, LangChain converts it into a vector representation using embeddings. This vector captures the semantic meaning of the query.
6. Similarity Search: This vector is compared with vectors stored in a database to find the most relevant matches based on meaning.
7. Fetching Relevant Information: The system retrieves the most relevant data or context from the vector database to support the response.
8. Generating a Response: The retrieved context is passed to a language model, which processes it and generates a meaningful answer.
For example, if the query is about the weather, the LLM might generate a response like, โTodayโs weather is sunny with a high of 75ยฐF.โ
Implementation
Let's implement a model using LangChain
Step 1: Install the dependencies
We will install all the required dependencies for our model.
langchain: The core LangChain framework (chains, prompts, tools, memory, etc.).
langchain-google-genai: LangChain integration for accessing Google Gemini models through the Gemini API.
Step 2: Import Libraries
We will import all the required libraries.
ChatGoogleGenerativeAI: Enables interaction with Google Gemini models through LangChain.
PromptTemplate: define structured prompts with placeholders.
StrOutputParser: ensures model response is returned as clean string text.
Step 3: Configure API Key
We need to provide the Gemini API key to authenticate and access Google's Gemini models. In this example, the API key is added directly in the code and passed while initializing the model.
Step 4: Initialize the Gemini Model
model: Specifies which Gemini model to use.
temperature=0.7: temperature Controls creativity (0 = more deterministic, 1 = more creative).
google_api_key=api_key: Authenticates access to Gemini.
Step 5: Run a Simple Prompt
We will check by running a simple prompt.
.invoke(): sends prompt to LLM and returns text output.