rag_sentemo: Structured Emotion/Sentiment via RAG (Small LLMs)

View source: R/rag_sentemo.R

rag_sentemoR Documentation

Structured Emotion/Sentiment via RAG (Small LLMs)

Description

Convenience wrapper around rag() that keeps vector retrieval but simplifies getting structured outputs for emotion or sentiment analysis using small local LLMs (1–4B) with sensible defaults.

Usage

rag_sentemo(
  text = NULL,
  path = NULL,
  task = c("emotion", "sentiment"),
  labels_set = NULL,
  max_labels = 5,
  transformer = c("TinyLLAMA", "Gemma3-1B", "Gemma3-4B", "Qwen3-1.7B", "Ministral-3B"),
  similarity_top_k = 5,
  response_mode = c("compact", "refine", "simple_summarize"),
  output = c("table", "json", "csv"),
  global_analysis = FALSE,
  ...
)

Arguments

text

Character vector or list. Text to analyze. One entry per document.

path

Character. Optional directory with files to index (e.g., PDFs). If provided, overrides text.

task

Character. One of "emotion" or "sentiment".

labels_set

Character vector of allowed labels. If NULL, defaults to Emo8 for task = "emotion" and c("positive","neutral","negative") for task = "sentiment".

max_labels

Integer. Max number of labels to return.

transformer

Character. Small local LLM to use. One of:

  • "TinyLLAMA" (default)

  • "Gemma3-1B"

  • "Gemma3-4B"

  • "Qwen3-0.6B"

  • "Qwen3-1.7B"

  • "Ministral-3B"

similarity_top_k

Integer. Retrieval depth per query. Default 5.

response_mode

Character. LlamaIndex response mode. Default "compact".

output

Character. "table" (default) or "json".

global_analysis

Logical. If TRUE, analyze all documents jointly. Default FALSE.

...

Additional arguments passed to rag() (e.g., device, keep_in_env).

Value

For Gemma3-1B/4B and output = "table"/"csv", a data.frame with columns
doc_id, text, label, confidence.
For Gemma3-1B/4B and output = "json", a JSON array of per-doc objects with those fields.
For other models, structured outputs are not supported; the function falls back to output = "text" and returns a free-text "rag" object.

Examples

## Not run: 
texts <- c(
  "I feel so happy and grateful today!",
  "This is frustrating and makes me angry."
)
rag_sentemo(texts, task = "emotion", output = "table")
rag_sentemo(texts, task = "sentiment", output = "json")

## End(Not run)


transforEmotion documentation built on Jan. 8, 2026, 5:06 p.m.