4
4
<titleabbrev>Semantic search with the {infer} API</titleabbrev>
5
5
++++
6
6
7
- The instructions in this tutorial shows you how to use the {infer} API with the
8
- Open AI service to perform semantic search on your data. The following example
9
- uses OpenAI's `text-embedding-ada-002` second generation embedding model. You
7
+ The instructions in this tutorial shows you how to use the {infer} API with the
8
+ Open AI service to perform semantic search on your data. The following example
9
+ uses OpenAI's `text-embedding-ada-002` second generation embedding model. You
10
10
can use any OpenAI models, they are all supported by the {infer} API.
11
11
12
12
13
13
[discrete]
14
14
[[infer-openai-requirements]]
15
15
==== Requirements
16
16
17
- An https://openai.com/[OpenAI account] is required to use the {infer} API with
18
- the OpenAI service.
17
+ An https://openai.com/[OpenAI account] is required to use the {infer} API with
18
+ the OpenAI service.
19
19
20
20
21
21
[discrete]
@@ -39,23 +39,23 @@ PUT _inference/text_embedding/openai_embeddings <1>
39
39
------------------------------------------------------------
40
40
// TEST[skip:TBD]
41
41
<1> The task type is `text_embedding` in the path.
42
- <2> The API key of your OpenAI account. You can find your OpenAI API keys in
43
- your OpenAI account under the
44
- https://platform.openai.com/api-keys[API keys section]. You need to provide
45
- your API key only once. The <<get-inference-api>> does not return your API
42
+ <2> The API key of your OpenAI account. You can find your OpenAI API keys in
43
+ your OpenAI account under the
44
+ https://platform.openai.com/api-keys[API keys section]. You need to provide
45
+ your API key only once. The <<get-inference-api>> does not return your API
46
46
key.
47
- <3> The name of the embedding model to use. You can find the list of OpenAI
48
- embedding models
47
+ <3> The name of the embedding model to use. You can find the list of OpenAI
48
+ embedding models
49
49
https://platform.openai.com/docs/guides/embeddings/embedding-models[here].
50
50
51
51
52
52
[discrete]
53
53
[[infer-openai-mappings]]
54
54
==== Create the index mapping
55
55
56
- The mapping of the destination index - the index that contains the embeddings
57
- that the model will create based on your input text - must be created. The
58
- destination index must have a field with the <<dense-vector, `dense_vector`>>
56
+ The mapping of the destination index - the index that contains the embeddings
57
+ that the model will create based on your input text - must be created. The
58
+ destination index must have a field with the <<dense-vector, `dense_vector`>>
59
59
field type to index the output of the OpenAI model.
60
60
61
61
[source,console]
@@ -67,7 +67,7 @@ PUT openai-embeddings
67
67
"content_embedding": { <1>
68
68
"type": "dense_vector", <2>
69
69
"dims": 1536, <3>
70
- "element_type": "byte ",
70
+ "element_type": "float ",
71
71
"similarity": "dot_product" <4>
72
72
},
73
73
"content": { <5>
@@ -80,15 +80,15 @@ PUT openai-embeddings
80
80
<1> The name of the field to contain the generated tokens. It must be refrenced
81
81
in the {infer} pipeline configuration in the next step.
82
82
<2> The field to contain the tokens is a `dense_vector` field.
83
- <3> The output dimensions of the model. Find this value in the
84
- https://platform.openai.com/docs/guides/embeddings/embedding-models[OpenAI documentation]
83
+ <3> The output dimensions of the model. Find this value in the
84
+ https://platform.openai.com/docs/guides/embeddings/embedding-models[OpenAI documentation]
85
85
of the model you use.
86
- <4> The faster` dot_product` function can be used to calculate similarity
87
- because OpenAI embeddings are normalised to unit length. You can check the
86
+ <4> The faster` dot_product` function can be used to calculate similarity
87
+ because OpenAI embeddings are normalised to unit length. You can check the
88
88
https://platform.openai.com/docs/guides/embeddings/which-distance-function-should-i-use[OpenAI docs]
89
- about which similarity function to use.
89
+ about which similarity function to use.
90
90
<5> The name of the field from which to create the sparse vector representation.
91
- In this example, the name of the field is `content`. It must be referenced in
91
+ In this example, the name of the field is `content`. It must be referenced in
92
92
the {infer} pipeline configuration in the next step.
93
93
<6> The field type which is text in this example.
94
94
@@ -98,8 +98,8 @@ the {infer} pipeline configuration in the next step.
98
98
==== Create an ingest pipeline with an inference processor
99
99
100
100
Create an <<ingest,ingest pipeline>> with an
101
- <<inference-processor,{infer} processor>> and use the OpenAI model you created
102
- above to infer against the data that is being ingested in the
101
+ <<inference-processor,{infer} processor>> and use the OpenAI model you created
102
+ above to infer against the data that is being ingested in the
103
103
pipeline.
104
104
105
105
[source,console]
@@ -119,8 +119,8 @@ PUT _ingest/pipeline/openai_embeddings
119
119
]
120
120
}
121
121
--------------------------------------------------
122
- <1> The name of the inference model you created by using the
123
- <<put-inference-api>>.
122
+ <1> The name of the inference model you created by using the
123
+ <<put-inference-api>>.
124
124
<2> Configuration object that defines the `input_field` for the {infer} process
125
125
and the `output_field` that will contain the {infer} results.
126
126
@@ -179,9 +179,9 @@ POST _reindex?wait_for_completion=false
179
179
number makes the update of the reindexing process quicker which enables you to
180
180
follow the progress closely and detect errors early.
181
181
182
- NOTE: The
183
- https://platform.openai.com/account/limits[rate limit of your OpenAI account]
184
- may affect the throughput of the reindexing process. If this happens, change
182
+ NOTE: The
183
+ https://platform.openai.com/account/limits[rate limit of your OpenAI account]
184
+ may affect the throughput of the reindexing process. If this happens, change
185
185
`size` to `3` or a similar value in magnitude.
186
186
187
187
The call returns a task ID to monitor the progress:
@@ -192,7 +192,7 @@ GET _tasks/<task_id>
192
192
----
193
193
// TEST[skip:TBD]
194
194
195
- You can also cancel the reindexing process if you don't want to wait until the
195
+ You can also cancel the reindexing process if you don't want to wait until the
196
196
reindexing process is fully complete which might take hours:
197
197
198
198
[source,console]
@@ -206,12 +206,12 @@ POST _tasks/<task_id>/_cancel
206
206
[[infer-semantic-search]]
207
207
==== Semantic search
208
208
209
- After the dataset has been enriched with the embeddings, you can query the data
209
+ After the dataset has been enriched with the embeddings, you can query the data
210
210
using {ref}/knn-search.html#knn-semantic-search[semantic search]. Pass a
211
211
`query_vector_builder` to the k-nearest neighbor (kNN) vector search API, and
212
212
provide the query text and the model you have used to create the embeddings.
213
213
214
- NOTE: If you cancelled the reindexing process, you run the query only a part of
214
+ NOTE: If you cancelled the reindexing process, you run the query only a part of
215
215
the data which affects the quality of your results.
216
216
217
217
[source,console]
@@ -237,7 +237,7 @@ GET openai-embeddings/_search
237
237
--------------------------------------------------
238
238
// TEST[skip:TBD]
239
239
240
- As a result, you receive the top 10 documents that are closest in meaning to the
240
+ As a result, you receive the top 10 documents that are closest in meaning to the
241
241
query from the `openai-embeddings` index sorted by their proximity to the query:
242
242
243
243
[source,consol-result]
0 commit comments