-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassification-assistant.r
196 lines (163 loc) · 3.76 KB
/
classification-assistant.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#' Worldwide Trends
#'
#' This script is responsible for classifying the
#' terms using OpenAI API.
#'
#' ----- ----- ----- ----- :) ----- ----- ----- -----
#'
#'
#' As at 30 Jun 2024, the OpenAI's assistants API is not fully working.
#' Vector stores are being saved with error.
#'
#' Please ignore this script.
#'
#'
source("utils/utils.r")
get_requirements("./requirements.txt")
dotenv::load_dot_env(".Renviron")
# Retrieves grouped trends created in main.r
#
daily_top_terms_csv <- daily_top_terms_file()
print_m(
sprintf("Loading top terms %s...", daily_top_terms_csv)
)
if (!file.exists(daily_top_terms_csv)) {
stop("you must first generate the output file (see readme.md)")
}
# Since OpenAI assistants API doesn't supports CSV yet,
# converts the data to TXT format.
grouped_terms_txt <- "out/grouped-top-terms.txt"
top_terms <- read.csv(daily_top_terms_csv) |>
dplyr::group_by(
term, country
) |>
utils::write.table(
grouped_terms_txt,
sep = ",",
col.names = FALSE,
row.names = FALSE
)
# Saves the dataset using OpenAI files API
#
print_m(
sprintf("Saving dataset %s in OpenAI...", grouped_terms_txt)
)
file_obj <- NULL
filename <- stringr::str_split_i(grouped_terms_txt, "/", i = -1)
uploaded_files <- list_files_openai()
for (uploaded_file in uploaded_files$data) {
# Extracts only filename, without folders.
if (uploaded_file$filename == filename) {
file_obj <- uploaded_file
break
}
}
if (is.null(file_obj)) {
# If no file was found, uploads a new one.
file_obj <- add_file_openai(
grouped_terms_txt,
"assistants"
)
}
vector_store_obj <- create_vector_store_openai(
filename,
list(
file_obj$id
)
)
# Creates OpenAI assistant
#
print_m(
"Creating new assistant..."
)
tools_cfg <- list(
list(
type = "file_search"
)
)
assistant_obj <- create_assistant_openai(
name = "Topics Categorization",
instructions = "
You excel at categorizing items. When presented a item,
you will categorize with the most related topic
",
tools = tools_cfg
)
print_m(
sprintf("Assistant created, id %s", assistant_obj$id)
)
# Top trends classification by topics
#
topics <- c(
"Politics",
"Sports",
"Economy",
"Music",
"Science & Technology",
"Health & Medicine",
"Environment",
"Education",
"Business & Finance",
"Culture & Society",
"Entertainment",
"Travel & Tourism",
"Food & Drink",
"Lifestyle"
)
fmt_topics <- paste(paste("-", topics), collapse = "\n")
prompt <- paste(
"There is a stored file containing trend searchs in TXT format.
The TXT file contains daily trending terms from Google Trends grouped by countries.
The file uses the following delimiters: <term> and <country>.
Your job is to classify them with a set of topics from the following list.
You MUST use localization according to each country.
Provide your answer writing a new TXT file with the topic that categorizes each row.
Choose ONLY one option from the list of topics provided here for each term:",
fmt_topics,
sep = "\n"
)
first_msg <- list(
role = "user",
content = prompt,
attachments = list(
list(
file_id = file_obj$id,
tools = tools_cfg
)
)
)
thread <- list(
messages = list(
first_msg
)
)
print_m(
"Initializing new run...",
sprintf("Prompt: %s...", substr(prompt, 1, 300))
)
run_obj <- create_run_openai(
assistant_obj$id,
thread
)
print_m(
sprintf("Run created, id %s", run_obj$id),
"Awaiting messages..."
)
max_it <- 10
while (TRUE) {
if (max_it == 0) {
stop("could not complete run")
}
Sys.sleep(5)
run_status <- get_run_openai(run_obj$thread_id, run_obj$id)
if (
run_status$status == "completed"
) {
break
}
max_it <- max_it - 1
}
messages <- get_messages_openai(run_obj$thread_id)
for (msg in messages) {
print(msg)
}