Skip to contents

The ai_summary() function allows you to summarize documents using an LLM. It generates a summary for each document in a character vector and stores it as a new character vector which can be added as a document variable in a quanteda corpus element. The function uses a predefined type_object argument from ellmer to structure the LLM’s response, producing succinct summaries of each document. Users need to provide a character vector of documents to summarize and choose the LLM provider they want to use for summarization.

Loading packages and data

## Package version: 4.3.1
## Unicode version: 15.1
## ICU version: 74.2
## Parallel computing: disabled
## See https://quanteda.io for tutorials and examples.
## Loading required package: ellmer
data_corpus_inaugural <- data_corpus_inaugural[57:60]

Using ai_summary() for summarization of documents

result <- ai_summary(data_corpus_inaugural, chat_fn = chat_openai, model = "gpt-4o",
                    api_args = list(temperature = 0, seed = 42))
## Using `chat_fn()` with model "gpt-4o"
## ■■■■■■■■■                        1/4 |  25% | ETA: 14s | 2013-Obama
## 
## ■■■■■■■■■■■■■■■■                 2/4 |  50% | ETA:  8s | 2017-Trump
## 
## ■■■■■■■■■■■■■■■■■■■■■■■          3/4 |  75% | ETA:  4s | 2021-Biden
## 
## ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■  4/4 | 100% | ETA:  0s | 2025-Trump
## 
## 
## 
##  Processed 4 documents successfully
library(kableExtra)
  result %>%
  kable("html", escape = FALSE) %>%
  kable_styling(bootstrap_options = c("striped", "hover")) %>%
  column_spec(2)
id summary
2013-Obama The document is a speech delivered during a presidential inauguration, emphasizing the enduring strength of the U.S. Constitution and the democratic ideals of equality, liberty, and the pursuit of happiness. It highlights the need for collective action to address modern challenges, such as economic recovery, climate change, and social justice, while maintaining individual freedoms. The speech calls for unity and action to ensure prosperity and security for all Americans, advocating for equality, opportunity, and the protection of vulnerable populations. It stresses the importance of civic duty and the role of citizens in shaping the nation’s future.
2017-Trump The document is a speech delivered by a U.S. President during an inauguration ceremony. The speech emphasizes a commitment to returning power to the American people, addressing economic and social challenges, and prioritizing American interests in policy decisions. It criticizes past administrations for benefiting a small group in Washington at the expense of the general populace and promises to focus on rebuilding the nation’s infrastructure, economy, and military. The speech also highlights themes of unity, patriotism, and a new era of action, with a focus on “America first” policies. The President pledges to fight for American workers and families, and to restore national pride and prosperity.
2021-Biden The document is a speech emphasizing the importance of democracy, unity, and the peaceful transfer of power in the United States. It acknowledges the challenges faced by the nation, including the COVID-19 pandemic, racial injustice, political extremism, and climate change. The speaker calls for unity and collective action to overcome these issues, highlighting the resilience of the American people and the need to uphold truth and democracy. The speech also pays tribute to past leaders and the sacrifices made by Americans, urging the nation to work together to build a better future.
2025-Trump The document is a speech by a political leader, likely Donald Trump, marking the beginning of a new presidential term. The speech emphasizes a vision of restoring America’s greatness, sovereignty, and prosperity. It outlines plans to prioritize American interests, address issues like border security, energy independence, and economic revitalization. The leader promises to end government corruption, restore trust, and unify the nation. The speech also highlights a commitment to national defense, law and order, and a return to traditional values. It concludes with a call for national unity and ambition, promising a new era of American success and leadership.