282
282
in (
283
283
"text/csv" ,
284
284
"application/jsonlines" ,
285
+ "application/json" ,
285
286
"image/jpeg" ,
286
287
"image/png" ,
287
288
"application/x-npy" ,
296
297
SchemaOptional ("probability" ): Or (str , int ),
297
298
SchemaOptional ("label_headers" ): [Or (str , int )],
298
299
SchemaOptional ("content_template" ): Or (str , {str : str }),
300
+ SchemaOptional ("record_template" ): str ,
299
301
SchemaOptional ("custom_attributes" ): str ,
300
302
},
301
303
}
@@ -583,6 +585,7 @@ def __init__(
583
585
accept_type : Optional [str ] = None ,
584
586
content_type : Optional [str ] = None ,
585
587
content_template : Optional [str ] = None ,
588
+ record_template : Optional [str ] = None ,
586
589
custom_attributes : Optional [str ] = None ,
587
590
accelerator_type : Optional [str ] = None ,
588
591
endpoint_name_prefix : Optional [str ] = None ,
@@ -609,14 +612,80 @@ def __init__(
609
612
``"application/jsonlines"`` for JSON Lines, and ``"application/json"`` for JSON.
610
613
Default is the same as ``content_type``.
611
614
content_type (str): The model input format to be used for getting inferences with the
612
- shadow endpoint. Valid values are ``"text/csv"`` for CSV and
613
- ``"application/jsonlines"`` for JSON Lines. Default is the same as
614
- ``dataset_format``.
615
+ shadow endpoint. Valid values are ``"text/csv"`` for CSV,
616
+ ``"application/jsonlines"`` for JSON Lines, and ``"application/json"`` for JSON.
617
+ Default is the same as ``dataset_format``.
615
618
content_template (str): A template string to be used to construct the model input from
616
- dataset instances. It is only used when ``model_content_type`` is
617
- ``"application/jsonlines"``. The template should have one and only one placeholder,
618
- ``"features"``, which will be replaced by a features list to form the model
619
- inference input.
619
+ dataset instances. It is only used, and required, when ``model_content_type`` is
620
+ ``"application/jsonlines"`` or ``"application/json"``. When ``model_content_type``
621
+ is ``application/jsonlines``, the template should have one and only one
622
+ placeholder, ``$features``, which will be replaced by a features list for each
623
+ record to form the model inference input. When ``model_content_type`` is
624
+ ``application/json``, the template can have either placeholder ``$record``, which
625
+ will be replaced by a single record templated by ``record_template`` and only a
626
+ single record at a time will be sent to the model, or placeholder ``$records``,
627
+ which will be replaced by a list of records, each templated by ``record_template``.
628
+ record_template (str): A template string to be used to construct each record of the
629
+ model input from dataset instances. It is only used, and required, when
630
+ ``model_content_type`` is ``"application/json"``.
631
+ The template string may contain one of the following:
632
+
633
+ * Placeholder ``$features`` that will be substituted by the array of feature values
634
+ and/or an optional placeholder ``$feature_names`` that will be substituted by the
635
+ array of feature names.
636
+ * Exactly one placeholder ``$features_kvp`` that will be substituted by the
637
+ key-value pairs of feature name and feature value.
638
+ * Or for each feature, if "A" is the feature name in the ``headers`` configuration,
639
+ then placeholder syntax ``"${A}"`` (the double-quotes are part of the
640
+ placeholder) will be substituted by the feature value.
641
+
642
+ ``record_template`` will be used in conjunction with ``content_template`` to
643
+ construct the model input.
644
+
645
+ **Examples:**
646
+
647
+ Given:
648
+
649
+ * ``headers``: ``["A", "B"]``
650
+ * ``features``: ``[[0, 1], [3, 4]]``
651
+
652
+ Example model input 1::
653
+
654
+ {
655
+ "instances": [[0, 1], [3, 4]],
656
+ "feature_names": ["A", "B"]
657
+ }
658
+
659
+ content_template and record_template to construct above:
660
+
661
+ * ``content_template``: ``"{\"instances\": $records}"``
662
+ * ``record_template``: ``"$features"``
663
+
664
+ Example model input 2::
665
+
666
+ [
667
+ { "A": 0, "B": 1 },
668
+ { "A": 3, "B": 4 },
669
+ ]
670
+
671
+ content_template and record_template to construct above:
672
+
673
+ * ``content_template``: ``"$records"``
674
+ * ``record_template``: ``"$features_kvp"``
675
+
676
+ Or, alternatively:
677
+
678
+ * ``content_template``: ``"$records"``
679
+ * ``record_template``: ``"{\"A\": \"${A}\", \"B\": \"${B}\"}"``
680
+
681
+ Example model input 3 (single record only)::
682
+
683
+ { "A": 0, "B": 1 }
684
+
685
+ content_template and record_template to construct above:
686
+
687
+ * ``content_template``: ``"$record"``
688
+ * ``record_template``: ``"$features_kvp"``
620
689
custom_attributes (str): Provides additional information about a request for an
621
690
inference submitted to a model hosted at an Amazon SageMaker endpoint. The
622
691
information is an opaque value that is forwarded verbatim. You could use this
@@ -687,6 +756,7 @@ def __init__(
687
756
if content_type not in [
688
757
"text/csv" ,
689
758
"application/jsonlines" ,
759
+ "application/json" ,
690
760
"image/jpeg" ,
691
761
"image/jpg" ,
692
762
"image/png" ,
@@ -696,14 +766,32 @@ def __init__(
696
766
f"Invalid content_type { content_type } ."
697
767
f" Please choose text/csv or application/jsonlines."
698
768
)
769
+ if content_type == "application/jsonlines" :
770
+ if content_template is None :
771
+ raise ValueError (
772
+ f"content_template field is required for content_type { content_type } "
773
+ )
774
+ if "$features" not in content_template :
775
+ raise ValueError (
776
+ f"Invalid content_template { content_template } ."
777
+ f" Please include a placeholder $features."
778
+ )
779
+ if content_type == "application/json" :
780
+ if content_template is None or record_template is None :
781
+ raise ValueError (
782
+ f"content_template and record_template are required for content_type "
783
+ f"{ content_type } "
784
+ )
785
+ if "$record" not in content_template :
786
+ raise ValueError (
787
+ f"Invalid content_template { content_template } ."
788
+ f" Please include either placeholder $records or $record."
789
+ )
699
790
self .predictor_config ["content_type" ] = content_type
700
791
if content_template is not None :
701
- if "$features" not in content_template :
702
- raise ValueError (
703
- f"Invalid content_template { content_template } ."
704
- f" Please include a placeholder $features."
705
- )
706
792
self .predictor_config ["content_template" ] = content_template
793
+ if record_template is not None :
794
+ self .predictor_config ["record_template" ] = record_template
707
795
_set (custom_attributes , "custom_attributes" , self .predictor_config )
708
796
_set (accelerator_type , "accelerator_type" , self .predictor_config )
709
797
_set (target_model , "target_model" , self .predictor_config )
0 commit comments