
š¦š LangChain Introduction: An Open-Source Language Model Framework
This article was translated through AI, there might be inaccuracies.
This article is mostly translated by GPT-4, here is the original article
TL;DR
1ļøā£ LangChain defines the responsibilities of each component in language model applications, enabling developers to develop customized language model applications through a consistent interface. LangChain provides two language SDKs, Python and Javascriptć
2ļøā£ The core components of LangChain include Modals, Prompts, Indexes, Memory, Chains, Agents, etc. These components allow developers to integrate internal and external data resources, maximizing the capabilities and use of large-scale language models.
3ļøā£ An example of a food ordering bot is used to illustrate how to apply various components of LangChain to implement specific applications.
4ļøā£ Large-scale language models will open up new possibilities for problem-solving, and technologies like LangChain will gradually add more solutions to meet future needs.
About the Series
The recent advancements in large language models have sparked a great deal of imagination about future software applications, and many tools and technologies have emerged. During a casual chat with my ex-colleague, I learned about LangChain, a development framework for language model applications. Feeling curious, I wanted to delve deeper into this technology. After reading LangChain's official documents, I felt a desire to learn about this future technology and hoped that sharing my learning could help others who are also interested and curious about LangChain.
In this LangChain series, I will introduce various components defined in the LangChain framework. Although the official documents already exist, some parts might not be described in great detail. So, I hope to share with everyone in a more accessible way through my understanding of LangChain.
Components of LangChain that are planned to be written about in the series:
- Modals
- Prompts
- Indexes
- Memory
- Chains
- Agents
What is LangChain?
LangChain is an open-source framework designed to simplify the development of applications using Large Language Models (LLMs). LangChain clearly defines the responsibilities of each component in a language model, with components communicating through a certain interface. This allows developers to systematically integrate internal and external data resources. Combined with prompt engineering, it maximizes the capabilities and data usage of large language models. Both developers and data scientists can obtain fully customized solutions in a simpler and more consistent way. LangChain currently provides two language SDKs, Python and Javascript.
To understand the role of LangChain, we can use an analogy to a mobile app. Let's look at these three elements:
- Infra - iPhone provides powerful basic capabilities such as internet access, computing power, etc.
- Data - iPhone users have a lot of data, such as photos, post content.
- App - such as YouTube, Instagram, Facebook.
In the mobile app world, we need custom apps to maximize the benefits of the basic capabilities and data, such as users using the Google Photos mobile app to view family photos on the iPhone.
In the application scenario of language models, Infra are equivalent to the powerful language models. However, the problems that can be solved by the model alone are still limited. We need data and Apps (language model applications) to solve our problems to the greatest extent. The role of LangChain is to play a bridge, connecting Infra and data, and building customized language model Apps to solve problems in various application scenarios.
Core Components of LangChain
Before we start looking at some application scenarios, let's briefly understand the core components provided by LangChain:
Modals
LangChain itself is not a model provider; it simply provides a standard interface that allows LangChain to call various different language models in the same way, such as OpenAI's GPT3.5, GPT4.
Prompts
LangChain provides a series of tools to help developers manipulate prompt inputs and outputs, such as:
- Prompt Template: Provides a template for input prompts to the language model.
- Output Parser: Provides instructions on how the language model should return output text.
- Example Selector: If the developer's prompt has more output/input templates, examples can be loaded into the prompt instruction in the form of code.
Indexes
Indexes is an important part of LangChain's data source processing. From the perspective of the data index lifecycle, it can be further subdivided into the following components:
- Document Loader: Reads various local files, webpage content, etc.
- Text Splitter: Since models have input token number limits, the data read needs to be split into smaller slices for storage, facilitating later reading.
- Vector Store: The split and preprocessed data is stored in the Vector Store, which can be requested based on vector similarity comparison later.
- Retriever: Developers can request the Vector Store based on vectors, and can also customize different acquisition rules.
Memory
Large language models themselves do not have memory. The ability for a language model to have memory requires the model caller to input the context of the conversation into the subsequent conversation. LangChain provides different methods to handle context memory in conversations.
Chains
A Chain can be seen as one process in the input and output Pipeline. When a language model application requires multiple processes for handling, LangChain provides the Chain interface to allow different processing flows to interact with each other.
Agents
Some applications need to dynamically determine what Pipeline should be used to handle requests when there are user input requests. This is when Agents are used. Agents can pick the appropriate tool to handle the request based on the description of different tools.
In addition to the various tools natively provided by LangChain, developers can also define tools to handle requests.
Finally, let's illustrate the relationship among LangChain components using a language model application as an example:
Simple Use Case of LangChain
With the understanding of the above basic components of LangChain, we can more clearly understand the positioning of each basic component in the application by discussing application scenarios. Suppose we opened a burger shop, we could use the menu information we have to implement a chatbot process for an ordering system:
- The user expresses to the chatbot that they want to order.
- The chatbot uses the language model to recognize the intent to order, selecting the tool for handling orders (Modal, Agent).
- The ordering tool retrieves the menu from data storage (Indexes).
- Returns the menu to the user in a list format (Prompt - Output Parser).
- After looking at the list, the user inputs the order message, such as: "I want 2 egg burgers and 1 cup of iced milk tea."
- The chatbot recognizes the intent to create an order, looking for the tool to create an order (Agent, Memory).
- The order tool parses and structures the user input (Prompt - Output Parser).
- Returns the output to the user to confirm whether the order content is correct.
- The user confirms it's correct, the structured order message is stored in the database, triggering subsequent flows such as meal preparation (Memory).
Although the process simplifies many details, it's hoped that everyone can more concretely understand how to decompose subtasks when a chatbot serves end-users, and how to use the component capabilities of LangChain to meet the needs of subtasks.
Conclusion - The Future of Language Model Applications
The problem-solving ability of large language models is like opening the door to a new world. If the space inside a bottle is metaphorically all the problems AI can solve, ChatGPT is like a big stone placed inside this empty bottle. This stone has solved many problems, but there is still a lot of space in the bottle. There are still many problems in our world that AI has not yet solved. These problems require more targeted, customized solutions. We are waiting for technologies like LangChain to gradually add more pebbles and fine sand into the bottle to solve more problems. We look forward to new technologies that can help us live and work more efficiently, allowing us to have more time to do more important things.
By the way, LangChain's recent valuation is approximately 200 million USD. This represents the market's high expectations for the AI field. As for what the future will look like, let's continue watching...