Choice Prompt Template#

If you want to ask the LLM to pick from a series of choices, you can use ChoicePromptTemplate to help you do so.

[1]:
from langchain_contrib.prompts import ChoicePromptTemplate

colors = ["red", "green", "blue"]

template = ChoicePromptTemplate.from_template(
    "This {product} is available in {choices}. Which color should I pick?",
    choices=colors,
)
template.format(product="dress")
[1]:
'This dress is available in red, green, or blue. Which color should I pick?'

This works with Chat messages as well:

[2]:
from langchain.prompts.chat import (
    HumanMessagePromptTemplate,
    SystemMessagePromptTemplate,
)

chat_template = ChoicePromptTemplate.from_messages(
    messages=[
        SystemMessagePromptTemplate.from_template(
            "You are helping the user pick a {product}."
        ),
        HumanMessagePromptTemplate.from_template(
            "This {product} is available in {choices}. Which color should I pick?"
        ),
    ],
    choices=colors,
)
chat_template.format_prompt(product="dress").to_messages()
[2]:
[SystemMessage(content='You are helping the user pick a dress.', additional_kwargs={}),
 HumanMessage(content='This dress is available in red, green, or blue. Which color should I pick?', additional_kwargs={})]

Serializing the choices#

You can pick a different way of serializing the list of choices if you’d like.

The default one is the Oxford comma formatter, with which you can also customize the conjunction used:

[3]:
from langchain_contrib.prompts.choice import get_oxford_comma_formatter

template = ChoicePromptTemplate.from_template(
    "This {product} is available in {choices}. Which color should I pick?",
    choices=["red", "green"],
    choices_formatter = get_oxford_comma_formatter(conjunction="and")
)
template.format(product="car")
[3]:
'This car is available in red and green. Which color should I pick?'

Or, you could simply go with a regular Python join as well:

[4]:
from langchain_contrib.prompts.choice import get_simple_joiner

template.choices_formatter = get_simple_joiner()
template.format(product="car")
[4]:
'This car is available in red, green. Which color should I pick?'

Or, you could even go with a list:

[5]:
from langchain_contrib.prompts.choice import list_of_choices

template = ChoicePromptTemplate.from_template(
    """Your available actions are

{choices}

Which will you pick?""",
    choices=["Page a human", "Retry", "Proceed"],
    choices_formatter = list_of_choices,
)
print(template.format())
Your available actions are

1. Page a human
2. Retry
3. Proceed

Which will you pick?

Customizing the choice variable#

If you want to use a different key than choice in your template, you can also specify that as well. For example, to use tool_names instead of choice in the template:

[6]:
template = ChoicePromptTemplate.from_template(
    "Your task is to {task}. You have access to {tool_names}. Begin.",
    choices=["Google", "a Bash terminal"],
    choices_formatter=get_oxford_comma_formatter("and"),
    choice_format_key="tool_names",
)
template.format(task="take over the world")
[6]:
'Your task is to take over the world. You have access to Google and a Bash terminal. Begin.'

Chain and LLM interop#

The ChoicePromptTemplate is designed to be compatible with the ChoiceChain, which actually executes a subsequent chain based on the decision made.

It also generates a Choice PromptValue that retains information about which choices were used to generate the prompt. This is used, for example, by the Human “LLM” to show a terminal menu to the user when presented with such a prompt.