infer-trained-model-deployment.asciidoc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. [role="xpack"]
  2. [[infer-trained-model-deployment]]
  3. = Infer trained model deployment API
  4. [subs="attributes"]
  5. ++++
  6. <titleabbrev>Infer trained model deployment</titleabbrev>
  7. ++++
  8. Evaluates a trained model.
  9. deprecated::[8.3.0,Replaced by <<infer-trained-model>>.]
  10. [[infer-trained-model-deployment-request]]
  11. == {api-request-title}
  12. `POST _ml/trained_models/<model_id>/deployment/_infer`
  13. ////
  14. [[infer-trained-model-deployment-prereq]]
  15. == {api-prereq-title}
  16. ////
  17. ////
  18. [[infer-trained-model-deployment-desc]]
  19. == {api-description-title}
  20. ////
  21. [[infer-trained-model-deployment-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-deployment-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-deployment-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 a field matching your configured trained model input. Typically, the field
  37. name is `text_field`. Currently, only a single value is allowed.
  38. ////
  39. [[infer-trained-model-deployment-results]]
  40. == {api-response-body-title}
  41. ////
  42. ////
  43. [[ml-get-trained-models-response-codes]]
  44. == {api-response-codes-title}
  45. ////
  46. [[infer-trained-model-deployment-example]]
  47. == {api-examples-title}
  48. The response depends on the task the model is trained for. If it is a
  49. text classification task, the response is the score. For example:
  50. [source,console]
  51. --------------------------------------------------
  52. POST _ml/trained_models/model2/deployment/_infer
  53. {
  54. "docs": [{"text_field": "The movie was awesome!!"}]
  55. }
  56. --------------------------------------------------
  57. // TEST[skip:TBD]
  58. The API returns the predicted label and the confidence.
  59. [source,console-result]
  60. ----
  61. {
  62. "predicted_value" : "POSITIVE",
  63. "prediction_probability" : 0.9998667964092964
  64. }
  65. ----
  66. // NOTCONSOLE
  67. For named entity recognition (NER) tasks, the response contains the annotated
  68. text output and the recognized entities.
  69. [source,console]
  70. --------------------------------------------------
  71. POST _ml/trained_models/model2/deployment/_infer
  72. {
  73. "docs": [{"text_field": "Hi my name is Josh and I live in Berlin"}]
  74. }
  75. --------------------------------------------------
  76. // TEST[skip:TBD]
  77. The API returns in this case:
  78. [source,console-result]
  79. ----
  80. {
  81. "predicted_value" : "Hi my name is [Josh](PER&Josh) and I live in [Berlin](LOC&Berlin)",
  82. "entities" : [
  83. {
  84. "entity" : "Josh",
  85. "class_name" : "PER",
  86. "class_probability" : 0.9977303419824,
  87. "start_pos" : 14,
  88. "end_pos" : 18
  89. },
  90. {
  91. "entity" : "Berlin",
  92. "class_name" : "LOC",
  93. "class_probability" : 0.9992474323902818,
  94. "start_pos" : 33,
  95. "end_pos" : 39
  96. }
  97. ]
  98. }
  99. ----
  100. // NOTCONSOLE
  101. Zero-shot classification tasks require extra configuration defining the class labels.
  102. These labels are passed in the zero-shot inference config.
  103. [source,console]
  104. --------------------------------------------------
  105. POST _ml/trained_models/model2/deployment/_infer
  106. {
  107. "docs": [
  108. {
  109. "text_field": "This is a very happy person"
  110. }
  111. ],
  112. "inference_config": {
  113. "zero_shot_classification": {
  114. "labels": [
  115. "glad",
  116. "sad",
  117. "bad",
  118. "rad"
  119. ],
  120. "multi_label": false
  121. }
  122. }
  123. }
  124. --------------------------------------------------
  125. // TEST[skip:TBD]
  126. The API returns the predicted label and the confidence, as well as the top classes:
  127. [source,console-result]
  128. ----
  129. {
  130. "predicted_value" : "glad",
  131. "top_classes" : [
  132. {
  133. "class_name" : "glad",
  134. "class_probability" : 0.8061155063386439,
  135. "class_score" : 0.8061155063386439
  136. },
  137. {
  138. "class_name" : "rad",
  139. "class_probability" : 0.18218006158387956,
  140. "class_score" : 0.18218006158387956
  141. },
  142. {
  143. "class_name" : "bad",
  144. "class_probability" : 0.006325615787634201,
  145. "class_score" : 0.006325615787634201
  146. },
  147. {
  148. "class_name" : "sad",
  149. "class_probability" : 0.0053788162898424545,
  150. "class_score" : 0.0053788162898424545
  151. }
  152. ],
  153. "prediction_probability" : 0.8061155063386439
  154. }
  155. ----
  156. // NOTCONSOLE
  157. The tokenization truncate option can be overridden when calling the API:
  158. [source,console]
  159. --------------------------------------------------
  160. POST _ml/trained_models/model2/deployment/_infer
  161. {
  162. "docs": [{"text_field": "The Amazon rainforest covers most of the Amazon basin in South America"}],
  163. "inference_config": {
  164. "ner": {
  165. "tokenization": {
  166. "bert": {
  167. "truncate": "first"
  168. }
  169. }
  170. }
  171. }
  172. }
  173. --------------------------------------------------
  174. // TEST[skip:TBD]
  175. When the input has been truncated due to the limit imposed by the model's `max_sequence_length`
  176. the `is_truncated` field appears in the response.
  177. [source,console-result]
  178. ----
  179. {
  180. "predicted_value" : "The [Amazon](LOC&Amazon) rainforest covers most of the [Amazon](LOC&Amazon) basin in [South America](LOC&South+America)",
  181. "entities" : [
  182. {
  183. "entity" : "Amazon",
  184. "class_name" : "LOC",
  185. "class_probability" : 0.9505460915724254,
  186. "start_pos" : 4,
  187. "end_pos" : 10
  188. },
  189. {
  190. "entity" : "Amazon",
  191. "class_name" : "LOC",
  192. "class_probability" : 0.9969992804311777,
  193. "start_pos" : 41,
  194. "end_pos" : 47
  195. }
  196. ],
  197. "is_truncated" : true
  198. }
  199. ----
  200. // NOTCONSOLE