Skip to contents

Make LLM answer as a list of items

Usage

answer_as_list(
  prompt,
  item_name = "item",
  item_explanation = NULL,
  n_unique_items = NULL,
  list_mode = c("bullet", "comma")
)

Arguments

prompt

A single string or a tidyprompt() object

item_name

(optional) Name of the items in the list

item_explanation

(optional) Additional explanation of what an item should be. Item explanation should be a single string. It will be appended after the list instruction

n_unique_items

(optional) Number of unique items required in the list

list_mode

(optional) Mode of the list. Either "bullet" or "comma". "bullet mode expects items to be listed with "–" before each item, with a new line for each item (e.g., "– item1\n– item2\n– item3"). "comma" mode expects items to be listed with a number and a period before (e.g., "1. item1, 2. item2, 3. item3"). "comma" mode may be easier for smaller LLMs to use

Value

A tidyprompt() with an added prompt_wrap() which will ensure that the LLM response is a list of items

Examples

if (FALSE) { # \dontrun{
  "What are some delicious fruits?" |>
    answer_as_list(item_name = "fruit", n_unique_items = 5) |>
    send_prompt()
  # --- Sending request to LLM provider (llama3.1:8b): ---
  # What are some delicious fruits?
  #
  # Respond with a list, like so:
  #   -- <<fruit 1>>
  #   -- <<fruit 2>>
  #   etc.
  # The list should contain 5 unique items.
  # --- Receiving response from LLM provider: ---
  # Here's the list of delicious fruits:
  #   -- Strawberries
  #   -- Pineapples
  #   -- Mangoes
  #   -- Blueberries
  #   -- Pomegranates
  # [[1]]
  # [1] "Strawberries"
  #
  # [[2]]
  # [1] "Pineapples"
  #
  # [[3]]
  # [1] "Mangoes"
  #
  # [[4]]
  # [1] "Blueberries"
  #
  # [[5]]
  # [1] "Pomegranates"
} # }