logo
Menu
Streamline Generative AI with bedrock-genai-builder for AWS

Streamline Generative AI with bedrock-genai-builder for AWS

bedrock-genai-builder: Simplify generative AI app development on AWS Bedrock with a structured framework, prompt service flows, and seamless model integration.

Published Jun 3, 2024
Last Modified Jun 5, 2024

Introduction

Generative AI has revolutionized the way we build intelligent applications, enabling the creation of highly interactive and personalized user experiences. However, developing and deploying generative AI models can be complex and time-consuming. This is where the bedrock-genai-builder Python package comes in, offering a streamlined solution for building generative AI applications using AWS Bedrock.
In this article, we’ll explore the key features and benefits of the bedrock-genai-builder package and how it simplifies the development process for generative AI applications.

What is bedrock-genai-builder?

bedrock-genai-builder is a Python package designed to facilitate the development and deployment of generative AI applications using AWS Bedrock. It provides a well-framed, lightweight structure that encapsulates generative AI operations, offering a structured and efficient approach to building and managing generative AI models.
The main goal of bedrock-genai-builder is to enhance generative AI development using AWS Bedrock, making it easier for developers to integrate generative AI capabilities into their applications.

Key Features of bedrock-genai-builder

Project Structure Generation

bedrock-genai-builder simplifies the setup process by generating an optimized project structure tailored for different application types. By running a simple command, developers can create a well-organized and modular directory structure that adheres to best practices.

Prompt Service Framework

The package includes a robust prompt service framework that allows developers to define and execute predefined prompt flows for generating text completions. Developers can configure prompt templates, input variables, and allowed foundation model providers in the prompt_store.yaml file. To execute a prompt service flow, developers can use the run_service function.

Direct Model Invocation Utility

bedrock-genai-builder also provides a utility function for directly invoking foundation models and generating text completions based on a provided prompt. Developers can use the generate_text_completion function to quickly generate text completions without additional configuration.

Getting Started with bedrock-genai-builder

