In LangChain, an agent is a system that uses a language model to decide which actions to take. It executes those actions using tools, observes the results, and repeats the process until the task is complete.
In LangChain, tools are functions that agents call to perform specific actions. Tools allow agents to interact with external systems such as web search engines, APIs, or databases. By selecting and executing tools, agents can access information or perform tasks beyond the language model’s built-in knowledge.
@tool DecoratorThe @tool decorator in LangChain converts a regular Python function into a tool that an agent can call. It uses the function name as the tool name and the docstring as the tool description. The description helps the language model understand when and how to use the tool.
from langchain_core.tools import tool@tooldef add_numbers(a: float, b: float) -> float:"""Add two numbers together.Use this tool to calculate the sum of two numbers.Args:a: The first numberb: The second number"""return a + b
In LangChain, create_tool_calling_agent() creates an agent by combining a language model, a list of tools, and a prompt template. The resulting agent can iteratively decide which tools to call while completing a task. This function helps build agents that can reason about actions and interact with external tools.
from langchain.agents import create_tool_calling_agentagent = create_tool_calling_agent(llm,tools,prompt)
AgentExecutorThe AgentExecutor in LangChain manages the agent’s run loop. It repeatedly runs the agent to generate tool calls, executes those tools, and passes the results back to the agent. This cycle continues until the agent produces a final answer or a stopping condition is reached.
from langchain.agents import AgentExecutor# Create the executor to manage the agent loopagent_executor = AgentExecutor(agent=agent,tools=tools,verbose=True,stream_runnable=False)# Run the agent with a questionresult = agent_executor.invoke({"input": "What is 42 plus 58?"})
In LangChain, the ReAct framework describes how an agent reasons and acts in a loop. Each iteration follows three steps: Thought (the model reasons about the next step), Action (a tool is selected and called), and Observation (the tool output is returned). This cycle repeats until the agent produces a final answer.
In LangChain, Retrieval-Augmented Generation (RAG) combines information retrieval with language model generation. The system retrieves relevant information from external data sources and provides it as context to the model. This allows the model to produce responses that are more accurate and grounded in retrieved data.
In LangChain, document loaders import data from external sources and convert it into Document objects. Each Document contains page_content, which stores the text, and metadata, which stores information about the source. These objects allow documents to be processed in retrieval workflows.
from langchain_community.document_loaders import TextLoader# Load a text file into a Document objectloader = TextLoader("sample_document.txt")documents = loader.load()print(documents[0].page_content)print(documents[0].metadata)
In LangChain, text splitters divide large documents into smaller chunks before processing. These chunks fit within the language model’s context window, enabling retrieval systems to locate relevant information more accurately. Splitting documents into smaller segments improves the effectiveness of retrieval pipelines.
from langchain_text_splitters import RecursiveCharacterTextSplitter# Split documents into smaller chunkstext_splitter = RecursiveCharacterTextSplitter(chunk_size=200,chunk_overlap=50)chunks = text_splitter.split_documents(documents)
In LangChain, embeddings represent text as numerical vectors that capture semantic meaning. These vectors allow systems to compare text based on similarity. By comparing the embeddings of documents and user queries, retrieval systems can identify the most relevant information.
from langchain_openai import OpenAIEmbeddings# Initialize the embedding modelembeddings = OpenAIEmbeddings(model="text-embedding-3-small")# Convert text to a vectorvector = embeddings.embed_query("What is LangChain?")print(len(vector)) # e.g., 1536 dimensions
In LangChain, a vector store stores embeddings of documents along with metadata. These embeddings allow systems to perform a similarity search to find documents that are most relevant to a user query. Vector stores enable efficient retrieval in retrieval-augmented generation workflows.
In LangChain, vector stores can be created from documents using .from_documents(), which generates embeddings and stores them in the index. Additional documents can later be inserted using .add_documents(), which embeds the new documents and adds them to the existing vector store.
from langchain_community.vectorstores import FAISS# Create a vector store from an initial set of chunksvector_store = FAISS.from_documents(chunks, embeddings)# Add more documents latervector_store.add_documents(new_chunks)
In LangChain, a retriever is an interface that accepts a query and returns relevant Document objects. It abstracts the underlying search mechanism, such as a vector store, so that it can be used consistently across pipelines like RAG chains.
# Convert a vector store into a retrieverretriever = vector_store.as_retriever(search_kwargs={"k": 3})# Retrieve relevant documents for a querydocs = retriever.invoke("What are the main components of LangChain?")