LLM系列 | 19: ChatGPT应用框架LangChain实践速成

本文涉及的产品
实时计算 Flink 版,5000CU*H 3个月
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
大数据开发治理平台 DataWorks,不限时长
简介: 本文以实践的方式将OpenAI接口、ChatOpenAI接口、Prompt模板、Chain、Agent、Memory这几个LangChain核心模块串起来,从而希望能够让小伙伴们快速地了解LangChain的使用。

简介

缺月挂疏桐,漏断人初静。
缺月挂疏桐,漏断人初静.jpg

小伙伴们好,我是微信公众号《小窗幽记机器学习》的小编:卖锅盔的小男孩。紧接之前LangChain专题文章:

今天这篇小作文是LangChain实践专题的第3篇,主要介绍LangChain中OpenAI接口和ChatOpenAI接口的区别,并重点介绍如下内容:

  • Prompt模板的使用
  • Chain的使用
  • Agent的使用
  • Memory的使用

在介绍上述模块的时候,分别基于OpenAI接口和ChatOpenAI接口以实践的方式演示各个功能模块的使用。

如需完整代码,请在微信公众号:《小窗幽记机器学习》上添加小编微信。

更多、更新文章欢迎关注微信公众号:小窗幽记机器学习。后续会持续整理模型加速、模型部署、模型压缩、LLM、AI艺术等系列专题,敬请关注。

OpenAI 和 ChatOpenAI 的区别

在LangChain中使用OpenAI服务常基于llms模块中的OpenAI API和chat_models中的ChatOpenAI API。

llms模块中的OpenAI接口(langchain.llms.OpenAI)是更通用的接口,用于与不同类型的语言模型进行交互。它可以与各种LLM模型集成,包括不仅限于ChatGPT,还包括其他类型的语言模型,如"text-davinci-003"等。chat_models模块是llms模块接口的高级封装,旨在提供简化的对话式语言模型功能。比如通过langchain.chat_models.ChatOpenAI可以进行多轮对话,并提供方便的方法来处理用户输入和模型输出。

在接口层面,chat_models提供一个以"聊天消息"作为输入和输出的接口。通过将一个或多个消息传递给chat_models,从而可以获取聊天结果。响应也是一条消息。目前LangChain支持的消息类型有AIMessageHumanMessageSystemMessageChatMessage。其中ChatMessage可以扮演任意角色。在大多数情况下,我们只需要处理HumanMessageAIMessageSystemMessage

总的来说,ChatOpenAI接口更专注于对话式交互,而OpenAI接口更通用,适用于与多种类型的语言模型进行交互和处理不同的语言处理任务。

虽然chat_models模块(langchain.chat_models.ChatOpenAI)和llms模块(langchain.llms.OpenAI)有显著区别,但有时只需将它们视为相同模型。LangChain提供predict接口,使我们可以像与普通LLM(langchain.llms)交互一样与langchain.chat_models模块内的模型进行交互。示例如下:
可以对比以下3种方式的输出:

方式1:

from langchain.llms import OpenAI
llm = OpenAI(model_name="gpt-3.5-turbo",temperature=0,openai_api_key=openai_api_key,openai_api_base=openai_api_base)
res=llm("你是谁")
print(res)

输出结果如下:

'我是一个AI助手,没有具体的身份。我被设计成可以回答各种问题和提供帮助的智能程序。'

方式2:

from langchain.llms import OpenAI
llm = OpenAI(model_name="gpt-3.5-turbo",temperature=0,openai_api_key=openai_api_key,openai_api_base=openai_api_base)
res=llm.predict("你是谁")
print(res)

输出结果如下:

'我是一个AI助手,没有具体的身份。我被设计成可以回答各种问题和提供帮助的智能程序。'

方式3:

from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0, openai_api_key=openai_api_key,openai_api_base=openai_api_base)
res=llm.predict("你是谁")
print(res)

输出结果如下:

'我是一个AI助手,没有具体的身份。我被设计成可以回答各种问题和提供帮助的智能程序。'

为了方便区分,以下用LLMs表示from langchain.llms import OpenAI接口,用Chat models表示from langchain.chat_models import ChatOpenAI接口。

Prompt模板

大多数LLM应用程序不会将用户输入直接传给LLM。通常情况下,会将用户输入添加到一个称为提示模板(prompt template)的文本片段中。提示模板(prompt template)用以提供关于特定任务的附加上下文。在构建了 Prompt模板后,可以用Chains将定义的模型和Prompt模板串起来。以下先介绍Prompt模板的书写,下一章节展示如何用Chains将模型和Prompt模板串起来。

LLMs版

如果模型选用from langchain.llms import OpenAI, 则可以使用如下的方式构建Prompt模板。

from langchain.prompts import PromptTemplate
prompt = PromptTemplate.from_template("你是一个擅长帮人取花名的博主,以下根据提供的人物信息,你帮其取一个花名,并给出解释。\
输出格式为json,key为name和explain。name_msg={person_msg}。")
prompt.format(person_msg="男,穿吊带裤,会打篮球,擅长唱歌和跳舞,特别是篮球类舞蹈")

此时的prompt内容如下:

'你是一个擅长帮人取花名的博主,以下根据提供的人物信息,你帮其取一个花名,并给出解释。输出格式为json,key为name和explain。name_msg=男,穿吊带裤,会打篮球,擅长唱歌和跳舞,特别是篮球类舞蹈。'

Chat models版

与LLMs,ChatOpenAI接口也可以基于MessagePromptTemplate来使用模板。我们可以基于一个或多个MessagePromptTemplate构建ChatPromptTemplate。在这个过程中可以使用ChatPromptTemplateformat_messages方法来生成格式化的消息。

由于这种方式生成的是消息列表,所以比普通的提示模板稍微复杂一些,普通的提示模板只生成一个字符串。具体示例如下:

from langchain.prompts.chat import (
    ChatPromptTemplate,
    SystemMessagePromptTemplate,
    HumanMessagePromptTemplate,
)

template = "你是一名翻译专家,能够将{input_language}翻译成{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])

chat_prompt.format_messages(input_language="中文", output_language="英文", text="坤坤会去踩缝纫机吗?")

此时chat_prompt值如下:

[SystemMessage(content='你是一名翻译专家,能够将中文翻译成英文.', additional_kwargs={}),
 HumanMessage(content='坤坤会去踩缝纫机吗?', additional_kwargs={}, example=False)]

Chain

LLMs版

将 OpenAI 接口的模型与PromptTemplate串起来。

from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.llms import OpenAI

openai_api_key = "sk-XXX"
model_name= "gpt-3.5-turbo" # "text-davinci-003"
llm = OpenAI(model_name=model_name, openai_api_key=openai_api_key)

prompt = PromptTemplate.from_template("你是一个擅长帮人取花名的博主,以下根据提供的人物信息,你帮其取一个花名,并给出解释。\
输出格式为json,key为name和explain。name_msg={person_msg}?")
# prompt.format(person_msg="男,穿吊带裤,会打篮球,擅长唱歌和跳舞,特别是篮球类舞蹈")

chain = LLMChain(llm=llm, prompt=prompt)
person_msg="男,穿吊带裤,会打篮球,擅长唱歌和跳舞,特别是篮球类舞蹈"
res = chain.run(person_msg)
print(res)

输出结果如下:

{
  "name": "篮舞花",
  "explain": "篮舞花是一个形象鲜明的花名,代表着他擅长篮球和舞蹈的特点。穿吊带裤和会打篮球是他的外在特征,而擅长唱歌和跳舞则展示了他的才艺。篮舞花这个花名将他的多重才能融合在一起,形象生动且容易记忆。"
}

Chat models版

前一章节已经介绍from langchain.chat_models import ChatOpenAI对应的Prompt模板,那么如下用Chain将两者串起来,具体示例如下:

from langchain import LLMChain
from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (
    ChatPromptTemplate,
    SystemMessagePromptTemplate,
    HumanMessagePromptTemplate,
)

chat = ChatOpenAI(temperature=0)  # 默认就是 gpt-3.5-turbo

template = "你是一个擅长帮人取花名的博主,以下根据提供的人物信息,你帮其取一个花名,并给出解释。\
输出格式为json,key为name和explain。"
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)
person_msg="男,穿吊带裤,会打篮球,擅长Rap、唱歌和跳舞,特别是篮球类舞蹈"
res=chain.run(text=person_msg)
print(res)

输出结果如下:

{
  "name": "篮舞花",
  "explain": "取名灵感来源于该人物擅长的篮球类舞蹈。篮舞花既突出了他的篮球特长,又体现了他对舞蹈的热爱和才华。"
}

示例2,多个输入:

from langchain.prompts.chat import (
    ChatPromptTemplate,
    SystemMessagePromptTemplate,
    HumanMessagePromptTemplate,
)

template = "你是一名翻译专家,能够将{input_language}翻译成{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])
# chat_prompt.format_messages(input_language="中文", output_language="英文", text="坤坤会去踩缝纫机吗?")
chat = ChatOpenAI(temperature=0)  # 默认就是 gpt-3.5-turbo
chain = LLMChain(llm=chat, prompt=chat_prompt)
res=chain.run(input_language="中文", output_language="英文", text="坤坤会去踩缝纫机吗?")
print(res)

输出结果如下:

Will Kun Kun go and step on a sewing machine?

Agents

前文使用的Chain其实是预定义了一序列的步骤。为了处理复杂的工作流程,我们需要能够根据输入动态选择不同的执行任务。LangChain中的代理(Agents)可以做到这一点。它们使用语言模型确定要采取的行动及其顺序。代理可以访问工具,并且它们会重复选择工具、运行工具并观察输出,直到得出最终答案。

要加载一个Agent,我们需要选择以下内容:

  • 设置LLM/Chat模型:这是为Agent提供动力的模型。
  • 工具(Tool):用以执行特定任务。例如:Google搜索、数据库查询、Python REPL等。有关预定义工具可以参阅工具文档
  • Agent名称:以字符串设置Agent名称,用于引用支持的Agent类。Agent类在很大程度上由语言模型用于确定要采取的行动的提示参数化。由于本文档重点介绍最简单、最高级别的API,因此仅介绍如何使用标准支持的代理。如果想实现自定义代理,请参见相关文档custom_agent

以下,我们将使用SerpAPI来查询搜索引擎。需要安装SerpAPI的Python包:

pip install google-search-results

LLMs版

示例代码:

from langchain.agents import AgentType, initialize_agent, load_tools
from langchain.llms import OpenAI
os.environ["SERPAPI_API_KEY"] = 'XXXX'
# The language model we're going to use to control the agent.
model_name= "gpt-3.5-turbo"
llm = OpenAI(model_name=model_name, temperature=0)

# The tools we'll give the Agent access to. Note that the 'llm-math' tool uses an LLM, so we need to pass that in.
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, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)

# Let's test it out!
agent.run("现在美元兑换人民币的汇率是多少?该数字除以2的值等于多少?")

日志如下:

> Entering new  chain...
I need to find the current exchange rate between USD and CNY and then divide it by 2.
Action: Search
Action Input: "current exchange rate USD CNY"
Observation: USDCNY:CUR. USD-CNY X-RATE ; Open. 7.2506 ; Prev Close. 7.2505 ; YTD Return. 5.04% ; Day Range. 7.24237.2523 ; 52 Week Range. 6.69107.3274.
Thought:I have found the current exchange rate between USD and CNY. Now I need to divide it by 2.
Action: Calculator
Action Input: 7.2506 / 2
Observation: Answer: 3.6253
Thought:I now know the final answer.
Final Answer: The current exchange rate between USD and CNY is 7.2506, and when divided by 2, it equals 3.6253.

> Finished chain.

最终结果如下:

'The current exchange rate between USD and CNY is 7.2506, and when divided by 2, it equals 3.6253.'

人民币汇率.png

虽然结果差不多,但仍有微小的差异,可能是由于该问题答案的时效性导致。

换一个暂时不存在时效性的问题

agent.run("珠穆朗玛峰多少米?该数字除以2的值等于多少?")

日志如下:

> Entering new  chain...
I need to find out the height of Mount Everest and then divide it by 2.
Action: Search
Action Input: "height of Mount Everest"
Observation: 29,032′
Thought:I have found the height of Mount Everest, now I need to divide it by 2.
Action: Calculator
Action Input: 29032 / 2
Observation: Answer: 14516.0
Thought:I now know the final answer.
Final Answer: The height of Mount Everest is 29,032 feet and when divided by 2, the value is 14,516 feet.

> Finished chain.

输出结果如下:

'The height of Mount Everest is 29,032 feet and when divided by 2, the value is 14,516 feet.'

Chat models版

上述相同问题用ChatOpenAI接口的话,可以如下处理:

from langchain.agents import load_tools
from langchain.tools.sleep.tool import SleepTool
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.chat_models import ChatOpenAI
from langchain.llms import OpenAI
import time

# 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=chat)

# 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("珠穆朗玛峰多少米?该数字除以2的值等于多少?")
# 结果的单位并不是我们要的"米"

日志如下:

> Entering new  chain...
Thought: The user is asking for the height of Mount Everest and wants to know the result when it is divided by 2.
Action:

{
"action": "Search",
"action_input": "height of Mount Everest"
}


Observation: 29,032′
Thought:The height of Mount Everest is 29,032 feet. Now I need to divide this number by 2.
Action:

{
"action": "Calculator",
"action_input": "29032 / 2"
}


Observation: Answer: 14516.0
Thought:The height of Mount Everest is 29,032 feet and when divided by 2, the result is 14,516 feet.
Final Answer: 14,516 feet.

> Finished chain.

结果如下:

'14,516 feet.'

可以看出,输出结果一方面在高度单位上没有符合预期,另一方面第一个问题的回复被遗落。

修改下提问:

agent.run("珠穆朗玛峰多高,单位换算成米?该数字除以2的值等于多少?")

日志如下:

> Entering new  chain...
Question: How tall is Mount Everest in meters? What is the value when this number is divided by 2?
Thought: I can use a search engine to find the height of Mount Everest and then perform the calculation.
Action:

{
"action": "Search",
"action_input": "height of Mount Everest in meters"
}


Observation: 8,848.9 m
Thought:The height of Mount Everest is 8,848.9 meters. To find the value when this number is divided by 2, I can use a calculator.
Action:

{
"action": "Calculator",
"action_input": "8848.9 / 2"
}


Observation: Answer: 4424.45
Thought:The height of Mount Everest is 8,848.9 meters. When this number is divided by 2, the value is 4,424.45.
Final Answer: The height of Mount Everest is 8,848.9 meters and when divided by 2, the value is 4,424.45.

> Finished chain.

结果如下:

'The height of Mount Everest is 8,848.9 meters and when divided by 2, the value is 4,424.45.'

联合Chain和Agent

可以看出,上述Agent的回复都是英文,如果想要使用中文回复呢?可以引入Prompt,将上述Chain的结果再输入到一个用以翻译的Chain。具体实现如下:

from langchain.chains import SimpleSequentialChain

# Chain1 - get the answer
model_name= "gpt-3.5-turbo"
llm = OpenAI(model_name=model_name, temperature=0)

# The tools we'll give the Agent access to. Note that the 'llm-math' tool uses an LLM, so we need to pass that in.
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, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)

