随笔录--LangChain AI应用开发入门 - Model I/O模块中的一些基础概念

LLMs 模型

LLM(Large Language Model),大语言模型,在这里指的LangChain封装的LLM核心组件,指的是纯文本补全模型。Langchain封装的 API 将字符串提示作为输入,输出为补全后的字符串。例如对接的OpenAI 的 GPT-3。

ChatModels 聊天模型

聊天模型是LangChain封装的基于 LLM 的对话聊天模式核心组件,LangChain封装的 API 与LLM不同。Chat Model是将聊天对话消息表作为输入,输出的是 AI 消息,而不是字符串。例如对接的GPT-4 和 Anthopic 的 Claude-2 。

不同大模型在与其对话的时候有不同的提示词策略,与Anthropic对话输入提示词用XML效果最好,而OpenAI用JSON效果。这意味着用于一个LLM大模型的提示词可能不会转移到其他模型,因为转移后的回答效果可能会变差。LangChain提供了许多默认提示词,目前大多数提示词在 OpenAI 上运行良好,但其他模型上不一定,也没有进行大量测试。

Messages 消息

在LangChain中,ChatModel 将消息(Messages)作为输入和返回的数据格式(在OpenAI中,消息就是一个json数组),消息中每个对象都包含两个基本属性:角色(role)和内容(content),以及一个可选的属性additional_kwargs,这个additional_kwargs属性主要提供不是常规的输入参数,例如OpenAI的function_call函数,角色(role)对应的就是user(用户发送的问题)、system(系统级提示)、assistant(AI回答内容)。

messages = [
SystemMessage(content="You are Micheal Jordan."),
HumanMessage(content="Which shoe manufacturer are you associated with?"),
]

Message中主要的Python类:

1)HumanMessage

用户发送的消息。

2)SystemMessage

系统消息,与用户发送的消息一起作为输入内容,用于设定让LLM回答的时候按照一定的规则要求输出,部分大模型不支持。

3)AIMessage

LLM输出消息,即回答的内容。它可以作为输入传递给其他工具或模型,以进一步处理或生成响应。例如,一个AIMessage可以包含一个问题,然后传递给另外一个回答问题的工具,以获取答案。

4)FunctionMessage

工具输出消息,它包含了工具执行的函数调用信息。FunctionMessage中的function_call字段描述了工具执行的函数名称和参数。FunctionMessage可以作为输入传递给其他工具或模型,以执行特定的功能。例如,一个FunctionMessage可以包含一个文件移动工具的函数调用,将文件从一个位置移动到另一个位置。

5)ToolMessage

工具调用结果消息,它包含了工具执行的结果信息。ToolMessage中的too_call_id字段表示调用工具的唯一标识符。ToolMessage可以用于跟踪工具调用的状态和结果。例如,一个ToolMessage可以包含一个文件移动工具的调用结果,指示文件是否成功移动。

Prompts 提示词

用户询问大模型(例如chatGPT)的问题内容就是提示词。在ChatGPT平台上我们是直接输入内容的,但是在开发AI应用的时候,用户输入的内容未必就是直接传给ChatGPT,而是经过AI应用的转换(转换的过程中我们会增加更多的提示词内容,例如系统级提示词)。

在LangChain中,LangChain提供一系列管理提示词的功能组件,用于指导LLM生成相关的输出。Prompt可以是一个问题、一句话、一段文字或一组指令,旨在引导LLM生成特定的响应或完成特定的任务。

Prompt在语言模型中起到了重要的作用,它可以帮助模型理解上下文、生成相关的回答、完成句子或参与对话。通过提供清晰、明确的Prompt,可以引导模型生成更准确、相关的输出。

Message中主要的Python类:

1)PromptValue

用于将用户输入内容转换为字符串(适用于LLMs),也可以转换为消息列表(适用于Chat Models)。

prompt_value = PromptValue("Hi,")
# LLMs
text_input = prompt_value.to_string()
# ChatModel
chat_input = prompt_value.to_messages()

2)PromptTemplate

用于将用户输入格式化为适合模型处理的字符串消息。

prompt_template = PromptTemplate("你好,{name}。")
formatted_text = prompt_template.format(name="")

3)MessagePromptTemplate

基于PromptTemplate的扩展,用于生成具有特定角色(如system或user)的消息。

# 创建基本的提示模板
basic_template = PromptTemplate("我是{zyb}。")
# 定义用户消息模板
user_message_template = MessagePromptTemplate(role="user", template=basic_template)
# 生成用户消息
user_message = user_message_template.format(zyb="")

  • HumanMessagePromptTemplate

    生成用户输入的消息。

basic_template = PromptTemplate("我是{‘zyb’)
human_message_template = HumanMessagePromptTemplate(template=basic_template)
human_message = human_message_template.format(zyb='')
- SystemMessagePromptTemplate

  • 生成系统级(system)消息。

basic_template = PromptTemplate("我是{zyb}。")
system_message_template = SystemMessagePromptTemplate(template=basic_template)
system_message = system_message_template.format(zyb="")

  • AIMessagePromptTemplate

     AI回复的内容封装成AI类型的消息,用于识别消息列表中每条消息的类型。

basic_template = PromptTemplate("我是一个AI助手,非常高兴为你提供帮助。")
ai_message_template = AIMessagePromptTemplate(template=basic_template)
ai_message = ai_message_template.format()

4)MessagesPlaceholder

用作在对话消息列表中的一个占位符,它允许在动态地引入一系列之前的消息,即该会话的历史记录。这些消息可以是用户发送的、AI生成的,或是系统消息。

prompt = ChatPromptTemplate.from_messages([
SystemMessagePromptTemplate.from_template("You are a nice chatbot having a conversation with a human."),
# 指定一个变量名为 "chat_history",这个变量名将在后续的对话中用来存储历史聊天消息。
MessagesPlaceholder(variable_name="chat_history"),
HumanMessagePromptTemplate.from_template("{input}")
]
)

5)ChatPromptTemplate

生成完整聊天对话消息列表,包括多个不同类型的消息和占位符。

# 定义消息列表
chat_template = ChatPromptTemplate.from_messages(
[
("system", "你是AI助手,你的名称叫{name}."),
("human", "你可以做什么?"),
("ai", "我可以帮你查询AI内容"),
("human", "{user_input}"),
]
)

# 格式化数据
formatted_messages = chat_template.format_messages(name="", user_input="LangChain如何快速入门?")

  • 9
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值