Skip to contents

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 by pc_search() or pc_combine(), containing at minimum title and abstract columns.

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/completions endpoint but not the newer OpenAI Responses API. Falls back to the value set with pc_configure(), then "anthropic".

model

Character. Model identifier. If NULL, uses the provider default as determined by ellmer.

api_key

Character. API key for the LLM provider. Falls back to the value set with pc_configure(), then the PUBCLASSIFY_LLM_KEY environment 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 with pc_configure(), then the PUBCLASSIFY_LLM_BASE_URL environment variable. If still NULL, 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, retrieves top_k candidate fields via embedding similarity before the LLM call. Requires embed_provider and a valid API key. Default FALSE.

embed_provider

Character. Embedding provider (e.g. "openai", "cohere"). Required when use_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. Default 5.

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 ellmer chat 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")
} # }