put-inference.asciidoc 14 KB

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