Make LLM answer match a specific regex
Usage
answer_as_regex(prompt, regex, mode = c("full_match", "extract_matches"))
Arguments
- prompt
A single string or a
tidyprompt()
object- regex
A character string specifying the regular expression the response must match
- mode
A character string specifying the mode of the regex match. Options are "exact_match" (default) and "extract_all_matches". Under "full_match", the full LLM response must match the regex. If it does not, the LLM will be sent feedback to retry. The full LLM response will be returned if the regex is matched. Under "extract_matches", all matches of the regex in the LLM response will be returned (if present). If the regex is not matched at all, the LLM will be sent feedback to retry. If there is at least one match, the matches will be returned as a character vector
Value
A tidyprompt()
with an added prompt_wrap()
which will ensure
that the LLM response matches the specified regex
See also
Other pre_built_prompt_wraps:
add_text()
,
add_tools()
,
answer_as_boolean()
,
answer_as_code()
,
answer_as_integer()
,
answer_as_list()
,
answer_as_named_list()
,
answer_by_chain_of_thought()
,
answer_by_react()
,
prompt_wrap()
,
quit_if()
,
set_system_prompt()
Other answer_as_prompt_wraps:
answer_as_boolean()
,
answer_as_code()
,
answer_as_integer()
,
answer_as_list()
,
answer_as_named_list()
Examples
if (FALSE) { # \dontrun{
"What would be a suitable e-mail address for cupcake company?" |>
answer_as_regex("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$") |>
send_prompt(llm_provider_ollama())
# --- Sending request to LLM provider (llama3.1:8b): ---
# What would be a suitable e-mail address for cupcake company?
#
# You must answer with a response that matches this regex format:
# ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
# (use no other characters)
# --- Receiving response from LLM provider: ---
# sweet.treats.cupcakes@gmail.com
# [1] "sweet.treats.cupcakes@gmail.com"
"What would be a suitable e-mail address for cupcake company?" |>
add_text("Give three ideas.") |>
answer_as_regex(
"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}",
mode = "extract_matches"
) |>
send_prompt(llm_provider_ollama())
# --- Sending request to LLM provider (llama3.1:8b): ---
# What would be a suitable e-mail address for cupcake company?
#
# Give three ideas.
#
# You must answer with a response that matches this regex format:
# [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
# --- Receiving response from LLM provider: ---
# Here are three potential email addresses for a cupcake company:
#
# 1. sweettreats.cupcakes@yummail.com
# 2. cupcakes.and.love@flourpower.net
# 3. thecupcakery@gmail.com
# [1] "sweettreats.cupcakes@yummail.com" "cupcakes.and.love@flourpower.net"
# "thecupcakery@gmail.com"
} # }