LangChain — the LLM framework everyone is talking about! LangChain - 人人都在談論的LLM框架!

LangChain is a powerful framework that simplifies the process of building advanced language model applications.
LangChain 是一個強大的框架,可以簡化建立高級語言模型應用程式的過程。

Hey there! Let me introduce you to LangChain, an awesome library that empowers developers to build powerful applications using large language models (LLMs) and other computational resources. In this guide, I’ll give you a quick rundown on how LangChain works and explore some cool use cases, like question-answering, chatbots, and agents. I’ll also walk you through a quick-start guide to help you get going. Let’s begin!
嗨!讓我向你介紹LangChain,這是一個很棒的庫,它可以讓開發人員使用大型語言模型(LLMs)和其他計算資源來構建強大的應用程序。在這個指南中,我將為你快速介紹LangChain的工作原理,並探索一些很酷的用例,例如問答、聊天機器人和代理人。我還會帶你通過快速入門指南,幫助你開始使用。讓我們開始吧!

What is LangChain?LangChain 是什麼?

LangChain is a powerful framework designed to help developers build end-to-end applications using language models. It offers a suite of tools, components, and interfaces that simplify the process of creating applications powered by large language models (LLMs) and chat models.
LangChain 是一個強大的框架,旨在幫助開發人員使用語言模型構建端到端應用程序。它提供了一套工具、組件和接口,簡化了創建由大型語言模型(LLMs)和聊天模型驅動的應用程序的過程。
LangChain makes it easy to manage interactions with language models, chain together multiple components, and integrate additional resources, such as APIs and databases.
LangChain 讓管理與語言模型的互動、串接多個元件以及整合其他資源(例如 API 和資料庫)變得容易。

LangChain has a number of core concepts. Let’s go through them one by one:
LangChain 有一些核心概念。讓我們逐一了解它們:

1. Components and Chains1. 元件與鏈結

In LangChain, components are modular building blocks that can be combined to create powerful applications. A chain is a sequence of components (or other chains) put together to accomplish a specific task.
在LangChain中,元件是可組合成強大應用程式的模組化建構塊。鏈是一系列元件(或其他鏈)的組合,用於完成特定任務。
For example, a chain might include a prompt template, a language model, and an output parser, all working together to handle user input, generate a response, and process the output.
例如,一個鏈可能包括提示模板、語言模型和輸出解析器,它們共同工作以處理用戶輸入、生成回應和處理輸出。

2. Prompt Templates and Values2. 提示範本和值

A Prompt Template is responsible for creating a PromptValue, which is what’s eventually passed to the language model. Prompt Templates help convert user input and other dynamic information into a format suitable for the language model.
提示範本負責創建提示值(PromptValue),最終傳遞給語言模型。提示範本有助於將用戶輸入和其他動態信息轉換為適合語言模型的格式。
PromptValues are classes with methods that can be converted to the exact input types each model type expects (like text or chat messages).
PromptValues 是具有方法的類別,可以轉換為每個模型類型所期望的精確輸入類型(例如文本或聊天訊息)。

3. Example Selectors3. 範例選擇器

Example Selectors are useful when you want to include examples in a prompt dynamically. They take user input and return a list of examples to use in the prompt, making it more powerful and context-specific.
當您想要動態地在提示中包含範例時,示例選擇器非常有用。它們接受使用者輸入並返回一個範例清單以在提示中使用,使其更加強大和上下文特定。

4. Output Parsers4. 輸出解析器

Output Parsers are responsible for structuring language model responses into a more usable format. They implement two main methods: one for providing formatting instructions and another for parsing the language model’s response into a structured format.
輸出解析器負責將語言模型的回應結構化為更可用的格式。它們實現了兩種主要方法:一種用於提供格式化指令,另一種用於將語言模型的回應解析為結構化格式。
This makes it easier to work with the output data in your application.
這使得在您的應用程式中處理輸出數據更加容易。