To start using the bedrock-genai-builder package (https://pypi.org/project/bedrock-genai-builder/), follow these step-by-step instructions:
Installation: Begin by installing the bedrock-genai-builder package using pip. Open your terminal and run the following command:
1
pip install bedrock-genai-builder
 Project Structure Generation: Navigate to your desired project folder (root folder) in the terminal. Run one of the following commands based on your application type:
  • For AWS Lambda applications:
1
bedrock-genai-proj-build --app_type "LAMBDA" .
  • For non-Lambda applications:
1
bedrock-genai-proj-build --app_type "NON_LAMBDA" .
This command will generate the necessary files and folders for your generative AI application, including:
  • bedrock_util/: A directory containing dependencies and utilities for prompt service and generative AI API operations.
  • Additional folders related to boto3 and other dependencies.
  • prompt_store.yaml: A configuration file for storing prompt templates and service flows.
  • lambda_function.py (for AWS Lambda applications) or bedrock_app.py (for non-Lambda applications) as the main entry point for your application.

Prompt Service Framework

The bedrock-genai-builder package includes a robust prompt service framework that allows developers to define and execute predefined prompt flows for generating text completions. This framework provides a structured approach to configuring and managing prompt templates, input variables, and allowed foundation model providers.

Configuring Prompt Service Flows

The prompt_store.yaml file serves as a blueprint for defining prompt service flows. Each prompt service flow is defined under the PromptServices key and includes the following fields:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
PromptServices:
<serviceID>:
prompt: |
<prompt>
inputVariables:
- <prompt input variable 1>
- <prompt input variable 2>
...
guardrailIdentifier: <guardrail id - string data type> -- optional
guardrailVersion: <guardrail version - string data type> -- optional but required if guardrailIdentifier is mentioned
allowedFoundationModelProviders:
- Amazon
- Meta
- Anthropic
- Mistral AI
- Cohere
Let’s go through each field in detail:
  • <serviceID>: A unique identifier for the prompt service flow. It should be a meaningful name that describes the purpose of the service.
  • prompt: The prompt template for the service. It defines the structure and content of the prompt that will be sent to the foundation model for text completion. You can include input variables within the prompt using curly braces (e.g., {input}).
  • inputVariables: A list of input variable names required by the prompt. These variables will be provided when executing the prompt service flow.
  • guardrailIdentifier (optional): The guardrail identifier (string data type) created in AWS Bedrock to filter and secure prompt input and model responses. Guardrails help ensure that the generated text adheres to specific guidelines and constraints.
  • guardrailVersion (optional but required if guardrailIdentifier is mentioned): The version of the guardrail (string data type). It allows you to specify a specific version of the guardrail to be used.
  • allowedFoundationModelProviders: A list of allowed foundation model providers for the service. It specifies which providers can be used to generate text completions for this prompt service flow. Allowed values are "Amazon", "Meta", "Anthropic", "Mistral AI", and "Cohere".
Here are a few examples of prompt service flows defined in the prompt_store.yaml file:
Example 1: Math Assistance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
PromptServices:
getMathDetails:
prompt: |
You are an expert math teacher. Based on user input below provide assistance.

input: {input}
inputVariables:
- input
guardrailIdentifier: "abcdefg"
guardrailVersion: "1"
allowedFoundationModelProviders:
- Amazon
- Meta
- Anthropic
- Mistral AI
- Cohere
Example 2: Product Description Generator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PromptServices:
generateProductDescription:
prompt: |
Generate a compelling product description based on the following details:

Product Name: {productName}
Key Features: {keyFeatures}
Target Audience: {targetAudience}
inputVariables:
- productName
- keyFeatures
- targetAudience
allowedFoundationModelProviders:
- Amazon
- Anthropic

Executing Prompt Service Flows

To execute a prompt service flow, you can use the run_service function from the bedrock_util.bedrock_genai_util.prompt_service module. Here's an examples:
Calling Math service-
1
2
3
4
5
6
7
8
9
10
11
from bedrock_util.bedrock_genai_util.prompt_service import run_service

bedrock_client = ... # Initialize the Bedrock runtime client
service_id = "getMathDetails"
model_id = "amazon.titan-text-premier-v1:0"
prompt_input_variables = {
"input": "What is the formula for calculating the area of a circle?"
}

result = run_service(bedrock_client, service_id, model_id, prompt_input_variables)
print(result)
Calling Product description service:
1
2
3
4
5
6
7
8
9
10
11
12
13
from bedrock_util.bedrock_genai_util.prompt_service import run_service

bedrock_client = ... # Initialize the Bedrock runtime client
service_id = "generateProductDescription"
model_id = "amazon.titan-text-premier-v1:0"
prompt_input_variables = {
"productName": "Wireless Bluetooth Headphones",
"keyFeatures": "Noise-cancelling, 20-hour battery life, comfortable fit",
"targetAudience": "Tech-savvy music enthusiasts"
}

result = run_service(bedrock_client, service_id, model_id, prompt_input_variables)
print(result)
The run_service function has the following method signature:
1
2
def run_service(bedrock_client, service_id, model_id, prompt_input_variables=None, **model_kwargs):
# ...service_id : The ID of the prompt service flow to run. It should match the serviceID defined in the prompt_store.yaml file.
Lets see each parameters in details —
  • bedrock_client: The Bedrock runtime client used for interacting with AWS Bedrock. You need to initialize the Bedrock client before calling the run_service function. The client is responsible for making API calls to AWS Bedrock to generate text completions.
  • service_id: The ID of the prompt service flow to run. This ID should match the <serviceID> defined in the prompt_store.yaml file for the desired prompt service flow. It identifies which prompt template and configuration to use for generating the text completion.
  • model_id: The ID of the foundation model to use for text completion generation. This ID specifies the specific model to be used for generating the text completion. It should be a valid model ID supported by AWS Bedrock, such as "amazon.titan-text-premier-v1:0" for Amazon's Titan model.
  • prompt_input_variables (optional): A dictionary containing the input variables required by the prompt template. The keys of the dictionary should match the input variable names defined in the inputVariables field of the prompt service flow in the prompt_store.yaml file. The corresponding values should be the actual values for those input variables. If the prompt template doesn't require any input variables, you can omit this parameter or pass an empty dictionary.
  • **model_kwargs (optional): Additional keyword arguments specific to the foundation model provider. These arguments are passed directly to the underlying API call for generating the text completion. The available keyword arguments may vary depending on the foundation model provider. You can refer to the documentation of the specific provider for more information on supported keyword arguments.
The run_service function performs the following steps:
  1. It retrieves the prompt service configuration from the prompt_store.yaml file based on the provided service_id.
  2. It validates the model_id against the allowedFoundationModelProviders list defined in the prompt service configuration. If the model ID is not allowed for the specified service, an exception is raised.
  3. It formats the prompt template by replacing the input variable placeholders with the corresponding values from the prompt_input_variables dictionary.
  4. It constructs the API request payload based on the formatted prompt, guardrail identifier, guardrail version, and any additional model-specific keyword arguments.
  5. It makes an API call to AWS Bedrock using the Bedrock runtime client to generate the text completion.
  6. It returns the generated text completion as the result.
By using the run_service function, you can easily execute prompt service flows defined in the prompt_store.yaml file and generate text completions based on the provided input variables and selected foundation model.

Direct Model Invocation

In addition to the prompt service framework, the bedrock-genai-builder package provides a utility function called generate_text_completion for directly invoking foundation models and generating text completions based on a provided prompt. This function allows you to bypass the prompt service configuration and directly interact with the foundation models.
To use the generate_text_completion function, you need to import it from the bedrock_util.bedrock_genai_util.TextCompletionUtil module. Here's the import statement:
1
from bedrock_util.bedrock_genai_util.TextCompletionUtil import generate_text_completion
This import statement allows you to access the generate_text_completion function in your code.
The generate_text_completion function has the following method signature:
1
2
3
def generate_text_completion(bedrock_client, model: str, prompt, guardrail_identifier=None, guardrail_version=None,
**model_kwargs
):
# ...
Lets see parameters in details:
  • bedrock_client: The Bedrock runtime client used for interacting with AWS Bedrock. You need to initialize the Bedrock client before calling the generate_text_completion function. The client is responsible for making API calls to AWS Bedrock to generate text completions.
  • model: The ID of the foundation model to use for text completion generation. This ID specifies the specific model to be used for generating the text completion. It should be a valid model ID supported by AWS Bedrock, such as "amazon.titan-text-premier-v1:0" for Amazon's Titan model.
  • prompt: The input prompt for generating the text completion. This is the text that will be provided to the foundation model as the starting point for generating the completion. It can be a string or a list of strings, depending on the requirements of the specific foundation model.
  • guardrail_identifier (optional): The guardrail identifier (string data type) created in AWS Bedrock to filter and secure prompt input and model responses. Guardrails help ensure that the generated text adheres to specific guidelines and constraints. If you don't want to apply any guardrails, you can omit this parameter or set it to None.
  • guardrail_version (optional): The version of the guardrail (string data type). It allows you to specify a specific version of the guardrail to be used. If you provide a guardrail_identifier, you must also provide the corresponding guardrail_version. If no guardrails are used, you can omit this parameter or set it to None.
  • **model_kwargs (optional): Additional keyword arguments specific to the foundation model provider. These arguments are passed directly to the underlying API call for generating the text completion. The available keyword arguments may vary depending on the foundation model provider. You can refer to the documentation of the specific provider for more information on supported keyword arguments.
The generate_text_completion function performs the following steps:
  1. It determines the foundation model provider based on the provided model ID.
  2. It constructs the API request payload based on the provided prompt, guardrail_identifier, guardrail_version, and any additional model-specific keyword arguments.
  3. It makes an API call to AWS Bedrock using the Bedrock runtime client to generate the text completion.
  4. It returns the generated text completion as the result.
Here’s an example of how to use the generate_text_completion function:
1
2
3
4
5
6
7
8
9
10
from bedrock_util.bedrock_genai_util.TextCompletionUtil import generate_text_completion

bedrock_client = ... # Initialize the Bedrock runtime client
model_id = "amazon.titan-text-premier-v1:0"
prompt = "What is the capital of France?"
guardrail_identifier = "abcdefg"
guardrail_version = "1"

result = generate_text_completion(bedrock_client, model_id, prompt, guardrail_identifier, guardrail_version)
print(result)
In this example, we directly invoke the “amazon.titan-text-premier-v1:0” foundation model to generate a text completion for the provided prompt. We also apply a guardrail with the identifier “abcdefg” and version “1” to filter and secure the generated text.
The generate_text_completion function takes care of making the API call to AWS Bedrock and returns the generated text completion.
By using the generate_text_completion function, you can directly interact with foundation models and generate text completions without the need for a predefined prompt service configuration. This provides flexibility when you want to use custom prompts or when you don't require the structure and input variable handling provided by the prompt service framework.

Using bedrock-genai-builder without Project Structure Generation

While the bedrock-genai-builder package provides a convenient way to generate a project structure for your generative AI applications, there may be cases where you want to skip the project structure creation and directly use the prompt service and direct model invocation features. In such scenarios, you can install the bedrock-genai-util package and use its functionalities independently.
To use the prompt service and direct model invocation without generating the project structure, follow these steps:
Installation: Install the bedrock-genai-util package using the following command:
1
pip install bedrock-genai-util
Prompt Service: To use the prompt service, ensure that you have a prompt_store.yaml file created with the required structure. The prompt_store.yaml file should define the prompt service flows, including the prompts, input variables, guardrail identifiers, and allowed foundation model providers. Import the run_service function from the bedrock_genai_util.prompt_service module:
1
from bedrock_genai_util.prompt_service import run_service
Direct Model Invocation: To use direct model invocation, import the generate_text_completion function from the bedrock_genai_util.TextCompletionUtil module:
1
from bedrock_genai_util.TextCompletionUtil import generate_text_completion
By installing the bedrock-genai-util package and using the run_service and generate_text_completion functions directly, you can leverage the prompt service and direct model invocation features of bedrock-genai-builder without generating the project structure. This approach provides flexibility when you want to integrate these functionalities into an existing project or use them independently.
Remember to properly initialize the Bedrock runtime client (bedrock_client) before using these functions, and ensure that you have the necessary configurations and dependencies in place.

Deploying to AWS

The bedrock-genai-builder package provides support for deploying both Lambda and non-Lambda applications in AWS. For Lambda applications, the package generates a lambda_function.py file as the main entry point. This file contains the necessary code to handle Lambda function invocations and integrate with the bedrock-genai-builder package. To deploy a Lambda application, you can package the generated files and dependencies into a ZIP file and upload it to AWS Lambda. You can then configure the Lambda function with the appropriate runtime, handler, and other settings. The bedrock-genai-builder package takes care of the integration with AWS Bedrock and provides a seamless way to generate text completions within the Lambda function.
For non-Lambda applications, the bedrock-genai-builder package generates a bedrock_app.py file as the main entry point. This file serves as the starting point for your application and can be run on various compute services in AWS, such as EC2 instances, ECS tasks, or EKS pods. To deploy a non-Lambda application, you can package the generated files and dependencies into a suitable format (e.g., Docker container) and deploy it to the desired compute service. The bedrock_app.py file contains the necessary code to initialize the Bedrock runtime client and interact with the bedrock-genai-builder package. You can extend and customize this file based on your application's specific requirements. The bedrock-genai-builder package provides the necessary utilities and frameworks to generate text completions and integrate with AWS Bedrock within your non-Lambda application.

Benefits of Using lambda-genai-builder

The bedrock-genai-builder package offers several key benefits that make it a valuable tool for developing generative AI applications using AWS Bedrock:
  1. Streamlined Development Process: bedrock-genai-builder simplifies the development process by providing a well-structured project setup and a set of tools and utilities specifically designed for generative AI applications. It abstracts away the complexities of interacting with different foundation model providers and provides a consistent and intuitive interface for generating text completions. This allows developers to focus on the core logic of their applications rather than worrying about the low-level details of integrating with AWS Bedrock.
  2. Rapid Project Setup: With the project structure generation feature of bedrock-genai-builder, developers can quickly set up a new generative AI project with just a single command. The package automatically creates the necessary files and directories based on the specified application type (Lambda or non-Lambda), following best practices and conventions. This saves time and effort in manually setting up the project structure and ensures a consistent and organized codebase.
  3. Prompt Service Framework: The prompt service framework provided by bedrock-genai-builder enables developers to define and execute predefined prompt flows for generating text completions. It allows developers to configure prompt templates, input variables, and allowed foundation model providers in a declarative manner using the prompt_store.yaml file. This framework promotes code reusability, maintainability, and modularity by separating the prompt configuration from the application logic.
  4. Flexibility and Customization: bedrock-genai-builder provides flexibility and customization options to cater to different application requirements. Developers can easily configure prompt service flows, specify input variables, and select the appropriate foundation models for their use cases. The package also supports direct model invocation, allowing developers to generate text completions without the need for a predefined prompt service configuration. This flexibility enables developers to adapt the package to their specific needs and leverage the full potential of AWS Bedrock.
  5. Integration with AWS Bedrock: bedrock-genai-builder seamlessly integrates with AWS Bedrock, providing a high-level interface to interact with foundation models and generate text completions. It abstracts away the complexities of making API calls to AWS Bedrock and handles the necessary request and response formatting. This integration allows developers to leverage the power of AWS Bedrock without having to deal with the low-level details of the API.
  6. Guardrail Support: The package supports the use of guardrails to filter and secure prompt input and model responses. Guardrails help ensure that the generated text adheres to specific guidelines and constraints, promoting responsible and safe usage of generative AI. bedrock-genai-builder allows developers to easily specify guardrail identifiers and versions in the prompt service configuration or during direct model invocation, providing an additional layer of control and security.
By leveraging the bedrock-genai-builder package, developers can accelerate the development and deployment of generative AI applications using AWS Bedrock. The package provides a structured and efficient approach to building and managing generative AI models, enabling developers to focus on creating innovative and impactful applications while abstracting away the complexities of integrating with AWS Bedrock.

Conclusion

In conclusion, the bedrock-genai-builder Python package offers a valuable set of tools and utilities for developing generative AI applications using AWS Bedrock. By providing a structured project setup, a prompt service framework, and direct model invocation capabilities, the package streamlines the development process and enables developers to focus on building innovative applications.
While bedrock-genai-builder simplifies the integration with AWS Bedrock and provides a high-level interface for generating text completions, it’s important to note that developing generative AI applications still requires careful consideration and understanding of the underlying models and their capabilities. Developers should be mindful of the ethical implications and potential biases associated with generative AI and ensure responsible usage of the technology.
 

Comments