这是用户在 2025-4-15 11:12 为 https://www.command.ai/blog/langchain-guide/ 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?
is being acquired by
正被收购
• Read more here.
• 点击此处阅读更多内容。
Latest Update September 10, 2024
最新更新于 2024 年 9 月 10 日

In the rapidly evolving landscape of artificial intelligence, Large Language Models (LLMs) like Open AI’s ChatGPT and Google’s Bard are revolutionizing the way we engage with technology. Tech giants and SaaS companies alike are racing to harness the power of LLMs to create more intelligent and useful applications. However, the true potential of these AIs is unlocked not when they stand alone, but when they are integrated with other tools, plugins, and knowledge bases.
在人工智能快速发展的浪潮中,像 OpenAI 的 ChatGPT 和谷歌的 Bard 这样的大型语言模型(LLMs)正在彻底改变我们与技术互动的方式。无论是科技巨头还是 SaaS 公司,都在竞相利用LLMs的力量来打造更智能、更有价值的应用。然而,这些 AI 的真正潜力并非孤立存在,而是当它们与其他工具、插件和知识库集成时才能充分释放。

Enter LangChain - a cutting-edge framework designed to give superpowers to language models, and redefine the boundaries of what's possible in the world of AI.
于是 LangChain 应运而生——这个前沿框架旨在为语言模型赋予超能力,重新定义 AI 世界的可能性边界。

But what is LangChain, and why is it a game-changer? Why is everyone suddenly talking about it?
但究竟什么是 LangChain?它为何具有颠覆性?为何突然成为热议焦点?

The importance of LangChain cannot be overstated. As you’re about to learn in this ultimate guide, LangChain gives seemingly magical abilities to companies who want to integrate LLMs into their software. From autonomous Agents (hello Skynet), to the ability to retrieve & understand incredibly large volumes of data instantly.
LangChain 的重要性再怎么强调也不为过。正如你即将在这份终极指南中了解到的,LangChain 为希望将LLMs集成到其软件中的公司赋予了看似神奇的能力。从自主代理(你好,天网),到即时检索和理解海量数据的能力。

As we continue to advance in the field of AI, language models are becoming increasingly integral to a wide range of apps and services we use today. From chatbots and virtual assistants to content generation and beyond, LangChain, with its ability to leverage the power of these models, is playing a pivotal role.
随着我们在人工智能领域的不断进步,语言模型正日益成为当今使用的各种应用程序和服务中不可或缺的一部分。从聊天机器人、虚拟助手到内容生成及其他领域,LangChain 凭借其利用这些模型力量的能力,正在发挥关键作用。

In this ultimate guide, we'll demystify all-the-things LangChain, diving deep into its framework, components, and real-world use cases like our very own HelpHub. And don't worry, we've got you covered even if you're not an engineer or a computer scientist. We'll break it all down, making the complex super simple and accessible.
在这份终极指南中,我们将揭开 LangChain 的一切神秘面纱,深入探讨其框架、组件以及像我们自己的 HelpHub 这样的实际用例。别担心,即使你不是工程师或计算机科学家,我们也会为你详细解析,让复杂的内容变得超级简单易懂。

Welcome to the ultimate guide to LangChain.
欢迎阅读 LangChain 终极指南。

LangChain Lingo: A Crash Course in Key Concepts
LangChain 术语:关键概念速成课

The name 'LangChain' is derived from a fusion of 'Lang' (short for language) and 'Chain', symbolizing the linking (or chaining) of different elements to create advanced applications around these LLMs. There are several key concepts that form the backbone of this functionality that we’re about to cover. These concepts are not just technical jargon, but they represent the unique strategies and techniques that LangChain uses to carry out all of its magic.
'LangChain'名称源自'Lang'(语言缩写)与'Chain'的结合,象征着通过链接(或串联)不同元素来构建围绕这些LLMs的高级应用。我们将要介绍构成该功能核心的几个关键概念,这些概念不仅是技术术语,更代表了 LangChain 实现其神奇功能所采用的独特策略与技术。

Note: From here on out, when you see “model” referred to, just know that we’re referencing an LLM like OpenAI’s GPT-4 for example. When you see “prompting”, that’s just a way of saying how you “talk” to the models by asking specific questions or making certain statements to guide it.
注:从此刻起,当文中提到"模型"时,请理解为指代类似 OpenAI 的 GPT-4 这类LLM;而"提示"则是指通过提出特定问题或陈述来引导模型的交互方式。

Let’s get into the Key Concepts of LangChain:
现在让我们深入理解 LangChain 的关键概念:

  1. Chain of Thought (CoT):  思维链(CoT):
    Picture a language model that thinks out loud, providing a step-by-step breakdown of its thought process. That's Chain of Thought. It's a prompting technique that encourages the model to generate a series of intermediate reasoning steps, providing a glimpse into its cognitive process. It's like having a conversation with the model where it explains its reasoning, making it a more effective tool for tasks that require complex problem-solving.
    想象一个语言模型能够大声思考,逐步展示其思维过程,这就是思维链。这是一种提示技术,鼓励模型生成一系列中间推理步骤,让人一窥其认知过程。就像与模型进行对话,它会解释自己的推理,使其成为需要复杂问题解决任务的更有效工具。
  2. Action Plan Generation:   行动计划生成:
    This is a prompting technique that uses a language model to generate actions to take. The results of these actions can then be fed back into the language model to generate a subsequent action. It's like having a dynamic conversation with the model where it suggests actions and then reacts to the outcomes of those actions. This makes the model more interactive and responsive, capable of adapting to changing circumstances and requirements.
    这是一种提示技术,利用语言模型生成要采取的行动。这些行动的结果随后可以反馈给语言模型,以生成后续行动。就像与模型进行动态对话,它建议行动并对这些行动的结果做出反应。这使得模型更具互动性和响应性,能够适应不断变化的环境和需求。
  3. ReAct:   反应式行动(ReAct):
    You've seen how LangChain can generate a sequence of actions with Action Plan Generation, and how it can provide a glimpse into its cognitive process with Chain of Thought. Now, imagine if you could have the best of both worlds. That's where ReAct comes into play. ReAct, short for "Reasoning and Action", is like a dynamic dance between thought and action. It's not just about the model suggesting an action, it's about understanding the 'why' behind the action. It's about the model taking a moment to contemplate, to reason, and then to act. This creates a more insightful dialogue with the model, making your interaction not just a series of commands and responses, but a dynamic conversation.
    你已经见识过 LangChain 如何通过“行动计划生成”来产生一系列行动,以及如何通过“思维链”让你一窥其认知过程。现在,想象一下如果能够兼得两者之长,这就是“反应式行动”(ReAct)的用武之地。ReAct 是“推理与行动”的缩写,宛如思维与行动之间的动态舞蹈。它不仅仅是模型建议一个行动,更是理解行动背后的“原因”。模型会花时间思考、推理,然后行动。这创造了一种更具洞察力的对话方式,使你的互动不再只是一系列命令和响应,而是一场动态的对话。
  4. Self-ask:   自问:
    Now imagine a language model that doesn't just respond to prompts, but actively asks itself follow-up questions. That's Self-ask. It's a prompting method that encourages the model to be more proactive and inquisitive internally. These questions are not directed to users or external entities like search engines. Instead, they are used “in the mind” of the model to guide the generation of subsequent responses. It's about transforming the model from a passive responder to an active learner, making it a more effective tool for tasks that require exploration and learning or when the model doesn’t know the right answer.
    现在想象一个语言模型,它不仅能响应提示,还能主动向自己提出后续问题。这就是 Self-ask。这是一种提示方法,鼓励模型在内部更加主动和好奇。这些问题并非针对用户或搜索引擎等外部实体,而是在模型的“思维”中用于引导后续回答的生成。其目的是将模型从被动响应者转变为主动学习者,使其成为需要探索和学习或当模型不知道正确答案时更有效的工具。
  5. Prompt Chaining:   提示链(Prompt Chaining)
    This involves combining multiple LLM calls, with the output of one step being the input of the next - thus creating the chaining effect. It's like having a relay race where the baton is passed from one runner to the next, each contributing to the overall progress of the team.
    这涉及将多个LLM调用串联起来,前一步的输出作为下一步的输入——由此形成链式效应。就像接力赛跑,接力棒从一位选手传给下一位,每个人都为团队的整体进展贡献力量。
  6. Memetic Proxy:   模因代理:
    This concept leverages the language model's ability to simulate well-known figures or situations to shape its responses. By framing the discussion in a context that the model recognizes, such as a conversation between a student and a teacher or a moral question posed to a famous philosopher, the model is encouraged to respond in a certain way. This method effectively encodes complex tasks and assumptions about the context of the question, making the model more adaptable and responsive. It's about tailoring the model's responses to fit the context of the conversation and using cultural information and well-known scenarios to guide the model's behavior.
    这一概念利用语言模型模拟知名人物或情境来塑造其回应的能力。通过将对话置于模型可识别的框架中(例如学生与教师的对话,或向著名哲学家提出的道德问题),模型被引导以特定方式回应。这种方法有效编码了复杂任务及问题背景的假设,使模型更具适应性和响应性。其核心在于根据对话语境定制模型回应,并借助文化信息和广为人知的场景来引导模型行为。
  7. Self Consistency:   自我一致性:
    This concept makes AI responses more reliable by creating multiple solutions and choosing the most consistent one. It helps ensure the AI's answers make sense and don't contradict each other, making it a more trustworthy and reliable tool.
    这一概念通过生成多个解决方案并选择最一致的一个,使 AI 的响应更加可靠。它有助于确保 AI 的答案合理且不自相矛盾,从而使其成为更值得信赖和可靠的工具。
  8. Inception (First Person Instruction):
    开端(第一人称指令):

    This is a concept that encourages the model to think a certain way by including the start of the model’s response in the prompt. It's about guiding the model's thought process, making it a more effective tool for tasks that require a specific line of reasoning or approach. For example, you might ask the model to “Tell me a joke. Make it start with “Knock, knock”. Where “Make it start with Knock Knock” is the guiding instruction you’re giving.
    这是一个通过将模型回应的开头包含在提示中来鼓励模型以特定方式思考的概念。它旨在引导模型的思维过程,使其成为需要特定推理或方法的任务中更有效的工具。例如,你可能会要求模型“讲个笑话。让它以‘咚咚,谁呀’开头”。这里的“让它以‘咚咚,谁呀’开头”就是你给出的引导指令。
  9. MemPrompt:
    This concept is much like a personal assistant that learns from past interactions. It keeps a record of times when it misunderstood what you were asking for, along with your feedback on how to do better. The next time you ask something similar, MemPrompt uses this 'memory' to improve its response. It's a way for the system to continuously improve and adapt to your needs, making it more reliable and effective over time.
    这个概念很像一个能从过往互动中学习的私人助手。它会记录下误解你需求的时刻,以及你关于如何改进的反馈。下次当你提出类似请求时,MemPrompt 会利用这段“记忆”来优化它的回应。这是系统持续改进并适应你需求的一种方式,随着时间的推移,它会变得更加可靠和高效。

