Sys.setenv(OPENAI_API_KEY = "insert your key here")Session 2: LLMs
Overview
In this session we introduce large language models (LLMs) and explore how they can be used in ecological and environmental applications. We begin by accessing commercial and open-source models, then move on to repeated prompting, model comparison, and a press release example based on a scientific paper.
Packages
1. Accessing the OpenAI API
We are going to use the ellmer package to access LLMs. First we will use a commercial model provided by OpenAI and ask for an opinion about New Zealand’s sustainability.
To access an OpenAI model, you will need an API key. A safe way to do this is to set it outside the script as an environment variable.
Example:
We can then open a chat using the chat_openai() function and send a question to the model.
newchat <- chat_openai(
model = "gpt-4o",
system_prompt = "You are an expert on global sustainability."
)
res <- newchat$chat(
"How sustainable is New Zealand's economy?",
echo = "none"
)
print(res)New Zealand's economy has several sustainable features, but it also faces
challenges that need continued attention to ensure long-term sustainability.
Here's an overview of various aspects of sustainability within New Zealand's
economy:
1. **Renewable Energy**: New Zealand is a leader in renewable energy, with a
significant proportion of its electricity generated from renewable sources,
particularly hydropower. In 2020, about 83% of its electricity was generated
from renewable sources. The country is also investing in wind and solar energy
to further increase its renewable energy capacity.
2. **Agriculture**: Agriculture is a major part of New Zealand's economy,
particularly dairy, meat, and wine production. While the sector is vital for
the economy, it poses sustainability challenges. Livestock farming contributes
significantly to greenhouse gas emissions, particularly methane. New Zealand
has been working on reducing these emissions through research into sustainable
farming practices and technology.
3. **Biodiversity and Natural Resources**: New Zealand is rich in biodiversity
and natural resources, which are essential for tourism and the economy.
However, there are concerns about the impact of economic activities on natural
habitats and biodiversity loss. Conservation efforts are ongoing, aimed at
protecting native species and managing invasive species that threaten the
ecosystem.
4. **Tourism**: Tourism is a significant part of New Zealand’s economy, and the
government is focusing on sustainable tourism practices. These include
minimizing environmental impact, promoting eco-friendly travel, and ensuring
that tourism benefits local communities. The COVID-19 pandemic highlighted the
need for a more resilient and sustainable tourism sector.
5. **Climate Change Policies**: New Zealand has committed to various
international climate agreements and has set targets to reduce emissions. The
Climate Change Response (Zero Carbon) Amendment Act aims for carbon neutrality
by 2050, excluding biological methane. The government is developing policies to
transition to a low-carbon economy, including emissions trading schemes and
promoting electric vehicles.
6. **Economic Diversification and Innovation**: Diversification of the economy
and investment in innovation are crucial for sustainability. New Zealand is
focusing on sectors like technology, education, and creative industries to
reduce reliance on traditional sectors like agriculture and tourism.
7. **Social Sustainability**: Ensuring economic growth that provides equitable
opportunities and benefits across the population is vital. New Zealand
addresses social sustainability through policies aimed at improving housing,
healthcare, and education, although challenges remain, including addressing
income inequality and affordable housing shortages.
Overall, New Zealand's economy demonstrates a commitment to sustainability
through renewable energy and environmental policies. However, ongoing
challenges, particularly in reducing agricultural emissions and balancing
economic growth with environmental preservation, require continued innovation
and policy development. Sustainable practices in all sectors will be crucial
for the long-term resilience of New Zealand's economy.
2. Accessing an open-source local model
We can also try an open-source alternative using the ollamar package. This requires a little more setup because we first need to install a local model.
list_models() name size parameter_size quantization_level
1 all-minilm:latest 46 MB 23M F16
2 deepseek-r1:1.5b 1.1 GB 1.8B Q4_K_M
3 nomic-embed-text:latest 274 MB 137M F16
4 qwen2.5:0.5b 398 MB 494.03M Q4_K_M
5 tinyllama:1.1b 638 MB 1B Q4_0
modified
1 2025-11-21T10:06:23
2 2025-11-12T08:27:53
3 2026-03-13T09:44:42
4 2026-03-13T09:44:43
5 2026-03-16T21:27:26
Let’s install a really tiny one so that it can run quickly on a normal laptop.
pull("tinyllama:1.1b")<httr2_response>
POST http://127.0.0.1:11434/api/pull
Status: 200 OK
Content-Type: application/x-ndjson
Body: In memory (849 bytes)
Once a model has been installed, we can access it in a similar way to the OpenAI model.
newchat <- chat_ollama(
model = "tinyllama:1.1b",
system_prompt = "You are an expert on global sustainability."
)
res <- newchat$chat(
"How sustainable is New Zealand's economy?",
echo = "none"
)
print(res)New Zealand's economy has been rated as "sustainable" by the International
Monetary Fund (IMF) in two of the past five years. The IMF classification
involves assessing the country's economy's resilience to shocks that can have
long-lasting and deleterious effects.
In 2018, the IMF classified New Zealand as "sustainable," a designation that
acknowledges the country's fiscal and monetary stability, low public debt,
strong investor confidence, and a balanced macroeconomic system. Some of the
factors that led to this assessment were the country's relatively low interest
rates, low inflation, and stable currency. In contrast, the IMF noted that
while New Zealand's private sector debt-to-GDP ratio is higher than the global
median, its level is still below most developed countries.
The IMF also noted that New Zealand's macroeconomic framework seems well-suited
to accommodating growth, inflation, and a stable currency even with uncertainty
about the 2017-2019 mining tax. The tax was initially intended to fund
infrastructure projects to aid economic growth, but New Zealand has had to
delay some infrastructure projects as a result. Overall, New Zealand's economy
is described as having a strong fiscal and monetary stance, while the country's
overall growth outlook remains relatively stable.
3. Asking a series of questions
In environmental social science we may be interested in asking land use questions to virtual “agents”. This is increasingly common in simulation studies and agent-based modelling.
Here we consider a situation where a farmer might change part of their farm from sheep farming to native forestry. Whether or not the farmer plants trees may depend on the size of the financial incentive available.
Next we send these prompts to the OpenAI model.
If the responses are collected successfully, we can plot the results.
plot(chatgpt ~ dollars,
pch = 16,
col = "red",
data = agentq,
xlab = "Incentive ($)",
ylab = "Forested area (ha)")
m1 <- glm(cbind(chatgpt,150-chatgpt) ~ dollars,
data = agentq,
family = "binomial")
lines(1:500,
predict(m1,
newdata = data.frame(dollars = 1:500),
type = "response")*150,
lty = 3)
So this is cool! The simulated farmer is making economically-driven decisions, displaying some financial logic. At a low incentive level, they choose to keep their farms in productive agriculture. However, if they are provided with a large enough incentive, they will switch their behaviour and start planting trees. You can read more about this topic in our recent paper.
4. Repeating the survey with an open-source model
We can repeat the same loop using a local model.
We can inspect the responses.
agentq$open [1] NA NA NA NA NA NA NA NA NA NA NA NA NA
Oh dear, the model has not been able to perform the task properly, likely because it is not complex enough. Let’s see what it had to say;
agentq$open [1] NA NA NA NA NA NA NA NA NA NA NA NA NA
resPlease provide your decision regarding the land use for the upcoming year.
[Insert number of hectares desired to be converted to natives trees]
Have a good life.
It is actually trying to provide some logic, but unfortunately has not been able to appreciate the request or particularly the importance of providing a simple numerical response.
5. Press release example
LLMs can also be used for science communication. In this example we extract the text from a PDF and ask the model to prepare a press release. You can use any pdf file that you like for this test. I used a recent paper about the potential of generative AI for climate change adaptation.
pdf_path <- "Data/Richards et al 2024.pdf"
papertext <- pdftools::pdf_text(pdf_path)We then ask the model to write a press release.
newchat <- chat_openai(
model = "gpt-4o",
system_prompt = "You are an expert in writing press releases for scientific papers."
)
res <- newchat$chat(
paste0(
"Please read the following paper and prepare a press release;\n\n",
papertext,
"\nThe press release should be less than 500 words."
),
echo = "none"
)
print(res)**FOR IMMEDIATE RELEASE**
**Revolutionizing Climate Action: The Untapped Carbon Potential of Small Tree
Patches in Aotearoa-New Zealand's Grasslands**
*Lincoln, New Zealand – November 22, 2024* – Groundbreaking research led by
Manaaki Whenua – Landcare Research scientists, published today in the journal
Climate Policy, has revealed the significant yet overlooked role of small tree
patches in carbon sequestration efforts across Aotearoa-New Zealand's grassland
landscapes. Conducted by a team including Daniel Richards, Kara Allen, and
David Whitehead, this study emphasizes the transformative potential of these
small ecosystems as a strategic component in global climate mitigation efforts.
Amid mounting ecological crises and climate pledges, the study identifies over
1.6 million small tree patches, spread across an estimated 186,000 hectares of
grasslands in Aotearoa-New Zealand. These patches, typically less than one
hectare in size, could hold between 11.6 to 29.3 million tonnes of aboveground
carbon—equivalent to roughly 2.9% to 7.8% of the nation's agricultural
emissions. Furthermore, the annual carbon sequestration from these patches
ranges between 0.3 million to 0.8 million tonnes, marking them as critical,
natural carbon sinks.
"This research challenges existing paradigms by showcasing the indispensable
role of small tree patches—not merely as ecological artifacts but as dynamic
carbon absorbers," notes Daniel Richards, the lead author. "Their inclusion in
carbon policies could revolutionize current approaches to enhance climate
resilience."
Despite their evident potential, small tree patches are excluded from New
Zealand’s national carbon emissions trading scheme (ETS), which primarily
focuses on larger forested areas. This oversight represents a missed
opportunity both economically, where these patches possess an unrealized market
value of up to NZ$ 237.6 million annually, and ecologically, given their
contribution to biodiversity and ecosystem services.
The study highlights the urgent need for policy reevaluation, suggesting
significant adjustments to incentivize the protection and establishment of
small tree patches. This could include modifications to existing afforestation
incentives and carbon valuation methods, providing a dual benefit of
agricultural productivity and climate mitigation.
In addition to carbon storage, these patches offer vital co-benefits such as
erosion control, shade for livestock, and biodiversity support, alongside
cultural significance within the Te Ao Māori worldview, which emphasizes the
interconnectedness of all living things and our responsibility to protect the
environment.
"We're just beginning to uncover the co-benefits these small patches provide,
from supporting diverse ecosystems to enhancing cultural landscapes," explains
co-author Nikki Harcourt. "It's time we recognize and integrate them into our
environmental and economic frameworks."
As global leaders converge for climate talks, this research lays the groundwork
for a pioneering approach—embracing small tree patches in grassland landscapes
as a bedrock for sustainable, scalable carbon management. It heralds a new era
where these natural resources are leveraged not only for their climate benefits
but also to foster a future where economic, ecological, and cultural priorities
harmoniously align.
For more information, access the full study in Climate Policy or contact Daniel
Richards at richardsd@landcareresearch.co.nz.
**Media Contact:**
Daniel Richards
Manaaki Whenua – Landcare Research
Email: richardsd@landcareresearch.co.nz
--- End ---
*Please consider the environment before printing this press release.*
We can also request a social media post.
res2 <- newchat$chat(
"Thanks, please could you now write a suitable social media post e.g. for LinkedIn?",
echo = "none"
)
print(res2)🌿 Exciting news for climate action! Our latest research reveals the hidden
potential of small tree patches in Aotearoa-New Zealand's grasslands as
powerful carbon sinks. These patches could sequester up to 0.8 million tonnes
of carbon annually, equivalent to offsetting up to 7.8% of the nation's
agricultural emissions. Despite this, they often go unrecognized in current
carbon markets. It's time for a policy shift to harness their full potential!
🌍💡
Explore how these small but mighty ecosystems can redefine sustainable
practices: [Link to the full article]
#ClimateAction #CarbonSequestration #Sustainability #Grasslands #PolicyChange
#AotearoaNewZealand #ResearchImpact #BiodiversityConservation
In this session we used both commercial and open-source LLMs, explored repeated prompting in a land-use decision example, and used an LLM to help communicate a scientific paper. These examples show both the usefulness of LLMs and some of their practical limitations.