【LangChain】Prompts custom prompt template

LangChain learning documentation


overview

Suppose we wish LLMto generate an English explanation of a given function name. To achieve this task, we will create a custom prompt template that takes a function name as input and format the prompt template to provide the source code of the function.

Why do you need custom prompt templates?

LangChainA set of default prompt templates are provided that can be used to generate prompts for various tasks. However, in some cases, the default prompt template may not meet our needs. For example, we might want to create a prompt template that contains specific dynamic instructions that fit our language model. In this case, you can create a custom prompt template.

View the current set of default prompt templates here .

Creating a Custom Prompt Template

There are essentially two different prompt templates available - 字符串提示模板and 聊天提示模板.

1. The string prompt template provides simple prompts in string format.

2. Chat prompt template generates more structured prompts for use with the chat API.

In this guide, we will create a custom prompt using the String prompt template.

To create a custom string prompt template, there are two requirements:

① It has an input_variablesattribute that exposes the input variables required by the prompt template.
② It exposes a format method that accepts input_variableskeyword arguments corresponding to the expected and returns a formatted prompt.

We'll create a custom prompt template that takes a function name as input and formats the prompt to provide the function's source code. To achieve this, we first create a function that will return the source code of the function given the name.

import inspect


def get_source_code(function_name):
    # 获取函数的源码
    return inspect.getsource(function_name)

Next, we'll create a custom prompt template that takes the function name as input and format the prompt template to provide the function's source code.

from langchain.prompts import StringPromptTemplate
from pydantic import BaseModel, validator

#给定函数名称和源代码,生成该函数的英语解释。
#函数名称:{函数名称}
#源代码:
#{源代码}
#解释:
PROMPT = """\
Given the function name and source code, generate an English language explanation of the function.
Function Name: {function_name}
Source Code:
{source_code}
Explanation:
"""


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."""

    @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

    def format(self, **kwargs) -> str:
        # 获取函数的源码
        source_code = get_source_code(kwargs["function_name"])

        # 生成要发送到语言模型的提示
        # __name__是当前模块名
        prompt = PROMPT.format(
            function_name=kwargs["function_name"].__name__, source_code=source_code
        )
        return prompt

    def _prompt_type(self):
        return "function-explainer"

Reference api:

Use the custom prompt template

Now that we have created a custom prompt template, we can use it to generate prompts for our tasks.

fn_explainer = FunctionExplainerPromptTemplate(input_variables=["function_name"])

# 生成函数“get_source_code”的提示
prompt = fn_explainer.format(function_name=get_source_code)
print(prompt)

result:

    给定函数名称和源代码,生成该函数的英语解释。
    函数名称: get_source_code
    源码:
    def get_source_code(function_name):
        # Get the source code of the function
        return inspect.getsource(function_name)
    
    Explanation:
    

Summarize

This article explains how to create custom prompts:

  1. First define a string containing variables. The variables are used {}, such as:
"""\
Given the function name and source code, generate an English language explanation of the function.
Function Name: {function_name}
Source Code:
{source_code}
Explanation:
"""
  1. Use PROMPT.format(xxx)functions to format, such as:
prompt = PROMPT.format(
            function_name=kwargs["function_name"].__name__, source_code=source_code
        )

Reference address:

https://python.langchain.com/docs/modules/model_io/prompts/prompt_templates/custom_prompt_template

Guess you like

Origin blog.csdn.net/u013066244/article/details/132138776
Recommended