So far, we've explored the Key Concepts that underpin LangChain, which are the theoretical foundations and innovative AI techniques that guide its operation. These concepts provide the 'why' and 'what' of LangChain - why it works the way it does and what it aims to achieve. As we transition into the Components section, we'll be shifting our focus to the 'how'.
到目前为止,我们已经探讨了支撑 LangChain 的关键概念,这些是指导其运作的理论基础和创新 AI 技术。这些概念提供了 LangChain 的“为什么”和“是什么”——它为何以这种方式工作以及它旨在实现什么目标。当我们过渡到组件部分时,我们将把重点转向“如何”。

The Power of LangChain: Understanding the Core Components that Drive the System's Capabilities
LangChain 的力量:理解驱动系统能力的核心组件

As we delve deeper into the world of LangChain, we encounter its core framework, a meticulously designed structure that brings together various elements to create a powerful tool for language model applications. This framework is built around two main ideas: Components and Use-Case Specific Chains.
随着我们深入探索 LangChain 的世界,我们会遇到其核心框架,这是一个精心设计的结构,它将各种元素结合在一起,为语言模型应用打造了一个强大的工具。该框架围绕两个主要理念构建:组件(Components)和特定用例链(Use-Case Specific Chains)。

Components are the building blocks of LangChain. They are the individual elements that come together to create the overall system. These components include Models, Prompts, Indexes, Memory, Chains, and Agents. Each component plays a specific role in the LangChain ecosystem, and understanding these roles is crucial for grasping the full potential of LangChain.
组件是 LangChain 的构建模块。它们是构成整个系统的独立元素,包括模型(Models)、提示(Prompts)、索引(Indexes)、记忆(Memory)、链(Chains)和代理(Agents)。每个组件在 LangChain 生态系统中都扮演着特定角色,理解这些角色对于充分掌握 LangChain 的潜力至关重要。

On the other hand, Use-Case Specific Chains are the “get started quickly” solutions that LangChain offers. Just think of what you want to build, pick the chain that most closely resembles your goal, and build from there. They are pre-built, customized chains that are designed to fit specific use cases like Personal Assistants, ChatBots, Extraction and Summarization, and more, making it a truly versatile tool.
另一方面,特定用例链(Use-Case Specific Chains)是 LangChain 提供的“快速上手”解决方案。只需设想你想构建的内容,选择最接近你目标的链,然后在此基础上进行构建。它们是预构建、定制化的链,专为适应个人助手、聊天机器人、信息提取与摘要等特定用例而设计,使其成为一款真正多功能的工具。

