@@ -167,21 +167,18 @@ q_thought <- tribble(
167
167
output_thought <- query(q_thought , output = " text" )
168
168
# >
169
169
# > ── Answer from llama3.2:3b-instruct-q8_0 ─────────────────────────────
170
- # > I would assign a ** negative** sentiment to this text.
170
+ # > I would assign a negative sentiment to this text.
171
171
# >
172
172
# > Here's why:
173
173
# >
174
- # > * The word "terrible" is a strong adjective that implies a strongly
175
- # > unfavorable opinion. It suggests that the speaker has had a very poor
176
- # > experience with the pizza.
177
- # > * The tone of the sentence is direct and critical, implying that the
178
- # > speaker was not just mildly dissatisfied but actively disliked the
179
- # > pizza.
180
- # > * There's no nuance or mixed sentiment in this text; it's a
181
- # > straightforward expression of dislike.
174
+ # > * The word "terrible" is a strong adjective that conveys a strong
175
+ # > negative opinion.
176
+ # > * The tone of the sentence is also quite critical and dismissive.
177
+ # > * There are no positive or neutral phrases in the text, which
178
+ # > suggests that the author has a clear dislike for the pizza.
182
179
# >
183
- # > Overall, the language and tone used suggest that the speaker had a
184
- # > very negative experience with the pizza.
180
+ # > Overall, the language and tone used in this text indicate a strong
181
+ # > negative sentiment towards the pizza.
185
182
```
186
183
187
184
In the next step we can use the assistant's reasoning and a user prompt with the classification question.
@@ -267,15 +264,19 @@ q_os <- make_query(
267
264
template = " {text}\n {prompt}" ,
268
265
prompt = " Categories: positive, neutral, negative" ,
269
266
system = " You assign texts into categories. Answer with just the correct category." ,
267
+ example = examples_os ,
270
268
)
271
269
print(q_os )
272
270
# > [[1]]
273
- # > # A tibble: 2 × 2
274
- # > role content
275
- # > <chr> <glue>
276
- # > 1 system You assign texts into categories. Answer with just the corre…
277
- # > 2 user the service is great
278
- # > Categories: positive, neutral, negative
271
+ # > # A tibble: 4 × 2
272
+ # > role content
273
+ # > <chr> <glue>
274
+ # > 1 system You assign texts into categories. Answer with just the co…
275
+ # > 2 user the pizza tastes terrible
276
+ # > Categories: positive, neutral, n…
277
+ # > 3 assistant negative
278
+ # > 4 user the service is great
279
+ # > Categories: positive, neutral, negati…
279
280
280
281
query(q_os )
281
282
# >
@@ -388,23 +389,27 @@ We can also use this approach in a 'tidy' coding style:
388
389
389
390
390
391
``` r
391
- library(dplyr )
392
+ library(dplyr , warn.conflicts = FALSE )
392
393
movie_reviews_annotated <- movie_reviews | >
393
394
mutate(
394
- sentiment = queries = make_query(
395
+ sentiment = make_query(
395
396
text = review ,
396
397
prompt = " Categories: positive, neutral, negative" ,
397
398
template = " {prefix}{text}\n {prompt}" ,
398
399
system = " Classify the sentiment of the movie review. Answer with just the correct category." ,
399
400
prefix = " Text to classify: "
400
- ) | >
401
+ ) | >
401
402
query(screen = FALSE , output = " text" )
402
403
)
403
404
movie_reviews_annotated
404
- # > Error in parse(text = input): <text>:4:25: unexpected '='
405
- # > 3: mutate(
406
- # > 4: sentiment = queries =
407
- # > ^
405
+ # > # A tibble: 5 × 4
406
+ # > review_id review annotation sentiment
407
+ # > <int> <chr> <chr> <chr>
408
+ # > 1 1 A stunning visual spectacle with a g… Positive Positive
409
+ # > 2 2 The plot was predictable, but the ac… positive positive
410
+ # > 3 3 An overrated film with underwhelming… Negative Negative
411
+ # > 4 4 A beautiful tale of love and adventu… Positive Positive
412
+ # > 5 5 The movie lacked depth, but the spec… Neutral Neutral
408
413
```
409
414
410
415
This takes a little longer than classic supervised machine learning or even classification with transformer models.
0 commit comments