Learn R Programming

promptr (version 1.0.0)

format_prompt: Format an LLM prompt

Description

Format a text prompt for a Large Language Model. Particularly useful for few-shot text classification tasks. Note that if you are planning to use one of OpenAI's chat models, like ChatGPT or GPT-4, you will want to use the format_chat() function instead.

Usage

format_prompt(
  text,
  instructions = "",
  examples = data.frame(),
  template = "Text: {text}\nClassification: {label}",
  prompt_template = "{instructions}{examples}{input}",
  separator = "\n\n"
)

Value

Returns a formatted prompt that can be used as input for complete_prompt() or openai::create_completion().

Arguments

text

The text to be classified. Can be a character vector or a single string.

instructions

Instructions to be included in the prompt (format them like you would format instructions to a human research assistant).

examples

A dataframe of "few-shot" examples. Must include one column called 'text' with the example text(s) and another column called "label" with the correct label(s).

template

The template for how examples and completions should be formatted, in glue syntax. If you are including few-shot examples in the prompt, this must contain the {text} and {label} placeholders.

prompt_template

The template for the entire prompt. Defaults to instructions, followed by few-shot examples, followed by the input to be classified.

separator

A character that separates examples. Defaults to two carriage returns.

Examples

Run this code
data(scotus_tweets_examples)

format_prompt(text = "I am disappointed with this ruling.",
              instructions = "Decide if the sentiment of this statement is Positive or Negative.",
              examples = scotus_tweets_examples,
              template = "Statement: {text}\nSentiment: {label}")

format_prompt(text = 'I am sad about the Supreme Court',
              examples = scotus_tweets_examples,
              template = '"{text}" is a {label} statement',
              separator = '\n')

Run the code above in your browser using DataLab