Debian Codegraph @

lib/grn_language_model.hpp

raw symbols SCIP
1 /*
2 Copyright (C) 2024-2025 Sutou Kouhei <[email protected]>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #pragma once
20
21 #include "grn_ctx.h"
22
23 #include <memory>
24 #include <string>
25 #include <string_view>
26 #include <vector>
27
28 namespace grn {
29 namespace language_model {
30 void
31 init_from_env();
32 void
33 fin_external_libraries();
34 } // namespace language_model
35
36 class LanguageModelInferencer;
37 class LanguageModelLoader;
38
39 class LanguageModel : public std::enable_shared_from_this<LanguageModel> {
40 public:
41 class Impl;
42
43 LanguageModel(Impl *impl);
44 ~LanguageModel();
45
46 std::unique_ptr<LanguageModelInferencer>
47 make_inferencer(grn_ctx *ctx);
48 uint32_t
49 get_n_embedding_dimensions(grn_ctx *ctx);
50
51 private:
52 std::unique_ptr<Impl> impl_;
53 };
54
55 class LanguageModelLoader {
56 public:
57 LanguageModelLoader(grn_ctx *ctx) : ctx_(ctx) {}
58
59 std::shared_ptr<LanguageModel>
60 load();
61
62 std::string model_path;
63 int32_t n_gpu_layers = GRN_LANGUAGE_MODEL_LOADER_N_GPU_LAYERS_DEFAULT;
64
65 private:
66 grn_ctx *ctx_;
67 };
68
69 class LanguageModelInferencer {
70 public:
71 class Impl;
72
73 LanguageModelInferencer(Impl *impl);
74 ~LanguageModelInferencer();
75
76 /// \brief Prepend `prefix` to all values of `input_column` in
77 /// \ref vectorize_in_batch.
78 ///
79 /// \param prefix The prefix for all values of `input_column.
80 ///
81 /// \since 15.1.9
82 void
83 set_input_column_value_prefix(std::string prefix);
84
85 /// \brief Set progress callback that is called on each batch
86 /// vectorization is completed.
87 ///
88 /// \param callback The callback.
89 /// \param user_data The data that is passed to the `callback` when
90 /// `callback` is called.
91 ///
92 /// \since 15.2.1
93 void
94 set_progress_callback(grn_progress_callback_func callback, void *user_data);
95
96 /// \brief Generate an embedding for a record
97 ///
98 /// \param ctx A \ref grn_ctx.
99 /// \param text The text to be vectorized.
100 /// \param output_vector It must be an Float32 vector. This
101 /// function doesn't validate it.
102 void
103 vectorize(grn_ctx *ctx, std::string_view text, grn_obj *output_vector);
104
105 /// \brief Generate embeddings for multiple records in batch
106 ///
107 /// \param ctx A \ref grn_ctx.
108 /// \param cursor It must be a valid \ref grn_table_cursor.
109 /// This function doesn't validate it.
110 /// \param input_column It must be an text-family scalar
111 /// column. This function doesn't validate it.
112
113 /// \param output It must be a `Float32` vector column or a
114 /// `Float32` vector. This function doesn't validate
115 /// it.
116 ///
117 /// If it's a `Float32` vector, output order is same
118 /// as IDs returned by the `cursor`.
119 void
120 vectorize_in_batch(grn_ctx *ctx,
121 grn_table_cursor *cursor,
122 grn_obj *input_column,
123 grn_obj *output);
124
125 private:
126 std::unique_ptr<Impl> impl_;
127 };
128 } // namespace grn