| 1 | <!-- groonga-command --> |
| 2 | <!-- database: functions_language_model_vectorize --> |
| 3 | |
| 4 | # `language_model_vectorize` |
| 5 | |
| 6 | ```{versionadded} 14.1.0 |
| 7 | |
| 8 | ``` |
| 9 | |
| 10 | ```{note} |
| 11 | This is an experimental feature. Currently, this feature is still not stable. |
| 12 | ``` |
| 13 | |
| 14 | ## Summary |
| 15 | |
| 16 | `language_model_vectorize` generates a normalized embedding from the |
| 17 | given text. |
| 18 | |
| 19 | See also {doc}`../language_model` how to prepare a language model. |
| 20 | |
| 21 | You can use {ref}`generated-column` to automate embeddings generation. |
| 22 | |
| 23 | To enable this function, register `functions/language_model` plugin by |
| 24 | the following command: |
| 25 | |
| 26 | ```shell |
| 27 | plugin_register functions/language_model |
| 28 | ``` |
| 29 | |
| 30 | ## Syntax |
| 31 | |
| 32 | `language_model_vectorize` requires two parameters and has optional parameters: |
| 33 | |
| 34 | ``` |
| 35 | language_model_vectorize(model_name, text) |
| 36 | language_model_vectorize(model_name, text, options) |
| 37 | ``` |
| 38 | |
| 39 | `model_name` is the name of language mode to be used. It's associated |
| 40 | with file name. If |
| 41 | `${PREFIX}/share/groonga/language_models/mistral-7b-v0.1.Q4_K_M.gguf` |
| 42 | exists, you can refer it by `mistral-7b-v0.1.Q4_K_M`. It's computed by |
| 43 | removing directory and `.gguf` extension. |
| 44 | |
| 45 | ```{versionadded} 15.1.8 |
| 46 | |
| 47 | You can also specify a Hugging Face URI for `model_name`. |
| 48 | |
| 49 | ``` |
| 50 | |
| 51 | `text` is the input text. |
| 52 | |
| 53 | ```{versionadded} 16.0.1 |
| 54 | |
| 55 | Added `options` |
| 56 | |
| 57 | ``` |
| 58 | |
| 59 | `options` uses the following format. All of the key-value pairs are optional: |
| 60 | |
| 61 | ``` |
| 62 | { |
| 63 | "prefix": prefix_string |
| 64 | } |
| 65 | ``` |
| 66 | |
| 67 | ## Requirements |
| 68 | |
| 69 | You need llama.cpp enabled Groonga. The official packages enable it. |
| 70 | |
| 71 | You need enough CPU/memory resources to use this feature. Language |
| 72 | model related features require more resources than other features. |
| 73 | |
| 74 | You can use GPU in the feature. |
| 75 | |
| 76 | ## Usage |
| 77 | |
| 78 | You need to register `functions/language_model` plugin at first: |
| 79 | |
| 80 | <!-- groonga-command --> |
| 81 | |
| 82 | ```{include} ../../example/reference/functions/language_model_vectorize/usage_register.md |
| 83 | plugin_register functions/language_model |
| 84 | ``` |
| 85 | |
| 86 | Here is a schema definition and sample data. |
| 87 | |
| 88 | Sample schema: |
| 89 | |
| 90 | <!-- groonga-command --> |
| 91 | |
| 92 | ```{include} ../../example/reference/functions/language_model_vectorize/usage_setup_schema.md |
| 93 | table_create --name Memos --flags TABLE_NO_KEY |
| 94 | column_create \ |
| 95 | --table Memos \ |
| 96 | --name content \ |
| 97 | --flags COLUMN_SCALAR \ |
| 98 | --type ShortText |
| 99 | ``` |
| 100 | |
| 101 | Sample data: |
| 102 | |
| 103 | <!-- groonga-command --> |
| 104 | |
| 105 | ```{include} ../../example/reference/functions/language_model_vectorize/usage_setup_data.md |
| 106 | load --table Memos |
| 107 | [ |
| 108 | {"content": "Groonga is fast and embeddable full text search engine."}, |
| 109 | {"content": "PGroonga is a PostgreSQL extension that uses Groonga."}, |
| 110 | {"content": "PostgreSQL is a RDBMS."} |
| 111 | ] |
| 112 | ``` |
| 113 | |
| 114 | Here is a schema that creates a {ref}`generated-column` that |
| 115 | generates embeddings of `Memos.content` automatically: |
| 116 | |
| 117 | <!-- groonga-command --> |
| 118 | |
| 119 | ```{include} ../../example/reference/functions/language_model_vectorize/usage_setup_generated_column.md |
| 120 | column_create \ |
| 121 | --table Memos \ |
| 122 | --name content_embedding \ |
| 123 | --flags COLUMN_VECTOR \ |
| 124 | --type Float32 \ |
| 125 | --source content \ |
| 126 | --generator 'language_model_vectorize("mistral-7b-v0.1.Q4_K_M", content)' |
| 127 | ``` |
| 128 | |
| 129 | You can re-rank matched records by using `distance_inner_product()` |
| 130 | not `distance_cosine()` because `language_model_vectorize()` returns a |
| 131 | normalized embedding. The following example uses all records instead |
| 132 | of filtered records to show this usage simply: |
| 133 | |
| 134 | <!-- groonga-command --> |
| 135 | |
| 136 | ```{include} ../../example/reference/functions/language_model_vectorize/usage_rerank.md |
| 137 | select \ |
| 138 | --table Memos \ |
| 139 | --columns[similarity].stage filtered \ |
| 140 | --columns[similarity].flags COLUMN_SCALAR \ |
| 141 | --columns[similarity].types Float32 \ |
| 142 | --columns[similarity].value 'distance_inner_product(content_embedding, language_model_vectorize("mistral-7b-v0.1.Q4_K_M", "high performance FTS"))' \ |
| 143 | --output_columns content,similarity \ |
| 144 | --sort_keys -similarity |
| 145 | ``` |
| 146 | |
| 147 | ## Parameters |
| 148 | |
| 149 | There are two required parameters and optional parameters. |
| 150 | |
| 151 | ### Required parameters |
| 152 | |
| 153 | #### `model_name` |
| 154 | |
| 155 | `model_name` is the name of language mode to be used. It's associated |
| 156 | with file name. If |
| 157 | `${PREFIX}/share/groonga/language_models/mistral-7b-v0.1.Q4_K_M.gguf` |
| 158 | exists, you can refer it by `mistral-7b-v0.1.Q4_K_M`. It's computed by |
| 159 | removing directory and `.gguf` extension. |
| 160 | |
| 161 | ````{versionadded} 15.1.8 |
| 162 | |
| 163 | You can specify a Hugging Face URI for `model_name`. |
| 164 | |
| 165 | When you specify a Hugging Face URI, the model will be automatically downloaded. |
| 166 | |
| 167 | The model is downloaded to the directory where the Groonga database files are located. |
| 168 | |
| 169 | Example of URI: `hf:///groonga/bge-m3-Q4_K_M-GGUF` for `https://huggingface.co/groonga/bge-m3-Q4_K_M-GGUF`. |
| 170 | |
| 171 | Example of function execution: |
| 172 | |
| 173 | ``` |
| 174 | language_model_vectorize("hf:///groonga/bge-m3-Q4_K_M-GGUF", content) |
| 175 | ``` |
| 176 | |
| 177 | ```` |
| 178 | |
| 179 | #### `text` |
| 180 | |
| 181 | `text` is the input text. |
| 182 | |
| 183 | ### Optional parameters |
| 184 | |
| 185 | #### `options` |
| 186 | |
| 187 | ```{versionadded} 16.0.1 |
| 188 | |
| 189 | ``` |
| 190 | |
| 191 | `options` can be passed as a JSON object. |
| 192 | |
| 193 | ##### `prefix` |
| 194 | |
| 195 | ```{versionadded} 16.0.1 |
| 196 | |
| 197 | ``` |
| 198 | |
| 199 | `prefix` specifies the prefix for `text`. |
| 200 | |
| 201 | Some models such as `multilingual-e5` require a prefix for search target text and query text: |
| 202 | |
| 203 | - Use `"passage: "` when vectorizing search target text. |
| 204 | - Use `"query: "` when vectorizing query text. |
| 205 | |
| 206 | Examples: |
| 207 | |
| 208 | ``` |
| 209 | language_model_vectorize("hf:///groonga/multilingual-e5-base-Q4_K_M-GGUF", \ |
| 210 | content, \ |
| 211 | {"prefix": "passage: "}) |
| 212 | ``` |
| 213 | |
| 214 | ``` |
| 215 | language_model_vectorize("hf:///groonga/multilingual-e5-base-Q4_K_M-GGUF", \ |
| 216 | "male child", \ |
| 217 | {"prefix": "query: "}) |
| 218 | ``` |
| 219 | |
| 220 | ## Return value |
| 221 | |
| 222 | `language_model_vectorize` returns `Float32` vector which as a |
| 223 | normalized embedding. |