What is LangChain?
LangChain is a powerful open-source orchestration framework designed for building LLM (Large Language Model) applications. It helps developers chain components like LLMs, retrievers, and agents with ease.
Core Concepts:
LangChain vs GUI Tools:
Pros:
Cons:
Ideal For:
Developers looking for flexibility and customization in building advanced AI workflows using large language models.
We at Codesmith cultivate technologists who are at the intersection of society and tech, able to meet this moment and thrive in an ever-changing world. Ready to become a modern software engineer?
The orchestration layer plays a pivotal role in the emerging LLM (Large Language Model) tech stack by interacting with various components across the data, model, and operational layers. In this article, we delve deeper into the LangChain framework, exploring its major concepts—such as components, chains, and agents—and key characteristics, including integrations, abstractions, and contexts.
LangChain is part of a broader language model application ecosystem, which includes compatible frameworks like LangGraph for complex control flows and LangSmith for observability and evaluation. We'll also compare programmatic and GUI orchestration frameworks and review the advantages and limitations of LangChain.
As a framework for developing applications powered by LLMs, LangChain comprises several open-source libraries available in JavaScript and Python:
Key characteristics of LangChain include:
Notably, LangChain defines and exposes common interfaces for components critical to LLM applications. For instance, the retriever interface provides a standard method to connect to different data stores, allowing all retrievers to be invoked uniformly, despite underlying implementation differences.
```JavaScript
const docs = await retriever.invoke(query);
```
Retriever code example (accessed November 2024). Source: LangChain Documentation
Components are the core building blocks used in LLM applications. LangChain defines many helpful components.
For the data layer (non-exhaustive list):
For the model layer (non-exhaustive list):
For the operational layer (non-exhaustive list):
Chains in LangChain are sequences of components that process inputs to produce outputs. They can be simple (e.g., a prompt followed by an LLM) or complex (e.g., involving multiple steps with conditionals).
```JavaScript
import { OpenAI } from "langchain/llms/openai";
import { PromptTemplate } from "langchain/prompts";
import { LLMChain } from "langchain/chains";
// Initialize the OpenAI model
const model = new OpenAI({
modelName: "text-davinci-003"
openAIApiKey: "YOUR_OPENAI_API_KEY",
temperature: 0.7
});
// Create a prompt template
const template = "What is a good name for a company that makes {product}?";
const myPrompt = new PromptTemplate({
template: template,
inputVariables: ["product"],
});
// Create the LLMChain
const chain = new LLMChain({ llm: model, prompt: myPrompt });
// Run the chain
const result = await chain.invoke({ product: "comfortable shoes" });
console.log(result);
```
Note: This is a simplified code example based on LangChain v0.1 for illustrative purposes only. At the time of writing, the latest version available is v0.3 and the above code may be deprecated.
The LangChain Expression Language (LCEL) provides a syntax to create arbitrary custom chains. It is based on the abstraction of a `Runnable`, which is "a generic unit of work to be invoked, batched, streamed, and/or transformed."
Agents add a layer of decision-making, allowing the system to choose which tools or actions to execute based on the input.
LangChain legacy agents use a language model as a "reasoning engine" to decide a sequence of actions to take. They are provided with user input (e.g. queries) and relevant steps executed previously. They can also interact with external resources via available tools. At the time of writing, advanced agent capabilities are moving to LangGraph.
From the original LangChain framework, a broader ecosystem of tools, services, and libraries has since been developed to address the evolving needs of building production-grade LLM applications.
A diagram of the LangChain ecosystem (accessed November 2024). Source: https://js.langchain.com/docs/introduction/
LangServe (not in the preceding diagram) is an open-source library (available in Python) for deploying LangChain runnables and chains as a REST API. It leverages FastAPI, a modern Python framework for building APIs, and Pydantic, a Python data validation library. A client is also provided, which can call runnables deployed on a server. LangServe is limited to deploying simple runnables.
LangGraph is an open-source library (available in JavaScript and Python) for "building stateful, multi-actor applications with LLMs" by modeling steps as edges and nodes in a graph. It supports single-agent and multi-agent workflows.
It offers the following main benefits:
Don't confuse it with the LangGraph Cloud, a commercial infrastructure platform for deploying and hosting LangGraph applications to production (built on top of the open-source LangGraph framework).
LangSmith is a commercial developer platform to monitor, debug, and evaluate LLM applications. It helps your LLM applications progress from prototype to production and offers easy integration with LangChain and LangGraph. With its tracing functionality, you can inspect and debug each step of your chains and agents. LangSmith provides evaluation features for your LLM applications, such as creating datasets, defining metrics, and running evaluators.
LangChain is a programmatic orchestration framework, requiring installation, importing, and usage within your LLM application's source code.
Alternatively, graphical user interface (GUI) frameworks are available for orchestration. For example, Flowise is an open-source tool built on top of LangChain components. Flowise can be run locally, self-hosted in your cloud infrastructure, or accessed via the commercial web application Flowise Cloud. It offers a simple user interface with drag-and-drop functionality for visually adding and chaining together major components.
Compared to the LLMChain code example in a prior section, the drag-and-drop UI is simpler to get started with, requiring no existing knowledge of the underlying libraries and programming syntax. If you're just beginning with LLM applications and orchestration frameworks, exploring a GUI orchestration tool can help familiarize you with various components. For straightforward chains, a GUI orchestration tool may suffice. However, a programmatic orchestration framework offers more fine-grained control and customization of components and workflows.
Framework TypeFrameworksProgrammingLangChain (open-source), Haystack (open-source)GUIFlowise (open-source), Stack AI (commercial)
A table of available orchestration frameworks (as of November 2024, non-exhaustive). Source: Author
Advantages:
Limitations:
LangChain serves as a powerful programmatic orchestration framework for building applications powered by large language models. Its modular architecture, extensive integrations, and support for dynamic agents make it a valuable tool for developers aiming to create sophisticated LLM applications. While it may present a learning curve for newcomers, the flexibility and control it offers can lead to highly customized and efficient solutions.
Explore CS Prep further in our beginner-friendly program.
Get more free resources and access to coding events every 2 weeks.
Connect with one of our graduates/recruiters.
Our graduates/recruiters work at:
Diana Cheung (ex-LinkedIn software engineer, USC MBA, and Codesmith alum) is a technical writer on technology and business. She is an avid learner and has a soft spot for tea and meows.
Connect with one of our recruiters to learn about their journeys.
Our graduates/recruiters work at: