博客内容Blog Content

LangChain框架简介 Introduction of LangChain Framework

BlogType : IT releaseTime : 2025-12-01 12:00:00

介绍LLM开发的背景,为什么会有LangChain,该框架基本概念,相关优点以及核心组件 Introduction to LLM Development: Background, Why LangChain Emerged, Basic Concepts, Advantages, and Core Components

Background

大模型一般有两种形态“呈现”在我们眼前,一种是训练好的那种二进制文件,另外一种是将大模型的二进制文件进行部署之后暴露出一些相应的接口,但是无论是那种形式,LLM 只提供了一个非常基础的调用方式,当我们要构建一个复杂的 Chat Bot 时,就需要考虑如何保存聊天的上下文、如何进行网络检索、如何加载本地数据、如何便捷管理 Prompt 等等工程问题。

Large Language Models (LLMs) generally "appear" to us in two forms: one is the trained binary file, and the other is the exposed set of corresponding APIs after the LLM binary file has been deployed. However, regardless of the form, an LLM only provides a very basic calling mechanism. When we need to build a complex Chat Bot, we must consider various engineering problems such as how to save chat context, how to perform web retrieval, how to load local data, how to conveniently manage the Prompt, and so on.


甚至是当我们切换到不同的 LLM 时,模型的输入和输出结构差异都非常巨大,微量的需求就要修改大量的代码,或者在业务代码中做大量的判断与识别,让代码可维护性极差,但是其实不同 LLM 的交互流程其实都非常接近,如下可以看成是一个基础聊天机器人的链条,传入提示词,输出对应的结果,流程如下:

Furthermore, when switching to different LLMs, the input and output structures of the models can vary greatly. Even minor requirements may necessitate modifying a large amount of code or implementing extensive checks and logic within the business code, leading to very poor code maintainability. In reality, the interaction flow for different LLMs is quite similar. The following diagram can be seen as the chain for a basic chatbot: input a prompt and output the corresponding result. The process is as follows:

image.png


除了在代码开发这方面有大量的疑难杂项之外,对 LLM 的运行流程、输出、费用统计、错误监控也是一个非常重要的步骤,这些功能 基础的 LLM 均没有提供,需要程序员自行开发与对接,开发这些功能的耗时甚至会超过业务的部分,极大提升了 AI 应用开发的难度。

In addition to numerous miscellaneous complexities in code development, monitoring the LLM's operational process, output, cost statistics, and error monitoring is also a very crucial step. None of these functionalities are provided by the basic LLM, requiring programmers to develop and integrate them independently. The time spent developing these features can even exceed the time spent on the core business logic, significantly increasing the difficulty of AI application development.




LangChain Introduction

为了解决以上这些问题,AI 应用开发框架应运而生,其中最热门、更新速度最快、最稳定的框架就是 LangChain,而且目前 LangChain 提供了 Python 和 JavaScript 两个版本,适配了当前 AI 环境下最热门的两种语言。

To solve these problems, AI application development frameworks have emerged. Among them, the most popular, fastest-updating, and most stable framework is LangChain. Currently, LangChain offers both Python and JavaScript versions, supporting the two most popular languages in the current AI environment.

  • LangChain-Python: https://github.com/langchain-ai/langchain

  • LangChain-JavaScript: https://github.com/langchain-ai/langchainjs


用一句话说:LangChain 等框架的出现,是为了简化大模型应用开发中上下文管理、数据接入、模型切换、流程编排、错误监控等复杂工程问题,让开发者更高效地构建可维护、可扩展的 AI 应用。

In a single sentence: Frameworks like LangChain are designed to simplify the complex engineering challenges of building AI applications—such as context management, data integration, model switching, prompt orchestration, and error tracking—so developers can build scalable and maintainable LLM-based systems more efficiently.




Advantages

为什么选择 LangChain?主要有几个方面:认可度、持续维护、易用性、可扩展性、稳定版本、可观察

  1. 首先是 认可度,目前在 Github 上短短一年多的时间 LangChain 已获得了 86.3k star,是目前 star 最多的 AI 应用开发框架,并且其上升速度非常恐怖,并且 LangChain 拥有活跃的开发者社区和丰富的文档资源,能够快速解决问题和获取帮助

  2. 其次是 持续维护 ,LangChain 框架经历数次融资,累计融资数千万美金,拥有足够的资源对框架进行长期及稳定更新的维护,在短短一年多的时间里 LangChain 团队提交了接近 10000 次 commit 进行数百个大小版本的更新与维护,创下 AI 应用开发领域之最。

  3. 项目也从早期的一个副业,迅速发展成数万个 LLM 应用的支柱,不乏一些知名项目:早期 Dify、MetaGPT、百度智能云千帆等,这些项目均校验了 LangChain 在生产环境中使用的可能。作为一个年轻而有活力的框架,LangChain 正在彻底改变工业和技术,改变我们与技术的每一次互动

  4. 除此之外,易用性 和 可扩展性 也是 LangChain 的特性,LangChain 屏蔽了不同 LLM 调用接口、输入格式、输出格式之间的差异,用户无需深究不同 LLM 的细节,创建模型后,就可以直接使用。

  5. 在扩展性方面,除了 LangChain 提供的大量第三方组件的集成,也可以将自有的本地大语言模型、工具、数据库、文本嵌入等任何内容快速接入到 LangChain 中,LangChain 好比一把瑞士军刀,提供了近 700 种集成,涵盖从 LLM 到向量存储,再到 Agent 使用的各种工具。

  6. 在 可观测性 上,LangChain 对比同类的 AI 应用框架,提供了独一无二的 LangSmith 平台,LangSmith 的一个核心优势是为你的 LLM 应用提供一流的调试体验,详细记录了正在执行的每个步骤、每个步骤的输入、输出、所需时间等数据。并以用户友好的方式展示这些信息,让你能够识别哪些步骤耗时最长,进入一个调试区域来处理 LLM 意外反馈的问题,追踪 Token 使用情况等。

至此不再需要为每一个任务找一个新工具,它提供了一站式的解决方案,不管你遇到什么问题,打钉子、拧螺丝、剪线,工具箱里总有一个合适的工具等着你。

Why Choose LangChain? The main reasons are several key factors: Community Adoption, Continuous Maintenance, Ease of Use, Extensibility, Stable Versioning, and Observability.

  1. First is Community Adoption. In just over a year, LangChain has garnered over 86.3k stars on GitHub, making it the most starred AI application development framework currently available, with an incredibly rapid growth rate. Furthermore, LangChain boasts an active developer community and extensive documentation resources, enabling quick problem resolution and support.

  2. Next is Continuous Maintenance. The LangChain framework has undergone multiple funding rounds, raising tens of millions of US dollars, ensuring it has sufficient resources for long-term and stable maintenance updates. In just over a year, the LangChain team has committed nearly 10,000 times, performing hundreds of major and minor version updates and maintenance cycles—a record in the field of AI application development.

  3. The project rapidly evolved from an early side project into the backbone of tens of thousands of LLM applications, including notable projects such as Dify, MetaGPT, and Baidu Smart Cloud Qianfan in its early days. These projects have validated LangChain's potential for use in production environments. As a young and dynamic framework, LangChain is fundamentally transforming industry and technology, changing every interaction we have with technology.

  4. In addition, Ease of Use and Extensibility are also key features of LangChain. LangChain abstracts away the differences in calling interfaces, input formats, and output formats across various LLMs. Users do not need to deeply investigate the details of different LLMs; once a model is created, it can be used directly.

  5. In terms of Extensibility, besides the large number of third-party integrations provided by LangChain, you can quickly integrate your own local large language models, tools, databases, text embeddings, and any other content into the framework. LangChain is like a Swiss Army knife, offering nearly 700 integrations, covering everything from LLMs to vector stores and various tools used by Agents.

  6. For Observability, LangChain offers the unique LangSmith platform, setting it apart from similar AI application frameworks. A core advantage of LangSmith is providing a first-class debugging experience for your LLM applications, meticulously recording data for every step executed, including the input, output, and time taken for each step. It displays this information in a user-friendly manner, allowing you to identify the longest-running steps, enter a debugging area to handle unexpected feedback from the LLM, and track token usage, among other things.

Thus, there is no longer a need to find a new tool for every task. It offers a one-stop solution—no matter what problem you encounter, whether it's hammering a nail, tightening a screw, or cutting a wire, there is always a suitable tool waiting for you in the toolbox.




Core Components

LangChain 主要提供了 6 大核心组件帮助我们更好的使用大语言模型,涵盖了 Models(模型)、Prompts(提示)、Indexes(索引)、Memory(记忆)、Chains(链)、Agents(代理),这些组件集成了数十种大语言模型、多样的知识库处理方法以及成熟的应用链、上百种可调用的工具箱,为用户提供了一个快速搭建和部署大语言模型智能应用程序的平台

LangChain primarily provides six core components to help us better utilize Large Language Models, covering Models, Prompts, Indexes, Memory, Chains, and Agents. These components integrate dozens of large language models, diverse knowledge base processing methods, mature application chains, and a toolbox of hundreds of callable tools, offering users a platform for quickly building and deploying intelligent applications based on large language models.

image.png