answer_as_category: Make LLM answer as a category

View source: R/answer_as_category.R

answer_as_categoryR Documentation

Make LLM answer as a category

Description

Make LLM answer as a category

Usage

answer_as_category(prompt, categories, descriptions = NULL)

Arguments

prompt

A single string or a tidyprompt() object

categories

A character vector of category names. Must not be empty and must not contain duplicates

descriptions

An optional character vector of descriptions, corresponding to each category. If provided, its length must match the length of categories. Defaults to NULL

Details

For multiple categories, see answer_as_multi_category().

Value

A tidyprompt() with an added prompt_wrap() which will ensure that the LLM response is the most fitting category of a text as a character vector of length one

See Also

Other pre_built_prompt_wraps: add_text(), answer_as_boolean(), answer_as_integer(), answer_as_json(), answer_as_list(), answer_as_multi_category(), 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_list(), answer_as_multi_category(), answer_as_named_list(), answer_as_regex_match(), answer_as_text()

Examples

## Not run: 
  "It is sunny, that makes me happy." |>
    answer_as_category(categories = c("environment", "weather", "work")) |>
    send_prompt()
  # --- Sending request to LLM provider (llama3.1:8b): ---
  #   You need to categorize a text.
  #
  # Text:
  #   'It is sunny, that makes me happy.'
  #
  # Possible categories:
  #   1. environment
  #   2. weather
  #   3. work
  #
  # Respond with the number of the category that best describes the text.
  # (Use no other words or characters.)
  # --- Receiving response from LLM provider: ---
  #   2
  # [1] "weather"

## End(Not run)

tidyprompt documentation built on Aug. 25, 2025, 5:11 p.m.