few-shot提示词模板就是在你向大模型提出一个问题,但是由于下面几种原因:
1.回复的格式有严格要求:例如需要返回json格式的数据
2.问题复杂,需要按照模板格式化输出
3.需要大模型模仿某种语气或风格进行回复
时,在提示词中插入一些例子供大模型学习
具体来看代码
from langchain.prompts import PromptTemplate,FewShotPromptTemplate
"""
下面是例子,一个字典列表,每个字典是一个输入输出对
"""
examples=[
{"input": "这电影太棒了,我看得热血沸腾!", "output": "正面"},
{"input": "等了两个小时,结果菜还是凉的。", "output": "负面"},
{"input": "今天的天气是晴天。", "output": "中性"},
{"input": "演员的演技很到位,但剧情实在太拖沓了。", "output": "负面"},
]
"""
示例格式化器example_prompt
"""
#首先给出每个例子的模板
example_template="""
用户输入:{input}
AI回复:{output}
"""
#然后将其放入提示词模板中
example_prompt=PromptTemplate(
input_variables=["input","output"],
template=example_template
)
"""
使用FewShotPromptTemplate将他们组装起来,
由例子字典列表、示例格式化器、前缀(系统提示词)、后缀(用户输入和AI回复的开头)、告诉大模型用户输入的是什么、例子分隔组成
"""
few_shot_prompt_template=FewShotPromptTemplate(
examples=examples,
example_prompt=example_prompt,
prefix="你是一个专业的情感分析专家,可以根据用户的输入判断其情感倾向",
suffix="用户输入:{user_input}\nAI回复:",
input_variables=["user_input"],
example_separator="\n\n"
)
user_ques="今天天气太好了"
prompt=few_shot_prompt_template.format(user_input=user_ques)
print(prompt)输出如下:

但是如果示例非常多,全放入提示词中会消耗大量token,我们可以使用示例选择器example_selector,最常用的选择器是长基选择器LengthBasedExampleSelector
使用方法也很简单,导入包,按照上面的方法定义好例子字典列表、示例格式化器后,定义一个长基示例选择器,将例子、格式化器、限长值传入其中,最后组合成few-shot提示词模板时,将原本的examples替换成example_selector即可,具体请看代码
from langchain.prompts import PromptTemplate,FewShotPromptTemplate
from langchain.prompts.example_selector import LengthBasedExampleSelector
"""
下面是例子,一个字典列表,每个字典是一个输入输出对
"""
examples=[
{"input": "这电影太棒了,我看得热血沸腾!", "output": "正面"},
{"input": "等了两个小时,结果菜还是凉的。", "output": "负面"},
{"input": "今天的天气是晴天。", "output": "中性"},
{"input": "演员的演技很到位,但剧情实在太拖沓了。", "output": "负面"},
]
"""
示例格式化器example_prompt
"""
#首先给出每个例子的模板
example_template="""
用户输入:{input}
AI回复:{output}
"""
#然后将其放入提示词模板中
example_prompt=PromptTemplate(
input_variables=["input","output"],
template=example_template
)
"""
创建示例选择器
"""
example_selector=LengthBasedExampleSelector(
examples=examples,
example_prompt=example_prompt,
max_length=15
)
"""
使用FewShotPromptTemplate将他们组装起来,
由例子字典列表、示例格式化器、前缀(系统提示词)、后缀(用户输入和AI回复的开头)、告诉大模型用户输入的是什么、例子分隔组成
"""
few_shot_prompt_template=FewShotPromptTemplate(
example_selector=example_selector,
example_prompt=example_prompt,
prefix="你是一个专业的情感分析专家,可以根据用户的输入判断其情感倾向",
suffix="用户输入:{user_input}\nAI回复:",
input_variables=["user_input"],
example_separator="\n\n"
)
user_ques="今天天气太好了"
prompt=few_shot_prompt_template.format(user_input=user_ques)
print(prompt)

除了长基选择器以外,还有基于最大余弦相似度的选择器和N-gram选择器,下面分别做介绍
1、最大余弦相似度选择器MaxMarginalRelevanceExampleSelector
这个选择器能够选出既在语义上与新问题相似,而示例之间内容重复度低的示例
需要的包有
pip install faiss-cup sentence-transforms langchain_community除此之外,还需要去Huggingface上下载一个用来做向量嵌入的模型bge-lange-zh-v1.5
做完上述准备工作就可以开始写最大余弦相似度选择器了
#首先配置好嵌入模型
model_path="这里填入bge-large-zh-v1.5的路径"
embeddings=HuggingFaceEmbeddings(model_name=model_path)
#创建最大余弦相似度选择器,将示例字典列表,嵌入模型,计算模块Faiss,需要的示例数作为参数传入
example_selector=MaxMarginalRelevanceExampleSelector.from_examples(
examples,
embeddings,
Faiss,
k=1
)写好之后直接替换上面的few-shot-prompt-template中的example_selector即可

2、N-gram重叠选择器
N-gram重叠选择器只看表面,而不去理解语义,重叠的内容越多,被选出来的可能性越大
需要的包有
pip install nltk导入必要的包后就可以写N-gram选择器了
example_selector=NGramOverlapExampleSelector(
examples=examples,
examples_prompt=examples_prompt,
threshold=-1
)同样替换上面的选择器即可
这里的阈值,若是-1,则按重叠分数给示例排序,从上到下分数递减:

若是大于1:排除所有示例,返回一个空列表
若是等于0:排除没有重叠内容的示例
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。