FewShotPromptTemplate Class — langchain Architecture
Architecture documentation for the FewShotPromptTemplate class in few_shot.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 938e1250_dc3f_175c_b4b0_06175d9760e1["FewShotPromptTemplate"] d2f3c5fa_0eaf_bafd_40aa_1d4884ff518c["_FewShotPromptTemplateMixin"] 938e1250_dc3f_175c_b4b0_06175d9760e1 -->|extends| d2f3c5fa_0eaf_bafd_40aa_1d4884ff518c af88ed16_4081_39eb_dd20_66bf45146eec["StringPromptTemplate"] 938e1250_dc3f_175c_b4b0_06175d9760e1 -->|extends| af88ed16_4081_39eb_dd20_66bf45146eec aa1f155e_f1a2_3609_0e63_d06bf3004d3f["few_shot.py"] 938e1250_dc3f_175c_b4b0_06175d9760e1 -->|defined in| aa1f155e_f1a2_3609_0e63_d06bf3004d3f 7cd01419_bca4_2df9_f724_5439d51db285["is_lc_serializable()"] 938e1250_dc3f_175c_b4b0_06175d9760e1 -->|method| 7cd01419_bca4_2df9_f724_5439d51db285 657d4103_fcc1_8b0d_2748_66dc4eda74cb["__init__()"] 938e1250_dc3f_175c_b4b0_06175d9760e1 -->|method| 657d4103_fcc1_8b0d_2748_66dc4eda74cb 00dbf837_a62a_5482_61ce_3931d54032a7["template_is_valid()"] 938e1250_dc3f_175c_b4b0_06175d9760e1 -->|method| 00dbf837_a62a_5482_61ce_3931d54032a7 aecf2087_50d0_5554_4bc1_b32b629d98fe["format()"] 938e1250_dc3f_175c_b4b0_06175d9760e1 -->|method| aecf2087_50d0_5554_4bc1_b32b629d98fe a1af40b0_77e5_5515_2418_602984733e1b["aformat()"] 938e1250_dc3f_175c_b4b0_06175d9760e1 -->|method| a1af40b0_77e5_5515_2418_602984733e1b c6d46ed0_6258_a608_62a1_7c7b5890a5f4["_prompt_type()"] 938e1250_dc3f_175c_b4b0_06175d9760e1 -->|method| c6d46ed0_6258_a608_62a1_7c7b5890a5f4 6557d49e_22cc_ae0f_6550_5a5c507f3acd["save()"] 938e1250_dc3f_175c_b4b0_06175d9760e1 -->|method| 6557d49e_22cc_ae0f_6550_5a5c507f3acd
Relationship Graph
Source Code
libs/core/langchain_core/prompts/few_shot.py lines 120–252
class FewShotPromptTemplate(_FewShotPromptTemplateMixin, StringPromptTemplate):
"""Prompt template that contains few shot examples."""
@classmethod
def is_lc_serializable(cls) -> bool:
"""Return `False` as this class is not serializable."""
return False
validate_template: bool = False
"""Whether or not to try validating the template."""
example_prompt: PromptTemplate
"""`PromptTemplate` used to format an individual example."""
suffix: str
"""A prompt template string to put after the examples."""
example_separator: str = "\n\n"
"""String separator used to join the prefix, the examples, and suffix."""
prefix: str = ""
"""A prompt template string to put before the examples."""
template_format: Literal["f-string", "jinja2"] = "f-string"
"""The format of the prompt template.
Options are: `'f-string'`, `'jinja2'`.
"""
def __init__(self, **kwargs: Any) -> None:
"""Initialize the few shot prompt template."""
if "input_variables" not in kwargs and "example_prompt" in kwargs:
kwargs["input_variables"] = kwargs["example_prompt"].input_variables
super().__init__(**kwargs)
@model_validator(mode="after")
def template_is_valid(self) -> Self:
"""Check that prefix, suffix, and input variables are consistent."""
if self.validate_template:
check_valid_template(
self.prefix + self.suffix,
self.template_format,
self.input_variables + list(self.partial_variables),
)
elif self.template_format:
self.input_variables = [
var
for var in get_template_variables(
self.prefix + self.suffix, self.template_format
)
if var not in self.partial_variables
]
return self
model_config = ConfigDict(
arbitrary_types_allowed=True,
extra="forbid",
)
def format(self, **kwargs: Any) -> str:
"""Format the prompt with inputs generating a string.
Use this method to generate a string representation of a prompt.
Args:
**kwargs: Keyword arguments to use for formatting.
Returns:
A string representation of the prompt.
"""
kwargs = self._merge_partial_and_user_variables(**kwargs)
# Get the examples to use.
examples = self._get_examples(**kwargs)
examples = [
{k: e[k] for k in self.example_prompt.input_variables} for e in examples
]
# Format the examples.
example_strings = [
self.example_prompt.format(**example) for example in examples
]
# Create the overall template.
Defined In
Source
Frequently Asked Questions
What is the FewShotPromptTemplate class?
FewShotPromptTemplate is a class in the langchain codebase, defined in libs/core/langchain_core/prompts/few_shot.py.
Where is FewShotPromptTemplate defined?
FewShotPromptTemplate is defined in libs/core/langchain_core/prompts/few_shot.py at line 120.
What does FewShotPromptTemplate extend?
FewShotPromptTemplate extends _FewShotPromptTemplateMixin, StringPromptTemplate.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free