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"
## ■ 0/4 | 0% | ETA: ? | NA
##
## ■ 0/4 | 0% | ETA: ? | 2013-Obama
##
## ■■■■■■■■■ 1/4 | 25% | ETA: 8s | 2013-Obama
##
## ■■■■■■■■■ 1/4 | 25% | ETA: 8s | 2017-Trump
##
## ■■■■■■■■■■■■■■■■ 2/4 | 50% | ETA: 6s | 2017-Trump
##
## ■■■■■■■■■■■■■■■■ 2/4 | 50% | ETA: 6s | 2021-Biden
##
## ■■■■■■■■■■■■■■■■■■■■■■■ 3/4 | 75% | ETA: 3s | 2021-Biden
##
## ■■■■■■■■■■■■■■■■■■■■■■■ 3/4 | 75% | ETA: 3s | 2025-Trump
##
## ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 4/4 | 100% | ETA: 0s | 2025-Trump
##
##
##
## ✔ Returned 4 documents (4 successful, 0 with NAs)
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 promise of democracy. It highlights the importance of unity, collective action, and the need to adapt to changing times while staying true to founding principles. The speech addresses various challenges, including economic recovery, social equality, climate change, and global peace, urging Americans to work together to overcome them. It calls for a commitment to equality, opportunity, and justice, and stresses the role of citizens in shaping the nation’s future. |
2017-Trump | This 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. The President pledges to focus on rebuilding the nation’s infrastructure, revitalizing the economy, and ensuring national security. The speech also highlights themes of unity, patriotism, and a new era of action, with a call to make America strong, wealthy, proud, and safe again. |
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 for a better future. |
2025-Trump | The document is a speech by a political leader, likely Donald Trump, marking the beginning of a new term in office. The speech emphasizes a vision of national renewal, focusing on restoring America’s sovereignty, safety, and prosperity. The leader outlines plans to prioritize American interests, address government inefficiencies, and tackle issues such as border security, energy independence, and economic revitalization. The speech also highlights a commitment to national unity, free speech, and a return to traditional values. The leader expresses confidence in overcoming challenges and achieving unprecedented national success, invoking themes of American exceptionalism and manifest destiny. |