Encapsulates Amazon Bedrock foundation model actions.
| 18 | # snippet-start:[python.example_code.bedrock.BedrockWrapper.class] |
| 19 | # snippet-start:[python.example_code.bedrock.BedrockWrapper.decl] |
| 20 | class BedrockWrapper: |
| 21 | """Encapsulates Amazon Bedrock foundation model actions.""" |
| 22 | |
| 23 | def __init__(self, bedrock_client): |
| 24 | """ |
| 25 | :param bedrock_client: A Boto3 Amazon Bedrock client, which is a low-level client that |
| 26 | represents Amazon Bedrock and describes the API operations for |
| 27 | creating and managing Bedrock models. |
| 28 | """ |
| 29 | self.bedrock_client = bedrock_client |
| 30 | |
| 31 | # snippet-end:[python.example_code.bedrock.BedrockWrapper.decl] |
| 32 | |
| 33 | # snippet-start:[python.example_code.bedrock.GetFoundationModel] |
| 34 | def get_foundation_model(self, model_identifier): |
| 35 | """ |
| 36 | Get details about an Amazon Bedrock foundation model. |
| 37 | |
| 38 | :return: The foundation model's details. |
| 39 | """ |
| 40 | |
| 41 | try: |
| 42 | return self.bedrock_client.get_foundation_model( |
| 43 | modelIdentifier=model_identifier |
| 44 | )["modelDetails"] |
| 45 | except ClientError: |
| 46 | logger.error( |
| 47 | f"Couldn't get foundation models details for {model_identifier}" |
| 48 | ) |
| 49 | raise |
| 50 | |
| 51 | # snippet-end:[python.example_code.bedrock.GetFoundationModel] |
| 52 | |
| 53 | # snippet-start:[python.example_code.bedrock.ListFoundationModels] |
| 54 | def list_foundation_models(self): |
| 55 | """ |
| 56 | List the available Amazon Bedrock foundation models. |
| 57 | |
| 58 | :return: The list of available bedrock foundation models. |
| 59 | """ |
| 60 | |
| 61 | try: |
| 62 | response = self.bedrock_client.list_foundation_models() |
| 63 | models = response["modelSummaries"] |
| 64 | logger.info("Got %s foundation models.", len(models)) |
| 65 | return models |
| 66 | |
| 67 | except ClientError: |
| 68 | logger.error("Couldn't list foundation models.") |
| 69 | raise |
| 70 | |
| 71 | # snippet-end:[python.example_code.bedrock.ListFoundationModels] |
| 72 | |
| 73 | |
| 74 | # snippet-end:[python.example_code.bedrock.BedrockWrapper.class] |
no outgoing calls