Prompt Templates

Before you start

Creating Prompt Templates

The following code constructs a simple prompt template for a customer support expert in a financial sector. The task is to classify customer complaints as related to a particular product from a list of products. The prompt includes a numbered list of products and a placeholder for the customer complaint, which matches the column name in the dataset.

project_dataset = lore.materialize_dataset(dataset)
df = project_dataset["train"].to_pandas()
products = "\n".join([f"{i}. {cat.strip()}" for i, cat in enumerate(df["Product"].unique())])

input = """\
You are a customer support expert in a financial sector. Your task is to classify customer complaints 
as related to a particular product from the following list. Don't include any explanation. 
Only respond with one product from the numbered list. 

Categories:
{products}

Customer complaint:
{{{{Consumer_complaint_narrative}}}}

Answer: 
"""
simple_prompt = PromptTemplate(
    name="easy-prompt",
    template=input.format(products=products, subproducts=subproducts),
)

print("############### Prompt ###############\n")
print(simple_prompt.template)

Applying Prompt Templates to Datasets

The following code applies a prompt template to a sample of your dataset as a new column, making it easy to see the prompt and the customer complaint together, as well as compare the prompt’s effectiveness to other prompts. The final line of code prints the first two rows of the dataset with the new prompt column.

# Create a sample of the dataset
dataset_sample = lore.sample_dataset(dataset, number_of_samples=10, as_dataset=True)

# Apply the prompt template to the dataset sample
processed_dataset = lore.apply_prompt_template(
    dataset_sample, easy_prompt, new_column_name="simple_prompt"
)

processed_dataset = lore.sample_dataset(
    processed_dataset, number_of_samples=10, as_dataset=False
)["train"]

pretty_print_df(
    processed_dataset.loc[:, ["Product", "simple_prompt"]].head(2)
)