Introduction to LangChain – LangChain 简介

最后修改: 2023年 10月 31日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.导言

In this tutorial, we’ll examine the details of LangChain, a framework for developing applications powered by language models. We’ll begin by gathering basic concepts around the language models that will help in this tutorial.

在本教程中,我们将研究 LangChain 的细节,这是一个用于开发由语言模型驱动的应用程序的框架。首先,我们将收集有关语言模型的基本概念,这些概念将对本教程有所帮助。

Although LangChain is primarily available in Python and JavaScript/TypeScript versions, there are options to use LangChain in Java. We’ll discuss the building blocks of LangChain as a framework and then proceed to experiment with them in Java.

尽管 LangChain 主要有 Python 和 JavaScript/TypeScript 版本,但也有在 Java 中使用 LangChain 的选项。我们将讨论作为框架的 LangChain 的构建模块,然后在 Java 中进行实验。

2. Background

2. 背景

Before we go deeper into why we need a framework for building applications powered by language models, it’s imperative that we first understand what language models are. We’ll also cover some of the typical complexities encountered when working with language models.

在深入探讨为什么我们需要一个框架来构建由语言模型驱动的应用程序之前,我们首先必须了解什么是语言模型。我们还将介绍在使用语言模型时遇到的一些典型复杂问题。

2.1. Large Language Models

2.1.大型语言模型

A language model is a probabilistic model of a natural language that can generate probabilities of a series of words. A large language model (LLM) is a language model characterized by its large size. They’re artificial neural networks with possibly billions of parameters.

语言模型是一种自然语言的概率模型,可以生成一系列单词的概率。大型语言模型 (LLM) 是一种以大型为特征的语言模型。它们是可能拥有数十亿参数的人工神经网络。

An LLM is often pre-trained on a vast amount of unlabeled data using self-supervised and semi-supervised learning techniques. Then, the pre-trained model is adapted for specific tasks using various techniques like fine-tuning and prompt engineering:

LLM 通常使用自我监督半监督学习技术,在大量无标记数据上进行预训练。然后,使用微调和提示工程等各种技术对预训练模型进行调整,使其适用于特定任务:

Large Language Model

These LLMs are capable of performing several natural language processing tasks like language translation and content summarization. They’re also capable of generative tasks like content creation. Hence, they can be extremely valuable in applications like answering questions.

这些 LLM 能够执行多种自然语言处理任务,如语言翻译和内容摘要。它们还能够执行生成任务,如内容创建。因此,它们在回答问题等应用中极具价值。

Almost all major cloud service providers have included large language models in their service offerings. For instance, Microsoft Azure offers LLMs like Llama 2 and OpenAI GPT-4. Amazon Bedrock offers models from AI21 Labs, Anthropic, Cohere, Meta, and Stability AI.

几乎所有主要云服务提供商都在其服务产品中包含了大型语言模型。例如,Microsoft Azure 提供了 LLM,如 Llama 2 和 OpenAI GPT-4。Amazon Bedrock 提供来自 AI21 Labs、Anthropic、Cohere、Meta 和 Stability AI 的模型。

2.2. Prompt Engineering

2.2.快速工程

LLMs are foundation models trained on a massive set of text data. Hence, they can capture the syntax and semantics inherent to human languages. However, they must be adapted to perform specific tasks that we want them to perform.

LLM 是在大量文本数据基础上训练出来的基础模型。因此,它们可以捕捉人类语言固有的语法和语义。但是,它们必须进行调整,以执行我们希望它们执行的特定任务

Prompt engineering is one of the quickest ways to adapt an LLM. It’s a process of structuring text that can be interpreted and understood by an LLM. Here, we use natural language text to describe the task that we expect an LLM to perform:

提示工程是适应 LLM 的最快方法之一。这是一个文本结构化的过程,可以被 LLM 解释和理解。在这里,我们使用自然语言文本来描述我们期望 LLM 执行的任务:

Prompt Engineering

The prompts we create help an LLM perform in-context learning, which is temporary. We can use prompt engineering to promote the safe usage of LLMs and build new capabilities like augmenting LLMs with domain knowledge and external tools.

我们创建的提示有助于 LLM 执行上下文学习,这是临时性的。我们可以使用提示工程来促进 LLM 的安全使用,并构建新的功能,例如利用领域知识和外部工具来增强 LLM。

This is an active area of research, and new techniques keep surfacing now and then. However, techniques like chain-of-thought prompting have already become quite popular. The idea here is for the LLMs to solve a problem as a series of intermediate steps before giving a final answer.

这是一个活跃的研究领域,新技术层出不穷。不过,思维链提示等技术已经相当流行。这种方法的理念是让 LLM 在给出最终答案之前,通过一系列中间步骤来解决问题。

2.3. Word Embeddings

2.3.词嵌入

As we’ve seen, LLMs are capable of processing a large volume of natural language text. The performance of LLMs vastly improves if we represent the words in natural languages as word embeddings. This is a real-valued vector capable of encoding words’ meanings.

正如我们所看到的,LLM 能够处理大量的自然语言文本。如果我们将自然语言中的单词表示为单词嵌入,那么 LLM 的性能将大大提高。这是一个实值向量,能够对单词的含义进行编码

Typically, the word embeddings are generated using an algorithm like Tomáš Mikolov’s Word2vec or Stanford University’s GloVe. The GloVe is an unsupervised learning algorithm trained on aggregated global word-word co-occurrence statistics from a corpus:

通常,词嵌入是通过 Tomáš Mikolov 的 Word2vec 斯坦福大学的 GloVe 等算法生成的。GloVe是一种无监督学习算法,根据语料库中的全局词-词共现统计数据进行训练:

Word Embedding Illustration

In prompt engineering, we convert the prompts into their word embeddings, allowing the model to understand better and respond to the prompt. Further, it’s also very helpful in augmenting the context we provide to the model allowing them to provide more contextual answers.

在 “提示工程 “中,我们将提示转换为单词嵌入,使模型能够更好地理解和响应提示。此外,这还有助于增强我们为模型提供的上下文,使其能够提供更多符合上下文的答案。

For instance, we can generate word embeddings from an existing dataset and store them in a vector database. Further, we can use the user-provided input to perform a semantic search into this vector database. Then we can use the search result as additional context to the model.

例如,我们可以从现有的数据集中生成单词嵌入,并将其存储到向量数据库中。此外,我们还可以使用用户提供的输入在该向量数据库中执行语义搜索。然后,我们可以将搜索结果作为模型的附加上下文。

3. The LLM Tech Stack With LangChain

3.使用 LangChain 的 LLM 技术栈

As we’ve seen already, creating effective prompts is a key element for successfully harnessing the power of LLMs in any application. This includes making the interaction with language models context-aware and being able to rely on a language model to reason.

正如我们已经看到的,创建有效的提示是在任何应用中成功利用 LLM 功能的关键因素。这包括使与语言模型的交互具有上下文感知能力,并能够依靠语言模型进行推理。

For this, we’re required to perform several tasks, like creating templates for prompts, making calls to language models, and providing user-specific data from multiple sources to language models. To make these tasks simpler, we require a framework like LangChain as part of our LLM tech stack:

为此,我们需要执行多项任务,例如为提示创建模板、调用语言模型以及从多个来源向语言模型提供用户特定数据。为了简化这些任务,我们需要一个像 LangChain 这样的框架作为 LLM 技术栈的一部分:

LLM Tech Stack with LangChain

The framework also helps in developing applications that require chaining multiple language models and being able to recall information about past interactions with a language model. Then, there are more complex use cases that involve using a language model as a reasoning engine.

该框架还有助于开发需要连锁多个语言模型和能够调用语言模型过去的交互信息的应用。此外,还有更复杂的使用案例,涉及将语言模型用作推理引擎。

Finally, we can perform logging, monitoring, streaming, and other essential tasks for maintenance and troubleshooting. The LLM tech stack is evolving rapidly to address many of these concerns. However, LangChain is fast becoming a valuable part of LLM tech stacks.

最后,我们可以执行日志记录、监控、流式传输和其他重要任务,以便进行维护和故障排除。LLM 技术栈正在快速发展,以解决上述许多问题。不过,LangChain 正在迅速成为 LLM 技术栈的重要组成部分。

4. LangChain for Java

4.Java 的 LangChain

LangChain was launched in 2022 as an open-source project and soon gathered momentum through community support. It was originally developed by Harrison Chase in Python and soon turned out to be one of the fastest-growing start-ups in the AI space.

LangChain于 2022 年作为开源项目启动,并很快在社区的支持下获得发展。它最初由 Harrison Chase 用 Python 开发,很快就成为人工智能领域发展最快的初创公司之一。

There was a JavaScript/TypeScript version of LangChain that followed the Python version in early 2023. It soon became quite popular and started supporting multiple JavaScript environments like Node.js, web browsers, CloudFlare workers, Vercel/Next.js, Deno, and Supabase Edge functions.

2023 年初,继 Python 版本之后,LangChain 推出了 JavaScript/TypeScript 版本。它很快变得相当流行,并开始支持多种 JavaScript 环境,如 Node.js、Web 浏览器、CloudFlare Worker、Vercel/Next.js、Deno 和 Supabase Edge 函数。

Unfortunately, there is no official Java version of LangChain that is available for Java/Spring applications. However, there is a community version of LangChain for Java called LangChain4j. It works with Java 8 or higher and supports Spring Boot 2 and 3.

遗憾的是,目前还没有适用于 Java/Spring 应用程序的 LangChain 官方 Java 版本。不过,有一个适用于 Java 的 LangChain 社区版本,名为 LangChain4j。它适用于 Java 8 或更高版本,并支持 Spring Boot 2 和 3。

The various dependencies of LangChain are available at Maven Central. We may need to add one or more dependencies in our application, depending on the features we use:

LangChain 的各种依赖可从 Maven Central 获取。我们可能需要在应用程序中添加一个或多个依赖项,这取决于我们使用的功能:

<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j</artifactId>
    <version>0.23.0</version>
</dependency>

For instance, we’ll also require the dependencies that support integration to OpenAI models, provide support for embeddings, and a sentence-transformer model like all-MiniLM-L6-v2 in the following sections of the tutorial.

例如,在本教程的后续章节中,我们还将需要支持与 OpenAI 模型集成、提供支持嵌入像 all-MiniLM-L6-v2 这样的句子转换器模型的依赖项。

With similar design goals to that of LangChain, LangChain4j provides a simple and coherent layer of abstractions along with its numerous implementations. It already supports several language model providers like OpenAI and embedding store providers like Pinecone.

LangChain4j 的设计目标与 LangChain 类似,提供了一个简单而连贯的抽象层以及众多实现。它已经支持多个语言模型提供商(如 OpenAI)和嵌入式存储提供商(如 Pinecone)。

However, since both LangChain and LangChain4j are evolving quickly, there may be features that are supported in the Python or JS/TS version that are not yet there in the Java version. Nevertheless, the fundamental concept, general structure, and vocabulary are largely the same.

不过,由于 LangChain 和 LangChain4j 都在快速发展,Python 或 JS/TS 版本中支持的功能可能在 Java 版本中还不存在。不过,基本概念、一般结构和词汇在很大程度上是相同的。

5. Building Blocks of LangChain

5.LangChain 的构件

LangChain offers several value propositions for our applications available as module components. Modular components provide useful abstractions along with a collection of implementations for working with language models. Let’s discuss some of these modules with examples in Java.

LangChain 以模块组件的形式为我们的应用程序提供了多种价值主张。模块组件提供了有用的抽象概念,以及一系列用于处理语言模型的实现。让我们以 Java 为例,讨论其中的一些模块。

5.1. Models I/O

5.1 输入/输出模型

When working with any language model, we need the ability to interface with it. LangChain provides the necessary building blocks like the ability to templatize prompts and to dynamically select and manage model inputs. Also, we can use output parsers to extract information from model outputs:

在使用任何语言模型时,我们都需要与之接口的能力。LangChain 提供了必要的构建模块,如 模板化提示以及动态选择和管理模型输入的能力。此外,我们还可以使用输出解析器从模型输出中提取信息:

LangChain Model

Prompt templates are pre-defined recipes for generating prompts for language models and may include instructions, few-shot examples, and specific context:

提示模板是为语言模型生成提示的预定义配方,可能包括说明、示例和特定上下文:

PromptTemplate promptTemplate = PromptTemplate
  .from("Tell me a {{adjective}} joke about {{content}}..");
Map<String, Object> variables = new HashMap<>();
variables.put("adjective", "funny");
variables.put("content", "computers");
Prompt prompt = promptTemplate.apply(variables);

Here, we create a prompt template capable of accepting multiple variables. The variables are something we receive from the user input and feed to the prompt template.

在这里,我们创建了一个能够接受多个变量的提示模板。这些变量是我们从用户输入中接收到的,并输入到提示模板中。

LangChain supports integrating with two types of models, language models and chat models. Chat models are also backed by language models but provide chat capabilities:

LangChain 支持与两种模型集成,即语言模型和聊天模型。聊天模型也由语言模型支持,但提供聊天功能:

ChatLanguageModel model = OpenAiChatModel.builder()
  .apiKey(<OPENAI_API_KEY>)
  .modelName(GPT_3_5_TURBO)
  .temperature(0.3)
  .build();
String response = model.generate(prompt.text());

Here, we create a chat model with a particular OpenAI model and the associated API key. We can obtain an API key from OpenAI by registering for free. The parameter temperature is used to control the randomness of the model output.

在这里,我们使用特定的 OpenAI 模型和相关的 API 密钥创建一个聊天模型。我们可以通过免费注册从 OpenAI 获取 API 密钥。参数温度用于控制模型输出的随机性。

Finally, the output from the language models may not be structured enough for presentation. LangChain provides output parsers that help us structure language model responses — for instance, extracting the information from the output as a POJO in Java.

最后,语言模型的输出结构可能不足以进行展示。LangChain 提供了输出解析器,可帮助我们构建语言模型响应,例如,以 Java POJO 的形式从输出中提取信息。

5.2. Memory

5.2.内存

Typically, an application leveraging a LLM has a conversational interface. An important aspect of any conversation is to be able to refer to information introduced earlier in the conversation. The ability to store information about past interactions is called memory:

通常情况下,利用 LLM 的应用程序具有一个对话界面。任何对话的一个重要方面都是能够参考对话早期引入的信息。存储过去交互信息的能力被称为记忆:

LangChain Memory

LangChain offers key enablers for adding memory to an application. For instance, we need the ability to read from the memory to augment the user input. Then, we need the ability to write the inputs and outputs of the current run to the memory:

LangChain 为应用程序添加内存提供了关键的辅助功能。例如,我们需要从内存中读取数据,以增强用户输入。然后,我们需要将当前运行的输入和输出写入内存的能力:

ChatMemory chatMemory = TokenWindowChatMemory
  .withMaxTokens(300, new OpenAiTokenizer(GPT_3_5_TURBO));
chatMemory.add(userMessage("Hello, my name is Kumar"));
AiMessage answer = model.generate(chatMemory.messages()).content();
System.out.println(answer.text()); // Hello Kumar! How can I assist you today?
chatMemory.add(answer);
chatMemory.add(userMessage("What is my name?"));
AiMessage answerWithName = model.generate(chatMemory.messages()).content();
System.out.println(answer.text()); // Your name is Kumar.
chatMemory.add(answerWithName);

Here, we implement a fixed window chat memory with TokenWindowChatMemory, which allows us to read and write chat messages that we exchange with the language model.

在这里,我们使用 TokenWindowChatMemory 实现了一个固定窗口聊天存储器,它允许我们读写与语言模型交换的聊天信息。

LangChain also offers more complex data structures and algorithms to return selected messages from the memory instead of returning everything. For instance, it has support for returning a summary of the past few messages, or only returning messages relevant to the current run.

LangChain 还提供了更复杂的数据结构和算法,用于从内存中返回选定的信息,而不是返回所有信息。例如,它支持返回过去几条信息的摘要,或仅返回与当前运行相关的信息。

5.3. Retrieval

5.3.检索

Large language models are generally trained on a vast quantity of text corpus. Hence, they’re quite efficient in general tasks but may not be so useful in a domain-specific task. For this, we need to retrieve relevant external data and pass it to the language model during the generation step.

大型语言模型通常是在大量的文本语料库中训练出来的。因此,它们在一般任务中相当高效,但在特定领域任务中可能并不那么有用。为此,我们需要在生成步骤中检索相关外部数据并将其传递给语言模型

This process is known as the Retrieval Augmented Generation (RAG). It helps in grounding the models on relevant and accurate information as well as giving us insight into the model’s generative process. LangChain provides the necessary building blocks to create RAG applications:

这个过程被称为 Retrieval Augmented Generation (RAG)。它有助于将模型建立在相关的准确信息基础上,并让我们深入了解模型的生成过程。LangChain 为创建 RAG 应用程序提供了必要的构建模块:

LangChain Retrieval

To begin with, LangChain provides document loaders that are used to retrieve a document from a storage location. Then, there are transformers available to prepare the documents for processing further. For instance, we can have it split a large document into smaller chunks:

首先,LangChain 提供文档加载器,用于从存储位置检索文档。然后,转换器可用于准备文档,以便进一步处理。例如,我们可以让它将大文档分割成小块:

Document document = FileSystemDocumentLoader.loadDocument("simpson's_adventures.txt");
DocumentSplitter splitter = DocumentSplitters.recursive(100, 0, 
  new OpenAiTokenizer(GPT_3_5_TURBO));
List<TextSegment> segments = splitter.split(document);

Here, we’re using the FileSystemDocumentLoader to load a document from the file system. Then, we split that document into smaller chunks using OpenAiTokenizer.

在这里,我们使用 FileSystemDocumentLoader 从文件系统加载文档。然后,我们使用 OpenAiTokenizer 将文档分割成小块。

To make the retrieval more efficient, the documents are generally converted into their embeddings and stored in vector databases. LangChain supports several embedding providers and methods and integrates with almost all popular vector stores:

为了提高检索效率,文档通常会被转换为嵌入式文档,并存储在矢量数据库中。LangChain 支持多种嵌入提供商和方法,并与几乎所有流行的矢量存储集成:

EmbeddingModel embeddingModel = new AllMiniLmL6V2EmbeddingModel();
List<Embedding> embeddings = embeddingModel.embedAll(segments).content();
EmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();
embeddingStore.addAll(embeddings, segments);

Here, we’re using the AllMiniLmL6V2EmbeddingModel to create the embeddings of document segments. Then, we store the embeddings in an in-memory vector store.

在这里,我们使用 AllMiniLmL6V2EmbeddingModel 来创建文档段的嵌入。然后,我们将嵌入存储在内存向量存储区中。

Now that we have our external data present in a vector store as embeddings, we’re ready to retrieve from it. LangChain supports several retrieval algorithms like simple semantic search and complex ones like ensemble retriever:

现在,我们的外部数据已经以嵌入的形式存在于向量存储中,我们可以从中进行检索了。LangChain 支持多种检索算法,如简单的语义搜索和复杂的集合检索:

String question = "Who is Simpson?";
//The assumption here is that the answer to this question is contained in the document we processed earlier.
Embedding questionEmbedding = embeddingModel.embed(question).content();
int maxResults = 3;
double minScore = 0.7;
List<EmbeddingMatch<TextSegment>> relevantEmbeddings = embeddingStore
  .findRelevant(questionEmbedding, maxResults, minScore);

We create the embedding of the user question and then use the question embedding to retrieve relevant matches from the vector store. Now, we can send the relevant matches retrieved as context by adding them to the prompt that we intend to send to the model.

我们创建用户问题的嵌入,然后使用问题嵌入从向量存储中检索相关匹配。现在,我们可以将检索到的相关匹配作为上下文发送,将它们添加到我们打算发送给模型的提示中。

6. Complex Applications of LangChain

6.LangChain 的复杂应用

So far, we’ve seen how to use individual components to create an application with a language model. LangChain also offers components to build more complex applications. For instance, we can use chains and agents to build more adaptive applications with enhanced capabilities.

到目前为止,我们已经了解了如何使用单个组件创建具有语言模型的应用程序。LangChain 还提供用于构建更复杂应用程序的组件。例如,我们可以使用链和代理来构建功能更强的自适应应用程序。

6.1. Chains

6.1.链条

Generally, an application will require multiple components to be called in a specific sequence. This is what’s referred to as a chain in LangChain. It simplifies developing more complex applications and makes it easier to debug, maintain, and improve.

一般来说,应用程序需要以特定顺序调用多个组件。这就是 LangChain 中所说的链。它简化了复杂应用程序的开发过程,使调试、维护和改进变得更加容易。

This is also useful for combining multiple chains to form more complex applications that potentially require an interface with more than one language model. LangChain offers convenient ways to create such chains and provides many pre-built chains:

这对于组合多个链以形成更复杂的应用程序也很有用,因为这些应用程序可能需要一个以上的语言模型接口。LangChain 提供了创建此类链的便捷方法,并提供了许多预构建链:

ConversationalRetrievalChain chain = ConversationalRetrievalChain.builder()
  .chatLanguageModel(chatModel)
  .retriever(EmbeddingStoreRetriever.from(embeddingStore, embeddingModel))
  .chatMemory(MessageWindowChatMemory.withMaxMessages(10))
  .promptTemplate(PromptTemplate
    .from("Answer the following question to the best of your ability: {{question}}\n\nBase your answer on the following information:\n{{information}}"))
  .build();

Here, we’re using a pre-built chain ConversationalRetreivalChain that allows us to use a chat model together with a retriever along with memory and prompt template. Now, we can simply use the chain to execute user queries:

在这里,我们使用了一个预构建的链 ConversationalRetreivalChain,它允许我们将聊天模型与检索器、内存和提示模板一起使用。现在,我们只需使用该链即可执行用户查询:

String answer = chain.execute("Who is Simpson?");

The chain comes with a default memory and prompt template that we can override. It’s also quite easy to create our custom chains. The ability to create chains makes it easier to achieve modular implementation of complex applications.

链自带默认内存和提示模板,我们可以覆盖。创建自定义链也非常简单。创建链的能力使复杂应用程序的模块化实施变得更加容易。

6.2. Agents

6.2.代理机构

LangChain also offers more powerful constructs like agents. Unlike chains, agents use a language model as a reasoning engine to determine which actions to take and in which order. We can also provide agents access to the right tools to perform necessary actions.

LangChain 还提供更强大的结构,如代理。与链不同的是,代理使用语言模型作为推理引擎来决定采取哪些行动以及行动的顺序。我们还可以让代理访问正确的工具,以执行必要的操作。

In LangChain4j, agents are available as AI Services to declaratively define complex AI behavior. Let’s see if we can provide a calculator as a tool for an AI Service and enable a language model to perform calculations.

在 LangChain4j 中,代理可作为 AI 服务来声明性地定义复杂的 AI 行为。让我们看看能否为人工智能服务提供一个计算器作为工具,并使语言模型能够执行计算。

First, we’ll define a class with some basic calculator functions and describe each function in natural language for the model to understand:

首先,我们将定义一个包含一些基本计算器功能的类,并用自然语言描述每个功能,以便模型理解:

public class AIServiceWithCalculator {
    static class Calculator {
        @Tool("Calculates the length of a string")
        int stringLength(String s) {
            return s.length();
        }
        @Tool("Calculates the sum of two numbers")
        int add(int a, int b) {
            return a + b;
        }
    }

Then, we’ll define the interface for our AI Service to build from. It’s quite simple here, but it can also describe more complex behaviors:

然后,我们将定义人工智能服务的接口。这里很简单,但也可以描述更复杂的行为:

interface Assistant {
    String chat(String userMessage);
}

Now, we’ll build an AI Service from the builder factory provided by LangChain4j using the interface we just defined and the tool we created:

现在,我们将使用刚刚定义的接口和创建的工具,从 LangChain4j 提供的构建器工厂中构建一个人工智能服务:

Assistant assistant = AiServices.builder(Assistant.class)
  .chatLanguageModel(OpenAiChatModel.withApiKey(<OPENAI_API_KEY>))
  .tools(new Calculator())
  .chatMemory(MessageWindowChatMemory.withMaxMessages(10))
  .build();

That’s it! We can now start sending questions that contain some calculations to be performed to our language model:

就是这样!现在,我们可以开始向语言模型发送包含一些计算的问题了:

String question = "What is the sum of the numbers of letters in the words \"language\" and \"model\"?";
String answer = assistant.chat(question);
System.out.prtintln(answer); // The sum of the numbers of letters in the words "language" and "model" is 13. 

When we run this code, we’ll observe that the language model is now capable of performing calculations.

当我们运行这段代码时,我们会发现语言模型现在能够执行计算了。

It’s important to note that language models have difficulty performing some tasks that require them to have the notion of time and space or perform complex arithmetic procedures. However, we can always supplement the model with the necessary tools to solve this problem.

值得注意的是,语言模型在执行某些要求它们具有时间和空间概念或执行复杂运算程序的任务时会遇到困难。不过,我们总是可以为模型补充必要的工具来解决这个问题。

7. Conclusion

7.结论

In this tutorial, we went through some of the basic elements of creating an application powered by large language models. Further, we discussed the value of including a framework like LangChain as part of the tech stack for developing such an application.

在本教程中,我们介绍了创建由大型语言模型驱动的应用程序的一些基本要素。此外,我们还讨论了将 LangChain 这样的框架作为开发此类应用程序的技术栈的一部分的价值。

This allowed us to explore some of the core elements of LangChain4j, a Java version of LangChain. These libraries will evolve rapidly in the days to come. But, they’re already making the process of developing applications powered by language models mature and fun!

这让我们得以探索 LangChain4j 的一些核心元素,LangChain4j 是 LangChain 的 Java 版本。这些库将在未来的日子里迅速发展。但是,它们已经让开发由语言模型驱动的应用程序的过程变得成熟而有趣!

Don’t forget to check out the full source code of this article over on GitHub.

别忘了查看本文在 GitHub 上的完整源代码。