Skip to contents

Make LLM answer as a number (between min and max)

Usage

answer_as_numeric(
  prompt,
  min = NULL,
  max = NULL,
  add_instruction_to_prompt = TRUE
)

Arguments

prompt

A single string or a tidyprompt() object

min

(optional) Minimum value for the number

max

(optional) Maximum value for the number

add_instruction_to_prompt

(optional) Add instruction for replying as a number to the prompt text. Set to FALSE for debugging if extractions/ validations are working as expected (without instruction the answer should fail the validation function, initiating a retry)

Value

A tidyprompt() with an added prompt_wrap() which will ensure that the LLM response is a number.

Examples

if (FALSE) { # \dontrun{
  "What is 10 divided by 4?" |>
    answer_as_numeric() |>
    send_prompt()
  # --- Sending request to LLM provider (llama3.1:8b): ---
  #   What is 10 divided by 4?
  #
  #   You must answer with only a number (use no other characters).
  # --- Receiving response from LLM provider: ---
  #   2.5
  # [1] 2.5
} # }