Learn R Programming

tidyprompt (version 0.0.1)

answer_as_list: Make LLM answer as a list of items

Description

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")
)

Value

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

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

See Also

answer_as_named_list()

Other pre_built_prompt_wraps: add_text(), answer_as_boolean(), answer_as_integer(), answer_as_json(), answer_as_named_list(), answer_as_regex_match(), answer_as_text(), answer_by_chain_of_thought(), answer_by_react(), answer_using_r(), answer_using_sql(), answer_using_tools(), prompt_wrap(), quit_if(), set_system_prompt()

Other answer_as_prompt_wraps: answer_as_boolean(), answer_as_integer(), answer_as_json(), answer_as_named_list(), answer_as_regex_match(), answer_as_text()

Examples

Run this code
if (FALSE) {
  "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:
  #   -- <>
  #   -- <>
  #   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"
}

Run the code above in your browser using DataLab