Integration of Langchain with LlamaIndex (original) (raw)

Integration of Langchain with Llama-Index

Last Updated : 29 May, 2026

Integrating LangChain with LlamaIndex enables intelligent systems to retrieve and understand data efficiently, where LlamaIndex handles data retrieval and LangChain manages reasoning and response generation. Its key advantages include:

Langchain is an open-source framework that helps build applications using large language models by managing prompts, connecting reasoning steps, and creating intelligent agents that use tools and data.

Llama-Index

Llama-Index is a toolkit for building powerful search systems on unstructured data like PDFs, websites and databases. It helps in loading, splitting, embedding and retrieving data, making it ideal for RAG and AI-driven analysis. Its key features include:

Implementation

This example builds an AI system using LlamaIndex for PDF processing and LangChain for reasoning, with DuckDuckGo for real time search. What it demonstrates:

Step 1: Install Dependencies

!pip install --upgrade llama-index llama-index-llms-gemini llama-index-readers-pdf llama-index-embeddings-gemini google-generativeai langchain langchain-google-genai duckduckgo-search !pip install -U duckduckgo-search langchain-community

`

Screenshot-2025-07-21-143845

Dependencies installed

Step 2: Set Environment and Initialize LLM and Embeddings

from llama_index.core import Settings from llama_index.embeddings.gemini import GeminiEmbedding from llama_index.llms.gemini import Gemini import os import getpass os.environ["GOOGLE_API_KEY"] = getpass.getpass(" Enter your Gemini API Key: ")

llm = Gemini(model="models/gemini-2.5-flash") embed_model = GeminiEmbedding(model_name="models/embedding-001") Settings.llm = llm Settings.embed_model = embed_model

`

Step 3: Upload and Load PDF Documents

from llama_index.core import VectorStoreIndex from llama_index.core.readers import SimpleDirectoryReader from google.colab import files uploaded = files.upload()

pdf_files = ["PDF1.pdf", "PDF2.pdf"] docs = SimpleDirectoryReader(input_files=pdf_files).load_data() index = VectorStoreIndex.from_documents(docs)

`

Screenshot-2025-07-22-100056

Upload and Load PDF Documents

Step 4: Prepare the Query Tool

from llama_index.core.tools import QueryEngineTool, ToolMetadata

ai_engine = index.as_query_engine(similarity_top_k=3)

query_tool = QueryEngineTool( query_engine=ai_engine, metadata=ToolMetadata( name="ai_pdfs", description="Answers questions based on the uploaded AI PDF documents." ), )

tool = query_tool.to_langchain_tool()

`

Step 5: Add Web Search Tool

from langchain_community.tools import DuckDuckGoSearchRun

search_tool = DuckDuckGoSearchRun() tools = [tool, search_tool]

`

Step 6: Build Langchain Agent

from langchain_google_genai import ChatGoogleGenerativeAI from langchain.agents.tool_calling_agent.base import create_tool_calling_agent from langchain.prompts import ChatPromptTemplate from langchain.agents import AgentExecutor

system_context = "You are an AI expert. Answer questions using the uploaded AI-related PDFs or the web." prompt = ChatPromptTemplate.from_messages([ ("system", system_context), ("placeholder", "{chat_history}"), ("human", "{input}"), ("placeholder", "{agent_scratchpad}"), ])

llm_agent = ChatGoogleGenerativeAI(model="gemini-1.5-flash") agent = create_tool_calling_agent(llm_agent, tools, prompt) agent_executor = AgentExecutor( agent=agent, tools=tools, verbose=True, return_intermediate_steps=True)

`

Step 7: Interactive Querying and Summarization

question = "What are the main contributions of these AI papers?" response = agent_executor.invoke({"input": question}) print(response['output']) summary = ai_engine.query("Can you summarize the first uploaded AI document?") print(summary)

`

Screenshot-2025-07-22-100011

Interactive Querying and Summarization

Applications

**Advantages