1.zero-shot(零样本学习)
# 导入PromptTemplate类,用于创建和管理提示模板
from langchain.prompts import PromptTemplate
# 定义一个提示模板字符串,其中包含一个占位符{subject},用于后续的故事主题
template = "给我将一个关于{subject}的故事"
# 创建PromptTemplate实例,传入模板字符串和输入变量列表
# 此处的输入变量即是我们之前定义的模板中的占位符,用于在生成提示时替换具体值
prompt = PromptTemplate(template=template, input_variables=["subject"])
# 使用.format()方法将特定主题(老虎)填入模板,生成具体的提示文本
prompt_text = prompt.format(subject="老虎")
# 打印生成的提示文本
print(prompt_text)
2.few-shot(少样本学习)
# 导入LangChain核心提示库中的FewShotPromptTemplate和PromptTemplate类
from langchain_core.prompts import FewShotPromptTemplate
from langchain_core.prompts import PromptTemplate
# 示例
examples = [
{'word': '大', 'antonym': '小'},
{'word': '黑', 'antonym': '白'},
{'word': '高', 'antonym': '低'},
{'word': '强', 'antonym': '弱'},
]
# 样例
prompt_template = '{word}的反义词是{antonym}'
# 创建一个PromptTemplate对象,用于具体示例的呈现
example_prompt = PromptTemplate(input_variables=['word', 'antonym'], template=prompt_template)
# 初始化一个FewShotPromptTemplate对象,用于基于少量示例进行提示
# 指定例子、提示格式、前缀说明、后缀提问格式、输入变量和例子分隔符
few_shot_prompt = FewShotPromptTemplate(
examples=examples,# 模型训练的案例
example_prompt=example_prompt,# 样例
prefix&#