Orchestration Framework: LangChain Deep Dive

Written by
Diana Cheung
7 mins
Dec 16, 2024
Subscribe for more insights
Thank you for your interest in the syllabus.

We'll be in touch soon with more information!

In the meantime, if you have any questions, don’t hesitate to reach out to our team.

Oops! Something went wrong while submitting the form.

TLDR - LangChain Orchestration Framework

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:

  • Components: Building blocks like document loaders, retrievers, and prompt templates.
  • Chains: Sequential workflows combining components.
  • Agents: Add decision-making to dynamically select tools and actions.
  • Integrations: Works seamlessly with tools like OpenAI, Anthropic, and various vector stores.

LangChain vs GUI Tools:

  • LangChain is programmatic—ideal for custom workflows.
  • GUI tools like Flowise simplify orchestration with drag-and-drop interfaces.

Pros:

  • Highly modular and extensible
  • Strong community support
  • Rich integrations for LLM apps

Cons:

  • Steeper learning curve for beginners
  • May be overly complex for simple use cases

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.

LangChain Overview

As a framework for developing applications powered by LLMs, LangChain comprises several open-source libraries available in JavaScript and Python:

  • langchain/core: Contains the base abstractions and LangChain Expression Language.
  • langchain/community: Houses third-party integrations, such as for OpenAI and Anthropic.
  • langchain: Contains the blocks, chains, agents, and retrievals that comprise an application's logic architecture.

Key characteristics of LangChain include:

  • Integrations: Extensive integrations with LLMs and cloud platforms.
  • Abstractions: Modularity and abstractions for flexible LLM application building.
  • Contexts: Context-awareness through data source connections and retrieval strategies, such as the Retrieval Augmented Generation (RAG) pattern.

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

Components are the core building blocks used in LLM applications. LangChain defines many helpful components.

For the data layer (non-exhaustive list):

  • Document Loaders: Load different formatted files from a variety of sources, including CSV, PDF, HTML, and more.
  • Text Splitters: Divide large documents into smaller chunks for processing.
  • Embeddings: Convert text into numerical vectors for similarity searches.
  • Vector Stores: Connect with vector databases to store and retrieve embeddings.
  • Retrievers: Fetch relevant documents based on queries.

For the model layer (non-exhaustive list):

  • Chat Models: Language models that take messages as input and output a message.
  • Messages: The input and output of chat models. Some messages have the `role` parameter defined, which specifies the source of the messages.
  • Prompt Templates: Format user input and other instructions into a formal structure that can be passed to a language model.
  • Output Parsers: Take the output of a language model and parse it into a structured format.
  • Memory: Provide simple utilities for adding memory to a system, such as retaining conversation history.

For the operational layer (non-exhaustive list):

  • Callbacks: Hook into the various stages of an LLM application's execution. Enable the running of custom auxiliary code in built-in components, such as streaming output or tracing intermediate steps.
  • Tools: Encapsulate a function and associated schema to be passed to chat models for calling. Tools expand an LLM's access to information and functionality. Some examples include Google Search, Wikipedia, and Wolfram Alpha.

Chains and Agents

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.

LangChain Ecosystem

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

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

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:

  • Cycles and Branching: Can define loops and conditionals in your flows.
  • Persistence: Has built-in persistence and automatically saves state after each step in the graph. The graph execution can be paused and resumed at any point to support error recovery, human-in-the-loop intervention, and time travel.
  • Controllability: Designed as a low-level framework, provides fine-grained control over the state and flow of your applications.
  • Integration: Works harmoniously with LangChain and LangSmith (if needed, but not required).

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

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.

Programmatic vs. GUI Orchestration Frameworks

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.

Orchestration Frameworks Overview

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 and Limitations of LangChain

Advantages:

  • Accelerated Development: As the langchain/community library contains many integrations with popular providers (e.g. OpenAI), this can reduce development time and effort.
  • Streamlined Development: The langchain/core and langchain libraries define a common interface for components and workflows. This provides a standardized approach to composing and chaining components.
  • Community Support: As an open-source project, LangChain benefits from active community contributions and support.

Limitations:

  • Complexity: Requires learning about the LangChain framework's concepts and components. It's not as simple as directly using the LLM APIs.
  • Overhead: Involves installation and updating of the LangChain libraries. Dependency on a framework can be problematic if it becomes outdated or unsupported.
  • Potential Inefficiencies: Identifying performance inefficiencies can be difficult due to the "multiple layers of abstraction and numerous components." Also, the framework's default configuration, such as the settings for token usage and API calls, may not be optimized for cost or latency.

Conclusion

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.

Find out how we cover AI/ML in our updated curriculum
Get your Syllabus
Special blog guest offer!

Explore CS Prep further in our beginner-friendly program.

Get 50% Off CS Prep
Learning code on your own?

Get more free resources and access to coding events every 2 weeks.

Thank you for your interest in the syllabus.

We'll be in touch soon with more information!

In the meantime, if you have any questions, don’t hesitate to reach out to our team.

Oops! Something went wrong while submitting the form.
Want to learn more about advancing your career in tech?

Connect with one of our graduates/recruiters.

Schedule a Call

Our graduates/recruiters work at:

ABOUT THE AUTHOR

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.

Diana Cheung
Alumna, Software Engineer

Related Articles

Meta Llama 2 vs. GPT-4: Which AI Model Comes Out on Top?

Tutorial
AI/ML
by
Diana Cheung
Nov 9, 2024
|
14 mins

Introducing the Emerging LLM Tech Stack

LLM
AI/ML
by
Diana Cheung
May 15, 2024
|
12 mins

Orchestration Framework: LangChain Deep Dive

LLM
AI/ML
by
Diana Cheung
Dec 16, 2024
|
7 mins

Start your journey to a coding career.

Thank you for your interest in the syllabus.

We'll be in touch soon with more information!

In the meantime, if you have any questions, don’t hesitate to reach out to our team.

Oops! Something went wrong while submitting the form.
Want to learn more about advancing your career in tech?

Connect with one of our recruiters to learn about their journeys.

Schedule a Call

Our graduates/recruiters work at: