Uses a large language model (via ellmer) to assign each publication in
pubs to a field in taxonomy. Classification text is built as
title + abstract when an abstract is present, or title alone when it
is not (see pc_fetch_abstracts() to backfill missing abstracts first).
Usage
pc_classify(
pubs,
taxonomy,
provider = NULL,
model = NULL,
api_key = NULL,
base_url = NULL,
system_prompt = NULL,
classify_instructions = NULL,
use_embeddings = FALSE,
embed_provider = NULL,
embed_key = NULL,
top_k = 5L,
batch_size = 10L,
...
)Arguments
- pubs
A
tibble::tibble()returned bypc_search()orpc_combine(), containing at minimumtitleandabstractcolumns.- taxonomy
A
pc_taxonomy()object defining the fields to classify into.- provider
Character. LLM provider passed to
ellmer. One of"anthropic","openai", or"openai-compatible". Use"openai-compatible"for any OpenAI-compatible gateway (e.g. a multi-model proxy) that implements the/chat/completionsendpoint but not the newer OpenAI Responses API. Falls back to the value set withpc_configure(), then"anthropic".- model
Character. Model identifier. If
NULL, uses the provider default as determined byellmer.- api_key
Character. API key for the LLM provider. Falls back to the value set with
pc_configure(), then thePUBCLASSIFY_LLM_KEYenvironment variable, then the provider's own default variable (e.g.OPENAI_API_KEY).- base_url
Character. Base URL for the LLM provider's API endpoint, for targeting a non-default deployment (e.g. a proxy, gateway, or cloud-hosted endpoint such as Azure AI Foundry). Used for
provider = "openai","openai-compatible", and"anthropic"; required for"openai-compatible". Falls back to the value set withpc_configure(), then thePUBCLASSIFY_LLM_BASE_URLenvironment variable. If stillNULL, each provider's standard endpoint is used.- system_prompt
Character. Replaces the default system message sent to the LLM. The default instructs the model to act as a scientific literature classifier and return structured JSON. Override this to change the model's persona or task framing. The output format instruction is always appended after this value.
- classify_instructions
Character. Optional additional instructions injected into the user message after the taxonomy definitions and before the publications. Use this for domain-specific guidance such as priority rules, tie-break logic, or category-specific signals.
- use_embeddings
Logical. If
TRUE, retrievestop_kcandidate fields via embedding similarity before the LLM call. Requiresembed_providerand a valid API key. DefaultFALSE.- embed_provider
Character. Embedding provider (e.g.
"openai","cohere"). Required whenuse_embeddings = TRUE.- embed_key
Character. Embedding API key. Falls back to the provider-specific environment variable when
NULL.- top_k
Integer. Number of candidate taxonomy fields passed to the LLM per publication when
use_embeddings = TRUE. Default5.- batch_size
Integer. Number of publications per LLM call. Batching reduces the number of API round-trips for large result sets. Default
10.- ...
Additional arguments passed to the
ellmerchat constructor (e.g.seed).
Value
The input tibble with four additional columns:
- pc_field
Assigned taxonomy field name (character)
- pc_rationale
Brief LLM reasoning for the assignment (character)
- pc_text_source
Text used for classification:
"title+abstract"or"title"(character)- pc_classified_by
Method used:
"llm-full"(full taxonomy) or"llm-rag"(embedding-narrowed) (character)
Details
By default (use_embeddings = FALSE), the full taxonomy is included in
every LLM prompt. For taxonomies with 40 or more fields, set
use_embeddings = TRUE to first narrow candidates via embedding similarity,
passing only the top top_k fields to the LLM per publication.
Examples
if (FALSE) { # \dontrun{
tax <- pc_taxonomy_example()
pubs <- pc_search("University of Edinburgh", field = "affiliation",
sources = "openalex")
pubs <- pc_classify(pubs, tax, provider = "openai", model = "gpt-4o")
} # }