自然语言处理从入门到应用——LangChain:模型(Models)-[聊天模型(Chat Models):基础知识]

分类目录:《自然语言处理从入门到应用》总目录


聊天模型是语言模型的一种变体。虽然聊天模型在内部使用语言模型,但它们公开的接口略有不同。它们不是提供一个"输入文本,输出文本"的API,而是提供一个以"聊天消息"作为输入和输出的接口。 聊天模型的API还比较新,因此我们仍在确定正确的抽象层次。本问将介绍如何开始使用聊天模型,该接口是基于消息而不是原始文本构建的:

dart 复制代码
from langchain.chat_models import ChatOpenAI
from langchain import PromptTemplate, LLMChain
from langchain.prompts.chat import (
    ChatPromptTemplate,
    SystemMessagePromptTemplate,
    AIMessagePromptTemplate,
    HumanMessagePromptTemplate,
)
from langchain.schema import (
    AIMessage,
    HumanMessage,
    SystemMessage
)
chat = ChatOpenAI(temperature=0)

通过向聊天模型传递一个或多个消息,可以获取聊天完成的结果。响应将是一个消息。LangChain目前支持的消息类型有AIMessageHumanMessageSystemMessageChatMessage,其中ChatMessage接受一个任意的角色参数。大多数情况下,我们只需要处理HumanMessageAIMessageSystemMessage

dart 复制代码
chat([HumanMessage(content="Translate this sentence from English to French. I love programming.")])

输出:

dart 复制代码
AIMessage(content="J'aime programmer.", additional_kwargs={})

OpenAI的聊天模型支持多个消息作为输入。更多信息请参见这里。以下是向聊天模型发送系统消息和用户消息的示例:

dart 复制代码
messages = [
    SystemMessage(content="You are a helpful assistant that translates English to French."),
    HumanMessage(content="I love programming.")
]
chat(messages)

输出:

dart 复制代码
AIMessage(content="J'aime programmer.", additional_kwargs={})

您还可以进一步生成多组消息的完成结果,使用generate方法实现。该方法将返回一个带有额外message参数的LLMResult

dart 复制代码
batch_messages = [
    [
        SystemMessage(content="You are a helpful assistant that translates English to French."),
        HumanMessage(content="I love programming.")
    ],
    [
        SystemMessage(content="You are a helpful assistant that translates English to French."),
        HumanMessage(content="I love artificial intelligence.")
    ],
]
result = chat.generate(batch_messages)
result

输出:

dart 复制代码
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': 57, 'completion_tokens': 20, 'total_tokens': 77}})

我们可以从LLMResult中获取诸如标记使用情况之类的信息:

dart 复制代码
result.llm_output

输出:

dart 复制代码
{'token_usage': {'prompt_tokens': 57,
  'completion_tokens': 20,
  'total_tokens': 77}}

PromptTemplates

我们可以使用模板来构建MessagePromptTemplate。我们可以从一个或多个MessagePromptTemplate构建一个ChatPromptTemplate。我们还可以使用ChatPromptTemplateformat_prompt方法,它将返回一个PromptValue,我们可以将其转换为字符串或消息对象,具体取决于我们是否希望将格式化后的值作为输入传递给LLM或Chat模型的输入。为了方便起见,模板上公开了一个from_template方法。如果您要使用此模板,代码如下所示:

dart 复制代码
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])

# 获取格式化后的消息的聊天完成结果
chat(chat_prompt.format_prompt(input_language="English", output_language="French", text="I love programming.").to_messages())

输出:

dart 复制代码
AIMessage(content="J'adore la programmation.", additional_kwargs={})

如果我们想直接更直接地构建MessagePromptTemplate,我们可以在外部创建一个PromptTemplate,然后将其传递进去,例如:

dart 复制代码
prompt=PromptTemplate(
    template="You are a helpful assistant that translates {input_language} to {output_language}.",
    input_variables=["input_language", "output_language"],
)
system_message_prompt = SystemMessagePromptTemplate(prompt=prompt)

LLMChain

我们可以以与以前非常相似的方式使用现有的LLMChain,即提供一个提示和一个模型:

dart 复制代码
chain = LLMChain(llm=chat, prompt=chat_prompt)
chain.run(input_language="English", output_language="French", text="I love programming.")

输出:

dart 复制代码
"J'adore la programmation."

Streaming

通过回调处理,ChatOpenAI支持流式处理。

dart 复制代码
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
chat = ChatOpenAI(streaming=True, callbacks=[StreamingStdOutCallbackHandler()], temperature=0)
resp = chat([HumanMessage(content="Write me a song about sparkling water.")])

输出:

dart 复制代码
Verse 1:
Bubbles rising to the top
A refreshing drink that never stops
Clear and crisp, it's pure delight
A taste that's sure to excite

Chorus:
Sparkling water, oh so fine
A drink that's always on my mind
With every sip, I feel alive
Sparkling water, you're my vibe

Verse 2:
No sugar, no calories, just pure bliss
A drink that's hard to resist
It's the perfect way to quench my thirst
A drink that always comes first

Chorus:
Sparkling water, oh so fine
A drink that's always on my mind
With every sip, I feel alive
Sparkling water, you're my vibe

Bridge:
From the mountains to the sea
Sparkling water, you're the key
To a healthy life, a happy soul
A drink that makes me feel whole

Chorus:
Sparkling water, oh so fine
A drink that's always on my mind
With every sip, I feel alive
Sparkling water, you're my vibe

Outro:
Sparkling water, you're the one
A drink that's always so much fun
I'll never let you go, my friend
Sparkling

参考文献:

1\] LangChain 🦜️🔗 中文网,跟着LangChain一起学LLM/GPT开发:https://www.langchain.com.cn/ \[2\] LangChain中文网 - LangChain 是一个用于开发由语言模型驱动的应用程序的框架:http://www.cnlangchain.com/

相关推荐
kngines2 分钟前
【字节跳动】数据挖掘面试题0007:Kmeans原理,何时停止迭代
人工智能·数据挖掘·kmeans
Kali_075 分钟前
使用 Mathematical_Expression 从零开始实现数学题目的作答小游戏【可复制代码】
java·人工智能·免费
贾全11 分钟前
第十章:HIL-SERL 真实机器人训练实战
人工智能·深度学习·算法·机器学习·机器人
每日摸鱼大王16 分钟前
互联网摸鱼日报(2025-07-01)
人工智能
GIS小天26 分钟前
AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年7月4日第128弹
人工智能·算法·机器学习·彩票
我是小哪吒2.037 分钟前
书籍推荐-《对抗机器学习:攻击面、防御机制与人工智能中的学习理论》
人工智能·深度学习·学习·机器学习·ai·语言模型·大模型
慕婉030741 分钟前
深度学习前置知识全面解析:从机器学习到深度学习的进阶之路
人工智能·深度学习·机器学习
荔枝吻1 小时前
【AI总结】Git vs GitHub vs GitLab:深度解析三者联系与核心区别
人工智能·git·github
Jamie201901062 小时前
高档宠物食品对宠物的健康益处有哪些?
大数据·人工智能
云卓SKYDROID2 小时前
无人机载重模块技术要点分析
人工智能·无人机·科普·高科技·云卓科技