5. Indexes and Retrievers5. 索引和檢索程式

Indexes are a way to organize documents to make it easier for language models to interact with them. Retrievers are interfaces for fetching relevant documents and combining them with language models.
索引是一種組織文件的方式,使語言模型更容易與它們互動。檢索器是檢索相關文件並將其與語言模型結合的接口。
LangChain provides tools and functionality for working with different types of indexes and retrievers, like vector databases and text splitters.
LangChain 提供了與不同類型的索引和檢索器(如向量數據庫和文本分割器)一起工作的工具和功能。

6. Chat Message History6. 聊天訊息歷史紀錄

LangChain primarily interacts with language models through a chat interface. The ChatMessageHistory class is responsible for remembering all previous chat interactions, which can then be passed back to the model, summarized, or combined in other ways.
LangChain 主要透過聊天介面與語言模型互動。ChatMessageHistory 類別負責記錄所有先前的聊天互動,這些互動可以傳回模型、總結或以其他方式結合。
This helps maintain context and improves the model’s understanding of the conversation.
這有助於維持上下文並提高模型對對話的理解。

7. Agents and Toolkits7. 代理人和工具包

Agents are entities that drive decision-making in LangChain. They have access to a suite of tools and can decide which tool to call based on user input. Toolkits are sets of tools that, when used together, can accomplish a specific task.
代理是在LangChain中推動決策的實體。他們可以使用一系列工具,並根據用戶輸入決定要調用哪個工具。工具包是一組工具,當它們一起使用時,可以完成特定的任務。
The Agent Executor is responsible for running agents with the appropriate tools.
代理執行者負責使用適當的工具運行代理。

By understanding and leveraging these core concepts, you can harness the power of LangChain to build advanced language model applications that are adaptable, efficient, and capable of handling complex use cases.
通過理解和利用這些核心概念,您可以利用 LangChain 的威力來建立先進的語言模型應用程式,這些應用程式具有適應性、高效性和處理複雜用例的能力。

What is a LangChain Agent?什麼是LangChain代理?

A LangChain Agent is an entity that drives decision-making in the framework. It has access to a set of tools and can decide which tool to call based on the user’s input. Agents help build complex applications that require adaptive and context-specific responses.
LangChain 代理是在框架中推動決策的實體。它可以訪問一組工具,並根據用戶的輸入決定要調用哪個工具。代理幫助構建需要適應性和上下文特定響應的複雜應用程序。
They are especially useful when there’s an unknown chain of interactions that depend on the user’s input and other factors.
當存在一個依賴於使用者輸入和其他因素的未知交互鏈時,它們尤其有用。

How would someone use LangChain?有人如何使用LangChain?

To use LangChain, a developer would start by importing the necessary components and tools, such as LLMs, chat models, agents, chains, and memory features. These components are combined to create an application that can understand, process, and respond to user inputs.
使用LangChain,開發人員會先匯入必要的元件和工具,例如LLMs、聊天模型、代理、鏈和記憶功能。這些元件結合在一起,創建一個應用程式,可以理解、處理和回應使用者輸入。

LangChain provides a variety of components for specific use cases, such as personal assistants, question answering over documents, chatbots, querying tabular data, interacting with APIs, extraction, evaluation, and summarization.
LangChain 提供各種針對特定用例的元件,例如個人助理、文件問答、聊天機器人、查詢表格數據、與 API 互動、提取、評估和摘要。

What’s a LangChain model?LangChain 模型是什麼?

A LangChain model is an abstraction that represents different types of models used in the framework. There are three main types of models in LangChain:
LangChain 模型是一種抽象,代表框架中使用的不同類型的模型。LangChain 中有三種主要類型的模型:

  1. LLMs (Large Language Models): These models take a text string as input and return a text string as output. They are the backbone of many language model applications.
    LLMs(大型語言模型):這些模型將文本字符串作為輸入並返回文本字符串作為輸出。它們是許多語言模型應用程序的支柱。
  2. Chat Models: Chat Models are backed by a language model but have a more structured API. They take a list of Chat Messages as input and return a Chat Message. This makes it easy to manage conversation history and maintain context.
    聊天模型:聊天模型由語言模型支持,但具有更結構化的 API。它們以聊天訊息列表作為輸入,並返回聊天訊息。這使得管理對話歷史和維護上下文變得容易。
  3. Text Embedding Models: These models take text as input and return a list of floats representing the text’s embeddings. These embeddings can be used for tasks like document retrieval, clustering, and similarity comparisons.
    文本嵌入模型:這些模型以文本作為輸入,並返回表示文本嵌入的浮點數列表。這些嵌入可以用於文檔檢索、聚類和相似性比較等任務。

Developers can choose the appropriate LangChain model for their use case and leverage the provided components to build their applications.
開發人員可以根據其使用情境選擇適當的LangChain模型,並利用提供的元件來建立其應用程式。

Key Features of LangChainLangChain 的主要特點:

LangChain is designed to support developers in six main areas:
LangChain旨在支援開發人員在六個主要領域:

  1. LLMs and Prompts: LangChain makes it easy to manage prompts, optimize them, and create a universal interface for all LLMs. Plus, it includes some handy utilities for working with LLMs.
    LLMs和Prompts:LangChain使得管理提示、優化它們和為所有LLMs創建通用界面變得容易。此外,它還包括一些方便的工具,可用於處理LLMs。
  2. Chains: These are sequences of calls to LLMs or other utilities. LangChain provides a standard interface for chains, integrates with various tools, and offers end-to-end chains for popular applications.
    鏈:這些是對LLM或其他工具的調用序列。LangChain為鏈提供了標準接口,與各種工具集成,並為流行應用程序提供端到端鏈。
  3. Data Augmented Generation: LangChain enables chains to interact with external data sources to gather data for the generation step. For example, it can help with summarizing long texts or answering questions using specific data sources.
    數據增強生成:LangChain 可讓鏈與外部數據源互動,以收集生成步驟所需的數據。例如,它可以幫助總結長文本或使用特定數據源回答問題。
  4. Agents: An agent lets an LLM make decisions about actions, take those actions, check the results, and keep going until the job’s done. LangChain provides a standard interface for agents, a variety of agents to choose from, and examples of end-to-end agents.
    代理人:代理人讓LLM做出行動決策、執行這些行動、檢查結果,並持續進行直到完成工作。LangChain為代理人提供了標準介面、多種可供選擇的代理人,以及端到端代理人的範例。
  5. Memory: LangChain has a standard interface for memory, which helps maintain state between chain or agent calls. It also offers a range of memory implementations and examples of chains or agents that use memory.
    記憶:LangChain 具有標準的記憶介面,有助於在鏈或代理呼叫之間維護狀態。它還提供了一系列的記憶實現和使用記憶的鏈或代理的示例。
  6. Evaluation: It’s tough to evaluate generative models with traditional metrics. That’s why LangChain provides prompts and chains to help developers assess their models using LLMs themselves.
    評估:使用傳統指標評估生成模型是很困難的。這就是為什麼LangChain提供提示和鏈接,以幫助開發人員使用LLM自行評估其模型的原因。

Use Cases

LangChain supports a bunch of use cases, like:
LangChain 支援許多使用案例,例如:

  • Question Answering over specific documents: Answer questions based on given documents, using the info in those documents to create answers.
    在特定文件上的問答:根據給定的文件回答問題,利用這些文件中的信息創建答案。
  • Chatbots: Build chatbots that can produce text by leveraging LLMs’ capabilities.
    聊天機器人:利用LLM的能力建立能夠產生文字的聊天機器人。
  • Agents: Develop agents that can decide on actions, take those actions, observe the results, and keep going until they’re done.
    代理人:開發能夠決定行動、執行這些行動、觀察結果並持續進行直到完成的代理人。

Quickstart Guide: Building an End-to-End Language Model Application with LangChain
快速入門指南:使用LangChain構建端到端語言模型應用程序

Installation

First, let’s get LangChain installed. Just run the following command:
首先,讓我們安裝LangChain。只需運行以下命令:

pip install langchain

Environment Setup

Now, since LangChain often needs to integrate with model providers, data stores, APIs, and more, we’ll set up our environment. In this example, we’re going to use OpenAI’s APIs, so we need to install their SDK:
現在,由於LangChain經常需要與模型提供者、數據存儲庫、API等進行集成,因此我們將設置我們的環境。在這個例子中,我們將使用OpenAI的API,因此我們需要安裝他們的SDK:

pip install openai

Next, let’s set up the environment variable in the terminal:
接下來,讓我們在終端機中設置環境變數:

export OPENAI_API_KEY="..."

Or, if you prefer to work inside a Jupyter notebook or Python script, you can set the environment variable like this:
或者,如果您喜歡在Jupyter筆記本或Python腳本中工作,您可以像這樣設置環境變量:

import os
os.environ["OPENAI_API_KEY"] = "..."

Building a Language Model Application: LLMs
建立語言模型應用程式:LLMs

With LangChain installed and the environment set up, we’re ready to start building our language model application. LangChain provides a bunch of modules that you can use to create language model applications.
安裝了LangChain並設置好環境後,我們準備開始建立語言模型應用程序。LangChain提供了一堆模組,您可以使用這些模組來創建語言模型應用程序。
You can combine these modules for more complex applications or use them individually for simpler ones.
您可以將這些模組結合使用以建立更複雜的應用程式,或是單獨使用以建立更簡單的應用程式。

Building a Language Model Application: Chat Models
建立語言模型應用程式:聊天模型

In addition to LLMs, you can also work with chat models. These are a variation of language models that use language models under the hood but have a different interface. Instead of a “text in, text out” API, chat models work with chat messages as inputs and outputs.
除了LLM之外,您還可以使用聊天模型進行工作。這些是語言模型的變體,使用語言模型作為核心,但具有不同的界面。聊天模型不使用“文本輸入,文本輸出”API,而是使用聊天消息作為輸入和輸出。
Chat model APIs are pretty new, so everyone is still figuring out the best abstractions.
聊天模型的 API 相當新,因此每個人仍在尋找最佳的抽象化方法。

To get chat completions, you’ll need to pass one or more messages to the chat model. LangChain currently supports AIMessage, HumanMessage, SystemMessage, and ChatMessage types. You’ll mostly work with HumanMessage, AIMessage, and SystemMessage.
為了獲得聊天完成度,您需要將一個或多個訊息傳遞給聊天模型。LangChain目前支援AIMessage、HumanMessage、SystemMessage和ChatMessage類型。您將主要使用HumanMessage、AIMessage和SystemMessage。

Here’s an example of using chat models:
這裡是使用聊天模型的範例:

from langchain.chat_models import ChatOpenAI
from langchain.schema import (
AIMessage,
HumanMessage,
SystemMessage
)
chat = ChatOpenAI(temperature=0)

You can get completions by passing in a single message:
您可以通過傳遞單個消息來獲取完成。

chat([HumanMessage(content="Translate this sentence from English to French. I love programming.")])
# -> AIMessage(content="J'aime programmer.", additional_kwargs={})

Or pass in multiple messages for OpenAI’s gpt-3.5-turbo and gpt-4 models:
或者傳入多個訊息給OpenAI的gpt-3.5-turbo和gpt-4模型:

messages = [
SystemMessage(content="You are a helpful assistant that translates English to French."),
HumanMessage(content="Translate this sentence from English to French. I love programming.")
]
chat(messages)
# -> AIMessage(content="J'aime programmer.", additional_kwargs={})

You can also generate completions for multiple sets of messages using generate. This returns an LLMResult with an additional message parameter:
您也可以使用 generate 為多組訊息生成完成項。這會返回一個帶有額外訊息參數的 LLMResult:

batch_messages = [
[
SystemMessage(content="You are a helpful assistant that translates English to French."),
HumanMessage(content="Translate this sentence from English to French. I love programming.")
],
[
SystemMessage(content="You are a helpful assistant that translates English to French."),
HumanMessage(content="Translate this sentence from English to French. I love artificial intelligence.")
],
]
result = chat.generate(batch_messages)
result
# -> LLMResult(generations=[[ChatGeneration(text="J'aime programmer.", generation_info=None, message=AIMessage(content="J'aime programmer.", additional_kwargs={}))], [ChatGeneration(text="J'aime l'intelligence artificielle.", generation_info=None, message=AIMessage(content="J'aime l'intelligence artificielle.", additional_kwargs={}))]], llm_output={'token_usage': {'prompt_tokens': 71, 'completion_tokens': 18, 'total_tokens': 89}})

And you can extract information like token usage from the LLMResult:
你可以從 LLMResult 中提取像是 token 使用情況等資訊:

result.llm_output['token_usage']
# -> {'prompt_tokens': 71, 'completion_tokens': 18, 'total_tokens': 89}

With chat models, you can also use templating by employing a MessagePromptTemplate. You can create a ChatPromptTemplate from one or more MessagePromptTemplates. The format_prompt method of ChatPromptTemplate returns a PromptValue, which you can convert to a string or Message object, depending on whether you want to use the formatted value as input to an LLM or chat model.
使用聊天模型時,您也可以使用模板,方法是使用 MessagePromptTemplate。您可以從一個或多個 MessagePromptTemplate 建立 ChatPromptTemplate。ChatPromptTemplate 的 format_prompt 方法會返回 PromptValue,您可以將其轉換為字符串或 Message 物件,具體取決於您是否想將格式化值用作 LLM 或聊天模型的輸入。

Here’s an example:這裡有一個例子:

from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
)
chat = ChatOpenAI(temperature=0)template="You are a helpful assistant that translates {input_language} to {output_language}."
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
human_template="{text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])# get a chat completion from the formatted messages
chat(chat_prompt.format_prompt(input_language="English", output_language="French", text="I love programming.").to_messages())
# -> AIMessage(content="J'aime programmer.", additional_kwargs={})

You can use the LLMChain with chat models as well:
您也可以將LLMChain與聊天模型一起使用:

from langchain.chat_models import ChatOpenAI
from langchain import LLMChain
from langchain.prompts.chat import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
)
chat = ChatOpenAI(temperature=0)template="You are a helpful assistant that translates {input_language} to {output_language}."
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
human_template="{text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])chain = LLMChain(llm=chat, prompt=chat_prompt)
chain.run(input_language="English", output_language="French", text="I love programming.")
# -> "J'aime programmer."

You can also use agents with chat models. Initialize an agent using AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION as the agent type:
您也可以在聊天模型中使用代理。使用 AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION 作為代理類型來初始化代理:

from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.chat_models import ChatOpenAI
from langchain.llms import OpenAI
# First, let's load the language model we're going to use to control the agent.
chat = ChatOpenAI(temperature=0)# Next, let's load some tools to use. Note that the `llm-math` tool uses an LLM, so we need to pass that in.
llm = OpenAI(temperature=0)
tools = load_tools(["serpapi", "llm-math"], llm=llm)# Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use.
agent = initialize_agent(tools, chat, agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True)# Now let's test it out!
agent.run("Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?")

In this example, the agent will interactively perform a search and calculation to provide the final answer.
在這個例子中,代理人將互動地進行搜索和計算,以提供最終答案。

