Skip to contents

This function applies AI-assisted analysis to each document in a character vector or a docvar in a corpus, using a structured type_object() to define the expected response format. It supports few-shot learning examples for better performance.

Usage

ai_text(
  .data,
  chat_fn,
  type_object,
  few_shot_examples = NULL,
  verbose = TRUE,
  result_env = NULL,
  ...
)

Arguments

.data

A character vector of texts

chat_fn

A function like chat_openai(). See https://ellmer.tidyverse.org/articles/structured-data.html for details.

type_object

an ellmer type_object()

few_shot_examples

Optional few-shot learning examples (data frame with text, score)

verbose

logical; whether to print progress

result_env

An environment to store results and allow resuming

...

additional arguments passed to chat_fn

Value

character; the response from the LLM with a length equal to the number of input documents; each single element defined by type_object() is added as a character vector

Examples

if (FALSE) { # \dontrun{
library(quanteda)
results <- quanteda::data_corpus_inaugural[1:3] %>%
  ai_text(chat_fn = chat_openai, model = "gpt-4o",
          api_args = list(temperature = 0, seed = 42),
          type_object =
            type_object("Summary of the document",
                        summary = type_string("Summarize the document in a few sentences.")),
  )
} # }