[标题内容]

释放双眼,带上耳机,听听看~!

三个要素:
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

  1. 从json文件加载,
    1. json文件内容
    {
        "_type": "prompt",//prompt类型
        "input_variables": ["adjective", "content"],//参数名称
        "template": "Tell me a {adjective} joke about {content}."//prompt Template
    }
    
    1. python文件内容
    from langchain.prompts import load_prompt
    
    prompt = load_prompt("simple_prompt.json")
    print(prompt.format(adjective="funny", content="chickens"))
    
  2. 通过json读取外部文件加载
    1. txt文件内容,存储 prompt Template
    Tell me a {adjective} joke about {content}.
    
    1. json文件内容
    {
    "_type": "prompt",//prompt类型
    "input_variables": ["adjective", "content"],//参数名称
    "template_path": "https://b2.7b2.com/Users/xxx/demo.txt"//prompt Template的文件地址
     }
    
    1. python文件内容
    prompt = load_prompt("https://b2.7b2.com/Users/xxx/demo.json")
    print(prompt.format(adjective="funny", content="chickens"))
    

少样本训练的template

  1. 样本数据文件+template文件:样本数据放在单独的文件里,通过json文件去读取 样本数据文件+template文件

    1. 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": "https://b2.7b2.com/Users/xxx/examples.json",//样本数据的来源文件地址
        "suffix": "Input: {adjective}nOutput:"//后缀,即问题
        }
    
    1. 样本数据文件
    [
    {"input": "happy", "output": "sad"},
    {"input": "tall", "output": "short"}
    ]
    
    1. 调用template
prompt = load_prompt("https://b2.7b2.com/Users/xxx/few_shot_prompt.json")
print(prompt.format(adjective="funny"))
  1. template文件:样本数据直接放在充当template配置项的json文件里,核心修改是examples配置项

    1. 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:"//后缀,即问题
    }
    
    1. 调用template
        prompt = load_prompt("https://b2.7b2.com/Users/xxx/few_shot_prompt_example_in.json")
        print(prompt.format(adjective="funny"))
    
  2. template文件+样本数据template文件+样本数据:样本也分割成template+数据

    1. 样本数据文件
    [
        {"input": "happy", "output": "sad"},
        {"input": "tall", "output": "short"}
    ]
    
    1. 样本template
    {
        "_type": "prompt",
        "input_variables": ["input", "output"],
        "template": "Input: {input}nOutput: {output}" 
    }
    
    1. template所在的json文件
    {
        "_type": "few_shot",
    "input_variables": ["adjective"],
        "prefix": "Write antonyms for the following words.",
        //指定样本数据模板的文件位置
        "example_prompt_path": "https://b2.7b2.com/Users/xxx/few_shot_prompt.json",
        //指定样本数据的文件位置
        "examples": "https://b2.7b2.com/Users/xxx/examples.json",
        "suffix": "Input: {adjective}nOutput:"
    }
    
  3. 调用template

    在template里加上解析输入的prompt以及输出格式

    1. 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"
    }
    
    1. 调用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模板

  1. 确定input_variables属性值
  2. 实现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:

本网站的内容主要来自互联网上的各种资源,仅供参考和信息分享之用,不代表本网站拥有相关版权或知识产权。如您认为内容侵犯您的权益,请联系我们,我们将尽快采取行动,包括删除或更正。
AI教程

ChatGPT介绍及使用方法

2023-12-22 13:50:14

AI教程

PyTorch 2.0.0新手教程:注册算子的主要方式

2023-12-22 14:01:00

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索