Skip to main content

Craft a Prompt Template

Overview

You can incorporate (multiple) columns from your dataset into an input prompt via the prompt template.

Creating a prompt template

You can use one (or more) column from your dataset as a variable in curly brackets {} to indicate where to insert the input feature. Multiple columns can be inserted, e.g.: The {color} {animal} jumped over the {size} {object}, where every term in curly brackets is a column in the dataset.

Additional tips

  • Provide necessary boilerplate needed to make the LLM respond in the correct way (for example, with a response to a question rather than a continuation of the input sequence).
  • Provide additional context to the model that can help it understand the task, or provide restrictions to prevent hallucinations.
  • For tabular datasets, you can combine multiple columns from a dataset into a single text input feature (see TabLLM).

Example Template

# Note the 4-space indentation, which is necessary for the YAML templating.
prompt_template = """Below is an instruction that describes a task, paired with an input
that may provide further context. Write a response that appropriately
completes the request.

### Instruction: {instruction}

### Input: {input}

### Response:
"""
info

Note: For multi-line prompt templates, be sure to use 4-space indentation at the start of each new line.

During training, the {instruction} and {input} template vars will be automatically replaced by the corresponding column values for each row in the dataset. Make sure to use names that match the columns from your chosen dataset!

Working with JSON

Curly brackets used in JSON syntax can interfere with Python's string interpolation syntax. While it's possible to escape brackets by doubling them, Predibase provides a PromptTemplate utility class to make things easier.

When using the PromptTemplate class, use a dollar sign ($) to denote insertion variables instead of curly braces. The PromptTemplate object can then be passed directly to the finetune() call or converted to an escaped string with PromptTemplate.render().

from predibase import PromptTemplate

tmpl = PromptTemplate("{'my_key': '$my_column'}")
tmpl.render() # => "{{'my_key': '{my_column}'}}"

llm = pc.LLM("hf://meta-llama/Llama-2-7b-hf")
ft_llm = llm.finetune(prompt_template=tmpl).get()
info

In the Predibase UI, you are required to include a prompt template with at least one column from your dataset included. For more advanced users, the prompt template is not strictly required. We recommend using the SDK to edit and train a more advanced, custom config.

Prompt Template Examples

  1. Sentiment Analysis:

    Given the following text, classify the sentiment as positive, negative, or neutral.
    Text: {input_text}
    Sentiment:
  2. Text Summarization:

    Summarize the following passage into a concise paragraph.
    Paragraph: {paragraph}
    Summary:
  3. Named Entity Recognition:

    Identify and label the named entities in the following sentence.
    Sentence: {sentence}
    Named Entities:
  4. Language Translation:

    Translate the following sentence from English to French.
    Sentence: {sentence}
    French Translation:
  5. Question Answering:

    Answer the following question based on the provided text.
    Question: {question}
    Answer:
  6. Text Classification:

    Assign the appropriate category label to the following text. The label choices are good, bad, neutral.
    Text: {input_text}
    Category Label:
  7. Paraphrasing:

    Rewrite the following sentence while retaining the original meaning - the goal is to paraphrase the sentence.
    Try not to use very complicated language and make the paraphrased sentences easy to understand.
    Sentence: {sentence}
    Paraphrase:
  8. Text Generation (Creative Writing):

    Here are a few lines from a story. Continue the story by writing the next paragraph.
    Story: {story}
    Continuation:
  9. Conversational AI:

    You are a conversation AI assistant. Generate a response for the following dialogue:
    Dialog: {dialog}
    Response: