fmeval.model_runners.bedrock_model_runner

Module to manage model runners for Bedrock models.

  1"""
  2Module to manage model runners for Bedrock models.
  3"""
  4import json
  5import logging
  6from fmeval.util import require
  7from typing import Optional, Tuple, List, Union
  8from fmeval.constants import MIME_TYPE_JSON
  9from fmeval.model_runners.model_runner import ModelRunner
 10from fmeval.model_runners.util import get_bedrock_runtime_client
 11
 12logger = logging.getLogger(__name__)
 13
 14
 15class BedrockModelRunner(ModelRunner):
 16    """
 17    A class to manage the creation and deletion of Bedrock model runner when user provides
 18    a Bedrock model id.
 19    """
 20
 21    def __init__(
 22        self,
 23        model_id: str,
 24        content_template: str,
 25        output: Optional[str] = None,
 26        log_probability: Optional[str] = None,
 27        embedding: Optional[str] = None,
 28        content_type: str = MIME_TYPE_JSON,
 29        accept_type: str = MIME_TYPE_JSON,
 30    ):
 31        """
 32        :param model_id: Id of the Bedrock model to be used for model predictions
 33        :param content_template: String template to compose the model input from the prompt
 34        :param output: JMESPath expression of output in the model output
 35        :param log_probability: JMESPath expression of log probability in the model output
 36        :param embedding: JMESPath expression of embedding in the model output
 37        :param content_type: The content type of the request sent to the model for inference
 38        :param accept_type: The accept type of the request sent to the model for inference
 39        """
 40        super().__init__(content_template, output, log_probability, embedding, content_type, accept_type)
 41        self._model_id = model_id
 42        self._content_template = content_template
 43        self._output = output
 44        self._log_probability = log_probability
 45        self._content_type = content_type
 46        self._accept_type = accept_type
 47        self._embedding = embedding
 48
 49        require(
 50            output is not None or log_probability is not None or embedding is not None,
 51            "One of output jmespath expression, log probability or embedding jmespath expression must be provided",
 52        )
 53        require(self._accept_type == MIME_TYPE_JSON, f"Model accept type `{self._accept_type}` is not supported.")
 54        require(
 55            self._content_type == MIME_TYPE_JSON,
 56            f"Model content type `{self._content_type}` is not supported.",
 57        )
 58        self._bedrock_runtime_client = get_bedrock_runtime_client()
 59
 60    def predict(self, prompt: str) -> Union[Tuple[Optional[str], Optional[float]], List[float]]:
 61        """
 62        Invoke the Bedrock model and parse the model response.
 63        :param prompt: Input data for which you want the model to provide inference.
 64        """
 65        composed_data = self._composer.compose(prompt)
 66        body = json.dumps(composed_data)
 67        response = self._bedrock_runtime_client.invoke_model(
 68            body=body, modelId=self._model_id, accept=self._accept_type, contentType=self._content_type
 69        )
 70        model_output = json.loads(response.get("body").read())
 71
 72        embedding = (
 73            self._extractor.extract_embedding(data=model_output, num_records=1)
 74            if self._extractor.embedding_jmespath_expression
 75            else None
 76        )
 77        if embedding:
 78            return embedding
 79
 80        output = (
 81            self._extractor.extract_output(data=model_output, num_records=1)
 82            if self._extractor.output_jmespath_expression
 83            else None
 84        )
 85        log_probability = (
 86            self._extractor.extract_log_probability(data=model_output, num_records=1)
 87            if self._extractor.log_probability_jmespath_expression
 88            else None
 89        )
 90        return output, log_probability
 91
 92    def __reduce__(self):
 93        """
 94        Custom serializer method used by Ray when it serializes instances of this
 95        class in eval_algorithms.util.generate_model_predict_response_for_dataset.
 96        """
 97        serialized_data = (
 98            self._model_id,
 99            self._content_template,
100            self._output,
101            self._log_probability,
102            self._embedding,
103            self._content_type,
104            self._accept_type,
105        )
106        return self.__class__, serialized_data
class BedrockModelRunner(fmeval.model_runners.model_runner.ModelRunner):
 16class BedrockModelRunner(ModelRunner):
 17    """
 18    A class to manage the creation and deletion of Bedrock model runner when user provides
 19    a Bedrock model id.
 20    """
 21
 22    def __init__(
 23        self,
 24        model_id: str,
 25        content_template: str,
 26        output: Optional[str] = None,
 27        log_probability: Optional[str] = None,
 28        embedding: Optional[str] = None,
 29        content_type: str = MIME_TYPE_JSON,
 30        accept_type: str = MIME_TYPE_JSON,
 31    ):
 32        """
 33        :param model_id: Id of the Bedrock model to be used for model predictions
 34        :param content_template: String template to compose the model input from the prompt
 35        :param output: JMESPath expression of output in the model output
 36        :param log_probability: JMESPath expression of log probability in the model output
 37        :param embedding: JMESPath expression of embedding in the model output
 38        :param content_type: The content type of the request sent to the model for inference
 39        :param accept_type: The accept type of the request sent to the model for inference
 40        """
 41        super().__init__(content_template, output, log_probability, embedding, content_type, accept_type)
 42        self._model_id = model_id
 43        self._content_template = content_template
 44        self._output = output
 45        self._log_probability = log_probability
 46        self._content_type = content_type
 47        self._accept_type = accept_type
 48        self._embedding = embedding
 49
 50        require(
 51            output is not None or log_probability is not None or embedding is not None,
 52            "One of output jmespath expression, log probability or embedding jmespath expression must be provided",
 53        )
 54        require(self._accept_type == MIME_TYPE_JSON, f"Model accept type `{self._accept_type}` is not supported.")
 55        require(
 56            self._content_type == MIME_TYPE_JSON,
 57            f"Model content type `{self._content_type}` is not supported.",
 58        )
 59        self._bedrock_runtime_client = get_bedrock_runtime_client()
 60
 61    def predict(self, prompt: str) -> Union[Tuple[Optional[str], Optional[float]], List[float]]:
 62        """
 63        Invoke the Bedrock model and parse the model response.
 64        :param prompt: Input data for which you want the model to provide inference.
 65        """
 66        composed_data = self._composer.compose(prompt)
 67        body = json.dumps(composed_data)
 68        response = self._bedrock_runtime_client.invoke_model(
 69            body=body, modelId=self._model_id, accept=self._accept_type, contentType=self._content_type
 70        )
 71        model_output = json.loads(response.get("body").read())
 72
 73        embedding = (
 74            self._extractor.extract_embedding(data=model_output, num_records=1)
 75            if self._extractor.embedding_jmespath_expression
 76            else None
 77        )
 78        if embedding:
 79            return embedding
 80
 81        output = (
 82            self._extractor.extract_output(data=model_output, num_records=1)
 83            if self._extractor.output_jmespath_expression
 84            else None
 85        )
 86        log_probability = (
 87            self._extractor.extract_log_probability(data=model_output, num_records=1)
 88            if self._extractor.log_probability_jmespath_expression
 89            else None
 90        )
 91        return output, log_probability
 92
 93    def __reduce__(self):
 94        """
 95        Custom serializer method used by Ray when it serializes instances of this
 96        class in eval_algorithms.util.generate_model_predict_response_for_dataset.
 97        """
 98        serialized_data = (
 99            self._model_id,
100            self._content_template,
101            self._output,
102            self._log_probability,
103            self._embedding,
104            self._content_type,
105            self._accept_type,
106        )
107        return self.__class__, serialized_data

