LangChain in Action: How to Build Intelligent AI Applications Easily and Efficiently ?
Mastering LangChain Retrievals: A Guide to Intelligent Information Access
In the realm of AI-powered applications, retrievals enable seamless access to relevant information from vast collections of unstructured data, such as documents, PDFs, or databases. Instead of treating every query as an isolated event, retrieval mechanisms in LangChain intelligently extract and utilize contextually relevant data to generate more accurate and insightful responses. By leveraging Retrieval Chains, LangChain integrates language models with retrieval systems, creating a powerful synergy for handling complex document interactions.
Use Cases for Retrievals
- Document Search and Summarization: Efficiently find and summarize key information from large document repositories, such as research papers or legal documents.
- Knowledge-Driven Chatbots: Build chatbots capable of answering specific user queries based on company documentation, product manuals, or FAQs.
- Healthcare Record Analysis: Extract insights from patient records to assist medical professionals in diagnosing conditions or reviewing histories.
- E-Learning Platforms: Create personalized study tools by retrieving content from educational materials based on user input.
- Customer Support Automation: Automate support workflows by retrieving relevant troubleshooting steps or policies from internal knowledge bases.
LangChain Retrieval Chains streamline these use cases by allowing applications to retrieve the most relevant information and use it as input for generating meaningful and context-aware responses. This approach not only enhances the efficiency and accuracy of retrieval but also enables the creation of more intelligent and adaptive AI systems.

