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

本文涉及的产品
RDS DuckDB + QuickBI 企业套餐,8核32GB + QuickBI 专业版
简介: 本文以实践的方式将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,因此仅介绍如何使用标准支持的代理。如果想实现自定义代理,请参见相关文档

以下,我们将使用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模块以实践的方式介绍两者的使用差异。

相关实践学习
AnalyticDB PostgreSQL 企业智能数据中台:一站式管理数据服务资产
企业在数据仓库之上可构建丰富的数据服务用以支持数据应用及业务场景;ADB PG推出全新企业智能数据平台,用以帮助用户一站式的管理企业数据服务资产,包括创建, 管理,探索, 监控等; 助力企业在现有平台之上快速构建起数据服务资产体系
相关文章
|
8月前
|
机器学习/深度学习 缓存 监控
139_剪枝优化:稀疏模型压缩 - 分析结构化剪枝的独特速度提升与LLM部署加速实践
随着大语言模型(LLM)规模的不断增长,模型参数量已从最初的数亿扩展到数千亿甚至万亿级别。这种规模的模型在推理过程中面临着巨大的计算和内存挑战,即使在最先进的硬件上也难以高效部署。剪枝优化作为一种有效的模型压缩技术,通过移除冗余或不重要的参数,在保持模型性能的同时显著减少计算资源需求。
1251 139
|
9月前
|
SQL 人工智能 监控
SLS Copilot 实践:基于 SLS 灵活构建 LLM 应用的数据基础设施
本文将分享我们在构建 SLS SQL Copilot 过程中的工程实践,展示如何基于阿里云 SLS 打造一套完整的 LLM 应用数据基础设施。
2804 110
|
11月前
|
人工智能 监控 数据可视化
BISHENG下一代企业AI应用的“全能型“LLM软件
杭州奥零数据科技有限公司成立于2023年,专注于数据中台业务,维护开源项目AllData并提供商业版解决方案。AllData提供数据集成、存储、开发、治理及BI展示等一站式服务,支持AI大模型应用,助力企业高效利用数据价值。
|
10月前
|
负载均衡 NoSQL Redis
不增加 GPU,首 Token 延迟下降50%|LLM 服务负载均衡的新实践
针对LLM服务的特点,Higress AI网关以插件形式提供了面向LLM服务的负载均衡算法,包括全局最小请求数负载均衡、前缀匹配负载均衡以及GPU感知负载均衡,能够在不增加硬件成本的前提下,提升系统的吞吐能力、降低响应延迟,并实现更公平、高效的任务调度。
1003 136
|
数据采集 算法 数据挖掘
CLIMB自举框架:基于语义聚类的迭代数据混合优化及其在LLM预训练中的应用
英伟达提出的CLIMB框架,是一种自动化优化大型语言模型(LLM)预训练数据混合的创新方法。通过语义嵌入与聚类技术,CLIMB能系统地发现、评估并优化数据混合策略,无需人工干预。该框架包含数据预处理、迭代自举及最优权重确定三大阶段,结合小型代理模型与性能预测器,高效搜索最佳数据比例。实验表明,基于CLIMB优化的数据混合训练的模型,在多项推理任务中显著超越现有方法,展现出卓越性能。此外,研究还构建了高质量的ClimbMix数据集,进一步验证了框架的有效性。
694 0
CLIMB自举框架:基于语义聚类的迭代数据混合优化及其在LLM预训练中的应用
|
人工智能 自然语言处理 数据可视化
两大 智能体框架 Dify vs Langchain 的全面分析,该怎么选?资深架构师 做一个彻底的解密
两大 智能体框架 Dify vs Langchain 的全面分析,该怎么选?资深架构师 做一个彻底的解密
两大 智能体框架 Dify vs Langchain 的全面分析,该怎么选?资深架构师 做一个彻底的解密
|
8月前
|
机器学习/深度学习 缓存 PyTorch
131_推理加速:ONNX与TensorRT深度技术解析与LLM模型转换优化实践
在大语言模型(LLM)时代,高效的推理加速已成为部署高性能AI应用的关键挑战。随着模型规模的不断扩大(从BERT的数亿参数到GPT-4的数千亿参数),推理过程的计算成本和延迟问题日益突出。ONNX(开放神经网络交换格式)和TensorRT作为业界领先的推理优化框架,为LLM的高效部署提供了强大的技术支持。本文将深入探讨LLM推理加速的核心原理,详细讲解PyTorch模型转换为ONNX和TensorRT的完整流程,并结合2025年最新优化技术,提供可落地的代码实现与性能调优方案。
1900 4
|
8月前
|
机器学习/深度学习 PyTorch 算法框架/工具
118_LLM模型量化与压缩:从理论到2025年实践技术详解
大型语言模型(LLM)在自然语言处理领域取得了前所未有的成功,但模型规模的快速增长带来了巨大的计算和存储挑战。一个典型的大型语言模型(如GPT-4或LLaMA 3)可能包含数千亿甚至万亿参数,需要数百GB甚至TB级的存储空间,并且在推理时需要大量的计算资源。这种规模使得这些模型难以在边缘设备、移动设备甚至资源有限的云服务器上部署和使用。
1396 3
|
8月前
|
机器学习/深度学习 人工智能 自然语言处理
12_机器翻译入门:多语言LLM应用
在全球化背景下,语言障碍一直是信息交流、商业合作和文化传播的重要阻碍。2025年,随着多语言大语言模型(LLM)技术的突破,机器翻译已经从简单的单词转换发展为能够理解上下文、处理复杂句式、适应文化差异的智能系统。本文将带您入门多语言LLM在机器翻译领域的应用,重点介绍使用mT5(多语言T5)模型实现英语到中文的翻译,并探讨文化适应等高级话题。
596 0
|
8月前
|
机器学习/深度学习 自然语言处理 算法
48_动态架构模型:NAS在LLM中的应用
大型语言模型(LLM)在自然语言处理领域的突破性进展,很大程度上归功于其庞大的参数量和复杂的网络架构。然而,随着模型规模的不断增长,计算资源消耗、推理延迟和部署成本等问题日益凸显。如何在保持模型性能的同时,优化模型架构以提高效率,成为2025年大模型研究的核心方向之一。神经架构搜索(Neural Architecture Search, NAS)作为一种自动化的网络设计方法,正在为这一挑战提供创新性解决方案。本文将深入探讨NAS技术如何应用于LLM的架构优化,特别是在层数与维度调整方面的最新进展,并通过代码实现展示简单的NAS实验。
389 0