library(ellmer)
chat <- chat_openai()
# Using model = "gpt-4.1".Demo of Structured Data in ellmer
Introduction
This short document demonstrates the structured data (aka structured output) feature of LLMs using the ellmer R package. Structured output refers to the scenario where models can be instructed to respond with a particular output structure. To read more about structured outputs, take a look at LangChain’s article on Structured outputs.
This demo will use Open AI’s gpt-4.1 model using my personal API key, since the Integral’s Azure private OpenAI model gpt-4.0 doesn’t support PDFs as a file content type.
Basics
In this example, we give the model a sentence and request that it extract structured information by defining a type specification that includes the name (string), job (string), and company (string):
chat$chat_structured(
"My name is Howard and I'm a data/solution engineer at Integral Consulting Inc.",
type = type_object(
name = type_string(),
job = type_string(),
company = type_string()
)
)
# $name
# [1] "Howard"
# $job
# [1] "Data/Solution Engineer"
# $company
# [1] "Integral Consulting Inc."Article Summarization
In this example, we provide the model with a PDF case document on the 3M Security Litigation Case.
pdf <- content_pdf_url(
"https://ia903408.us.archive.org/30/items/gov.uscourts.mnd.191222/gov.uscourts.mnd.191222.108.0.pdf"
)
type_summary <- type_object(
"Summary of the litigation case document.",
case_number = type_string("Case number"),
plaintiff = type_string("Name of Plaintiff"),
defendant = type_string("Name of defendant"),
result = type_string("Whether the Court grants the motion or not"),
summary = type_string(
"Summary of the case document. One or two paragraphs max"
)
)
chat$chat_structured(
pdf,
type = type_summary
)
# $case_number
# [1] "20‐CV‐2488 (NEB/KMM)"
# $plaintiff
# [1] "A putative class of purchasers of 3M common stock (Plaintiffs)"
# $defendant
# [1] "3M Company and certain chief executives (Defendants)"
# $result
# [1] "The Court grants the motion to dismiss. The Complaint is dismissed."
# $summary
# [1] "This case involved a securities litigation against 3M Company and certain executives, alleging violations of Sections 10(b) and 20(a) of the Securities Exchange Act of 1934 and SEC Rule 10b‐5. Plaintiffs claimed that 3M materially understated its potential legal and financial exposure related to PFAS liabilities in its public disclosures and financial statements. The Court found that Plaintiffs failed to meet the heightened pleading requirements of the Private Securities Litigation Reform Act (PSLRA), specifically failing to plausibly allege that 3M's statements were false or misleading or that the defendants acted with the required scienter (intent or recklessness). The Court held that 3M’s disclosures about PFAS-related liabilities complied with relevant accounting standards (ASC 450), and there were no sufficient factual allegations showing the defendants knew their statements were false when made. As a result, the Court granted the defendants' motion to dismiss and dismissed the Complaint."This is accurate. Based on a review of the case document:
- The case is Case No. 20‐CV‐2488 (NEB/KMM) (Page 1)
- The plaintiff is putative class of purchasers of 3M common stock between February 9, 2017, and July 8, 2019 (“Class Period”) (Page 17)
- The defendant is 3M Company and certain of its chief executives (Page 1)
- The result is the Court grants the motion (Page 1)
You also have the option to omit the type specification entirely and simply request a summary:
chat$chat(
"I've uploaded a litigation case document. Summarise the first three pages of the case.",
pdf
)Here is a summary of the first three pages of the litigation document In re 3M Company Securities Litigation (Case No. 20-CV-2488 (NEB/KMM)):
Case Overview & Initiating Motion
- Plaintiffs’ Allegations:
Plaintiffs claim that 3M Company and some of its senior executives violated Sections 10(b) and 20(a) of the Securities Exchange Act of 1934, along with SEC Rule 10b-5. The alleged violation centers on 3M materially understating its potential legal and financial liabilities arising from its historic manufacture, distribution, and disposal of PFAS (per- and polyfluoroalkyl substances). - Defendants’ Response:
The defendants filed a motion to dismiss the complaint, arguing that the plaintiffs fail to meet the heightened pleading standards set by the Private Securities Litigation Reform Act (PSLRA). - Court’s Decision (First Three Pages):
The court summarizes the background and states that, for the reasons to follow, the motion to dismiss is granted.
Key Factual Allegations (Pages 1-3)
Background on PFAS and 3M’s Activities
- PFAS are synthetic compounds developed by 3M starting in the 1940s, used in various commercial and industrial products (e.g., firefighting foams).
- 3M produced PFAS in several U.S. and foreign facilities. The company allegedly disposed of PFAS waste into public waterways and unlined dumps, leading to widespread contamination.
- Historical Knowledge (Alleged):
- Since the 1950s, 3M allegedly knew about PFAS toxicity through internal studies and documents, but continued public sales for decades.
- Warnings about toxicity to humans and environmental risks go back decades, with studies in the 1950s (toxicity to animals and humans), 1960s (pollution of drinking sources), and 1970s (toxicity to fish and food supply). Internal investigations and recommendations to reduce worker exposures were also conducted.
- 1998 Onward:
- 3M shared data with the EPA. Internal scientists identified PFAS as more dangerous than PCBs and a strong cancer promoter.
- In 2019, a 3M executive publicly acknowledged decades-long studies into PFAS impacts.
- 2000-2008: 3M began and completed phasing out PFAS production.
Litigation and Disclosure Issues
- EPA and Regulators:
- In 2016, the EPA raised PFAS safety standards, broadening 3M’s exposure. Some states (New Jersey, Minnesota) imposed stricter standards.
- Other companies (e.g., DuPont) faced massive PFAS settlements ($671 million in 2017).
- Minnesota Attorney General Lawsuit:
- In 2010, a state lawsuit accused 3M of deliberate disregard for the environmental and health risks. This is known as the AG Litigation.
- 3M’s Financial Disclosures:
- The claims against 3M focus not on the underlying environmental litigation, but on whether 3M committed securities fraud in its disclosures.
- In 2016-2017, 3M’s regulatory filings disclosed the ongoing lawsuits but stated that any potential liability was “not probable and estimable,” so no specific losses were recognized.
- 2017 Form 10-K:
- Filed just before the Minnesota AG case was set for trial, 3M disclosed that the state claimed $5 billion in damages but continued to maintain legal defenses and did not record a liability as “not probable and estimable.”
Summary:
The opening of the court document lays out the nature of the securities litigation—allegations that 3M misled investors regarding the magnitude and likelihood of its PFAS-related liabilities, despite internal knowledge of the risks and external regulatory/litigation developments. The summary includes 3M’s historical involvement with PFAS, early knowledge of dangers, governmental action, and the specifics of how 3M reported legal risks and pending litigation in its financial filings.
If you need more detailed breakdowns or specific legal arguments as presented on further pages, please let me know!