langchain_contrib.prompts.choice package#

Submodules#

langchain_contrib.prompts.choice.prompt_value module#

Module defining prompts involving choices.

class langchain_contrib.prompts.choice.prompt_value.BaseChoicePrompt(*, prompt: PromptValue, choices: List[str])#

Bases: PromptValue

A prompt that involves picking from a number of choices.

This is just a wrapper around a regular PromptValue that preserves the choice information.

class Config#

Bases: object

Configuration for this pydantic object.

extra = 'forbid'#
choices: List[str]#

The list of choices to choose from.

classmethod from_prompt(prompt: PromptValue, choices: List[str]) BaseChoicePrompt#

Create one of the child classes from a base prompt.

prompt: PromptValue#

The encapsulated prompt that provides the actual prompt value.

to_messages() List[BaseMessage]#

Return prompt as messages.

to_string() str#

Return prompt as string.

class langchain_contrib.prompts.choice.prompt_value.ChatChoicePrompt(*, messages: List[BaseMessage], prompt: PromptValue, choices: List[str])#

Bases: BaseChoicePrompt, ChatPromptValue

A chat prompt that involves picking from a number of choices.

to_messages() List[BaseMessage]#

Return prompt as messages.

to_string() str#

Return prompt as string.

class langchain_contrib.prompts.choice.prompt_value.ChoiceStr(f_str: F, choices: List[str])#

Bases: F

String that keeps track of choices used to create this string.

choices: List[str]#
class langchain_contrib.prompts.choice.prompt_value.StringChoicePrompt(*, text: str, prompt: PromptValue, choices: List[str])#

Bases: BaseChoicePrompt, StringPromptValue

A string prompt that involves picking from a number of choices.

to_messages() List[BaseMessage]#

Return prompt as messages.

to_string() str#

Return prompt as string.

langchain_contrib.prompts.choice.template module#

Module that defines the choice prompt.

class langchain_contrib.prompts.choice.template.ChoicePromptTemplate(*, input_variables: List[str], output_parser: BaseOutputParser | None = None, partial_variables: Mapping[str, str | Callable[[], str]] = None, base_template: BasePromptTemplate, choices: List[str], choices_formatter: Callable[[List[str]], str] = None, choice_format_key: str = 'choices')#

Bases: BasePromptTemplate

A wrapper prompt template for picking from a number of choices.

This template preserves choice information in prompts.

base_template: BasePromptTemplate#

The base template that this class wraps around.

choice_format_key: str#

Which string is used for formatting choices in the template.

choices: List[str]#

The list of choices to pick from.

choices_formatter: ChoicesFormatter#

How to convert from the list of choices to a single string.

Utility functions to help with this include:

  • get_simple_joiner

  • get_oxford_comma_formatter

  • list_of_choices

format(**kwargs: Any) str#

Format the prompt with the inputs.

format_prompt(**kwargs: Any) BaseChoicePrompt#

Format the prompt while preserving the choices.

classmethod from_base_template(base_template: BasePromptTemplate, choices: List[str], **kwargs: Any) ChoicePromptTemplate#

Load a ChoicePromptTemplate from base templates.

classmethod from_messages(messages: Sequence[BaseMessagePromptTemplate | BaseMessage], choices: List[str], **kwargs: Any) ChoicePromptTemplate#

Load a ChoicePromptTemplate from message templates.

classmethod from_template(template: str, choices: List[str], **kwargs: Any) ChoicePromptTemplate#

Load a ChoicePromptTemplate from a text template.

input_variables: List[str]#

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

output_parser: BaseOutputParser | None#

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

partial_variables: Mapping[str, str | Callable[[], str]]#
langchain_contrib.prompts.choice.template.get_oxford_comma_formatter(conjunction: str = 'or') Callable[[List[str]], str]#

Get a choice formatter that respects the Oxford comma.

langchain_contrib.prompts.choice.template.get_simple_joiner(joiner: str = ', ') Callable[[List[str]], str]#

Get a choice formatter that’s just a simple joining of strings.