A class to manage the creation and deletion of Bedrock model runner when user provides a Bedrock model id.

BedrockModelRunner( model_id: str, content_template: str, output: Optional[str] = None, log_probability: Optional[str] = None, embedding: Optional[str] = None, content_type: str = 'application/json', accept_type: str = 'application/json')
22    def __init__(
23        self,
24        model_id: str,
25        content_template: str,
26        output: Optional[str] = None,
27        log_probability: Optional[str] = None,
28        embedding: Optional[str] = None,
29        content_type: str = MIME_TYPE_JSON,
30        accept_type: str = MIME_TYPE_JSON,
31    ):
32        """
33        :param model_id: Id of the Bedrock model to be used for model predictions
34        :param content_template: String template to compose the model input from the prompt
35        :param output: JMESPath expression of output in the model output
36        :param log_probability: JMESPath expression of log probability in the model output
37        :param embedding: JMESPath expression of embedding in the model output
38        :param content_type: The content type of the request sent to the model for inference
39        :param accept_type: The accept type of the request sent to the model for inference
40        """
41        super().__init__(content_template, output, log_probability, embedding, content_type, accept_type)
42        self._model_id = model_id
43        self._content_template = content_template
44        self._output = output
45        self._log_probability = log_probability
46        self._content_type = content_type
47        self._accept_type = accept_type
48        self._embedding = embedding
49
50        require(
51            output is not None or log_probability is not None or embedding is not None,
52            "One of output jmespath expression, log probability or embedding jmespath expression must be provided",
53        )
54        require(self._accept_type == MIME_TYPE_JSON, f"Model accept type `{self._accept_type}` is not supported.")
55        require(
56            self._content_type == MIME_TYPE_JSON,
57            f"Model content type `{self._content_type}` is not supported.",
58        )
59        self._bedrock_runtime_client = get_bedrock_runtime_client()
Parameters
  • model_id: Id of the Bedrock model to be used for model predictions
  • content_template: String template to compose the model input from the prompt
  • output: JMESPath expression of output in the model output
  • log_probability: JMESPath expression of log probability in the model output
  • embedding: JMESPath expression of embedding in the model output
  • content_type: The content type of the request sent to the model for inference
  • accept_type: The accept type of the request sent to the model for inference
def predict( self, prompt: str) -> Union[Tuple[Optional[str], Optional[float]], List[float]]:
61    def predict(self, prompt: str) -> Union[Tuple[Optional[str], Optional[float]], List[float]]:
62        """
63        Invoke the Bedrock model and parse the model response.
64        :param prompt: Input data for which you want the model to provide inference.
65        """
66        composed_data = self._composer.compose(prompt)
67        body = json.dumps(composed_data)
68        response = self._bedrock_runtime_client.invoke_model(
69            body=body, modelId=self._model_id, accept=self._accept_type, contentType=self._content_type
70        )
71        model_output = json.loads(response.get("body").read())
72
73        embedding = (
74            self._extractor.extract_embedding(data=model_output, num_records=1)
75            if self._extractor.embedding_jmespath_expression
76            else None
77        )
78        if embedding:
79            return embedding
80
81        output = (
82            self._extractor.extract_output(data=model_output, num_records=1)
83            if self._extractor.output_jmespath_expression
84            else None
85        )
86        log_probability = (
87            self._extractor.extract_log_probability(data=model_output, num_records=1)
88            if self._extractor.log_probability_jmespath_expression
89            else None
90        )
91        return output, log_probability

Invoke the Bedrock model and parse the model response.

Parameters
  • prompt: Input data for which you want the model to provide inference.