Create a function to prompt the user for data
Source:R/compose_prompt.R
create_usr_data_prompter.Rd
This function create a function that can be used to prompt the user for data in a specific context. Given the interested context, the function created will accept a string of text as input and return the complete prompt based on the desired context.
Value
(function) a function that can be used to prompt the user, accepting a string of text as input and returning the complete prompt based on the desired context.
Examples
usr_prompt <- compose_prompt(
role = "You are the assistant of a university professor.",
context = "
You are analyzing the comments of the students of the last course.",
task = "Your task is to extract information from a text provided.",
instructions = "
You should extract the first and last words of the text.",
output = "
Return the first and last words of the text separated by a dash,
i.e., `first - last`.",
style = "
Do not add any additional information,
return only the requested information.",
examples = "
# Examples:
text: 'This is an example text.'
output: 'This - text'
text: 'Another example text!!!'
output: 'Another - text'"
)
prompter <- create_usr_data_prompter(
usr_prompt = usr_prompt
)
prompter("This is an example text.")
#> [1] "You are the assistant of a university professor.\n\n You are analyzing the comments of the students of the last course.\nYour task is to extract information from a text provided.\n\n You should extract the first and last words of the text.\n\n Return the first and last words of the text separated by a dash,\n i.e., `first - last`.\n\n Do not add any additional information,\n return only the requested information.\n\n # Examples:\n text: 'This is an example text.'\n output: 'This - text'\n text: 'Another example text!!!'\n output: 'Another - text'\n\"\"\"\nThis is an example text.\n\"\"\""
prompter("Another example text!!!")
#> [1] "You are the assistant of a university professor.\n\n You are analyzing the comments of the students of the last course.\nYour task is to extract information from a text provided.\n\n You should extract the first and last words of the text.\n\n Return the first and last words of the text separated by a dash,\n i.e., `first - last`.\n\n Do not add any additional information,\n return only the requested information.\n\n # Examples:\n text: 'This is an example text.'\n output: 'This - text'\n text: 'Another example text!!!'\n output: 'Another - text'\n\"\"\"\nAnother example text!!!\n\"\"\""
# You can also use it with a data frame to programmatically create
# prompts for each row of a data frame's column.
db <- data.frame(
text = c("This is an example text.", "Another example text!!!")
)
db$text |> purrr::map_chr(prompter)
#> [1] "You are the assistant of a university professor.\n\n You are analyzing the comments of the students of the last course.\nYour task is to extract information from a text provided.\n\n You should extract the first and last words of the text.\n\n Return the first and last words of the text separated by a dash,\n i.e., `first - last`.\n\n Do not add any additional information,\n return only the requested information.\n\n # Examples:\n text: 'This is an example text.'\n output: 'This - text'\n text: 'Another example text!!!'\n output: 'Another - text'\n\"\"\"\nThis is an example text.\n\"\"\""
#> [2] "You are the assistant of a university professor.\n\n You are analyzing the comments of the students of the last course.\nYour task is to extract information from a text provided.\n\n You should extract the first and last words of the text.\n\n Return the first and last words of the text separated by a dash,\n i.e., `first - last`.\n\n Do not add any additional information,\n return only the requested information.\n\n # Examples:\n text: 'This is an example text.'\n output: 'This - text'\n text: 'Another example text!!!'\n output: 'Another - text'\n\"\"\"\nAnother example text!!!\n\"\"\""