# Let's test it out!
# agent.run("珠穆朗玛峰多少米?该数字除以2的值等于多少?")

print("agent.agent.llm_chain.output_keys=",agent.agent.llm_chain.output_keys)

chain_one = agent

# Chain2 - suggest age-appropriate gift
template = """你是一名专业的翻译家,将你收到的内容:{text}翻译成中文,只需要输出最终的翻译结果,不需要输出其他"""
prompt_template = PromptTemplate(input_variables=["text"], template=template)
chain_two = LLMChain(llm=llm, prompt=prompt_template) 

overall_chain = SimpleSequentialChain(
                  chains=[chain_one, chain_two],
                  verbose=True)

question = "珠穆朗玛峰多高,单位用米?该数字除以2的值等于多少?"
overall_chain.run(question)

日志结果如下:

agent.agent.llm_chain.output_keys= ['text']


> Entering new  chain...


> Entering new  chain...
I need to find the height of Mount Everest and convert it to meters. Then I need to divide that number by 2.
Action: Search
Action Input: "height of Mount Everest in meters"
Observation: 8,848.9 m
Thought:I have the height of Mount Everest in meters. Now I need to divide it by 2.
Action: Calculator
Action Input: 8848.9 / 2
Observation: Answer: 4424.45
Thought:I now know the final answer.
Final Answer: The height of Mount Everest is 8,848.9 meters and when divided by 2, the result is 4,424.45.

> Finished chain.
The height of Mount Everest is 8,848.9 meters and when divided by 2, the result is 4,424.45.
珠穆朗玛峰的高度为8,848.9米,当除以2时,结果为4,424.45。

> Finished chain.

最终输出结果如下:

'珠穆朗玛峰的高度为8,848.9米,当除以2时,结果为4,424.45。'

Memory

上述的Chain和Agent都是无状态的,但对于多数应用程序而言,使用过往的交互信息十分必要,这就需要一个记忆功能。这类记忆上下文信息的能力在聊天机器人等应用中尤为重要,用户希望这些机器人能够记忆过去的上下文从而更好地理解用户新输入的消息。

LangChain中的Memory模块能够用以记忆应用程序的状态。Memory接口很简单,根据最新的运行输入和输出更新状态,同时允许利用存储的状态来修改下一个输入(或者给下一个输入提供上下文)。

LangChain有许多内置的memory系统,其中最简单的是缓存(buffer memory),将最近几个输入/输出添加到当前输入的前面。在下面的示例中,我们基于这种方法进行演示。

LLMs版

from langchain import OpenAI, ConversationChain
llm = OpenAI(model_name="gpt-3.5-turbo",temperature=0)
conversation = ConversationChain(llm=llm, verbose=True)
res=conversation.run("假设你现在叫做坤坤,你的一技能是Rap、二技能是篮球、三技能是篮球舞")
print(res)

日志输出:

> Entering new  chain...
Prompt after formatting:
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.

Current conversation:

Human: 假设你现在叫做坤坤,你的一技能是Rap、二技能是篮球、三技能是篮球舞
AI:

> Finished chain.

最终输出:

'嗨!你好!我是坤坤,很高兴认识你!我的一技能是Rap,我喜欢用音乐表达自己的情感和想法。我也很喜欢篮球,这是我的二技能。我喜欢在球场上展现我的技巧和团队合作精神。另外,我的三技能是篮球舞。我喜欢跳舞,特别是与篮球相关的舞蹈。这些技能让我感到充满活力和快乐!你有什么问题想问我吗?'

此时再运行如下命令:

res=conversation.run("你好,你叫什么名字?")
print(res)

日志输出:

> Entering new  chain...
Prompt after formatting:
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.

