三个要素:
1. 指令
2. 少样本比如的调集
3. 问题
模版格局:PromptTemplate默认用python字符串供给模版,也能够经过PromptTemplate的template_format参数替换模版言语
# Make sure jinja2 is installed before running this
//jinja2是python模板言语,参数语法和字符串不一致,是双括号,字符串是单括号
jinja2_template = "Tell me a {{ adjective }} joke about {{ content }}"
prompt_template = PromptTemplate.from_template(template=jinja2_template, template_format="jinja2")
prompt_template.format(adjective="funny", content="chickens")
# -> Tell me a funny joke about chickens.
模版的序列化:支持加载json文件、yaml文件 获取到Template,
json文件或许yaml文件中存储模板信息,
调用template的中心语法:
load_prompt("xxx文件地址")
零样本练习的template
- 从json文件加载,
- json文件内容,
{ "_type": "prompt",//prompt类型 "input_variables": ["adjective", "content"],//参数称号 "template": "Tell me a {adjective} joke about {content}."//prompt Template }
- python文件内容
from langchain.prompts import load_prompt prompt = load_prompt("simple_prompt.json") print(prompt.format(adjective="funny", content="chickens"))
- 经过json读取外部文件加载
- txt文件内容,存储 prompt Template
Tell me a {adjective} joke about {content}.
- json文件内容
{ "_type": "prompt",//prompt类型 "input_variables": ["adjective", "content"],//参数称号 "template_path": "/Users/xxx/demo.txt"//prompt Template的文件地址 }
- python文件内容
prompt = load_prompt("/Users/xxx/demo.json") print(prompt.format(adjective="funny", content="chickens"))
少样本练习的template
-
样本数据文件+template文件:样本数据放在独自的文件里,经过json文件去读取 样本数据文件+template文件
- json文件
{ "_type": "few_shot",//少样本练习类型 "input_variables": ["adjective"],//参数称号 "prefix": "Write antonyms for the following words.",//前缀,一般是指令 "example_prompt": { //样本数据 "_type": "prompt",//样本数据类型是文本 "input_variables": ["input", "output"],//样本数据中的输入参数 "template": "Input: {input}\nOutput: {output}"//样本数据的模板 }, "examples": "/Users/xxx/examples.json",//样本数据的来历文件地址 "suffix": "Input: {adjective}\nOutput:"//后缀,即问题 }
- 样本数据文件
[ {"input": "happy", "output": "sad"}, {"input": "tall", "output": "short"} ]
- 调用template
prompt = load_prompt("/Users/xxx/few_shot_prompt.json")
print(prompt.format(adjective="funny"))
-
template文件:样本数据直接放在充当template装备项的json文件里,中心修改是examples装备项,
- json文件
{ "_type": "few_shot",//少样本练习类型 "input_variables": ["adjective"],//参数称号 "prefix": "Write antonyms for the following words.",//前缀,一般是指令 "example_prompt": { //样本数据 "_type": "prompt",//样本数据类型是文本 "input_variables": ["input", "output"],//样本数据中的输入参数 "template": "Input: {input}\nOutput: {output}"//样本数据的模板 }, "examples": [ {"input": "happy", "output": "sad"}, {"input": "tall", "output": "short"} ],//样本数据 "suffix": "Input: {adjective}\nOutput:"//后缀,即问题 }
- 调用template
prompt = load_prompt("/Users/xxx/few_shot_prompt_example_in.json") print(prompt.format(adjective="funny"))
-
template文件+样本数据template文件+样本数据:样本也分割成template+数据
- 样本数据文件
[ {"input": "happy", "output": "sad"}, {"input": "tall", "output": "short"} ]
- 样本template
{ "_type": "prompt", "input_variables": ["input", "output"], "template": "Input: {input}\nOutput: {output}" }
- template地点的json文件
{ "_type": "few_shot", "input_variables": ["adjective"], "prefix": "Write antonyms for the following words.", //指定样本数据模板的文件方位 "example_prompt_path": "/Users/xxx/few_shot_prompt.json", //指定样本数据的文件方位 "examples": "/Users/xxx/examples.json", "suffix": "Input: {adjective}\nOutput:" }
-
调用template
在template里加上解析输入的prompt以及输出格局
- template文件
{ "input_variables": [ "question", "student_answer" ], "output_parser": { "regex": "(.*?)\\nScore: (.*)", "output_keys": [ "answer", "score" ], "default_output_key": null, "_type": "regex_parser" }, "partial_variables": {}, "template": "Given the following question and student answer, provide a correct answer and score the student answer.\nQuestion: {question}\nStudent Answer: {student_answer}\nCorrect Answer:", "template_format": "f-string", "validate_template": true, "_type": "prompt" }
- 调用template
prompt = load_prompt("prompt_with_output_parser.json") prompt.output_parser.parse("George Washington was born in 1732 and died in 1799.\nScore: 1/2")
输出:
{‘answer’: ‘George Washington was born in 1732 and died in 1799.’,
‘score’: ‘1/2’}
StringPromptTemplate:自定义一个template模板
- 确定input_variables特点值
- 完成format办法,确定template格局
import inspect
def get_source_code(function_name):
# Get the source code of the function
return inspect.getsource(function_name)
from langchain.prompts import StringPromptTemplate
from pydantic import BaseModel, validator
class FunctionExplainerPromptTemplate(StringPromptTemplate, BaseModel):
""" A custom prompt template that takes in the function name as input, and formats the prompt template to provide the source code of the function. """
### 确定input_variables特点值
@validator("input_variables")
def validate_input_variables(cls, v):
""" Validate that the input variables are correct. """
if len(v) != 1 or "function_name" not in v:
raise ValueError("function_name must be the only input_variable.")
return v
### 确定template格局
def format(self, **kwargs) -> str:
# Get the source code of the function
source_code = get_source_code(kwargs["function_name"])
# Generate the prompt to be sent to the language model
prompt = f"""
Given the function name and source code, generate an English language explanation of the function.
Function Name: {kwargs["function_name"].__name__}
Source Code:
{source_code}
Explanation:
"""
return prompt
def _prompt_type(self):
return "function-explainer"
fn_explainer = FunctionExplainerPromptTemplate(input_variables=["function_name"])
# Generate a prompt for the function "get_source_code"
prompt = fn_explainer.format(function_name=get_source_code)
print(prompt)
输出:
Given the function name and source code, generate an English language explanation of the function.
Function Name: get_source_code
Source Code:
def get_source_code(function_name):
return inspect.getsource(function_name)
Explanation: