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

本文涉及的产品
实时数仓Hologres,5000CU*H 100GB 3个月
实时计算 Flink 版,5000CU*H 3个月
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 本文以实践的方式将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模块以实践的方式介绍两者的使用差异。

相关实践学习
阿里云百炼xAnalyticDB PostgreSQL构建AIGC应用
通过该实验体验在阿里云百炼中构建企业专属知识库构建及应用全流程。同时体验使用ADB-PG向量检索引擎提供专属安全存储,保障企业数据隐私安全。
AnalyticDB PostgreSQL 企业智能数据中台:一站式管理数据服务资产
企业在数据仓库之上可构建丰富的数据服务用以支持数据应用及业务场景;ADB PG推出全新企业智能数据平台,用以帮助用户一站式的管理企业数据服务资产,包括创建, 管理,探索, 监控等; 助力企业在现有平台之上快速构建起数据服务资产体系
相关文章
|
2月前
|
人工智能 监控 数据库
LLM 应用可观测性:从 Trace 视角展开的探索与实践之旅
基于大语言模型的应用在性能、成本、效果等方面存在一系列实际痛点,本文通过分析 LLM 应用模式以及关注点差异来阐明可观测技术挑战,近期阿里云可观测推出了面向 LLM 应用的可观测解决方案以及最佳实践,一起来了解下吧。
17782 21
LLM 应用可观测性:从 Trace 视角展开的探索与实践之旅
|
1月前
|
人工智能 PyTorch 算法框架/工具
Xinference实战指南:全面解析LLM大模型部署流程,携手Dify打造高效AI应用实践案例,加速AI项目落地进程
【8月更文挑战第6天】Xinference实战指南:全面解析LLM大模型部署流程,携手Dify打造高效AI应用实践案例,加速AI项目落地进程
Xinference实战指南:全面解析LLM大模型部署流程,携手Dify打造高效AI应用实践案例,加速AI项目落地进程
|
27天前
|
监控 数据安全/隐私保护 异构计算
借助PAI-EAS一键部署ChatGLM,并应用LangChain集成外部数据
【8月更文挑战第8天】借助PAI-EAS一键部署ChatGLM,并应用LangChain集成外部数据
52 1
|
28天前
|
存储 机器学习/深度学习 物联网
基于重要性加权的LLM自我改进:考虑分布偏移的新框架
本文提出一种新的大型语言模型(LLM)自我改进框架——基于重要性加权的自我改进(IWSI),旨在优化自动生成数据的质量。通过引入DS权重指标衡量数据的分布偏移程度(DSE),该方法不仅能确保答案正确性,还能过滤掉那些虽正确但分布上偏离较大的样本,以提升自我训练的效果。IWSI使用一个小的有效数据集来估算每个自生成样本的DS权重,并据此进行筛选。实验结果显示,相比于仅依赖答案正确性的传统方法,IWSI能更有效地提高LLM在多种任务上的表现。特别是在数学问题解答任务上,相较于基线方法,IWSI带来了显著的性能提升,证实了过滤高DSE样本的重要性及该方法的有效性。
27 0
基于重要性加权的LLM自我改进:考虑分布偏移的新框架
|
2月前
|
人工智能 前端开发 API
RAG+AI工作流+Agent:LLM框架该如何选择,全面对比MaxKB、Dify、FastGPT、RagFlow、Anything-LLM,以及更多推荐
【7月更文挑战第9天】RAG+AI工作流+Agent:LLM框架该如何选择,全面对比MaxKB、Dify、FastGPT、RagFlow、Anything-LLM,以及更多推荐
RAG+AI工作流+Agent:LLM框架该如何选择,全面对比MaxKB、Dify、FastGPT、RagFlow、Anything-LLM,以及更多推荐
|
1月前
|
存储 机器学习/深度学习 数据采集
深入解析大数据核心概念:数据平台、数据中台、数据湖与数据仓库的异同与应用
深入解析大数据核心概念:数据平台、数据中台、数据湖与数据仓库的异同与应用
|
2月前
|
自然语言处理 API 开发工具
初识langchain:LLM大模型+Langchain实战[qwen2.1、GLM-4]+Prompt工程
【7月更文挑战第6天】初识langchain:LLM大模型+Langchain实战[qwen2.1、GLM-4]+Prompt工程
初识langchain:LLM大模型+Langchain实战[qwen2.1、GLM-4]+Prompt工程
|
1月前
|
人工智能 异构计算
基于PAI-EAS一键部署ChatGLM及LangChain应用
【8月更文挑战第7天】基于PAI-EAS一键部署ChatGLM及LangChain应用
|
1月前
|
数据采集 自然语言处理 语音技术
LangChain进阶:创建多模态应用
【8月更文第4天】随着自然语言处理 (NLP) 和计算机视觉 (CV) 技术的不断发展,多模态应用变得越来越普遍。这些应用结合了文本、图像、音频等多种数据类型,以增强用户体验并解决复杂的问题。LangChain 作为一款强大的工具链,可以很好地支持多模态数据的处理,从而开发出具有高度互动性和实用性的应用。
69 1
|
1月前
|
存储 缓存 自然语言处理
LangChain在构建智能文档检索系统中的应用
【8月更文第3天】随着大数据时代的到来,企业和组织积累了大量的文档资料。如何有效地管理和检索这些文档成为了一个重要的问题。传统的关键词搜索虽然简单,但在面对复杂查询和模糊匹配时显得力不从心。LangChain 是一个强大的框架,旨在帮助开发者构建文本生成应用程序,它能够利用最新的自然语言处理技术来理解和响应自然语言查询。本文将探讨如何利用 LangChain 构建一个能够理解和响应自然语言查询的文档检索系统。
43 0