LangChain
Orchestration of LLMs and Agents
LangChain is a framework designed to simplify the creation of applications powered by Large Language Models (LLMs). It provides modular abstractions and standardized connectors to integrate LLMs with external data sources, memory, and tools.
🧱 Key Components
- Models: Standard interfaces to interact with various LLM providers (OpenAI, Hugging Face, Anthropic, etc.).
- Prompts: Templates for managing and parameterizing prompts, making dynamic reuse easier.
- Chains: Structured sequences of calls, where the output of one step (e.g., fetching data) becomes the input to the next (e.g., generating a summary).
- Agents: Unlike rigid chains, agents use the LLM itself as a reasoning engine to dynamically decide which tools to use and in what order.
- Memory: Components for maintaining state and conversation history between interactions.
⚙️ Practical Example (Agent)
from langchain.agents import initialize_agent, Tool
from langchain.llms import OpenAI
# Defining a custom tool for the Agent to use
def search_database(query: str) -> str:
return "Search result from the internal database."
tools = [
Tool(
name="Search",
func=search_database,
description="Useful for answering questions about company data."
)
]
llm = OpenAI(temperature=0)
agent = initialize_agent(tools, llm, agent="zero-shot-react-description")
# The LLM reasons and decides to invoke the 'Search' tool
response = agent.run("What are the company's policies regarding vacations?")
🎯 When to use
- RAG (Retrieval-Augmented Generation): To connect an LLM to a vector database and ask questions about private documents.
- Autonomous Agents: To build systems that can execute real-world actions, such as accessing APIs, running Python code, or querying SQL autonomously.
- Rapid Prototyping: LangChain’s architecture makes it easy to swap components (e.g., switching from OpenAI to Llama) without rewriting much code.
⚠️ Trade-offs and Pitfalls: LangChain can add an unnecessary layer of abstraction and hidden complexity for simple projects. If you just need a straightforward OpenAI API call and a simple prompt, using the direct OpenAI SDK might be more performant and easier to debug.
Related: harness-llm