Skip to content

Commit bcf7d6c

Browse files
committed
make_query
Introduce `make_query` for text classification and batch processing
1 parent 7b8038f commit bcf7d6c

File tree

5 files changed

+333
-164
lines changed

5 files changed

+333
-164
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export(create_model)
88
export(delete_model)
99
export(embed_text)
1010
export(list_models)
11+
export(make_query)
1112
export(new_chat)
1213
export(ping_ollama)
1314
export(pull_model)

R/chat.r

+100
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,103 @@ new_chat <- function() {
275275
the$responses <- NULL
276276
the$prompts <- NULL
277277
}
278+
279+
280+
281+
#' Generate and format queries for a language model
282+
#'
283+
#' `make_query` generates structured input for a language model, including system messages, user messages, and optional examples.
284+
#'
285+
#' @details The function supports the inclusion of examples, which are dynamically added to the structured input. Each example follows the same format as the primary user query.
286+
#'
287+
#' @param text A character vector of primary texts (queries) for which the input will be formatted.
288+
#' @param prompt A string defining the main task or question to be passed to the language model.
289+
#' @param user_template A string template for formatting user queries, containing placeholders like `{text}`, `{prompt_pre}`, and `{prompt_suff}`.
290+
#' @param systemprompt An optional string to specify a system-level instruction or context.
291+
#' @param prompt_pre A prefix string to prepend to each user query.
292+
#' @param prompt_suff A suffix string to append to each user query.
293+
#' @param examples A `tibble` with columns `text` and `answer`, representing example user messages and corresponding assistant responses.
294+
#'
295+
#' @return A list of tibbles, one for each input `text`, containing structured rows for system messages, user messages, and assistant responses.
296+
#' @export
297+
#'
298+
#' @examples
299+
#' user_template <- "{prompt_pre}{text}\n\n{prompt}{prompt_suff}"
300+
#' examples <- tibble::tribble(
301+
#' ~text, ~answer,
302+
#' "This movie was amazing, with great acting and story.", "positive",
303+
#' "The film was okay, but not particularly memorable.", "neutral",
304+
#' "I found this movie boring and poorly made.", "negative"
305+
#' )
306+
#' make_query(
307+
#' text = c("A stunning visual spectacle.", "Predictable but well-acted."),
308+
#' prompt = "Classify sentiment as positive, neutral, or negative.",
309+
#' user_template = user_template,
310+
#' systemprompt = "Provide a sentiment classification.",
311+
#' prompt_pre = "Review: ",
312+
#' prompt_suff = " Please classify.",
313+
#' examples = examples
314+
#' )
315+
make_query <- function(text,
316+
prompt,
317+
user_template,
318+
systemprompt = NULL,
319+
prompt_pre = NULL,
320+
prompt_suff = NULL,
321+
examples = NULL) {
322+
# Ensure optional parameters have default values
323+
prompt_pre <- if (is.null(prompt_pre)) "" else prompt_pre
324+
prompt_suff <- if (is.null(prompt_suff)) "" else prompt_suff
325+
326+
# Process each input text
327+
queries <- lapply(text, function(txt) {
328+
# Initialize structured query
329+
full_query <- tibble::tibble(role = character(), content = character())
330+
331+
# Add system message if provided
332+
if (!is.null(systemprompt)) {
333+
full_query <- full_query |>
334+
dplyr::add_row(role = "system", content = systemprompt)
335+
}
336+
337+
# Add examples if provided
338+
if (!is.null(examples)) {
339+
examples <- tibble::as_tibble(examples) |>
340+
dplyr::rowwise() |>
341+
dplyr::mutate(
342+
user_content = glue::glue(
343+
user_template,
344+
text = text,
345+
prompt = prompt,
346+
prompt_pre = prompt_pre,
347+
prompt_suff = prompt_suff
348+
)
349+
) |>
350+
dplyr::ungroup()
351+
352+
for (i in seq_len(nrow(examples))) {
353+
full_query <- full_query |>
354+
dplyr::add_row(role = "user", content = examples$user_content[i]) |>
355+
dplyr::add_row(role = "assistant", content = examples$answer[i])
356+
}
357+
}
358+
359+
# Add main user query
360+
main_query <- glue::glue(
361+
user_template,
362+
text = txt,
363+
prompt = prompt,
364+
prompt_pre = prompt_pre,
365+
prompt_suff = prompt_suff
366+
)
367+
full_query <- full_query |> dplyr::add_row(role = "user", content = main_query)
368+
369+
return(full_query)
370+
})
371+
372+
return(queries)
373+
}
374+
375+
376+
377+

man/make_query.Rd

+58
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)