langchain_contrib.prompts.choice.template.list_of_choices(choices: List[str]) str#

Return a numerical list of choices.

Module contents#

Prompts and templates for choices.

class langchain_contrib.prompts.choice.BaseChoicePrompt(*, prompt: PromptValue, choices: List[str])#

Bases: PromptValue

A prompt that involves picking from a number of choices.

This is just a wrapper around a regular PromptValue that preserves the choice information.

class Config#

Bases: object

Configuration for this pydantic object.

extra = 'forbid'#
choices: List[str]#

The list of choices to choose from.

classmethod from_prompt(prompt: PromptValue, choices: List[str]) BaseChoicePrompt#

Create one of the child classes from a base prompt.

prompt: PromptValue#

The encapsulated prompt that provides the actual prompt value.

to_messages() List[BaseMessage]#

Return prompt as messages.

to_string() str#

Return prompt as string.

class langchain_contrib.prompts.choice.ChatChoicePrompt(*, messages: List[BaseMessage], prompt: PromptValue, choices: List[str])#

Bases: BaseChoicePrompt, ChatPromptValue

A chat prompt that involves picking from a number of choices.

to_messages() List[BaseMessage]#

Return prompt as messages.

to_string() str#

Return prompt as string.

class langchain_contrib.prompts.choice.ChoicePromptTemplate(*, input_variables: List[str], output_parser: BaseOutputParser | None = None, partial_variables: Mapping[str, str | Callable[[], str]] = None, base_template: BasePromptTemplate, choices: List[str], choices_formatter: Callable[[List[str]], str] = None, choice_format_key: str = 'choices')#

Bases: BasePromptTemplate

A wrapper prompt template for picking from a number of choices.

This template preserves choice information in prompts.

base_template: BasePromptTemplate#

The base template that this class wraps around.

choice_format_key: str#

Which string is used for formatting choices in the template.

choices: List[str]#

The list of choices to pick from.

choices_formatter: ChoicesFormatter#

How to convert from the list of choices to a single string.

Utility functions to help with this include:

  • get_simple_joiner

  • get_oxford_comma_formatter

  • list_of_choices

format(**kwargs: Any) str#

Format the prompt with the inputs.

format_prompt(**kwargs: Any) BaseChoicePrompt#

Format the prompt while preserving the choices.

classmethod from_base_template(base_template: BasePromptTemplate, choices: List[str], **kwargs: Any) ChoicePromptTemplate#

Load a ChoicePromptTemplate from base templates.

classmethod from_messages(messages: Sequence[BaseMessagePromptTemplate | BaseMessage], choices: List[str], **kwargs: Any) ChoicePromptTemplate#

Load a ChoicePromptTemplate from message templates.

classmethod from_template(template: str, choices: List[str], **kwargs: Any) ChoicePromptTemplate#

Load a ChoicePromptTemplate from a text template.

input_variables: List[str]#

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

output_parser: BaseOutputParser | None#

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

partial_variables: Mapping[str, str | Callable[[], str]]#
class langchain_contrib.prompts.choice.ChoiceStr(f_str: F, choices: List[str])#

Bases: F

String that keeps track of choices used to create this string.

choices: List[str]#
parts: Parts#
class langchain_contrib.prompts.choice.StringChoicePrompt(*, text: str, prompt: PromptValue, choices: List[str])#

Bases: BaseChoicePrompt, StringPromptValue

A string prompt that involves picking from a number of choices.

to_messages() List[BaseMessage]#

Return prompt as messages.

to_string() str#

Return prompt as string.

langchain_contrib.prompts.choice.get_oxford_comma_formatter(conjunction: str = 'or') Callable[[List[str]], str]#

Get a choice formatter that respects the Oxford comma.

langchain_contrib.prompts.choice.get_simple_joiner(joiner: str = ', ') Callable[[List[str]], str]#

Get a choice formatter that’s just a simple joining of strings.

langchain_contrib.prompts.choice.list_of_choices(choices: List[str]) str#

Return a numerical list of choices.