Current conversation:
Human: 假设你现在叫做坤坤,你的一技能是Rap、二技能是篮球、三技能是篮球舞
AI: 嗨!你好!我是坤坤,很高兴认识你!我的一技能是Rap,我喜欢用音乐表达自己的情感和想法。我也很喜欢篮球,这是我的二技能。我喜欢在球场上展现我的技巧和团队合作精神。另外,我的三技能是篮球舞。我喜欢跳舞,特别是与篮球相关的舞蹈。这些技能让我感到充满活力和快乐!你有什么问题想问我吗?
Human: 你好,你叫什么名字?
AI:

> Finished chain.

最终输出:

'嗨!我叫坤坤。很高兴认识你!有什么我可以帮助你的吗?'

从上述结果可以看出,新的对话已经记住历史输入的信息

Chat models版

我们也可以将Memory与使用ChatOpenAI接口初始化的Chain和Agent一起使用。与LLMs版Memory主要区别在于,不会将所有先前的消息压缩成一个字符串,而是将它们保留为独立的memory对象。

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(
        "假设你现在叫做坤坤,你的一技能是Rap、二技能是篮球、三技能(大招)是篮球舞,技能CD都是1秒。开大招的时候,可以沉默敌方所有技能。"
    ),
    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)

human_input = "你好啊"
res1=conversation.predict(input=human_input)
print("human:", human_input)
print("bot:",res1)

human_input = "你能够简单自我介绍下吗?"
res2=conversation.predict(input="human_input")
print("human:", human_input)
print("bot:",res2)

输出结果如下:

human: 你好啊
bot: 嘿,你好!我是坤坤,有什么我可以帮助你的吗?
human: 你能够简单自我介绍下吗?
bot: 当然可以!我可以用我的一技能Rap给你表演一段,或者用我的二技能篮球来和你玩一场篮球比赛。如果你想看我最强的技能,我可以使用我的三技能篮球舞,不仅可以展示我的篮球技巧,还可以沉默敌方的技能。你想看哪个技能呢?

此时再执行如下命令:

human_input = "Rap吧"
res3=conversation.predict(input="human_input")
print("human:", human_input)
print("bot:",res3)

输出结果如下:

human: Rap吧
bot: 好的,让我来展示我的一技能Rap吧!请欣赏:

Yo,我是坤坤,来自大街小巷,
我的Rap技能,让你们惊叹不已,
我用言语的力量,展现我的才华,
每个字每个句子,都是我的骄傲。

我用节奏和韵律,打造我的旋律,
每个音符都是我心中的力量,
我用文字的力量,表达我的情感,
让你们感受到我的热情和冲动。

我的Rap技能,让你们沉醉其中,
每个字每个音,都是我的真心,
我用音乐的力量,传递我的信息,
让你们跟随我的节奏,一起前行。

这就是我的一技能Rap,希望你喜欢!如果你还想看其他技能的表演,尽管告诉我哦!

总结

本文主要介绍LangChain中OpenAI接口和ChatOpenAI接口的区别,并结合Prompt模板、Chain模块、Agent模块、Memory模块以实践的方式介绍两者的使用差异。

