1 Overview

This document shows how to use the {ollamar} and {ellmer} packages with an Ollama server running locally or via HPC/port-forwarding

If you’re using an HPC tunnel, make sure your local port (default 11434) forwards to the remote Ollama server.

2 Install packages (one-time)

install.packages("ollamar")
install.packages("ellmer")
library(ollamar)
#> 
#> Attaching package: 'ollamar'
#> The following object is masked from 'package:stats':
#> 
#>     embed
#> The following object is masked from 'package:methods':
#> 
#>     show
library(ellmer)
#> 
#> Attaching package: 'ellmer'
#> The following object is masked from 'package:ollamar':
#> 
#>     chat

3 Connection

# Will error if the Ollama server isn't reachable on base URL
test_connection()
#> Ollama local server running
#> <httr2_response>
#> GET http://localhost:11434/
#> Status: 200 OK
#> Content-Type: text/plain
#> Body: In memory (17 bytes)

If your server isn’t on the default URL, set it globally:

# Example for a custom url (uncomment and adapt if needed)
options(ollama.base_url = "http://localhost:11434")

4 Pull models

Pull models (this may take a while)

# Pull models (this may take a while and requires the Ollama server to have access)
pull("llama3.1")
pull("gpt-oss:120b")

5 List available models

Using ollamar

list_models()
#>              name    size parameter_size quantization_level            modified
#> 1    gpt-oss:120b 65.3 GB         116.8B              MXFP4 2025-08-27T11:11:44
#> 2 llama3.1:latest  4.9 GB           8.0B             Q4_K_M 2025-08-27T11:19:16

Using ellmer (slightly different metadata view)

models_ollama(base_url = "http://localhost:11434")
#>             id created_at        size              capabilities
#> 1     llama3.1 2025-08-27  4920753328          completion,tools
#> 2 gpt-oss:120b 2025-08-27 65290192208 completion,tools,thinking

6 Single-shot prompt with {ollamar}

resp <- generate("llama3.1", "tell me a 5-word story")
resp
#> <httr2_response>
#> POST http://127.0.0.1:11434/api/generate
#> Status: 200 OK
#> Content-Type: application/json
#> Body: In memory (405 bytes)

Extract just the text

resp_process(resp, "text")
#> [1] "She found him on Mars."

Or as a data frame

resp_process(resp, "df")
#> # A tibble: 1 × 3
#>   model    response               created_at                    
#>   <chr>    <chr>                  <chr>                         
#> 1 llama3.1 She found him on Mars. 2025-08-27T11:58:29.741764862Z

7 Chat with {ellmer}

model <- "gpt-oss:120b"

chat <- chat_ollama(
  system_prompt = NULL,
  base_url = "http://localhost:11434",
  model = model,
  seed = NULL,
  api_args = list(),
  echo = NULL,
  api_key = NULL,
  api_headers = character()
)

# Example prompts
chat$chat("Tell me three facts about the Living Planet Index")
#> **Three key facts about the Living Planet Index (LPI):**
#> 
#> 1. **What it measures** – The LPI tracks the *average change in population 
#> size* of ~ 4,000 vertebrate species (mammals, birds, fish, reptiles, and 
#> amphibians) worldwide. By comparing abundance data from the 1970s to the most 
#> recent surveys, it provides a single, time‑series indicator of overall 
#> biodiversity health.
#> 
#> 2. **Recent trend** – According to the 2024 Living Planet Report 
#> (WWF/UNEP 2023‑2024), global vertebrate populations have **declined by roughly 
#> 68 %** since 1970. The most dramatic drops are seen in freshwater species 
#> (‑84 %) and tropical amphibians (‑70 %), while marine species have lost 
#> about ‑58 % and terrestrial mammals about ‑45 %.
#> 
#> 3. **Policy relevance** – The LPI is the *official biodiversity indicator* 
#> adopted by the United Nations Convention on Biological Diversity (CBD). It is 
#> used to monitor progress toward the Aichi Biodiversity Targets (2010‑2020) and 
#> the post‑2020 Global Biodiversity Framework, helping governments and NGOs 
#> assess whether conservation actions are reversing species declines.
chat$chat("Tell me three facts about the hedgehogs")
#> **Three interesting facts about hedgehogs**
#> 
#> | # | Fact | Why it matters |
#> |---|------|----------------|
#> | **1** | **They can roll into a perfect “ball of spikes.”** When threatened, a
#> hedgehog contracts a set of 5 000–7 000 keratin spines (modified hairs) by 
#> contracting the *levator* and *retractor* muscles in its back. The spines stand
#> out at right angles, forming a hard, prickly sphere that deters most predators.
#> | This unique defense is a textbook example of an anti‑predator adaptation and 
#> is why hedgehogs have inspired the “defensive ball” metaphor in many cultures. 
#> |
#> | **2** | **Hedgehogs are “insectivores” with a very varied diet.** In the wild
#> they eat up to 80 % invertebrates—earthworms, beetles, slugs, and 
#> caterpillars—plus occasional fruits, berries, and even small vertebrates (frogs
#> or nestling birds). Their highly acidic stomach (pH ≈ 2) lets them digest the 
#> tough exoskeletons of insects efficiently. | By controlling insect populations,
#> hedgehogs provide natural pest‑control services in gardens and farmland, 
#> reducing the need for chemical pesticides. |
#> | **3** | **Most European hedgehogs hibernate, but the timing is flexible.** 
#> When ambient temperature falls below ~10 °C and food becomes scarce, a hedgehog
#> enters torpor, lowering its body temperature from ~35 °C to as low as 5 °C and 
#> reducing its metabolic rate to < 5 % of normal. A single individual can lose up
#> to 30 % of its body weight during the 4–6‑month hibernation period, relying on 
#> stored fat reserves. | Hibernation makes hedgehogs highly sensitive to climate 
#> change and habitat fragmentation; warmer winters can interrupt hibernation 
#> cycles, leading to premature emergence when food is still scarce, which is one 
#> reason many populations are now in decline. |
#> 
#> *Bonus tidbit*: The world’s smallest hedgehog species, the **Etruscan 
#> shrew‑hedgehog** (*Atelerix albiventris* “dwarf” morph), weighs only 
#> **300 g**—roughly the size of a large mouse—yet still produces the classic 
#> spiny “ball” defense.

8 Notes

  • If you’re tunnelling to an HPC instance, ensure your SSH command forwards localhost:11434 to the remote Ollama port.
  • Large models (e.g., gpt-oss:120b) require substantial VRAM and disk; if pulls fail, try a smaller model first (e.g., llama3.1).
  • For reproducibility, you can pass seed to chat_ollama() or use deterministic settings where supported.