langchain.prompts.few_shot.FewShotPromptTemplate

class langchain.prompts.few_shot.FewShotPromptTemplate(*, input_variables: List[str], output_parser: Optional[BaseOutputParser] = None, partial_variables: Mapping[str, Union[str, Callable[[], str]]] = None, examples: Optional[List[dict]] = None, example_selector: Optional[BaseExampleSelector] = None, validate_template: bool = True, example_prompt: PromptTemplate, suffix: str, example_separator: str = '\n\n', prefix: str = '', template_format: str = 'f-string')[source]

Bases: _FewShotPromptTemplateMixin, StringPromptTemplate

Prompt template that contains few shot examples.

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

param example_prompt: PromptTemplate [Required]

PromptTemplate used to format an individual example.

param example_selector: Optional[BaseExampleSelector] = None

ExampleSelector to choose the examples to format into the prompt. Either this or examples should be provided.

param example_separator: str = '\n\n'

String separator used to join the prefix, the examples, and suffix.

param examples: Optional[List[dict]] = None

Examples to format into the prompt. Either this or example_selector should be provided.

param input_variables: List[str] [Required]

A list of the names of the variables the prompt template expects.

param output_parser: Optional[BaseOutputParser] = None

How to parse the output of calling an LLM on this formatted prompt.

param partial_variables: Mapping[str, Union[str, Callable[[], str]]] [Optional]
param prefix: str = ''

A prompt template string to put before the examples.

param suffix: str [Required]

A prompt template string to put after the examples.

param template_format: str = 'f-string'

The format of the prompt template. Options are: ‘f-string’, ‘jinja2’.

param validate_template: bool = True

Whether or not to try validating the template.

validator check_examples_and_selector  »  all fields

Check that one and only one of examples/example_selector are provided.

dict(**kwargs: Any) Dict[source]

Return a dictionary of the prompt.

format(**kwargs: Any) str[source]

Format the prompt with the inputs.

Parameters

**kwargs – Any arguments to be passed to the prompt template.

Returns

A formatted string.

Example:

prompt.format(variable1="foo")
format_prompt(**kwargs: Any) PromptValue

Create Chat Messages.

invoke(input: Dict, config: langchain.schema.runnable.RunnableConfig | None = None) PromptValue
partial(**kwargs: Union[str, Callable[[], str]]) BasePromptTemplate

Return a partial of the prompt template.

save(file_path: Union[Path, str]) None

Save the prompt.

Parameters

file_path – Path to directory to save prompt to.

Example: .. code-block:: python

prompt.save(file_path=”path/prompt.yaml”)

validator template_is_valid  »  all fields[source]

Check that prefix, suffix, and input variables are consistent.

to_json() Union[SerializedConstructor, SerializedNotImplemented]
to_json_not_implemented() SerializedNotImplemented
validator validate_variable_names  »  all fields

Validate variable names do not include restricted names.

property lc_attributes: Dict

Return a list of attribute names that should be included in the serialized kwargs. These attributes must be accepted by the constructor.

property lc_namespace: List[str]

Return the namespace of the langchain object. eg. [“langchain”, “llms”, “openai”]

property lc_secrets: Dict[str, str]

Return a map of constructor argument names to secret ids. eg. {“openai_api_key”: “OPENAI_API_KEY”}

property lc_serializable: bool

Return whether the prompt template is lc_serializable.

Returns

Boolean indicating whether the prompt template is lc_serializable.

model Config[source]

Bases: object

Configuration for this pydantic object.

arbitrary_types_allowed = True
extra = 'forbid'

Examples using FewShotPromptTemplate