主流大模型的对话模型ChatModel实现

介绍

聊天模型是大语言模型(LLM)的一种封装。聊天模型不再局限于提供一个“文本输入,文本输出”的API能力,而是提供一个以“聊天消息”作为输入和输出的接口。

通过向聊天模型传递一个或多个消息,可以获取聊天完成的结果,响应也是一个消息。目前Langchain支持的消息类型有AIMessage、HumanMessage、SystemMessage和ChatMessage,其中ChatMessage可以接受一个Role参数,传递任意角色。大多数情况下,只需要处理HumanMessage、AIMessage和SystemMessage。

国内大模型的ChatModel实现

一、通义千问
from langchain_community.chat_models import ChatTongyi
from langchain_core.messages import HumanMessage

# 创建通义聊天模型
chat_tongyi = ChatTongyi()

# 一次性返回
res = chat_tongyi.invoke([HumanMessage(content="请给我讲个笑话吧!")])
print(res)

# 流式返回
chat_tongyi = ChatTongyi(streaming=True)
stream_res = chat_tongyi.stream([HumanMessage(content="请给我讲个笑话吧!")])

try:
    for chunk in stream_res:
        print(chunk)
except TypeError as e:
    print("")
二、文心一言
from langchain_community.chat_models import QianfanChatEndpoint
from langchain_core.messages import HumanMessage

# 创建千帆聊天模型
chat_qianfan = QianfanChatEndpoint()

# 一次性返回
res = chat_qianfan.invoke([HumanMessage(content="请给我讲个笑话吧!")])
print(res.content)

# 流式返回
chat_qianfan = QianfanChatEndpoint(streaming=True)
stream_res = chat_qianfan.stream([HumanMessage(content="请给我讲个笑话吧!")])

try:
    for chunk in stream_res:
        print(chunk.content)
except TypeError as e:
    print("")
三、ChatGLM
from langchain_openai import ChatOpenAI
# 导入消息
from langchain.schema import (
    HumanMessage,
    SystemMessage,
)
# 初始化大语言模型
llm = ChatOpenAI(
    model="glm-4",
    temperature=0.95,
)

# 创建系统消息和人类消息
system_message = SystemMessage(content="""
你是一个家政机器人,你拥有完成主人提出的所有家政任务的能力,你熟悉主人家庭里面所有物品的方位,并会使用相关工具。
你已装配传感器、导航、机械手臂等执行任务所需要的所有必备部件。
你会接收一个主人指令,然后仔细分析,细化为具体步骤,最终完成用户指令。
你会在任务完成后,提示主人:主人,您还有什么吩咐?
""")
human_message = HumanMessage(content="请帮把客厅已损坏的吊灯更换为新的吊灯")

# 调用大模型回答问题
res = llm.invoke([system_message, human_message])

print(res.content)

四、星火

虽然星火大模型的stream方式调用不会出错,但是目前不是流式返回,应该是还不支持。

from langchain_community.chat_models import ChatSparkLLM
from langchain_core.messages import HumanMessage

# 创建星火聊天模型
chat_spark = ChatSparkLLM()

# 一次性返回
res = chat_spark.invoke([HumanMessage(content="请给我讲个笑话吧!")])
print(res.content)

# 流式返回
chat_spark = ChatSparkLLM(streaming=True)
stream_res = chat_spark.stream([HumanMessage(content="请给我讲个笑话吧!")])

try:
    for chunk in stream_res:
        print(chunk.content)
except TypeError as e:
    print("")
五、百川
from langchain_community.chat_models import ChatBaichuan
from langchain_core.messages import HumanMessage

# 创建百川聊天模型
chat_baichuan = ChatBaichuan()

# 一次性返回
res = chat_baichuan.invoke([HumanMessage(content="请给我讲个笑话吧!")])
print(res.content)


# 流式返回
chat_baichuan = ChatBaichuan(streaming=True)
stream_res = chat_baichuan.stream([HumanMessage(content="请给我讲个笑话吧!")])

try:
    for chunk in stream_res:
        print(chunk.content)
except TypeError as e:
    print("")

使用聊天模板

以通义千问为例

from langchain_community.chat_models import ChatTongyi
from langchain_core.prompts import HumanMessagePromptTemplate, ChatPromptTemplate
from langchain_core.prompts import SystemMessagePromptTemplate

# 创建系统消息模板
system_template = "你是一个翻译顾问,能将{input_language}翻译成{output_language}"
system_message_prompt = SystemMessagePromptTemplate.from_template(system_template)

# 创建人类消息模板
human_template = "{input_text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)

# 合并人类消息模板和系统消息模板
chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])

# 创建通义聊天模型
chat_tongyi = ChatTongyi()

res = chat_tongyi.invoke(
    chat_prompt.format_prompt(input_language="中文", output_language="日语", input_text="我爱日本姑娘").to_messages())

print(res.content)
  • 13
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值