In the following sections, we'll take a closer look at these components and use-case-specific chains, shedding light on their roles, their importance, and how they come together to make LangChain the powerful tool that it is. Buckle up, as we're about to dive deep into the heart of LangChain!
在接下来的章节中,我们将深入探讨这些组件和特定用例链,揭示它们的作用、重要性以及如何协同工作,使 LangChain 成为如此强大的工具。系好安全带,我们即将深入 LangChain 的核心!

Schema: The Blueprint of LangChain
Schema:LangChain 的蓝图

The schema in LangChain is the underlying structure that guides how data is interpreted and interacted with. It's very similar to a blueprint of a building, outlining where everything goes and how it all fits together.
LangChain 中的 schema 是指导数据如何被解释和交互的基础结构。它非常类似于建筑物的蓝图,概述了所有内容的布局以及它们如何相互配合。

As of today, the primary interface for interacting with language models is through text. This is where the concept of "text in, text out" comes into play. As an oversimplification, many models in LangChain operate on this principle. You input text, the model processes it, and then outputs text. Therefore, understanding the Schema is fundamental as it forms the basis of how LangChain structures and interprets data. It's like setting out the rules of a game before you start playing.
截至目前,与语言模型交互的主要方式是通过文本。这就是“文本输入,文本输出”概念的由来。简而言之,LangChain 中的许多模型都基于这一原则运作。你输入文本,模型处理它,然后输出文本。因此,理解 Schema 至关重要,因为它构成了 LangChain 构建和解析数据的基础。这就像在开始游戏前设定规则一样。

Models: The Engines of LangChain
模型:LangChain 的引擎

We’ve already mentioned models quite a lot so far because they’re truly the engines that drive LangChain’s capabilities. However, it’s worth digging into things a little deeper to get a handle on just how integral these models are. After all, they process all the inputs and generate all the outputs - without them, LangChain wouldn’t exist.
我们已经多次提到模型,因为它们确实是驱动 LangChain 能力的引擎。不过,值得更深入地探讨这些模型的核心作用。毕竟,它们处理所有输入并生成所有输出——没有它们,LangChain 就不会存在。

LangChain employs three primary types of models to use and interact with:
LangChain 主要采用三种类型的模型来进行使用和交互:

  • Large Language Models (LLMs)
    大型语言模型(LLMs)
  • Chat Models, and  聊天模型,以及
  • Text Embedding Models.  文本嵌入模型。

Each of these models plays a unique role in the LangChain ecosystem, contributing to its versatility and power.
这些模型在 LangChain 生态系统中各司其职,共同增强了其多功能性和强大性能。

Large Language Models (LLMs)
大型语言模型(LLMs)

LLMs, such as OpenAI’s GPT, Google’s PaLM, and META’s LLaMA, form the core of LangChain. These models are created by training on vast amounts of text data, learning patterns, and structures of language to generate meaningful output. They process language input and generate responses, enabling dynamic and interactive language applications. Each LLM has its unique strengths, making them versatile tools for a variety of tasks within the LangChain ecosystem.
LLMs,如 OpenAI 的 GPT、Google 的 PaLM 和 META 的 LLaMA,构成了 LangChain 的核心。这些模型通过海量文本数据的训练创建,学习语言模式与结构以生成有意义的输出。它们处理语言输入并生成响应,为动态交互式语言应用提供支持。每个LLM都具备独特优势,使其成为 LangChain 生态系统中适用于多种任务的通用工具。

Chat Models  聊天模型

Chat Models, on the other hand, offer a more structured approach. Although they are typically backed by a language model, the way you interact with them is much more structured. They take a list of messages as input and return a message as output. Generally, each message in the input list has two properties: 'role' and 'content'. The 'role' is usually made up of 'system', 'user', or 'assistant', and 'content' contains the text of the message. This is exactly how ChatGPT works! This defined structure allows for more nuanced and interactive conversations with the LLM and gives it a human-like chat feeling.
另一方面,聊天模型提供了一种更为结构化的方法。尽管它们通常由语言模型支持,但您与它们的交互方式要结构化得多。它们接收消息列表作为输入,并返回一条消息作为输出。通常,输入列表中的每条消息都有两个属性:“role”和“content”。“role”通常由“system”、“user”或“assistant”组成,而“content”包含消息的文本。这正是 ChatGPT 的工作原理!这种定义好的结构允许与LLM进行更细致、更具互动性的对话,并赋予其类似人类聊天的感觉。

