Debian Codegraph @

plugins/functions/language_model.c

raw symbols SCIP
1 // Copyright (C) 2024 Sutou Kouhei <[email protected]>
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17 #ifdef GRN_EMBEDDED
18 # define GRN_PLUGIN_FUNCTION_TAG functions_language_model
19 #endif
20
21 #include <groonga/plugin.h>
22
23 static grn_obj *
24 func_language_model_vectorize(grn_ctx *ctx,
25 int n_args,
26 grn_obj **args,
27 grn_user_data *user_data)
28 {
29 const char *tag = "language_model_vectorize():";
30
31 if (!(n_args == 2 || n_args == 3)) {
32 GRN_PLUGIN_ERROR(ctx,
33 GRN_INVALID_ARGUMENT,
34 "%s wrong number of arguments (%d for 2..3)",
35 tag,
36 n_args);
37 return NULL;
38 }
39
40 grn_obj *vector = NULL;
41 grn_language_model_loader *loader = grn_language_model_loader_open(ctx);
42 grn_language_model *model = NULL;
43 grn_language_model_inferencer *inferencer = NULL;
44
45 grn_obj *model_name = args[0];
46 if (!grn_obj_is_text_family_bulk(ctx, model_name)) {
47 grn_obj inspected;
48 GRN_TEXT_INIT(&inspected, 0);
49 grn_inspect(ctx, &inspected, model_name);
50 GRN_PLUGIN_ERROR(ctx,
51 GRN_INVALID_ARGUMENT,
52 "%s the 1st argument must be model name: %.*s",
53 tag,
54 (int)(GRN_TEXT_LEN(&inspected)),
55 GRN_TEXT_VALUE(&inspected));
56 GRN_OBJ_FIN(ctx, &inspected);
57 goto exit;
58 }
59 grn_language_model_loader_set_model(ctx,
60 loader,
61 GRN_TEXT_VALUE(model_name),
62 GRN_TEXT_LEN(model_name));
63
64 grn_obj *text = args[1];
65 if (!grn_obj_is_text_family_bulk(ctx, text)) {
66 grn_obj inspected;
67 GRN_TEXT_INIT(&inspected, 0);
68 grn_inspect(ctx, &inspected, text);
69 GRN_PLUGIN_ERROR(ctx,
70 GRN_INVALID_ARGUMENT,
71 "%s the 2nd argument must be text to vectorize: %.*s",
72 tag,
73 (int)(GRN_TEXT_LEN(&inspected)),
74 GRN_TEXT_VALUE(&inspected));
75 GRN_OBJ_FIN(ctx, &inspected);
76 goto exit;
77 }
78
79 grn_obj *options = NULL;
80 if (n_args == 3) {
81 options = args[2];
82 }
83
84 grn_obj *prefix = NULL;
85 if (options) {
86 grn_rc rc = grn_proc_options_parse(ctx,
87 options,
88 tag,
89 "prefix",
90 GRN_PROC_OPTION_VALUE_RAW,
91 &prefix,
92 NULL);
93 if (rc != GRN_SUCCESS) {
94 goto exit;
95 }
96
97 if (prefix && !grn_obj_is_text_family_bulk(ctx, prefix)) {
98 grn_obj inspected;
99 GRN_TEXT_INIT(&inspected, 0);
100 grn_inspect(ctx, &inspected, prefix);
101 GRN_PLUGIN_ERROR(ctx,
102 GRN_INVALID_ARGUMENT,
103 "%s prefix must be a string: %.*s",
104 tag,
105 (int)(GRN_TEXT_LEN(&inspected)),
106 GRN_TEXT_VALUE(&inspected));
107 GRN_OBJ_FIN(ctx, &inspected);
108 goto exit;
109 }
110 }
111
112 model = grn_language_model_loader_load(ctx, loader);
113 if (!model) {
114 GRN_PLUGIN_ERROR(ctx,
115 ctx->rc,
116 "%s failed to load model: %s",
117 tag,
118 ctx->errbuf);
119 goto exit;
120 }
121
122 inferencer = grn_language_model_open_inferencer(ctx, model);
123 if (!inferencer) {
124 GRN_PLUGIN_ERROR(ctx,
125 ctx->rc,
126 "%s failed to open model inferencer: %s",
127 tag,
128 ctx->errbuf);
129 goto exit;
130 }
131
132 vector =
133 grn_plugin_proc_alloc(ctx, user_data, GRN_DB_FLOAT32, GRN_OBJ_VECTOR);
134 if (!vector) {
135 return NULL;
136 }
137
138 grn_rc rc;
139 if (prefix) {
140 grn_obj prefixed_text;
141 GRN_TEXT_INIT(&prefixed_text, 0);
142 GRN_TEXT_PUT(ctx,
143 &prefixed_text,
144 GRN_TEXT_VALUE(prefix),
145 GRN_TEXT_LEN(prefix));
146 GRN_TEXT_PUT(ctx, &prefixed_text, GRN_TEXT_VALUE(text), GRN_TEXT_LEN(text));
147 rc = grn_language_model_inferencer_vectorize(ctx,
148 inferencer,
149 GRN_TEXT_VALUE(&prefixed_text),
150 GRN_TEXT_LEN(&prefixed_text),
151 vector);
152 GRN_OBJ_FIN(ctx, &prefixed_text);
153 } else {
154 rc = grn_language_model_inferencer_vectorize(ctx,
155 inferencer,
156 GRN_TEXT_VALUE(text),
157 GRN_TEXT_LEN(text),
158 vector);
159 }
160
161 if (rc != GRN_SUCCESS) {
162 GRN_PLUGIN_ERROR(ctx,
163 ctx->rc,
164 "%s failed to vectorize: %s",
165 tag,
166 ctx->errbuf);
167 grn_obj_close(ctx, vector);
168 vector = NULL;
169 goto exit;
170 }
171
172 exit:
173 grn_language_model_inferencer_close(ctx, inferencer);
174 grn_language_model_close(ctx, model);
175 grn_language_model_loader_close(ctx, loader);
176
177 return vector;
178 }
179
180 static grn_rc
181 applier_language_model_vectorize(grn_ctx *ctx, grn_applier_data *data)
182 {
183 const char *tag = "language_model_vectorize():";
184
185 size_t n_args;
186 grn_obj **args = grn_applier_data_get_args(ctx, data, &n_args);
187
188 if (!(n_args == 2 || n_args == 3)) {
189 GRN_PLUGIN_ERROR(ctx,
190 GRN_INVALID_ARGUMENT,
191 "%s wrong number of arguments (%d for 2..3)",
192 tag,
193 (uint32_t)n_args);
194 return ctx->rc;
195 }
196
197 grn_language_model_loader *loader = grn_language_model_loader_open(ctx);
198 grn_language_model *model = NULL;
199 grn_language_model_inferencer *inferencer = NULL;
200
201 grn_obj *model_name = args[0];
202 if (!grn_obj_is_text_family_bulk(ctx, model_name)) {
203 grn_obj inspected;
204 GRN_TEXT_INIT(&inspected, 0);
205 grn_inspect(ctx, &inspected, model_name);
206 GRN_PLUGIN_ERROR(ctx,
207 GRN_INVALID_ARGUMENT,
208 "%s the 1st argument must be model name: %.*s",
209 tag,
210 (int)(GRN_TEXT_LEN(&inspected)),
211 GRN_TEXT_VALUE(&inspected));
212 GRN_OBJ_FIN(ctx, &inspected);
213 goto exit;
214 }
215 grn_language_model_loader_set_model(ctx,
216 loader,
217 GRN_TEXT_VALUE(model_name),
218 GRN_TEXT_LEN(model_name));
219
220 grn_obj *input_column = args[1];
221 if (!(grn_obj_is_text_family_scalar_column(ctx, input_column) ||
222 grn_obj_is_text_family_scalar_accessor(ctx, input_column))) {
223 grn_obj inspected;
224 GRN_TEXT_INIT(&inspected, 0);
225 grn_inspect(ctx, &inspected, input_column);
226 GRN_PLUGIN_ERROR(ctx,
227 GRN_INVALID_ARGUMENT,
228 "%s the 2nd argument must be a text family column "
229 "or accessor to vectorize: %.*s",
230 tag,
231 (int)(GRN_TEXT_LEN(&inspected)),
232 GRN_TEXT_VALUE(&inspected));
233 GRN_OBJ_FIN(ctx, &inspected);
234 goto exit;
235 }
236
237 grn_obj *options = NULL;
238 if (n_args == 3) {
239 options = args[2];
240 }
241
242 model = grn_language_model_loader_load(ctx, loader);
243 if (!model) {
244 GRN_PLUGIN_ERROR(ctx,
245 ctx->rc,
246 "%s failed to load model: %s",
247 tag,
248 ctx->errbuf);
249 goto exit;
250 }
251
252 inferencer = grn_language_model_open_inferencer(ctx, model);
253 if (!inferencer) {
254 GRN_PLUGIN_ERROR(ctx,
255 ctx->rc,
256 "%s failed to open model inferencer: %s",
257 tag,
258 ctx->errbuf);
259 goto exit;
260 }
261
262 if (options) {
263 grn_obj *prefix = NULL;
264 grn_rc rc = grn_proc_options_parse(ctx,
265 options,
266 tag,
267 "prefix",
268 GRN_PROC_OPTION_VALUE_RAW,
269 &prefix,
270 NULL);
271 if (rc != GRN_SUCCESS) {
272 goto exit;
273 }
274
275 if (prefix && !grn_obj_is_text_family_bulk(ctx, prefix)) {
276 grn_obj inspected;
277 GRN_TEXT_INIT(&inspected, 0);
278 grn_inspect(ctx, &inspected, prefix);
279 GRN_PLUGIN_ERROR(ctx,
280 GRN_INVALID_ARGUMENT,
281 "%s prefix must be a string: %.*s",
282 tag,
283 (int)(GRN_TEXT_LEN(&inspected)),
284 GRN_TEXT_VALUE(&inspected));
285 GRN_OBJ_FIN(ctx, &inspected);
286 goto exit;
287 }
288 if (prefix && GRN_TEXT_LEN(prefix) > 0) {
289 grn_language_model_inferencer_set_input_column_value_prefix(
290 ctx,
291 inferencer,
292 GRN_TEXT_VALUE(prefix),
293 (int64_t)GRN_TEXT_LEN(prefix));
294 if (ctx->rc != GRN_SUCCESS) {
295 goto exit;
296 }
297 }
298 }
299
300 grn_rc rc = grn_language_model_inferencer_vectorize_applier(ctx,
301 inferencer,
302 input_column,
303 data);
304 if (rc != GRN_SUCCESS) {
305 GRN_PLUGIN_ERROR(ctx,
306 ctx->rc,
307 "%s failed to vectorize: %s",
308 tag,
309 ctx->errbuf);
310 goto exit;
311 }
312
313 exit:
314 grn_language_model_inferencer_close(ctx, inferencer);
315 grn_language_model_close(ctx, model);
316 grn_language_model_loader_close(ctx, loader);
317
318 return ctx->rc;
319 }
320
321 grn_rc
322 GRN_PLUGIN_INIT(grn_ctx *ctx)
323 {
324 return ctx->rc;
325 }
326
327 grn_rc
328 GRN_PLUGIN_REGISTER(grn_ctx *ctx)
329 {
330 grn_rc rc = GRN_SUCCESS;
331
332 {
333 grn_obj *proc = grn_proc_create(ctx,
334 "language_model_vectorize",
335 -1,
336 GRN_PROC_FUNCTION,
337 func_language_model_vectorize,
338 NULL,
339 NULL,
340 0,
341 NULL);
342 grn_proc_set_applier(ctx, proc, applier_language_model_vectorize);
343 }
344
345 return rc;
346 }
347
348 grn_rc
349 GRN_PLUGIN_FIN(grn_ctx *ctx)
350 {
351 return GRN_SUCCESS;
352 }