
Classify data with GPT
gpt_classify.RdClassifies the data into predefined classes using the instruction provided.
Usage
gpt_classify(
data,
instruction,
classes,
reminder = NULL,
...,
progress_bar_message = "Classifying with GPT",
append_classes = TRUE
)Arguments
- data
A vector of strings, the data to be classified by gpt
- instruction
A string instructing gpt about the task.
- classes
A vector of strings, the different classes gpt can choose from.
- reminder
A string instructing gpt to answer according to the classes. Defaults to "The most likely choice for the data and context provided above is choice number "
- progress_bar_message
Details
This function uses the normal gpt() fuction, but forces GPT to answer with only the predefined categories provided in in the classes argument. This is enabled by the "logit bias trick".
In the example below, the system_message will consist of the following two parts:
- The instruction: "What kind of text is this the first sentence of?"
- A description of the classes: "1: diary\n2: news article\n3: novel\n4: poem"
The "logit bias trick" consists of two parts. First, I boost the probability of the tokens "1", "2", "3" and "4" by so much that it's certain one of them are chosen. (Technically I add 100 to logit(p).) Second, I set max_tokens to 1. This way, GPT has no other choice than to answer with 1, 2, 3, or 4, and we know which classes these numbers correspond to. (If there are more classes, I boost more than four tokens.)
An effect of this technique is that the finish_reason will almost always be "length". This is a feature, not a bug.