langchain
langchain copied to clipboard
Using Custom Prompt with FewShotPromptTemplate
I am trying to use a Custom Prompt Template as an example_prompt to the FewShotPromptTemplate. Although, I am getting a 'key error' template issue. Does FewShotPromptTemplate support using Custom Prompt Template ?
A snippet example for reference
from langchain import PromptTemplate, FewShotPromptTemplate
class CustomPromptTemplate(StringPromptTemplate, BaseModel):
@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:
# 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 = CustomPromptTemplate(input_variables=["function_name"])
few_shot_prompt = FewShotPromptTemplate(
# These are the examples we want to insert into the prompt.
examples=examples,
# This is how we want to format the examples when we insert them into the prompt.
example_prompt= fn_explainer,
# The prefix is some text that goes before the examples in the prompt.
# Usually, this consists of intructions.
prefix="Give the antonym of every input",
# The suffix is some text that goes after the examples in the prompt.
# Usually, this is where the user input will go
suffix="Word: {input}\nAntonym:",
# The input variables are the variables that the overall prompt expects.
input_variables=["input"],
# The example_separator is the string we will use to join the prefix, examples, and suffix together with.
example_separator="\n\n",
)
I also have this issue. It seems like FewShotPromptTemplate
strictly expects a PromptTemplate
subclass that has a template
attribute. Since the point of the StringPromptTemplate
is to be more general than the PromptTemplate
(because the relationship between input variables and their realisation in the prompt may be complex and require implementing bespoke logic in format
, i.e. cannot be expressed as a simple one-to-one substitution using template
), it would be nice to support this case in a few-shot context.
Then again, I am pretty new to this library so perhaps this is not implemented because it's considered an anti-pattern. Anyone have insight into this? It feels like a pretty common use-case.
I am also facing the same issue!
how do you finally use the fewshotprompttemplate with the model, say gtp-3.5?
same here
Hi, @piyushumate,
I'm helping the LangChain team manage their backlog and am marking this issue as stale. The issue you opened discusses the use of a Custom Prompt Template with FewShotPromptTemplate and encountering a 'key error' template issue. Several other users have also reported facing the same issue and are looking for a resolution. It seems that the issue is still unresolved, and there is a request for insight into whether this is a common use-case and if it is supported by the library.
Could you please confirm if this issue is still relevant to the latest version of the LangChain repository? If it is, please let the LangChain team know by commenting on the issue. Otherwise, feel free to close the issue yourself, or the issue will be automatically closed in 7 days. Thank you!