put-inference.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. [role="xpack"]
  2. [[put-inference-api]]
  3. === Create {infer} API
  4. experimental[]
  5. Creates a model to perform an {infer} task.
  6. IMPORTANT: The {infer} APIs enable you to use certain services, such as built-in
  7. {ml} models (ELSER, E5), models uploaded through Eland, Cohere, OpenAI, or
  8. Hugging Face. For built-in models and models uploaded though
  9. Eland, the {infer} APIs offer an alternative way to use and manage trained
  10. models. However, if you do not plan to use the {infer} APIs to use these models
  11. or if you want to use non-NLP models, use the <<ml-df-trained-models-apis>>.
  12. [discrete]
  13. [[put-inference-api-request]]
  14. ==== {api-request-title}
  15. `PUT /_inference/<task_type>/<model_id>`
  16. [discrete]
  17. [[put-inference-api-prereqs]]
  18. ==== {api-prereq-title}
  19. * Requires the `manage_inference` <<privileges-list-cluster,cluster privilege>>
  20. (the built-in `inference_admin` role grants this privilege)
  21. [discrete]
  22. [[put-inference-api-desc]]
  23. ==== {api-description-title}
  24. The create {infer} API enables you to create and configure a {ml} model to
  25. perform a specific {infer} task.
  26. The following services are available through the {infer} API:
  27. * Cohere
  28. * ELSER
  29. * Hugging Face
  30. * OpenAI
  31. * Elasticsearch (for built-in models and models uploaded through Eland)
  32. [discrete]
  33. [[put-inference-api-path-params]]
  34. ==== {api-path-parms-title}
  35. `<inference_id>`::
  36. (Required, string)
  37. The unique identifier of the {infer} endpoint.
  38. `<task_type>`::
  39. (Required, string)
  40. The type of the {infer} task that the model will perform. Available task types:
  41. * `sparse_embedding`,
  42. * `text_embedding`.
  43. [discrete]
  44. [[put-inference-api-request-body]]
  45. == {api-request-body-title}
  46. `service`::
  47. (Required, string)
  48. The type of service supported for the specified task type.
  49. Available services:
  50. * `cohere`: specify the `text_embedding` task type to use the Cohere service.
  51. * `elser`: specify the `sparse_embedding` task type to use the ELSER service.
  52. * `hugging_face`: specify the `text_embedding` task type to use the Hugging Face
  53. service.
  54. * `openai`: specify the `text_embedding` task type to use the OpenAI service.
  55. * `elasticsearch`: specify the `text_embedding` task type to use the E5
  56. built-in model or text embedding models uploaded by Eland.
  57. `service_settings`::
  58. (Required, object)
  59. Settings used to install the {infer} model. These settings are specific to the
  60. `service` you specified.
  61. +
  62. .`service_settings` for the `cohere` service
  63. [%collapsible%closed]
  64. =====
  65. `api_key`:::
  66. (Required, string)
  67. A valid API key of your Cohere account. You can find your Cohere API keys or you
  68. can create a new one
  69. https://dashboard.cohere.com/api-keys[on the API keys settings page].
  70. IMPORTANT: You need to provide the API key only once, during the {infer} model
  71. creation. The <<get-inference-api>> does not retrieve your API key. After
  72. creating the {infer} model, you cannot change the associated API key. If you
  73. want to use a different API key, delete the {infer} model and recreate it with
  74. the same name and the updated API key.
  75. `embedding_type`::
  76. (Optional, string)
  77. Specifies the types of embeddings you want to get back. Defaults to `float`.
  78. Valid values are:
  79. * `byte`: use it for signed int8 embeddings (this is a synonym of `int8`).
  80. * `float`: use it for the default float embeddings.
  81. * `int8`: use it for signed int8 embeddings.
  82. `model_id`::
  83. (Optional, string)
  84. The name of the model to use for the {infer} task. To review the available
  85. models, refer to the
  86. https://docs.cohere.com/reference/embed[Cohere docs]. Defaults to
  87. `embed-english-v2.0`.
  88. =====
  89. +
  90. .`service_settings` for the `elser` service
  91. [%collapsible%closed]
  92. =====
  93. `num_allocations`:::
  94. (Required, integer)
  95. The number of model allocations to create. `num_allocations` must not exceed the
  96. number of available processors per node divided by the `num_threads`.
  97. `num_threads`:::
  98. (Required, integer)
  99. The number of threads to use by each model allocation. `num_threads` must not
  100. exceed the number of available processors per node divided by the number of
  101. allocations. Must be a power of 2. Max allowed value is 32.
  102. =====
  103. +
  104. .`service_settings` for the `hugging_face` service
  105. [%collapsible%closed]
  106. =====
  107. `api_key`:::
  108. (Required, string)
  109. A valid access token of your Hugging Face account. You can find your Hugging
  110. Face access tokens or you can create a new one
  111. https://huggingface.co/settings/tokens[on the settings page].
  112. IMPORTANT: You need to provide the API key only once, during the {infer} model
  113. creation. The <<get-inference-api>> does not retrieve your API key. After
  114. creating the {infer} model, you cannot change the associated API key. If you
  115. want to use a different API key, delete the {infer} model and recreate it with
  116. the same name and the updated API key.
  117. `url`:::
  118. (Required, string)
  119. The URL endpoint to use for the requests.
  120. =====
  121. +
  122. .`service_settings` for the `openai` service
  123. [%collapsible%closed]
  124. =====
  125. `api_key`:::
  126. (Required, string)
  127. A valid API key of your OpenAI account. You can find your OpenAI API keys in
  128. your OpenAI account under the
  129. https://platform.openai.com/api-keys[API keys section].
  130. IMPORTANT: You need to provide the API key only once, during the {infer} model
  131. creation. The <<get-inference-api>> does not retrieve your API key. After
  132. creating the {infer} model, you cannot change the associated API key. If you
  133. want to use a different API key, delete the {infer} model and recreate it with
  134. the same name and the updated API key.
  135. `model_id`:::
  136. (Optional, string)
  137. The name of the model to use for the {infer} task. Refer to the
  138. https://platform.openai.com/docs/guides/embeddings/what-are-embeddings[OpenAI documentation]
  139. for the list of available text embedding models.
  140. `organization_id`:::
  141. (Optional, string)
  142. The unique identifier of your organization. You can find the Organization ID in
  143. your OpenAI account under
  144. https://platform.openai.com/account/organization[**Settings** > **Organizations**].
  145. `url`:::
  146. (Optional, string)
  147. The URL endpoint to use for the requests. Can be changed for testing purposes.
  148. Defaults to `https://api.openai.com/v1/embeddings`.
  149. =====
  150. +
  151. .`service_settings` for the `elasticsearch` service
  152. [%collapsible%closed]
  153. =====
  154. `model_id`:::
  155. (Required, string)
  156. The name of the model to use for the {infer} task. It can be the
  157. ID of either a built-in model (for example, `.multilingual-e5-small` for E5) or
  158. a text embedding model already
  159. {ml-docs}/ml-nlp-import-model.html#ml-nlp-import-script[uploaded through Eland].
  160. `num_allocations`:::
  161. (Required, integer)
  162. The number of model allocations to create. `num_allocations` must not exceed the
  163. number of available processors per node divided by the `num_threads`.
  164. `num_threads`:::
  165. (Required, integer)
  166. The number of threads to use by each model allocation. `num_threads` must not
  167. exceed the number of available processors per node divided by the number of
  168. allocations. Must be a power of 2. Max allowed value is 32.
  169. =====
  170. `task_settings`::
  171. (Optional, object)
  172. Settings to configure the {infer} task. These settings are specific to the
  173. `<task_type>` you specified.
  174. +
  175. .`task_settings` for the `text_embedding` task type
  176. [%collapsible%closed]
  177. =====
  178. `input_type`:::
  179. (optional, string)
  180. For `cohere` service only. Specifies the type of input passed to the model.
  181. Valid values are:
  182. * `classification`: use it for embeddings passed through a text classifier.
  183. * `clusterning`: use it for the embeddings run through a clustering algorithm.
  184. * `ingest`: use it for storing document embeddings in a vector database.
  185. * `search`: use it for storing embeddings of search queries run against a
  186. vector data base to find relevant documents.
  187. `truncate`:::
  188. (Optional, string)
  189. For `cohere` service only. Specifies how the API handles inputs longer than the
  190. maximum token length. Defaults to `END`. Valid values are:
  191. * `NONE`: when the input exceeds the maximum input token length an error is
  192. returned.
  193. * `START`: when the input exceeds the maximum input token length the start of
  194. the input is discarded.
  195. * `END`: when the input exceeds the maximum input token length the end of
  196. the input is discarded.
  197. =====
  198. [discrete]
  199. [[put-inference-api-example]]
  200. ==== {api-examples-title}
  201. This section contains example API calls for every service type.
  202. [discrete]
  203. [[inference-example-cohere]]
  204. ===== Cohere service
  205. The following example shows how to create an {infer} endpoint called
  206. `cohere_embeddings` to perform a `text_embedding` task type.
  207. [source,console]
  208. ------------------------------------------------------------
  209. PUT _inference/text_embedding/cohere-embeddings
  210. {
  211. "service": "cohere",
  212. "service_settings": {
  213. "api_key": "<api_key>",
  214. "model_id": "embed-english-light-v3.0",
  215. "embedding_type": "byte"
  216. }
  217. }
  218. ------------------------------------------------------------
  219. // TEST[skip:TBD]
  220. [discrete]
  221. [[inference-example-e5]]
  222. ===== E5 via the elasticsearch service
  223. The following example shows how to create an {infer} endpoint called
  224. `my-e5-model` to perform a `text_embedding` task type.
  225. [source,console]
  226. ------------------------------------------------------------
  227. PUT _inference/text_embedding/my-e5-model
  228. {
  229. "service": "elasticsearch",
  230. "service_settings": {
  231. "num_allocations": 1,
  232. "num_threads": 1,
  233. "model_id": ".multilingual-e5-small" <1>
  234. }
  235. }
  236. ------------------------------------------------------------
  237. // TEST[skip:TBD]
  238. <1> The `model_id` must be the ID of one of the built-in E5 models. Valid values
  239. are `.multilingual-e5-small` and `.multilingual-e5-small_linux-x86_64`. For
  240. further details, refer to the {ml-docs}/ml-nlp-e5.html[E5 model documentation].
  241. [discrete]
  242. [[inference-example-elser]]
  243. ===== ELSER service
  244. The following example shows how to create an {infer} endpoint called
  245. `my-elser-model` to perform a `sparse_embedding` task type.
  246. [source,console]
  247. ------------------------------------------------------------
  248. PUT _inference/sparse_embedding/my-elser-model
  249. {
  250. "service": "elser",
  251. "service_settings": {
  252. "num_allocations": 1,
  253. "num_threads": 1
  254. }
  255. }
  256. ------------------------------------------------------------
  257. // TEST[skip:TBD]
  258. Example response:
  259. [source,console-result]
  260. ------------------------------------------------------------
  261. {
  262. "inference_id": "my-elser-model",
  263. "task_type": "sparse_embedding",
  264. "service": "elser",
  265. "service_settings": {
  266. "num_allocations": 1,
  267. "num_threads": 1
  268. },
  269. "task_settings": {}
  270. }
  271. ------------------------------------------------------------
  272. // NOTCONSOLE
  273. [discrete]
  274. [[inference-example-hugging-face]]
  275. ===== Hugging Face service
  276. The following example shows how to create an {infer} endpoint called
  277. `hugging-face_embeddings` to perform a `text_embedding` task type.
  278. [source,console]
  279. ------------------------------------------------------------
  280. PUT _inference/text_embedding/hugging-face-embeddings
  281. {
  282. "service": "hugging_face",
  283. "service_settings": {
  284. "api_key": "<access_token>", <1>
  285. "url": "<url_endpoint>" <2>
  286. }
  287. }
  288. ------------------------------------------------------------
  289. // TEST[skip:TBD]
  290. <1> A valid Hugging Face access token. You can find on the
  291. https://huggingface.co/settings/tokens[settings page of your account].
  292. <2> The {infer} endpoint URL you created on Hugging Face.
  293. Create a new {infer} endpoint on
  294. https://ui.endpoints.huggingface.co/[the Hugging Face endpoint page] to get an
  295. endpoint URL. Select the model you want to use on the new endpoint creation page
  296. - for example `intfloat/e5-small-v2` - then select the `Sentence Embeddings`
  297. task under the Advanced configuration section. Create the endpoint. Copy the URL
  298. after the endpoint initialization has been finished.
  299. [discrete]
  300. [[inference-example-eland]]
  301. ===== Models uploaded by Eland via the elasticsearch service
  302. The following example shows how to create an {infer} endpoint called
  303. `my-msmarco-minilm-model` to perform a `text_embedding` task type.
  304. [source,console]
  305. ------------------------------------------------------------
  306. PUT _inference/text_embedding/my-msmarco-minilm-model
  307. {
  308. "service": "elasticsearch",
  309. "service_settings": {
  310. "num_allocations": 1,
  311. "num_threads": 1,
  312. "model_id": "msmarco-MiniLM-L12-cos-v5" <1>
  313. }
  314. }
  315. ------------------------------------------------------------
  316. // TEST[skip:TBD]
  317. <1> The `model_id` must be the ID of a text embedding model which has already
  318. been
  319. {ml-docs}/ml-nlp-import-model.html#ml-nlp-import-script[uploaded through Eland].
  320. [discrete]
  321. [[inference-example-openai]]
  322. ===== OpenAI service
  323. The following example shows how to create an {infer} endpoint called
  324. `openai_embeddings` to perform a `text_embedding` task type.
  325. [source,console]
  326. ------------------------------------------------------------
  327. PUT _inference/text_embedding/openai_embeddings
  328. {
  329. "service": "openai",
  330. "service_settings": {
  331. "api_key": "<api_key>",
  332. "model_id": "text-embedding-ada-002"
  333. }
  334. }
  335. ------------------------------------------------------------
  336. // TEST[skip:TBD]