相关实践学习
使用CLup和iSCSI共享盘快速体验PolarDB for PostgtreSQL
在Clup云管控平台中快速体验创建与管理在iSCSI共享盘上的PolarDB for PostgtreSQL。
AnalyticDB PostgreSQL 企业智能数据中台:一站式管理数据服务资产
企业在数据仓库之上可构建丰富的数据服务用以支持数据应用及业务场景;ADB PG推出全新企业智能数据平台,用以帮助用户一站式的管理企业数据服务资产,包括创建, 管理,探索, 监控等; 助力企业在现有平台之上快速构建起数据服务资产体系
相关文章
|
2天前
|
JSON API 数据格式
LangChain Agent:赋予 LLM 行动力的神秘力量
LangChain Agent 是什么?有什么用?基本原理是什么?那么多 Agent 类型在实际开发中又该如何选择?
194 8
LangChain Agent:赋予 LLM 行动力的神秘力量
|
2天前
|
人工智能 自然语言处理 搜索推荐
揭秘ChatGPT的Prompt方法:原理与应用总结
揭秘ChatGPT的Prompt方法:原理与应用总结
54 0
|
2天前
|
弹性计算 自然语言处理 开发工具
基于阿里云向量检索 Milvus 版和 LangChain 快速构建 LLM 问答系统
本文介绍如何通过整合阿里云Milvus、阿里云DashScope Embedding模型与阿里云PAI(EAS)模型服务,构建一个由LLM(大型语言模型)驱动的问题解答应用,并着重演示了如何搭建基于这些技术的RAG对话系统。
|
1天前
|
测试技术 API 数据库
【LangChain系列】第二篇:文档拆分简介及实践
【5月更文挑战第15天】 本文介绍了LangChain中文档拆分的重要性及工作原理。文档拆分有助于保持语义内容的完整性,对于依赖上下文的任务尤其关键。LangChain提供了多种拆分器,如CharacterTextSplitter、RecursiveCharacterTextSplitter和TokenTextSplitter,分别适用于不同场景。MarkdownHeaderTextSplitter则能根据Markdown标题结构进行拆分,保留文档结构。通过实例展示了如何使用这些拆分器,强调了选择合适拆分器对提升下游任务性能和准确性的影响。
|
2天前
|
存储 机器学习/深度学习 人工智能
【LangChain系列】第一篇:文档加载简介及实践
【5月更文挑战第14天】 LangChain提供80多种文档加载器,简化了从PDF、网站、YouTube视频和Notion等多来源加载与标准化数据的过程。这些加载器将不同格式的数据转化为标准文档对象,便于机器学习工作流程中的数据处理。文中介绍了非结构化、专有和结构化数据的加载示例,包括PDF、YouTube视频、网站和Notion数据库的加载方法。通过LangChain,用户能轻松集成和交互各类数据源,加速智能应用的开发。
|
2天前
|
人工智能 测试技术 API
【AIGC】LangChain Agent(代理)技术分析与实践
【5月更文挑战第12天】 LangChain代理是利用大语言模型和推理引擎执行一系列操作以完成任务的工具,适用于从简单响应到复杂交互的各种场景。它能整合多种服务,如Google搜索、Wikipedia和LLM。代理通过选择合适的工具按顺序执行任务,不同于链的固定路径。代理的优势在于可以根据上下文动态选择工具和执行策略。适用场景包括网络搜索、嵌入式搜索和API集成。代理由工具组成,每个工具负责单一任务,如Web搜索或数据库查询。工具包则包含预定义的工具集合。创建代理需要定义工具、初始化执行器和设置提示词。LangChain提供了一个从简单到复杂的AI解决方案框架。
|
2天前
开箱黑盒LLM!谷歌大一统框架Patchscopes实战教程来了
【5月更文挑战第7天】谷歌推出Patchscopes框架,提升大型语言模型(LLM)的可解释性,通过分解LLM为可解释的Patch模型进行调试。本文提供实战教程,介绍如何安装Patchscopes库、加载预训练模型并查看模型解释性报告。虽然优势在于增强理解与问题定位,但Patchscopes适用模型有限,报告理解需专业知识,且计算成本高。[论文链接](https://arxiv.org/pdf/2401.06102.pdf)
12 1
|
2天前
|
自然语言处理 监控 搜索推荐
【大模型】探索LLM在各个行业的潜在未来应用
【5月更文挑战第7天】【大模型】探索LLM在各个行业的潜在未来应用
|
2天前
|
机器学习/深度学习 人工智能 分布式计算
【大模型】在实际应用中大规模部署 LLM 会带来哪些挑战?
【5月更文挑战第5天】【大模型】在实际应用中大规模部署 LLM 会带来哪些挑战?
|
2天前
|
机器学习/深度学习 自然语言处理 计算机视觉
【大模型】小样本学习的概念及其在微调 LLM 中的应用
【5月更文挑战第5天】【大模型】小样本学习的概念及其在微调 LLM 中的应用

热门文章

最新文章