1
0

infer-trained-model.asciidoc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. [role="xpack"]
  2. [[infer-trained-model]]
  3. = Infer trained model API
  4. [subs="attributes"]
  5. ++++
  6. <titleabbrev>Infer trained model</titleabbrev>
  7. ++++
  8. Evaluates a trained model. The model may be any supervised model either trained by {dfanalytics} or imported.
  9. preview::[]
  10. [[infer-trained-model-request]]
  11. == {api-request-title}
  12. `POST _ml/trained_models/<model_id>/_infer`
  13. ////
  14. [[infer-trained-model-prereq]]
  15. == {api-prereq-title}
  16. ////
  17. ////
  18. [[infer-trained-model-desc]]
  19. == {api-description-title}
  20. ////
  21. [[infer-trained-model-path-params]]
  22. == {api-path-parms-title}
  23. `<model_id>`::
  24. (Required, string)
  25. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=model-id]
  26. [[infer-trained-model-query-params]]
  27. == {api-query-parms-title}
  28. `timeout`::
  29. (Optional, time)
  30. Controls the amount of time to wait for {infer} results. Defaults to 10 seconds.
  31. [[infer-trained-model-request-body]]
  32. == {api-request-body-title}
  33. `docs`::
  34. (Required, array)
  35. An array of objects to pass to the model for inference. The objects should
  36. contain the fields matching your configured trained model input. Typically for
  37. NLP models, the field name is `text_field`. Currently for NLP models, only a
  38. single value is allowed. For {dfanalytics} or imported classification or
  39. regression models, more than one value is allowed.
  40. //Begin inference_config
  41. `inference_config`::
  42. (Required, object)
  43. The default configuration for inference. This can be: `regression`,
  44. `classification`, `fill_mask`, `ner`, `question_answering`,
  45. `text_classification`, `text_embedding` or `zero_shot_classification`.
  46. If `regression` or `classification`, it must match the `target_type` of the
  47. underlying `definition.trained_model`. If `fill_mask`, `ner`,
  48. `question_answering`, `text_classification`, or `text_embedding`; the
  49. `model_type` must be `pytorch`.
  50. +
  51. .Properties of `inference_config`
  52. [%collapsible%open]
  53. ====
  54. `classification`:::
  55. (Optional, object)
  56. Classification configuration for inference.
  57. +
  58. .Properties of classification inference
  59. [%collapsible%open]
  60. =====
  61. `num_top_classes`::::
  62. (Optional, integer)
  63. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-classification-num-top-classes]
  64. `num_top_feature_importance_values`::::
  65. (Optional, integer)
  66. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-classification-num-top-feature-importance-values]
  67. `prediction_field_type`::::
  68. (Optional, string)
  69. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-classification-prediction-field-type]
  70. `results_field`::::
  71. (Optional, string)
  72. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-results-field]
  73. `top_classes_results_field`::::
  74. (Optional, string)
  75. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-classification-top-classes-results-field]
  76. =====
  77. `fill_mask`:::
  78. (Optional, object)
  79. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-fill-mask]
  80. +
  81. .Properties of fill_mask inference
  82. [%collapsible%open]
  83. =====
  84. `num_top_classes`::::
  85. (Optional, integer)
  86. Number of top predicted tokens to return for replacing the mask token. Defaults to `0`.
  87. `results_field`::::
  88. (Optional, string)
  89. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-results-field]
  90. `tokenization`::::
  91. (Optional, object)
  92. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization]
  93. +
  94. .Properties of tokenization
  95. [%collapsible%open]
  96. ======
  97. `bert`::::
  98. (Optional, object)
  99. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-bert]
  100. +
  101. .Properties of bert
  102. [%collapsible%open]
  103. =======
  104. `truncate`::::
  105. (Optional, string)
  106. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-truncate]
  107. =======
  108. `roberta`::::
  109. (Optional, object)
  110. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-roberta]
  111. +
  112. .Properties of roberta
  113. [%collapsible%open]
  114. =======
  115. `truncate`::::
  116. (Optional, string)
  117. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-truncate]
  118. =======
  119. `mpnet`::::
  120. (Optional, object)
  121. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-mpnet]
  122. +
  123. .Properties of mpnet
  124. [%collapsible%open]
  125. =======
  126. `truncate`::::
  127. (Optional, string)
  128. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-truncate]
  129. =======
  130. ======
  131. =====
  132. `ner`:::
  133. (Optional, object)
  134. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-ner]
  135. +
  136. .Properties of ner inference
  137. [%collapsible%open]
  138. =====
  139. `results_field`::::
  140. (Optional, string)
  141. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-results-field]
  142. `tokenization`::::
  143. (Optional, object)
  144. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization]
  145. +
  146. .Properties of tokenization
  147. [%collapsible%open]
  148. ======
  149. `bert`::::
  150. (Optional, object)
  151. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-bert]
  152. +
  153. .Properties of bert
  154. [%collapsible%open]
  155. =======
  156. `truncate`::::
  157. (Optional, string)
  158. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-truncate]
  159. =======
  160. `roberta`::::
  161. (Optional, object)
  162. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-roberta]
  163. +
  164. .Properties of roberta
  165. [%collapsible%open]
  166. =======
  167. `truncate`::::
  168. (Optional, string)
  169. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-truncate]
  170. =======
  171. `mpnet`::::
  172. (Optional, object)
  173. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-mpnet]
  174. +
  175. .Properties of mpnet
  176. [%collapsible%open]
  177. =======
  178. `truncate`::::
  179. (Optional, string)
  180. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-truncate]
  181. =======
  182. ======
  183. =====
  184. `pass_through`:::
  185. (Optional, object)
  186. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-pass-through]
  187. +
  188. .Properties of pass_through inference
  189. [%collapsible%open]
  190. =====
  191. `results_field`::::
  192. (Optional, string)
  193. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-results-field]
  194. `tokenization`::::
  195. (Optional, object)
  196. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization]
  197. +
  198. .Properties of tokenization
  199. [%collapsible%open]
  200. ======
  201. `bert`::::
  202. (Optional, object)
  203. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-bert]
  204. +
  205. .Properties of bert
  206. [%collapsible%open]
  207. =======
  208. `truncate`::::
  209. (Optional, string)
  210. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-truncate]
  211. =======
  212. `roberta`::::
  213. (Optional, object)
  214. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-roberta]
  215. +
  216. .Properties of roberta
  217. [%collapsible%open]
  218. =======
  219. `truncate`::::
  220. (Optional, string)
  221. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-truncate]
  222. =======
  223. `mpnet`::::
  224. (Optional, object)
  225. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-mpnet]
  226. +
  227. .Properties of mpnet
  228. [%collapsible%open]
  229. =======
  230. `truncate`::::
  231. (Optional, string)
  232. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-truncate]
  233. =======
  234. ======
  235. =====
  236. `question_answering`:::
  237. (Optional, object)
  238. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-question-answering]
  239. +
  240. .Properties of question_answering inference
  241. [%collapsible%open]
  242. =====
  243. `max_answer_length`::::
  244. (Optional, integer)
  245. The maximum amount of words in the answer. Defaults to `15`.
  246. `num_top_classes`::::
  247. (Optional, integer)
  248. The number the top found answers to return. Defaults to `0`, meaning only the best found answer is returned.
  249. `question`::::
  250. (Required, string)
  251. The question to use when extracting an answer
  252. `results_field`::::
  253. (Optional, string)
  254. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-results-field]
  255. `tokenization`::::
  256. (Optional, object)
  257. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization]
  258. +
  259. Recommended to set `max_sequence_length` to `386` with `128` of `span` and set
  260. `truncate` to `none`.
  261. +
  262. .Properties of tokenization
  263. [%collapsible%open]
  264. ======
  265. `bert`::::
  266. (Optional, object)
  267. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-bert]
  268. +
  269. .Properties of bert
  270. [%collapsible%open]
  271. =======
  272. `span`::::
  273. (Optional, integer)
  274. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-span]
  275. `truncate`::::
  276. (Optional, string)
  277. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-truncate]
  278. =======
  279. `roberta`::::
  280. (Optional, object)
  281. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-roberta]
  282. +
  283. .Properties of roberta
  284. [%collapsible%open]
  285. =======
  286. `span`::::
  287. (Optional, integer)
  288. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-span]
  289. `truncate`::::
  290. (Optional, string)
  291. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-truncate]
  292. =======
  293. `mpnet`::::
  294. (Optional, object)
  295. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-mpnet]
  296. +
  297. .Properties of mpnet
  298. [%collapsible%open]
  299. =======
  300. `span`::::
  301. (Optional, integer)
  302. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-span]
  303. `truncate`::::
  304. (Optional, string)
  305. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-truncate]
  306. =======
  307. ======
  308. =====
  309. `regression`:::
  310. (Optional, object)
  311. Regression configuration for inference.
  312. +
  313. .Properties of regression inference
  314. [%collapsible%open]
  315. =====
  316. `num_top_feature_importance_values`::::
  317. (Optional, integer)
  318. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-regression-num-top-feature-importance-values]
  319. `results_field`::::
  320. (Optional, string)
  321. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-results-field]
  322. =====
  323. `text_classification`:::
  324. (Optional, object)
  325. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-text-classification]
  326. +
  327. .Properties of text_classification inference
  328. [%collapsible%open]
  329. =====
  330. `classification_labels`::::
  331. (Optional, string) An array of classification labels.
  332. `num_top_classes`::::
  333. (Optional, integer)
  334. Specifies the number of top class predictions to return. Defaults to all classes (-1).
  335. `results_field`::::
  336. (Optional, string)
  337. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-results-field]
  338. `tokenization`::::
  339. (Optional, object)
  340. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization]
  341. +
  342. .Properties of tokenization
  343. [%collapsible%open]
  344. ======
  345. `bert`::::
  346. (Optional, object)
  347. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-bert]
  348. +
  349. .Properties of bert
  350. [%collapsible%open]
  351. =======
  352. `span`::::
  353. (Optional, integer)
  354. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-span]
  355. `truncate`::::
  356. (Optional, string)
  357. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-truncate]
  358. =======
  359. `roberta`::::
  360. (Optional, object)
  361. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-roberta]
  362. +
  363. .Properties of roberta
  364. [%collapsible%open]
  365. =======
  366. `span`::::
  367. (Optional, integer)
  368. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-span]
  369. `truncate`::::
  370. (Optional, string)
  371. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-truncate]
  372. =======
  373. `mpnet`::::
  374. (Optional, object)
  375. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-mpnet]
  376. +
  377. .Properties of mpnet
  378. [%collapsible%open]
  379. =======
  380. `span`::::
  381. (Optional, integer)
  382. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-span]
  383. `truncate`::::
  384. (Optional, string)
  385. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-truncate]
  386. =======
  387. ======
  388. =====
  389. `text_embedding`:::
  390. (Object, optional)
  391. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-text-embedding]
  392. +
  393. .Properties of text_embedding inference
  394. [%collapsible%open]
  395. =====
  396. `results_field`::::
  397. (Optional, string)
  398. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-results-field]
  399. `tokenization`::::
  400. (Optional, object)
  401. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization]
  402. +
  403. .Properties of tokenization
  404. [%collapsible%open]
  405. ======
  406. `bert`::::
  407. (Optional, object)
  408. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-bert]
  409. +
  410. .Properties of bert
  411. [%collapsible%open]
  412. =======
  413. `truncate`::::
  414. (Optional, string)
  415. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-truncate]
  416. =======
  417. `roberta`::::
  418. (Optional, object)
  419. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-roberta]
  420. +
  421. .Properties of roberta
  422. [%collapsible%open]
  423. =======
  424. `truncate`::::
  425. (Optional, string)
  426. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-truncate]
  427. =======
  428. `mpnet`::::
  429. (Optional, object)
  430. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-mpnet]
  431. +
  432. .Properties of mpnet
  433. [%collapsible%open]
  434. =======
  435. `truncate`::::
  436. (Optional, string)
  437. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-truncate]
  438. =======
  439. ======
  440. =====
  441. `zero_shot_classification`:::
  442. (Object, optional)
  443. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-zero-shot-classification]
  444. +
  445. .Properties of zero_shot_classification inference
  446. [%collapsible%open]
  447. =====
  448. `labels`::::
  449. (Optional, array)
  450. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-zero-shot-classification-labels]
  451. `multi_label`::::
  452. (Optional, boolean)
  453. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-zero-shot-classification-multi-label]
  454. `results_field`::::
  455. (Optional, string)
  456. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-results-field]
  457. `tokenization`::::
  458. (Optional, object)
  459. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization]
  460. +
  461. .Properties of tokenization
  462. [%collapsible%open]
  463. ======
  464. `bert`::::
  465. (Optional, object)
  466. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-bert]
  467. +
  468. .Properties of bert
  469. [%collapsible%open]
  470. =======
  471. `truncate`::::
  472. (Optional, string)
  473. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-truncate]
  474. =======
  475. `roberta`::::
  476. (Optional, object)
  477. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-roberta]
  478. +
  479. .Properties of roberta
  480. [%collapsible%open]
  481. =======
  482. `truncate`::::
  483. (Optional, string)
  484. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-truncate]
  485. =======
  486. `mpnet`::::
  487. (Optional, object)
  488. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-mpnet]
  489. +
  490. .Properties of mpnet
  491. [%collapsible%open]
  492. =======
  493. `truncate`::::
  494. (Optional, string)
  495. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-nlp-tokenization-truncate]
  496. =======
  497. ======
  498. =====
  499. ====
  500. //End of inference_config
  501. ////
  502. [[infer-trained-model-results]]
  503. == {api-response-body-title}
  504. ////
  505. ////
  506. [[ml-get-trained-models-response-codes]]
  507. == {api-response-codes-title}
  508. ////
  509. [[infer-trained-model-example]]
  510. == {api-examples-title}
  511. The response depends on the kind of model.
  512. For example, for language identification the response is the predicted language and the score:
  513. [source,console]
  514. --------------------------------------------------
  515. POST _ml/trained_models/lang_ident_model_1/_infer
  516. {
  517. "docs":[{"text": "The fool doth think he is wise, but the wise man knows himself to be a fool."}]
  518. }
  519. --------------------------------------------------
  520. // TEST[skip:TBD]
  521. Here are the results predicting english with a high probability.
  522. [source,console-result]
  523. ----
  524. {
  525. "inference_results": [
  526. {
  527. "predicted_value": "en",
  528. "prediction_probability": 0.9999658805366392,
  529. "prediction_score": 0.9999658805366392
  530. }
  531. ]
  532. }
  533. ----
  534. // NOTCONSOLE
  535. When it is a text classification model, the response is the score and predicted classification.
  536. For example:
  537. [source,console]
  538. --------------------------------------------------
  539. POST _ml/trained_models/model2/_infer
  540. {
  541. "docs": [{"text_field": "The movie was awesome!!"}]
  542. }
  543. --------------------------------------------------
  544. // TEST[skip:TBD]
  545. The API returns the predicted label and the confidence.
  546. [source,console-result]
  547. ----
  548. {
  549. "inference_results": [{
  550. "predicted_value" : "POSITIVE",
  551. "prediction_probability" : 0.9998667964092964
  552. }]
  553. }
  554. ----
  555. // NOTCONSOLE
  556. For named entity recognition (NER) models, the response contains the annotated
  557. text output and the recognized entities.
  558. [source,console]
  559. --------------------------------------------------
  560. POST _ml/trained_models/model2/_infer
  561. {
  562. "docs": [{"text_field": "Hi my name is Josh and I live in Berlin"}]
  563. }
  564. --------------------------------------------------
  565. // TEST[skip:TBD]
  566. The API returns in this case:
  567. [source,console-result]
  568. ----
  569. {
  570. "inference_results": [{
  571. "predicted_value" : "Hi my name is [Josh](PER&Josh) and I live in [Berlin](LOC&Berlin)",
  572. "entities" : [
  573. {
  574. "entity" : "Josh",
  575. "class_name" : "PER",
  576. "class_probability" : 0.9977303419824,
  577. "start_pos" : 14,
  578. "end_pos" : 18
  579. },
  580. {
  581. "entity" : "Berlin",
  582. "class_name" : "LOC",
  583. "class_probability" : 0.9992474323902818,
  584. "start_pos" : 33,
  585. "end_pos" : 39
  586. }
  587. ]
  588. }]
  589. }
  590. ----
  591. // NOTCONSOLE
  592. Zero-shot classification models require extra configuration defining the class
  593. labels. These labels are passed in the zero-shot inference config.
  594. [source,console]
  595. --------------------------------------------------
  596. POST _ml/trained_models/model2/_infer
  597. {
  598. "docs": [
  599. {
  600. "text_field": "This is a very happy person"
  601. }
  602. ],
  603. "inference_config": {
  604. "zero_shot_classification": {
  605. "labels": [
  606. "glad",
  607. "sad",
  608. "bad",
  609. "rad"
  610. ],
  611. "multi_label": false
  612. }
  613. }
  614. }
  615. --------------------------------------------------
  616. // TEST[skip:TBD]
  617. The API returns the predicted label and the confidence, as well as the top
  618. classes:
  619. [source,console-result]
  620. ----
  621. {
  622. "inference_results": [{
  623. "predicted_value" : "glad",
  624. "top_classes" : [
  625. {
  626. "class_name" : "glad",
  627. "class_probability" : 0.8061155063386439,
  628. "class_score" : 0.8061155063386439
  629. },
  630. {
  631. "class_name" : "rad",
  632. "class_probability" : 0.18218006158387956,
  633. "class_score" : 0.18218006158387956
  634. },
  635. {
  636. "class_name" : "bad",
  637. "class_probability" : 0.006325615787634201,
  638. "class_score" : 0.006325615787634201
  639. },
  640. {
  641. "class_name" : "sad",
  642. "class_probability" : 0.0053788162898424545,
  643. "class_score" : 0.0053788162898424545
  644. }
  645. ],
  646. "prediction_probability" : 0.8061155063386439
  647. }]
  648. }
  649. ----
  650. // NOTCONSOLE
  651. Question answering models require extra configuration defining the question to
  652. answer.
  653. [source,console]
  654. --------------------------------------------------
  655. POST _ml/trained_models/model2/_infer
  656. {
  657. "docs": [
  658. {
  659. "text_field": "<long text to extract answer>"
  660. }
  661. ],
  662. "inference_config": {
  663. "question_answering": {
  664. "question": "<question to be answered>"
  665. }
  666. }
  667. }
  668. --------------------------------------------------
  669. // TEST[skip:TBD]
  670. The API returns a response similar to the following:
  671. [source,console-result]
  672. ----
  673. {
  674. "predicted_value": <string subsection of the text that is the answer>,
  675. "start_offset": <character offset in document to start>,
  676. "end_offset": <character offset end of the answer,
  677. "prediction_probability": <prediction score>
  678. }
  679. ----
  680. // NOTCONSOLE
  681. The tokenization truncate option can be overridden when calling the API:
  682. [source,console]
  683. --------------------------------------------------
  684. POST _ml/trained_models/model2/_infer
  685. {
  686. "docs": [{"text_field": "The Amazon rainforest covers most of the Amazon basin in South America"}],
  687. "inference_config": {
  688. "ner": {
  689. "tokenization": {
  690. "bert": {
  691. "truncate": "first"
  692. }
  693. }
  694. }
  695. }
  696. }
  697. --------------------------------------------------
  698. // TEST[skip:TBD]
  699. When the input has been truncated due to the limit imposed by the model's `max_sequence_length`
  700. the `is_truncated` field appears in the response.
  701. [source,console-result]
  702. ----
  703. {
  704. "inference_results": [{
  705. "predicted_value" : "The [Amazon](LOC&Amazon) rainforest covers most of the [Amazon](LOC&Amazon) basin in [South America](LOC&South+America)",
  706. "entities" : [
  707. {
  708. "entity" : "Amazon",
  709. "class_name" : "LOC",
  710. "class_probability" : 0.9505460915724254,
  711. "start_pos" : 4,
  712. "end_pos" : 10
  713. },
  714. {
  715. "entity" : "Amazon",
  716. "class_name" : "LOC",
  717. "class_probability" : 0.9969992804311777,
  718. "start_pos" : 41,
  719. "end_pos" : 47
  720. }
  721. ],
  722. "is_truncated" : true
  723. }]
  724. }
  725. ----
  726. // NOTCONSOLE