Document Querying: Using a Vector Store for Retrieval-Augmented Generation (RAG) with LangChain
This code demonstrates how to use LangChain with OpenAI’s ChatOpenAI model and OpenAIEmbeddings to implement Retrieval-Augmented Generation (RAG). Instead of relying solely on the AI model's pretrained knowledge, this approach enables dynamic retrieval of relevant information from a custom text dataset stored in a vector store. A vector store allows for efficient similarity search, meaning when a user asks a question, the system retrieves the most relevant text passages and uses them to generate a more accurate response. This method is particularly useful in chatbots, document search applications, and AI assistants that need context-aware responses.
╔════════════════════════════════════════════════════════════════════════════╗
║ 🌟 EDUCATIONAL EXAMPLE 🌟 ║
║ ║
║ 📌 This is a minimal and short working example for educational purposes. ║
║ ⚠️ Not optimized for production! ║
║ ║
║ 📦 Versions Used: ║
║ - "@langchain/core": "^0.3.38" ║
║ - "@langchain/openai": "^0.4.2" ║
║ ║
║ 🔄 Note: LangChain is transitioning from a monolithic structure to a ║
║ modular package structure. Ensure compatibility with future updates. ║
╚════════════════════════════════════════════════════════════════════════════╝
import { ChatOpenAI } from "@langchain/openai"; // import the ChatOpenAI class
import { OpenAIEmbeddings } from "@langchain/openai"; // import the OpenAIEmbeddings class
import { MemoryVectorStore } from "langchain/vectorstores/memory"; // import the MemoryVectorStore class
import dotenv from "dotenv"; // import the dotenv package
import fs from "fs/promises"; // import the fs/promises package
dotenv.config(); // load environment variables from .env file
// Initialize the ChatOpenAI model with the required parameters
const model = new ChatOpenAI({
modelName: "gpt-3.5-turbo",
temperature: 0.7,
openAIApiKey: process.env.OPENAI_API_KEY!,
});
const embeddings = new OpenAIEmbeddings({ openAIApiKey: process.env.OPENAI_API_KEY! }); // Initialize the OpenAIEmbeddings class with the required parameters
async function run() {
const filePath = "src/sample.txt"; // specify the path to the file containing the text to be processed
const fileContent = await fs.readFile(filePath, "utf8"); // read the file content
const splitDocuments = fileContent.split("
").filter(Boolean); // split the file content into an array of strings, filtering out any empty strings
// Initialize the MemoryVectorStore class with the required parameters
const vectorStore = await MemoryVectorStore.fromTexts(
splitDocuments,
splitDocuments.map((_, idx) => ({ id: idx })),
embeddings
);
const retriever = vectorStore.asRetriever({ searchType: "similarity", k: 2 }); // Initialize the retriever with the required parameters
const userQuery = "What is LangChain?"; // specify the user query
const retrievedDocs = await retriever.invoke(userQuery); // retrieve the documents that match the user query
const context = retrievedDocs.map((doc) => doc.pageContent).join("
"); // join the retrieved documents into a single string
const prompt = `Based on the following information, answer the question:
${context}
Question: ${userQuery}`; // construct the prompt
const response = await model.invoke(prompt); // invoke the model with the prompt
console.log("Retrieved Documents:", retrievedDocs); // log the retrieved documents
console.log("Response:", response.content); // log the response
}
run().catch(console.error); // call the run function and handle any errors
This implementation follows these key steps:
- Initialize the AI Model & Embeddings: The ChatOpenAI model is loaded, and OpenAIEmbeddings is used to create numerical representations (vectors) of the text data.
- Load & Process Text Data: A text file (sample.txt) is read and split into smaller document chunks.
- Retrieve Relevant Documents: When the user asks a question ("What is LangChain?"), the system searches for the most similar text snippets using semantic similarity.
- Generate a Response: The retrieved text is used as context in a prompt, ensuring that the AI provides an informed and relevant answer rather than relying purely on its training data.
This approach makes AI applications more dynamic, context-aware, and able to reference up-to-date information, making it ideal for knowledge bases, FAQ bots, customer support systems, and research tools.
API Retrievals: Using OpenAI to Extract Information from API Data
This script demonstrates how to fetch data from an external API, process it using OpenAI’s gpt-3.5-turbo model, and generate structured responses. This approach is useful for data analysis, automated summarization, and chatbot applications where structured information needs to be extracted and reformatted. Whether you're building an AI-powered assistant, a customer support bot, or a research tool, this method allows for intelligent data extraction and streamlined information delivery.
╔════════════════════════════════════════════════════════════════════════════╗
║ 🌟 EDUCATIONAL EXAMPLE 🌟 ║
║ ║
║ 📌 This is a minimal and short working example for educational purposes. ║
║ ⚠️ Not optimized for production! ║
║ ║
║ 📦 Versions Used: ║
║ - "@langchain/core": "^0.3.38" ║
║ - "@langchain/openai": "^0.4.2" ║
║ ║
║ 🔄 Note: LangChain is transitioning from a monolithic structure to a ║
║ modular package structure. Ensure compatibility with future updates. ║
╚════════════════════════════════════════════════════════════════════════════╝
import { ChatOpenAI } from "@langchain/openai"; // Import the ChatOpenAI class used for chat-based models
import { ChatPromptTemplate } from "@langchain/core/prompts"; // Import the ChatPromptTemplate class used for chat-based prompts
import dotenv from "dotenv"; // Import the dotenv package
import readline from "readline"; // Import the readline package used for user input
dotenv.config(); // Load environment variables from the .env file
const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); // Create a readline interface for user input
const model = new ChatOpenAI({ modelName: "gpt-3.5-turbo", temperature: 0.7, openAIApiKey: process.env.OPENAI_API_KEY }); // Create a new ChatOpenAI instance with the required parameters
// Define a function to fetch data from an API using fetch
const fetchFromAPI = (endpoint: string) => fetch(endpoint)
.then(res => (res.ok ? res.json() : Promise.reject(`HTTP error! Status: ${res.status}`)))
.catch(err => console.error("Error fetching data:", err)); // Define a function to fetch data from an API
async function run() {
// Prompt the user for input
rl.question("Enter a user ID to fetch posts for user ID: ", async (userId) => {
const apiData = await fetchFromAPI(`https://jsonplaceholder.typicode.com/posts?userId=${userId}`); // Fetch data from the API
if (!apiData?.length) return console.log("No data found."), rl.close(); // Check if the API data is empty
const prompt = await ChatPromptTemplate.fromMessages([
["system", "You are a creative assistant that extracts information from API data."], // Create system message
["human", "Show these posts: {apiData}"], // Create human message
]).formatMessages({ apiData: JSON.stringify(apiData, null, 2) }); // Create a ChatPromptTemplate with the API data
console.log(`[ASSISTANT]: ${(await model.generate([prompt])).generations[0][0].text}`); // Generate a summary using the ChatOpenAI model
rl.close(); // Close the readline interface
});
}
run().catch(console.error); // Call the run function and handle any errors
Code Explanation
- Fetches posts from an API using a given userId.
- Formats the API data into a structured prompt using ChatPromptTemplate.
- Uses OpenAI’s model to process and summarize the retrieved data.
- Outputs the AI-generated response in a user-friendly format.
The script is useful for building chatbots, AI assistants, or automated reporting tools that require real-time data processing and response generation.
Using MongoDB and OpenAI for Job Search with Vector Similarity
In the modern job market, retrieving relevant job listings efficiently is crucial. This script integrates MongoDB with OpenAI embeddings to create an intelligent job search system using vector similarity search. The script allows users to enter a job title or keyword, and retrieves the most relevant job listings based on their input.
The goal is to:
- Fetch job listings from a MongoDB database.
- Convert job descriptions into vector embeddings using OpenAI.
- Perform similarity-based searches to find the most relevant jobs based on user input.
This approach is ideal for job search engines, recommendation systems, and AI-driven filtering tools where semantic search is more effective than basic keyword matching.
╔════════════════════════════════════════════════════════════════════════════╗
║ 🌟 EDUCATIONAL EXAMPLE 🌟 ║
║ ║
║ 📌 This is a minimal and short working example for educational purposes. ║
║ ⚠️ Not optimized for production! ║
║ ║
║ 📦 Versions Used: ║
║ - "@langchain/core": "^0.3.38" ║
║ - "@langchain/openai": "^0.4.2" ║
║ ║
║ 🔄 Note: LangChain is transitioning from a monolithic structure to a ║
║ modular package structure. Ensure compatibility with future updates. ║
╚════════════════════════════════════════════════════════════════════════════╝
import { MongoClient } from "mongodb"; // import MongoDB driver
import { OpenAIEmbeddings } from "@langchain/openai"; // import OpenAI embeddings
import { MemoryVectorStore } from "langchain/vectorstores/memory"; // import MemoryVectorStore for in-memory vector store
import dotenv from "dotenv"; // import dotenv
import readline from "readline"; // import readline for console input
dotenv.config(); // load environment variables
const MONGO_URI = process.env.MONGO_URI || "mongodb://localhost:27017"; // MongoDB connection URI
const DATABASE_NAME = "jobsbg"; // MongoDB database name
const COLLECTION_NAME = "jobs"; // MongoDB collection name
const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); // create readline interface for console input
// function to connect to MongoDB
const connectToMongoDB = async () => {
const client = new MongoClient(MONGO_URI);
await client.connect();
console.log("✅ Connected to MongoDB");
return { client, collection: client.db(DATABASE_NAME).collection(COLLECTION_NAME) };
};
// function to fetch documents from MongoDB
const fetchMongoDBDocuments = async () => {
const { client, collection } = await connectToMongoDB(); // connect to MongoDB
const documents = await collection.find({}).toArray(); // fetch all documents
await client.close(); // close MongoDB connection
console.log("🔌 MongoDB connection closed.");
return documents; // return fetched documents
};
// function to retrieve documents
const retrieveDocuments = async (query: string) => {
const documents = await fetchMongoDBDocuments(); // fetch documents
if (!documents.length) return console.log("❌ No documents found in MongoDB."), []; // if no documents found
const vectorStore = await MemoryVectorStore.fromTexts(
documents.map(doc => `${doc.title} ${doc.description}`), // map documents to texts
documents.map(doc => ({ id: doc._id.toString(), title: doc.title })), // map documents to metadata
new OpenAIEmbeddings() // create OpenAI embeddings
);
console.log(`🔍 Searching for: "${query}"`);
return await vectorStore.similaritySearch(query, 2); // search for documents
};
async function run() {
// prompt user for input
rl.question("🔹 Enter a job title or keyword to search: ", async (query) => {
console.log("📄 Retrieved Documents:", JSON.stringify(await retrieveDocuments(query), null, 2)); // print retrieved documents
rl.close(); // close readline interface
});
};
run().catch(console.error); // catch errors
How the Code Works
This script connects to MongoDB, retrieves job listings, embeds the job descriptions as vectors, and searches for the most relevant jobs based on user input.
- Database Connection & Data Retrieval The script first connects to MongoDB using the MongoDB Node.js driver and fetches all job listings from the jobs collection. The job documents contain titles and descriptions.
- Embedding & Vector Storage The retrieved job listings are converted into text embeddings using OpenAIEmbeddings. These embeddings are stored in a MemoryVectorStore, an in-memory vector database that enables fast similarity search.
- Searching for Similar Jobs The user inputs a job title or keyword. The system retrieves the top 2 most relevant jobs using vector similarity search. Instead of simple keyword matching, the search considers semantic meaning.
- Displaying the Results The retrieved job listings are displayed in the console. The MongoDB connection is closed properly after fetching the data.
This approach ensures smarter, context-aware job retrieval instead of basic keyword searches.
Other Retrieval Chains
- Vector Database Retrieval: Integration with vector stores like Qdrant, Pinecone, Weaviate, Milvus, FAISS, etc., for similarity search. Storing and retrieving embeddings for documents or other data.
- ElasticSearch Retrieval: Use ElasticSearch for retrieving documents based on keyword or full-text search.
- BM25 Retrieval: Classic keyword-based document retrieval using term-frequency inverse document frequency (TF-IDF) or BM25 algorithms.
- SQL Database Retrieval: Query relational databases (e.g., MySQL, PostgreSQL, SQLite) for structured data retrieval.
- Text File or Directory Retrieval: Retrieve documents or data stored in plain text files or a directory.
- Hybrid Search Retrieval: Combines keyword-based and vector-based retrieval for enhanced performance.
- Custom Retrieval: Build your custom retrieval logic by implementing the Retriever interface.
- Google Drive Retrieval: Access documents stored in Google Drive.
- Notion Retrieval: Retrieve data from Notion databases and pages.
- Knowledge Graph Retrieval: Query knowledge graphs like Neo4j.
- Azure Cognitive Search: Azure's full-text search service for enterprise-level data retrieval.
- Document Loaders with Metadata Filtering: Filter and retrieve documents based on metadata tags.
- LangChain Index Retrieval: Use LangChain’s built-in document indexing and retrieval systems.
- ChatGPT Retrieval Plugin: A plugin to connect ChatGPT for retrieving specific datasets.
Conclusion
In the evolving landscape of AI and machine learning, LangChain Retrieval Chains offer an innovative way to bridge the gap between large datasets and intelligent applications. By seamlessly integrating language models with various retrieval mechanisms, LangChain enables developers to unlock valuable insights from structured and unstructured data with unparalleled efficiency.
Through practical examples like document querying and API retrievals, this tutorial highlights how LangChain can transform how we interact with data. These tools simplify complex tasks such as summarizing documents, answering user-specific queries, and retrieving relevant information from diverse sources like vector databases, REST APIs, and more.
The flexibility and scalability of LangChain Retrieval Chains empower developers to create AI-driven applications tailored to unique use cases—whether in healthcare, education, customer support, or beyond. As a result, they pave the way for more interactive, accurate, and adaptive AI systems that revolutionize data interaction and management.
By embracing LangChain, you can harness the power of advanced retrieval chains to build intelligent systems that not only process data but also provide meaningful, actionable insights. The possibilities are endless, and LangChain is your gateway to exploring them.