langchain系列:Model I/O模块之-Prompts


  langchain是基于大语言模型而开发的一个框架,既然是基于大语言模型,自然最重要的就是先要介绍Model I/O模块。

Model I/O简介

  Model I/O模块其实就是提供了语言模型的基础构建接口,那既然是提供构建的接口,我们首先要知道,构建一个模型到底需要哪一些部分。官方给出了一个图例如下:
在这里插入图片描述
  从上面可以看出,在整个工作的流程中,数据通过一定的格式(Format)组织起来,送入到模型中进行预测(Predict),最后将预测结果进行解析(Parse)输出。
从上面可以总结出,整个模型的构建包括了:

  • 输入部分
  • 语言模型构建部分
  • 输出部分

输入部分(Prompts)

  在LLM领域,我们把模型的输入称作Prompts,因为对于大语言模型来说,你如果要用好他,通常不会简单的抛出一个问题给他,而是会给他一些前提设定,甚至还会给他看一些案例,诸如你问chatgpt:“能不能帮我翻译一下下面的英文”,此时最好是不要直接发问,而是应该先和模型说:你是一个中英翻译专家,这样它才能回答得更好。所以我们可以大致拆解一下一个prompt大致有哪些东西组成

  • 对语言模型的身份设定描述(可选)
  • 一些回答案例,帮助回答得更好(可选)
  • 具体要问的问题(必选)

langchain针对以上的prompts的三个组成部分,提供了一些组件和接口来实现。

PromptTemplate

  PromptTemplate提供了语言模型输入的构建方式,下面我先抛出代码,然后进行解释

from langchain import PromptTemplate

prompt = PromptTemplate(
    input_variables=['product'],
    template="""
        what is a good name for a company that makes {product}?
    """
)

print(prompt.format(product="colorful socks"))
what is a good name for a company that makes colorful socks?

  在PromptTemplate中,需要指定两个东西:

template:我们要发问的问题,但是当我们用户在发问的时候,并不需要每一次都输入完整的这句话,而只需要输入一小部分,这一小部分就是用中括号括起来的变量,这个变量需要在input_variables变量中指定;
input_variables:指定template中实际需要用户输入的变量;

  从上面的解释可以看出,我们在对话的过程中,实际只需要输入input_variables的部分,其他的不需要重复输入,这也是PromptTemplate中有一个Template的原因,它是一个固定模板。同时也可以理解为什么最开始官方给的图中Format那里就像一个插槽一样,我们只需要讲用户输入的少部分数据插入到模板中,组成完整的prompt,然后送给模型就可以了。

改进一:设定模型角色
  为了让模型回答得更好,可以设定模型的角色,其实也就是添加了一句说明

from langchain import PromptTemplate

prompt = PromptTemplate(
    input_variables=['product'],
    template="""
        You are a naming consultant for new companies.
        what is a good name for a company that makes {product}?
    """
)

print(prompt.format(product="colorful socks"))
from_template

  当然以上的初始化需要显示的在input_variables中指定我们需要输入的变量,如果你角色这很麻烦,PromptTemplate还提供了一个from_template方法,让langchain自动识别出需要输入的变量。

from langchain import PromptTemplate
template1 = """
    You are a naming consultant for new companies.
    what is a good name for a company that makes {product}?
"""
prompt = PromptTemplate.from_template(template1)
print(prompt.format(product="colorful socks"))

ChatPromptTemplate

 在langchain中,对话模型(Chat Model)和大语言模型(LLMs)是稍有区分的,对话模型可以理解为是LLMs高级封装,它有更规范的输入和输出,而规范的输出就是通过ChatPromptTemplate来实现的。
  在上面一小节中,是针对LLMs的,可以看出,我们关于模型的身份设定,人类提问是不做区分的,就在一个字符串中一起描述就行,但是在Chat Model是严格区分的。分别用SystemMessagePromptTemplate和SystemMessagePromptTemplate来指定。

下面对上一小节的内容进行改造

from langchain.prompts import (
    ChatPromptTemplate,
    PromptTemplate,
    SystemMessagePromptTemplate,
    AIMessagePromptTemplate,
    HumanMessagePromptTemplate,
)

system_prompt = PromptTemplate(
    input_variables=['test'],
    template="""
        You are a naming consultant for new {test}.
    """
)
system_message_prompt = SystemMessagePromptTemplate(prompt=system_prompt)
human_prompt = PromptTemplate(
    input_variables=['product'],
    template="""
        what is a good name for a company that makes {product}?
    """
)
human_message_prompt = HumanMessagePromptTemplate(prompt=human_prompt)

chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt,human_message_prompt])
print(chat_prompt.format_prompt(test='compies',product='colorful socks'))

 可以看出,在这里对于模型身份的设定,会用SystemMessagePromptTemplate进行包装,而我们的提问,会用HumanMessagePromptTemplate进行包装。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The error message you encountered, "Can't load '/usr/local/MATLAB/R2017b/bin/glnxa64/libmwdastudio.so': /lib/x86_64-linux-gnu/libfontconfig.so.1: undefined symbol: FT_Done_MM_Var," suggests a problem with the fontconfig library used by MATLAB. To solve this issue, you can try the following steps: 1. Update fontconfig library: First, try updating the fontconfig library on your system. You can do this by running the following command in your terminal: ``` sudo apt-get update sudo apt-get install libfontconfig1 ``` This will update the fontconfig library to the latest version available in your Linux distribution's package manager. 2. Check library paths: Ensure that the library path for fontconfig is correctly set in MATLAB. You can do this by running the following command in MATLAB's command window: ``` getenv LD_LIBRARY_PATH ``` Make sure the output includes the correct path to the fontconfig library (e.g., `/usr/lib/x86_64-linux-gnu`). 3. Verify system compatibility: Verify that your system is compatible with MATLAB 2017b. Ensure that you are using a supported version of Linux and that all system requirements for MATLAB are met. 4. Reinstall MATLAB: If the above steps do not resolve the issue, you may need to reinstall MATLAB. Uninstall MATLAB completely and then reinstall it, making sure to follow all installation instructions and prompts carefully. If the problem persists after trying these steps, it might be helpful to contact MathWorks support for further assistance, as they can provide more specific guidance tailored to your system and setup.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值