It’s important to note that while both LLMs and Chat Models are used for generating text, the key difference lies in the way they handle inputs and outputs. LLMs work with single strings of text, while Chat Models work with structured lists of messages, making them more suitable for interactive dialogues like back-and-forth real-time conversations.
需要注意的是,虽然LLMs和聊天模型都用于生成文本,但关键区别在于它们处理输入和输出的方式。LLMs处理的是单个文本字符串,而聊天模型处理的是结构化的消息列表,这使得它们更适合于交互式对话,如来回的实时对话。

Text Embedding Models  文本嵌入模型

The third and final type of Model component that LangChain utilizes are the Text Embedding Models. Text Embedding Models are a bit like language translators, but instead of translating from one language to another, they translate from text to numbers. They take text as input and turn it into a list of numbers (or 'floats') creating a numerical representation of the original text and storing them in a database.
LangChain 使用的第三种也是最后一类模型组件是文本嵌入模型。文本嵌入模型有点像语言翻译器,但它们不是将一种语言翻译成另一种语言,而是将文本转换为数字。它们接收文本输入,并将其转换为一串数字(或称“浮点数”),从而创建原始文本的数值表示,并将这些数值存储在数据库中。

This conversion of text into numbers is particularly useful because it means the model can now access the text in what’s called a Vector Database (picture a 3D space where the LLM can “think and look” in all directions of the space - forward, back, up, down, left, right, in and out). This 4D database allows for a semantic-style search, where seemingly unrelated pieces of text can be compared for similarity across the vector space.
将文本转换为数字的这一过程尤为有用,因为它意味着模型现在可以通过所谓的向量数据库(想象一个三维空间,其中LLM能够在该空间的所有方向——前、后、上、下、左、右、内和外——进行“思考和观察”)来访问文本。这种四维数据库支持语义式搜索,可以在向量空间中对看似无关的文本片段进行相似性比较。

In the simplest terms, it’s a way in which an LLM can derive meaning from text without a human telling it what the meaning should be. Funnily enough, this is kind of like how our human brain works. We convert the inputs of our senses (text) into electrical signals (numerical values) and store them in our brains (Vector Databases).
简单来说,这是一种让LLM无需人类告知其含义就能从文本中推导意义的方式。有趣的是,这有点像我们人类大脑的工作方式。我们将感官输入(文本)转化为电信号(数值),并将其存储在大脑(向量数据库)中。

Whether it’s an LLM, Chat Model, or Text Embedding Model, they all work together as the engines that power LangChain’s functionality, and understanding them is key to harnessing the full potential of the system.
无论是LLM、聊天模型还是文本嵌入模型,它们都作为驱动 LangChain 功能的引擎协同工作,理解这些组件是释放系统全部潜力的关键。

In the real-world application of this, you might see them integrated into a chatbot as we did with HelpHub. Initially, we use a Chat Model to process user inputs and generate appropriate responses. In the event that HelpHub encounters a complex question, it calls upon an LLM to generate a more nuanced response. We also use, a Text Embedding Model to upload and convert the user's input (for example, their Help Docs, support resources, SOPs, etc) into a numerical format, which is stored in a Vector Database. When the chatbot needs to retrieve relevant information, it uses a search tool to efficiently scan the Vector Database and find the correct answer. Try HelpHub here and you’ll see this live in action.
在实际应用中,你可能会看到它们被集成到聊天机器人中,就像我们在 HelpHub 中所做的那样。最初,我们使用聊天模型来处理用户输入并生成适当的响应。当 HelpHub 遇到复杂问题时,它会调用LLM来生成更细致的回答。我们还使用文本嵌入模型来上传并转换用户的输入(例如他们的帮助文档、支持资源、SOP 等)为数值格式,并将其存储在向量数据库中。当聊天机器人需要检索相关信息时,它会使用搜索工具高效地扫描向量数据库并找到正确答案。点击这里试用 HelpHub,你就能看到这一流程的实际运作。

Prompts: Directing the Flow of Information in LangChain

You might have already heard the term “prompts” or “prompting” being thrown around online lately because Prompts are fundamentally the entry point to interacting with LLMs. Prompts, in the simplest terms, are the questions or statements that you feed into a model. They're like the steering wheel of a car, guiding the model in the direction you want it to go. When it comes to LangChain, the use of prompts is shaped by three essential aspects, PromptTemplates, Example Selectors, and Output Parsers.

PromptTemplate

A PromptTemplate is like a recipe for creating the input to the model. It's not just about what you ask the model, but how you ask it. A PromptTemplate can guide the model to present its response in a specific way, making the output more useful for your specific needs.

