Codecademy Logo

Agents and Retrival Systems in LangChain

Related learning

  • Build powerful AI applications using LangChain and LangGraph.
    • Includes 3 Courses
    • With Certificate
    • Intermediate.
      4 hours

Agent in LangChain

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.

LangChain Tools

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.

Python @tool Decorator

The @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
@tool
def 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 number
b: The second number
"""
return a + b

Create Tool Agent

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_agent
agent = create_tool_calling_agent(
llm,
tools,
prompt
)

LangChain AgentExecutor

The 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 loop
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
verbose=True,
stream_runnable=False
)
# Run the agent with a question
result = agent_executor.invoke({"input": "What is 42 plus 58?"})

LangChain Agent Steps

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.

LangChain RAG

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.

LangChain Document Loaders

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 object
loader = TextLoader("sample_document.txt")
documents = loader.load()
print(documents[0].page_content)
print(documents[0].metadata)

LangChain Splitters

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 chunks
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=200,
chunk_overlap=50
)
chunks = text_splitter.split_documents(documents)

LangChain Embeddings

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 model
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
# Convert text to a vector
vector = embeddings.embed_query("What is LangChain?")
print(len(vector)) # e.g., 1536 dimensions

LangChain Vector Store

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.

LangChain Vector Stores

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 chunks
vector_store = FAISS.from_documents(chunks, embeddings)
# Add more documents later
vector_store.add_documents(new_chunks)

LangChain Retrievers

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 retriever
retriever = vector_store.as_retriever(search_kwargs={"k": 3})
# Retrieve relevant documents for a query
docs = retriever.invoke("What are the main components of LangChain?")

Learn more on Codecademy

  • Build powerful AI applications using LangChain and LangGraph.
    • Includes 3 Courses
    • With Certificate
    • Intermediate.
      4 hours