| 1 | <!-- groonga-command --> |
| 2 | <!-- database: functions_language_model_knn --> |
| 3 | |
| 4 | # `language_model_knn` |
| 5 | |
| 6 | ```{versionadded} 15.1.8 |
| 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_knn` is a function for semantic search. |
| 17 | |
| 18 | Semantic search uses the k-Nearest Neighbors (k-NN) algorithm. |
| 19 | |
| 20 | You must use it with {doc}`../tokenizers/token_language_model_knn`. |
| 21 | |
| 22 | It can be used as a condition for `--filter` and as a sort key for `--sort_keys`. |
| 23 | |
| 24 | To enable this function, register `language_model/knn` plugin by the following command: |
| 25 | |
| 26 | ```shell |
| 27 | plugin_register language_model/knn |
| 28 | ``` |
| 29 | |
| 30 | ## Syntax |
| 31 | |
| 32 | `language_model_knn` requires two parameters: |
| 33 | |
| 34 | ``` |
| 35 | language_model_knn(column, query) |
| 36 | ``` |
| 37 | |
| 38 | `column` is the search target column. It must be a column with an index. |
| 39 | |
| 40 | `query` is a search query. |
| 41 | |
| 42 | ## Requirements |
| 43 | |
| 44 | You need Faiss enabled Groonga. The official packages enable it. |
| 45 | |
| 46 | ## Usage |
| 47 | |
| 48 | You need to register `language_model/knn` plugin at first: |
| 49 | |
| 50 | <!-- groonga-command --> |
| 51 | |
| 52 | ```{include} ../../example/reference/functions/language_model_knn/usage_register.md |
| 53 | plugin_register language_model/knn |
| 54 | ``` |
| 55 | |
| 56 | Here is a schema definition and sample data. |
| 57 | |
| 58 | Sample schema: |
| 59 | |
| 60 | <!-- groonga-command --> |
| 61 | |
| 62 | ```{include} ../../example/reference/functions/language_model_knn/usage_setup_schema.md |
| 63 | table_create --name Memos --flags TABLE_NO_KEY |
| 64 | column_create \ |
| 65 | --table Memos \ |
| 66 | --name content \ |
| 67 | --flags COLUMN_SCALAR \ |
| 68 | --type ShortText |
| 69 | ``` |
| 70 | |
| 71 | Sample data: |
| 72 | |
| 73 | <!-- groonga-command --> |
| 74 | |
| 75 | ```{include} ../../example/reference/functions/language_model_knn/usage_setup_data.md |
| 76 | load --table Memos |
| 77 | [ |
| 78 | {"content": "I am a boy."}, |
| 79 | {"content": "This is an apple."}, |
| 80 | {"content": "Groonga is a full text search engine."} |
| 81 | ] |
| 82 | ``` |
| 83 | |
| 84 | You need to store embedding information for each record. Here is how to create that column. |
| 85 | |
| 86 | <!-- groonga-command --> |
| 87 | |
| 88 | ```{include} ../../example/reference/functions/language_model_knn/column_create.md |
| 89 | column_create Memos embedding_code COLUMN_SCALAR ShortBinary |
| 90 | ``` |
| 91 | |
| 92 | Create an index for semantic search. |
| 93 | |
| 94 | Specify {doc}`../tokenizers/token_language_model_knn` as the tokenizer. |
| 95 | The tokenizer's arguments are `model` and `code_column`. |
| 96 | Specify the model to use for `model`, and specify the column to store the generated embedding information for `code_column`. |
| 97 | |
| 98 | <!-- groonga-command --> |
| 99 | |
| 100 | ```{include} ../../example/reference/functions/language_model_knn/index_column_create.md |
| 101 | table_create Centroids TABLE_HASH_KEY ShortBinary \ |
| 102 | --default_tokenizer \ |
| 103 | 'TokenLanguageModelKNN("model", "hf:///groonga/all-MiniLM-L6-v2-Q4_K_M-GGUF", \ |
| 104 | "code_column", "embedding_code")' |
| 105 | |
| 106 | column_create Centroids data_content COLUMN_INDEX Memos content |
| 107 | ``` |
| 108 | |
| 109 | This enables semantic search. |
| 110 | When you `load` data into `Memos.content`, Groonga automatically generates embeddings. |
| 111 | Users do not need to generate embeddings. |
| 112 | |
| 113 | Here is an example of semantic search: |
| 114 | |
| 115 | <!-- groonga-command --> |
| 116 | |
| 117 | ```{include} ../../example/reference/functions/language_model_knn/filter.md |
| 118 | select Memos \ |
| 119 | --filter 'language_model_knn(content, "male child")' \ |
| 120 | --output_columns content |
| 121 | ``` |
| 122 | |
| 123 | `language_model_knn` function can also be used as a sort key. |
| 124 | Specify `language_model_knn` for `--sort_keys`. |
| 125 | Since you likely need to fetch results in descending order of similarity, you add a `-` prefix to fetch them in descending order. |
| 126 | |
| 127 | Here is an example of filtering by `_id` and then sorting by similarity: |
| 128 | |
| 129 | <!-- groonga-command --> |
| 130 | |
| 131 | ```{include} ../../example/reference/functions/language_model_knn/sort_keys.md |
| 132 | select Memos \ |
| 133 | --filter '_id < 3' \ |
| 134 | --sort_keys '-language_model_knn(content, "male child")' \ |
| 135 | --output_columns content |
| 136 | ``` |
| 137 | |
| 138 | ## Parameters |
| 139 | |
| 140 | There are two required parameters. |
| 141 | |
| 142 | ### `column` |
| 143 | |
| 144 | `column` is the search target column. It must be a column with an index. |
| 145 | |
| 146 | ### `query` |
| 147 | |
| 148 | `query` is a search query. |
| 149 | |
| 150 | ### `k` |
| 151 | |
| 152 | ```{versionadded} 15.2.1 |
| 153 | |
| 154 | ``` |
| 155 | |
| 156 | Specify the number of records to return. |
| 157 | If you don't set this option, the upper limit of the number of records to return is `10`. |
| 158 | |
| 159 | You can specify a negative value. It means that the number of **matched records + `k` + 1**. |
| 160 | For example, `"{ "k" : -1 }"` outputs all records. It’s a very useful value to show all records. |
| 161 | |
| 162 | Here is a simple negative `k` value usage example. |
| 163 | |
| 164 | Sample data: |
| 165 | |
| 166 | <!-- groonga-command --> |
| 167 | |
| 168 | ```{include} ../../example/reference/functions/language_model_knn/usage_setup_data_for_k_option.md |
| 169 | load --table Memos |
| 170 | [ |
| 171 | {"content": "I am a boy."}, |
| 172 | {"content": "This is an apple."}, |
| 173 | {"content": "Groonga is a full text search engine."}, |
| 174 | {"content": "This is an orange."}, |
| 175 | {"content": "This is a banana."}, |
| 176 | {"content": "This is a tomato."}, |
| 177 | {"content": "This is a carrot."}, |
| 178 | {"content": "This is a cucumber."}, |
| 179 | {"content": "This is a pepper."}, |
| 180 | {"content": "This is a potato."}, |
| 181 | {"content": "This is an onion."} |
| 182 | ] |
| 183 | ``` |
| 184 | |
| 185 | <!-- groonga-command --> |
| 186 | |
| 187 | ```{include} ../../example/reference/functions/language_model_knn/usage_k_option.md |
| 188 | select Memos \ |
| 189 | --filter 'language_model_knn(text, "male child", { "k" : -1 })' \ |
| 190 | --output_columns content |
| 191 | ``` |
| 192 | |
| 193 | ## Return value |
| 194 | |
| 195 | This function works as a selector. It means that this function executes effectively. |
| 196 | |
| 197 | ## See also |
| 198 | |
| 199 | - {doc}`../tokenizers/token_language_model_knn` |