FewShotPromptWithTemplates Class — langchain Architecture
Architecture documentation for the FewShotPromptWithTemplates class in few_shot_with_templates.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 713e8d01_f24d_0ae6_d363_33f3f321e867["FewShotPromptWithTemplates"] a3097815_cadb_7ebc_b29a_72117ccf6e27["StringPromptTemplate"] 713e8d01_f24d_0ae6_d363_33f3f321e867 -->|extends| a3097815_cadb_7ebc_b29a_72117ccf6e27 5b0211c9_fbcc_94a9_d100_7d26aec78d95["few_shot_with_templates.py"] 713e8d01_f24d_0ae6_d363_33f3f321e867 -->|defined in| 5b0211c9_fbcc_94a9_d100_7d26aec78d95 a3f0d1d5_bb71_f698_1648_c88741366b81["get_lc_namespace()"] 713e8d01_f24d_0ae6_d363_33f3f321e867 -->|method| a3f0d1d5_bb71_f698_1648_c88741366b81 3ce04d7d_cb10_77fb_04d5_c0bbbb0e7bd0["check_examples_and_selector()"] 713e8d01_f24d_0ae6_d363_33f3f321e867 -->|method| 3ce04d7d_cb10_77fb_04d5_c0bbbb0e7bd0 6351a5e0_1324_9ff2_cc0b_c6855751b691["template_is_valid()"] 713e8d01_f24d_0ae6_d363_33f3f321e867 -->|method| 6351a5e0_1324_9ff2_cc0b_c6855751b691 cf752071_f46a_dd3b_3e62_5b429e8d1c30["_get_examples()"] 713e8d01_f24d_0ae6_d363_33f3f321e867 -->|method| cf752071_f46a_dd3b_3e62_5b429e8d1c30 93fc120e_b1a5_fcae_169a_ef5b5f0fa7ea["_aget_examples()"] 713e8d01_f24d_0ae6_d363_33f3f321e867 -->|method| 93fc120e_b1a5_fcae_169a_ef5b5f0fa7ea e11fdc59_f61b_01d0_db98_f83ce25c7a66["format()"] 713e8d01_f24d_0ae6_d363_33f3f321e867 -->|method| e11fdc59_f61b_01d0_db98_f83ce25c7a66 1be82c67_7cbe_640e_5a7c_6cbdd88cb446["aformat()"] 713e8d01_f24d_0ae6_d363_33f3f321e867 -->|method| 1be82c67_7cbe_640e_5a7c_6cbdd88cb446 54e512c7_1cdb_8653_99d2_1aa95d495416["_prompt_type()"] 713e8d01_f24d_0ae6_d363_33f3f321e867 -->|method| 54e512c7_1cdb_8653_99d2_1aa95d495416 e5717bb4_4164_ada5_65e0_50cbf5c4203e["save()"] 713e8d01_f24d_0ae6_d363_33f3f321e867 -->|method| e5717bb4_4164_ada5_65e0_50cbf5c4203e
Relationship Graph
Source Code
libs/core/langchain_core/prompts/few_shot_with_templates.py lines 18–230
class FewShotPromptWithTemplates(StringPromptTemplate):
"""Prompt template that contains few shot examples."""
examples: list[dict] | None = None
"""Examples to format into the prompt.
Either this or `example_selector` should be provided.
"""
example_selector: BaseExampleSelector | None = None
"""`ExampleSelector` to choose the examples to format into the prompt.
Either this or `examples` should be provided.
"""
example_prompt: PromptTemplate
"""`PromptTemplate` used to format an individual example."""
suffix: StringPromptTemplate
"""A `PromptTemplate` to put after the examples."""
example_separator: str = "\n\n"
"""String separator used to join the prefix, the examples, and suffix."""
prefix: StringPromptTemplate | None = None
"""A `PromptTemplate` to put before the examples."""
template_format: PromptTemplateFormat = "f-string"
"""The format of the prompt template.
Options are: `'f-string'`, `'jinja2'`, `'mustache'`.
"""
validate_template: bool = False
"""Whether or not to try validating the template."""
@classmethod
def get_lc_namespace(cls) -> list[str]:
"""Get the namespace of the LangChain object.
Returns:
`["langchain", "prompts", "few_shot_with_templates"]`
"""
return ["langchain", "prompts", "few_shot_with_templates"]
@model_validator(mode="before")
@classmethod
def check_examples_and_selector(cls, values: dict) -> Any:
"""Check that one and only one of examples/example_selector are provided."""
examples = values.get("examples")
example_selector = values.get("example_selector")
if examples and example_selector:
msg = "Only one of 'examples' and 'example_selector' should be provided"
raise ValueError(msg)
if examples is None and example_selector is None:
msg = "One of 'examples' and 'example_selector' should be provided"
raise ValueError(msg)
return values
@model_validator(mode="after")
def template_is_valid(self) -> Self:
"""Check that prefix, suffix, and input variables are consistent."""
if self.validate_template:
input_variables = self.input_variables
expected_input_variables = set(self.suffix.input_variables)
expected_input_variables |= set(self.partial_variables)
if self.prefix is not None:
expected_input_variables |= set(self.prefix.input_variables)
missing_vars = expected_input_variables.difference(input_variables)
if missing_vars:
msg = (
f"Got input_variables={input_variables}, but based on "
f"prefix/suffix expected {expected_input_variables}"
)
raise ValueError(msg)
else:
self.input_variables = sorted(
set(self.suffix.input_variables)
| set(self.prefix.input_variables if self.prefix else [])
Extends
Source
Frequently Asked Questions
What is the FewShotPromptWithTemplates class?
FewShotPromptWithTemplates is a class in the langchain codebase, defined in libs/core/langchain_core/prompts/few_shot_with_templates.py.
Where is FewShotPromptWithTemplates defined?
FewShotPromptWithTemplates is defined in libs/core/langchain_core/prompts/few_shot_with_templates.py at line 18.
What does FewShotPromptWithTemplates extend?
FewShotPromptWithTemplates extends StringPromptTemplate.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free