make_query: Generate and format queries for a language model

View source: R/chat.r

make_queryR Documentation

Generate and format queries for a language model

Description

make_query generates structured input for a language model, including system prompt, user messages, and optional examples (assistant answers).

Usage

make_query(
  text,
  prompt,
  template = "{prefix}{text}\n{prompt}\n{suffix}",
  system = NULL,
  prefix = NULL,
  suffix = NULL,
  examples = NULL
)

Arguments

text

A character vector of texts to be annotated.

prompt

A string defining the main task or question to be passed to the language model.

template

A string template for formatting user queries, containing placeholders like {text}, {prefix}, and {suffix}.

system

An optional string to specify a system prompt.

prefix

A prefix string to prepend to each user query.

suffix

A suffix string to append to each user query.

examples

A tibble with columns text and answer, representing example user messages and corresponding assistant responses.

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.

Value

A list of tibbles, one for each input text, containing structured rows for system messages, user messages, and assistant responses.

Examples

template <- "{prefix}{text}\n\n{prompt}{suffix}"
examples <- tibble::tribble(
  ~text, ~answer,
  "This movie was amazing, with great acting and story.", "positive",
  "The film was okay, but not particularly memorable.", "neutral",
  "I found this movie boring and poorly made.", "negative"
)
queries <- make_query(
  text = c("A stunning visual spectacle.", "Predictable but well-acted."),
  prompt = "Classify sentiment as positive, neutral, or negative.",
  template = template,
  system = "Provide a sentiment classification.",
  prefix = "Review: ",
  suffix = " Please classify.",
  examples = examples
)
print(queries)
if (ping_ollama()) { # only run this example when Ollama is running
  query(queries, screen = TRUE, output = "text")
}

rollama documentation built on April 3, 2025, 7:48 p.m.