llm_sentiment: Sentiment analysis

View source: R/llm-sentiment.R

llm_sentimentR Documentation

Sentiment analysis

Description

Use a Large Language Model (LLM) to perform sentiment analysis from the provided text

Usage

llm_sentiment(
  .data,
  col,
  options = c("positive", "negative", "neutral"),
  pred_name = ".sentiment",
  additional_prompt = ""
)

llm_vec_sentiment(
  x,
  options = c("positive", "negative", "neutral"),
  additional_prompt = "",
  preview = FALSE
)

Arguments

.data

A data.frame or tbl object that contains the text to be analyzed

col

The name of the field to analyze, supports tidy-eval

options

A vector with the options that the LLM should use to assign a sentiment to the text. Defaults to: 'positive', 'negative', 'neutral'

pred_name

A character vector with the name of the new column where the prediction will be placed

additional_prompt

Inserts this text into the prompt sent to the LLM

x

A vector that contains the text to be analyzed

preview

It returns the R call that would have been used to run the prediction. It only returns the first record in x. Defaults to FALSE Applies to vector function only.

Value

llm_sentiment returns a data.frame or tbl object. llm_vec_sentiment returns a vector that is the same length as x.

Examples


library(mall)

data("reviews")

llm_use("ollama", "llama3.2", seed = 100, .silent = TRUE)

llm_sentiment(reviews, review)

# Use 'pred_name' to customize the new column's name
llm_sentiment(reviews, review, pred_name = "review_sentiment")

# Pass custom sentiment options
llm_sentiment(reviews, review, c("positive", "negative"))

# Specify values to return per sentiment
llm_sentiment(reviews, review, c("positive" ~ 1, "negative" ~ 0))

# For character vectors, instead of a data frame, use this function
llm_vec_sentiment(c("I am happy", "I am sad"))

# To preview the first call that will be made to the downstream R function
llm_vec_sentiment(c("I am happy", "I am sad"), preview = TRUE)


mall documentation built on Oct. 24, 2024, 5:09 p.m.