Finally, let’s explore using Memory with chains and agents initialized with chat models. The main difference between this and Memory for LLMs is that we can keep previous messages as their own unique memory objects, rather than condensing them into a single string.
最後,讓我們探索使用具有聊天模型初始化的鏈和代理的記憶體。這與 LLM 的記憶體的主要區別在於,我們可以將先前的訊息保留為它們自己獨特的記憶體物件,而不是將它們壓縮成單一字串。

Here’s an example of using a ConversationChain:
這裡是使用 ConversationChain 的範例:

from langchain.prompts import (
ChatPromptTemplate,
MessagesPlaceholder,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate
)
from langchain.chains import ConversationChain
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferMemory
prompt = ChatPromptTemplate.from_messages([
SystemMessagePromptTemplate.from_template("The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know."),
MessagesPlaceholder(variable_name="history"),
HumanMessagePromptTemplate.from_template("{input}")
])llm = ChatOpenAI(temperature=0)
memory = ConversationBufferMemory(return_messages=True)
conversation = ConversationChain(memory=memory, prompt=prompt, llm=llm)conversation.predict(input="Hi there!")
# -> 'Hello! How can I assist you today?'conversation.predict(input="I'm doing well! Just having a conversation with an AI.")
# -> "That sounds like fun! I'm happy to chat with you. Is there anything specific you'd like to talk about?"conversation.predict(input="Tell me about yourself.")
# -> "Sure! I am an AI language model created by OpenAI. I was trained on a large dataset of text from the internet, which allows me to understand and generate human-like language. I can answer questions, provide information, and even have conversations like this one. Is there anything else you'd like to know about me?"

In this example, we used a ConversationChain to maintain the context of the conversation across multiple interactions with the AI.
在這個例子中,我們使用了一個 ConversationChain 來在多次與 AI 的互動中維持對話的上下文。

That’s it! Now you have a solid understanding of how to use LangChain to build end-to-end language model applications. By following these examples, you can develop powerful language model applications using LLMs, chat models, agents, chains, and memory features.
這就是了!現在你已經對如何使用LangChain來建立端到端的語言模型應用程式有了堅實的理解。通過遵循這些示例,你可以使用LLMs、聊天模型、代理、鏈和記憶功能開發強大的語言模型應用程式。

Conclusion

In conclusion, LangChain is a powerful framework that simplifies the process of building advanced language model applications by providing a modular and flexible approach.
總之,LangChain 是一個強大的框架,通過提供模塊化和靈活的方法,簡化了構建高級語言模型應用程序的過程。
By understanding the core concepts, such as components, chains, prompt templates, output parsers, indexes, retrievers, chat message history, and agents, you can create custom solutions tailored to your specific needs.
通過理解核心概念,例如組件、鏈、提示模板、輸出解析器、索引、檢索器、聊天消息歷史和代理,您可以創建定制的解決方案,以滿足您的特定需求。
LangChain’s adaptability and ease of use make it an invaluable tool for developers, enabling them to unlock the full potential of language models and create intelligent, context-aware applications across a wide range of use cases.
LangChain的適應性和易用性使其成為開發人員的寶貴工具,使他們能夠發揮語言模型的全部潛力,並在各種用例中創建智能、上下文感知的應用程序。

Thanks for being a part of our community! Before you go:
感謝您成為我們社群的一份子!在您離開之前:

  • 👏 Clap for the story and follow the author 👉
    👏 為故事鼓掌,並關注作者 👉
  • 📰 View more content in the Level Up Coding publication
    📰 在「Level Up Coding」出版物中查看更多內容。
  • 💰 Free coding interview course ⇒ View Course
    💰 免費編碼面試課程 ⇒ 查看課程
  • 🔔 Follow us: Twitter | LinkedIn | Newsletter
    🔔 跟隨我們:Twitter | LinkedIn | 時事通訊

🚀👉 Join the Level Up talent collective and find an amazing job
🚀👉 加入 Level Up 人才集團,找到一份令人驚艷的工作