LangChain, the duct tape of the AI Ecosystem
LangChain is a revolutionary software development framework designed to simplify the creation of applications using large language models (LLMs). It was launched in October 2022 as an open-source project by Harrison Chase, and quickly took off in popularity as an essential connecting framework to build out AI applications.
LangChain was developed to improve the way we interact with LLMs like GPT-4. It grew out of the need to connect LLM invocations with other actions, tools and data within a larger flow. The LangChain framework is data-aware and agentic, meaning it can connect a language model to other sources of data and allow it to interact with its environment.
This opens up a world of possibilities for developers, allowing them to create applications that can remember interactions, have knowledge about your data, and make decisions based on that information.
Today, you don’t need to be a data scientist or an ML engineer to make an AI application. AI models can be accessed through APIs, whether from AI model providers like OpenAI or Cohere, or the thousands of models aggregated on the Hugging Face model hub.
LangChain provides the connecting framework that builds on AI model APIs and takes them to the next level. As an essential connecting mechanism, LangChain is the duct tape of the AI ecosystem, bringing together the power of language models with other sources of data to create something truly amazing.
Some common use cases for LangChain include autonomous agents, agent simulations, personal assistants, and question answering. For example, personal assistants need to take actions, remember interactions, and have knowledge about your data. LangChain provides the interface code and guidance in these use cases.
Utilizing Lang Chain’s connecting code and AI model APIs, programmers can harness the power of AI in their applications without needing to know (or build) those underlying models. This democratizes access to AI technology, making it available to a wider audience of software developers, rather than being limited to specialized AI model builders. The result has been an explosion in creative AI applications, tools and projects.
It’s also made Harrison Chase’s LangChain Github repo very popular, with over 30,000 ‘stars’ on it.
How LangChain Works
The LangChain framework is provided as an open-source JavaScript (specifically TypeScript) and Python package, and it consists of several main modules. These modules can be used in a variety of ways and include:
Models: LangChain supports various model types (LLMs, chat models, and text embedding models) and integrations of models from a number of providers, including OpenAI, AI21, Anthropic, Google, etc.
Prompts: Prompts are inputs to LLMs. This module includes managing and optimizing prompts as well as prompt serialization.
Memory: Memory is the concept of persisting state between calls of a chain or agent. LangChain provides a standard interface for memory, a collection of memory implementations including connectors to vector databases. This allows for the creation of applications that can remember interactions and have knowledge about your data.
Indexes: This module contains utility functions for working with documents, including ways to structure documents so that LLMs can best interact with them.
Chains: Chains are sequences of calls to an LLM, tool or utility, to support applications that go beyond just a single LLM call. LangChain provides a standard interface for chains and their integrations with other tools, to enable more complex AI applications.
Agents: Agents involve an LLM making decisions about which actions to take, taking that action, observing results, then repeating that until done. LangChain provides a standard interface for agents and a selection of agents to choose from.
Lang Chain in Action
To get started using LangChain to create an application, you can check out their documentation or go through a tutorial. Developing an AI application is fairly easy if you are a Python or Javascript developer.
The stereotypical first ‘baby’ application that you try on a framework or programming language is “Hello World!”. The LangChain ‘hello world’ is hooking up to an LLM (in this case OpenAI GPT-3.5), prompting it and getting an output:
import os
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
from langchain.llms import OpenAI
llm = OpenAI(model_name="text-davinci-003",temperature=0.9)
input_prompt = "Tell me 'Hello World' in German, French and Latin"
llm(input_prompt)
#Output'German: Hallo Welt\n French: Bonjour monde\n Latin: Salve Mundi'
What’s magical is this trivial amount of code expresses tremendous power, thanks to the LLM’s API and LangChain. Add a few lines for a GUI wrapper and you’ve got a basic chatbot. Throw in access to memory via a vector database, and you have the ability to query over any loaded file, so you can connect an LLM to your own files and data.
That’s what this PDF-GPT reader does: Feed in any PDF and it will answer queries on it. This whole website can be coded in only a hundred or so lines, thanks to LangChain, and it could be coded in minutes by AI code generators like Gitub Co-Pilot. Powerful LLMs have collapsed complex NLP pipelines and turned them into simplified applications, and simplified applications can be custom-created on the fly.
Agents R Us
Making formerly complex flows trivial opens up the ability to add more layers of complexity to AI use cases. Specifically, just as we can connect LLM inputs with memory and retrieval, we can connect LLM responses with actions and other tools.
That’s what AI agents do. AI agents make decisions and take action on the basis of instructions and the data in their environment. LangChain supports agents that can call upon AI models and tools (via APIs) to execute as subroutines, following this flow:
Receive user input.
Decides which tool or AI model to use, and what to input that tool.
Call the tool or model with that input.
Pass the tool or model output back into the agent.
The agent then decides what step to take next, repeating steps 2 through 4, until the agent decides it’s done, and then it responds directly to the user.
This fairly simple flow can create powerful executable agents. However, it lacks planning ability. Inspired by BabyAGI and related agent developments, LangChain recently introduced plan-and-execute agents, to extend support for more complex agents that perform distinct planning and execution phases using other tools.
LangChain can support making complex agent-based automated AI applications, and it’s not too hard to build out. For example, this 25 minute tutorial shows how to make an autoGPT.
This has profound implications. An AI agent that automatically generates GPT-4 level insights is an AI capable of automating and speeding up many human-level intellectual tasks.
The Hobbyist Playground
As a kid, I liked to play with erector sets. My kids more recently enjoyed Legos and their virtual equivalent, Minecraft. Hobbyists, tinkerers and hackers are taking to these new LLMs and related AI models with gusto, looking for ways to creatively build with them.
LangChain is like a new set of erector set connectors, building out a robust AI ecosystem and filling a huge need for software developers eager to make the next creative AI application. The Cambrian explosion of AI apps and AI agents is a natural outcome.
If you are a developer, tinkerer or just want to try your hand at it, give LangChain a try, and build your favorite AI toy or tool. The barrier to entry in creating AI application never was lower.