Using this “recipe” format, a prompt can contain specific instructions for the language model, a question for the model, and even a set of "few-shot" examples. (Few-shot examples are essentially a small set of examples that you provide to the model with context, helping it understand the kind of response you're looking for.)

Example Selectors

Adding to this functionally, LangChain introduces Example Selectors. These tools allow you to include examples in your prompts. But instead of these examples being fixed (like the few-shot earlier), Example Selectors dynamically select them based on user input, adding an extra layer of adaptability to your prompts.

Output Parsers

Lastly, we have Output Parsers. These tools help structure the model's responses. They instruct the model on how to format its output and then transform this output into the desired format. This could be as simple as extracting a specific piece of information from the model's response, or as complex as transforming the response into a structured data table, graph, or even coding language.

Let's imagine a real-world scenario. Say you're a Product Marketer, and you want to use your LangChain-powered app to generate a list of potential features for a new product. You could use a PromptTemplate to instruct the model to generate its ideas in the form of a bulleted list. An Example Selector could dynamically select examples based on your input, adding depth and relevance to the model's responses. And an Output Parser could structure these responses into a clear, organized format that's easy to review and discuss with your team.

In a nutshell, Prompts in LangChain serve as navigational aids, directing the dialogue with the model, molding its responses, and maximizing the usefulness of its outputs. They act as both the blueprint and the safety measures that transform LangChain and LLMs into robust, flexible, and user-tailored instruments for engaging with language models.

Indexes: The Efficient Librarians of LangChain

In the vast digital library that is LangChain, Indexes serve as the knowledgeable librarian. They organize and retrieve information efficiently, working behind the scenes to ensure your interaction with LangChain is smooth and productive.

Imagine you're in a library with thousands of books, and you're looking for a specific one. Without a librarian or an index system, it would be a daunting task. Similarly, LangChain deals with a massive amount of data, and without indexes, finding specific information would be like finding a needle in a haystack.

Indexes in LangChain structure documents so that LLMs can interact with them effectively. This interaction often involves a "retrieval" step, akin to asking the librarian for a specific book. The librarian, or in this case, the index, returns the most relevant documents based on your query.

But how does this 'librarian' know which document is the most relevant? That’s where our Vector Databases come into play again. VectorDBs power the indexes, enabling them to understand and retrieve the most relevant documents. It's like having a super-smart librarian who not only knows where every book is but also understands the content of each book to give you the best recommendation.

Indexes in LangChain are not just a single entity but a system made up of several components, each playing a unique role. Let’s break down some of the specifics:

Document Loaders:

Think of Document Loaders as the delivery trucks that bring new books to our library. They are responsible for loading documents from various sources into LangChain. Whether the data comes from a local file, a database, or an online source, Document Loaders ensure that it's properly loaded into the system, ready to be indexed and retrieved when needed.

Text Splitters:

Once the books (documents) are in the library, they need to be organized in a way that makes sense. That's where Text Splitters come in. They break down large chunks of text into smaller, more manageable pieces. This is akin to dividing a long novel into chapters, making it easier to find specific sections.

The reason for this division is due to the context window of language models. Language models can only consider a certain amount of text at a time, known as the context window. By splitting the text into smaller pieces, we ensure that the language model can effectively process the information within its context window, thereby maximizing the accuracy and relevance of the results.

VectorStores:

VectorStores is just a technical name for Vector Databases as we chatted about earlier. They are the heart of the indexing system in LangChain. They are the most common type of index, which relies on embeddings. Remember when we discusses text being converted into a numerical value earlier? That’s what embedding is.

If our library analogy still holds, think of embeddings in VectorStores as the unique code assigned to each book. This code, or numerical value, represents the content of the document, making it easier to match with relevant queries.

Retrievers:

Finally, we have Retrievers, the librarians of our digital library. They are the interface used for fetching relevant documents to combine with language models. When a query comes in, the Retriever sifts through the indexes, finds the most relevant documents, and presents them for further processing by the language model.

In the real world, let's say you're a Customer Success Lead, and you're using LangChain to analyze customer feedback. The feedback is vast and varied, just like the books in a library. With the help of indexes, you can quickly retrieve specific feedback related to a particular feature of your product. This efficient retrieval of information allows you to gain insights faster and make data-driven decisions.

To wrap things up, all you need to know is that Indexes in LangChain are a powerful tool that structures and retrieves data efficiently. They are the unsung heroes that ensure your interaction with LangChain is smooth, efficient, and productive.

Memory: The Continuity Keeper of LangChain

In the world of LangChain, Memory plays a pivotal role in creating a seamless and interactive experience. Just like human memory, LangChain's memory is responsible for retaining and recalling information.

Imagine you're having a conversation with a friend. You don't start each sentence from scratch, forgetting everything that was said before. Instead, you remember the previous parts of the conversation and respond accordingly. This is similar to how memory works in LangChain.

But here's the subtle twist - LangChain's memory is divided into two types: Short Term and Long Term.

Short-Term Memory:

Short-Term Memory is like a notepad where you jot down things you need to remember for a short while. In LangChain, short-term memory keeps track of the current conversation. It's like remembering what was said a few moments ago in a conversation, which is crucial for understanding the context and responding appropriately.

Long-Term Memory:

Long-Term Memory is like a vast library of information that's been gathered over a long period. In LangChain, long-term memory is used to remember interactions between the AI and the user across different conversations. It's like remembering a friend's preferences or past experiences, which can be used to provide more personalized and relevant responses.

Indexes vs. Memory: The Catalog System vs. The Continuity Keeper

Now, you might be wondering, how is Memory different from the concept of Indexes or Vector Databases we discussed earlier? Here’s the distinction:

  • Indexes and vector databases are like the library's catalog system - they help in organizing and retrieving documents efficiently. They don't remember the sequence of interactions but focus on finding the most relevant response for the current query.
  • Memory, on the other hand, is like the librarian's knowledge - it helps in remembering past interactions and providing context for future conversations. In other words, understanding the context based on what has been said or asked before.

In the real world, suppose you're a Product Manager using LangChain for product development. You're in the process of gathering user feedback to improve your product. With LangChain's memory feature, the AI can remember past interactions with each user, understand the context of their feedback based on their usage history, and provide personalized responses to their suggestions or concerns. This allows you to gain deeper insights into user needs and preferences, helping you make more informed decisions about product improvements.

Chains: The Master Orchestrators of LangChain

Chains bring together all the different components discussed so far to create an interaction that generates meaningful and relevant responses from the language models. They serve as the master orchestrators, helping to create a cohesive and usable system.

You've already been introduced to several concepts that form the building blocks of Chains, including Chain of Thought, Action Plan Generation, ReAct, Self-ask, and Prompt Chaining. Now, let's dive even deeper.

A Chain is like a production line in a factory, where each component performs a specific task, and the final product is the result of all these tasks being performed in a particular sequence. There are two common types of chains, LLMChain and Index-related Chains.

LLMChain

By far, the most common type of Chain is an LLMChain. It combines a lot of the ingredients we’ve discussed so far which include:

  • A PromptTemplate: Prepares the raw materials (the input)
  • A Model (either an LLM or a ChatModel): Processes these materials, and
  • Guardrails (optional): The quality checks that ensure the final product meets the required standards.

You can think of LLMChains as pretty much the default way you’d typically be interacting and engaging with models.

Depending on your use case, you’ll also come across Index-related Chains as well. These chains are specifically designed for interacting with Indexes, combining your own data (stored in the Indexes) with LLMs. A prime use case is querying your own documents (much like we do with HelpHub).

There are four main types of Index-related chains in LangChain, each with its unique approach to handling multiple documents. Let's break them down:

  1. Stuffing: This is the simplest method, where all the related data is “stuffed” into the prompt as context to pass to the language model.

    Pros: Only makes a single call to the LLM. When generating text, the LLM has access to all the data at once.
    Cons: Most LLMs have a context length, and for large documents (or many documents) this will not work as it will result in a prompt larger than the context length.
  2. Map Reduce: This method involves running an initial prompt on each chunk of data (for summarization tasks, this could be a summary of that chunk; for question-answering tasks, it could be an answer based solely on that chunk). Then a different prompt is run to combine all the initial outputs.

    Pros: Can scale to larger documents (and more documents). The calls to the LLM on individual documents are independent and can therefore be carried out at the same time.
    Cons: Requires many more calls to the LLM than Stuffing. Loses some information during the final combined call.
  3. Refine: This method involves running an initial prompt on the first chunk of data, generating some output. For the remaining documents, that output is passed in, along with the next document, asking the LLM to refine the output based on the new document.

    Pros: Can pull in more relevant context, and may be more accurate than Map Reduce.
    Cons: Requires many more calls to the LLM than Stuffing. The calls are also not independent, meaning they cannot be carried out at the same time as Map Reduce. There are also some potential rules you need to follow on the ordering of the documents.
  4. Map-Rerank: This method involves running an initial prompt on each chunk of data, that not only tries to complete a task but also gives a score for how certain it is in its answer. The responses are then ranked according to this score, and the highest score is returned back to you.

    Pros: Similar pros as Map Reduce, though requires fewer calls compared to Map Reduce too.
    Cons: Cannot combine information between documents. This means it is most useful when you expect there to be a single simple answer in a single document.

These chains offer a range of options to handle multiple documents, catering to different needs and scenarios. Whether you're dealing with a small amount of data or a large document corpus, LangChain's Index-related chains provide a flexible and efficient way to interact with your data and LLMs.

Agents: The Decision-Makers in LangChain

Ever seen Terminator? Heard of Skynet? Worried AI might take over the world? Well, buckle up, because you’re about to learn what goes on underneath the hood of these AI systems to give credibility to those Sci-Fi fantasies.

Agents are the autonomous entities in LangChain that act on behalf of users, interacting with other agents and tools within the ecosystem. They are the decision-makers, choosing which tools to leverage based on user inputs and objectives. This adaptability and on-the-fly intelligence make agents a crucial component in handling complex, multi-step tasks.

Roles and Types of Agents

The role of Agents in LangChain is like the conductors of an orchestra, coordinating the various components to create a harmonious interaction. They are responsible for interpreting user inputs, deciding which tools to use, and managing the flow of information within the system. This adaptability and intelligence make agents a crucial component in handling complex, multi-step tasks.

LangChain supports two main types of agents: Action Agents and Plan-and-Execute Agents.

  • Action Agents are more conventional and good for small tasks. They are like the sprinters in a race, quick and focused on a single task
  • Plan-and-Execute Agents are more suited to complex or long-running tasks. They handle the initial planning step, which helps to maintain long-term objectives and focus. They are like marathon runners, strategic and endurance-focused.

In real-world scenarios, agents can be used in various ways. For instance, a Product Manager might use an agent to analyze customer feedback and generate insights. The agent could decide to use a sentiment analysis tool to understand the overall sentiment of the feedback, and then use a summarization tool to condense the feedback into key points.

Similarly, a Designer might use an agent to generate design ideas based on user inputs. The agent could decide to use a brainstorming tool to generate a list of ideas, and then use a ranking tool to prioritize the ideas based on certain criteria.

The Future of Agents: The Sky(net) is the Limit

As LangChain continues to evolve, the role of agents is expected to become even more significant. With advancements in AI and machine learning, agents could become capable of making more complex decisions, handling more sophisticated tasks, and providing more personalized and relevant responses. So, while we're not quite at the Skynet level yet, the future of agents in LangChain is certainly exciting and full of potential.

Enjoy Video More Than Reading?

If you’re more of a visual learner and want to soak in the ultimate video series on LangChain fundamentals, you're going to want to check out Greg Kamradt's amazing LangChain Fundamentals series on YouTube:

Conclusion: The Power of LangChain

As we wrap up this guide, it's clear that LangChain is more than just a tool—it's a revolutionary framework that's transforming the way we interact with language models. It's about harnessing the power of advanced language models like GPT-4 and making them accessible, adaptable, and highly functional.

Throughout this guide, we've explored the core concepts of LangChain, from Chains and Agents to the unique prompting techniques that guide the conversation with the model. We've seen how LangChain enables the creation of complex and dynamic language applications, capable of handling a wide range of use cases.

But the true power of LangChain lies in its versatility. Whether you're a product marketer looking to generate new product features, a data analyst seeking insights from large datasets, or a developer building a chatbot, LangChain provides the tools and framework to make it happen.

For technical and non-technical product people, LangChain opens up a world of possibilities. It's about taking the complexity out of interacting with language models and making it as simple as crafting a prompt. And with the ability to chain together multiple prompts and tools, you can create nuanced and sophisticated interactions that push the boundaries of what's possible with language models.

So, as we conclude this guide, we encourage you to explore LangChain and its potential. Dive into the concepts, experiment with the tools, and see for yourself how LangChain can transform the way you interact with language models. The future of language applications is here, and it's powered by LangChain.

Copy icon
X logo
LinkedIn logo

For more SaaS-relevant AI tactics 👇
获取更多 SaaS 相关 AI 策略 👇

Our stories 🤝 your inbox
我们的故事 🤝 您的收件箱

Email Icon

Up Next   接下来

Why are AI chatbots designed to sound like people?
为什么 AI 聊天机器人被设计得像人类一样说话?

For more SaaS-relevant AI tactics 👇

Weekly digest in your inbox.

Email Icon
Continue Reading

Up Next

Selecting keyboard shortcuts for your app

Read Now

What's hot