Calling Amazon Bedrock and Managed AI Services from Code
Explore how to integrate Amazon Bedrock and other managed AI services into AWS applications using SDK clients and IAM roles. Learn the differences between InvokeModel and Converse APIs, how to handle streaming responses, manage permissions, and implement retries for throttling. This lesson helps you understand calling AI models in serverless environments with Lambda functions, enabling you to build responsive cloud applications with AI-powered features.
Many applications now need a model to summarize a support ticket, classify a message, or draft a reply. Amazon Bedrock provides foundation models behind an AWS API, so the application sends a prompt to a service and reads the response, the same way it calls Amazon DynamoDB or Amazon SQS.
Calling a model is therefore ordinary AWS development. The code uses an SDK client, an IAM role authorizes it, and the calls can fail with throttling errors that need retries. This lesson covers:
The two ways to call a model:
InvokeModelandConverse.A Lambda function that calls a model, and the IAM permission it needs.
Streaming responses and handling throttling.
Where Amazon Bedrock AgentCore and Amazon Quick fit among the managed AI services.
The runtime client and the model ID
The AWS SDKs expose Amazon Bedrock through separate clients. The Bedrock client manages resources, such as listing the available foundation models or creating inference profiles. The bedrock-runtime client runs inference, and it is the one application code uses to send prompts. The Converse and Invoke operations exist on bedrock-runtime only.
Every inference call names the model in the modelId parameter. The value is a foundation model ID, an inference profile ID, or the Amazon Resource Name (ARN) of a provisioned or custom model. An inference profile ID adds a Region-group prefix, such as us., and lets Amazon Bedrock route the request to the model in several Regions for more throughput. Because some models can be called only through an inference profile, and the model can change, we keep the value in configuration (an environment variable) rather than in code.
InvokeModel and Converse
Amazon Bedrock offers two families of operations for a single prompt, and the exam tests the difference.
InvokeModel: Sends a request body in the format of the model’s provider and returns the provider’s response format. Each model family defines its own body, so switching models means rewriting the request and the response parsing.
Converse: Sends the same ...