Generative AI on AWS with Bedrock

Amazon Bedrock is a managed service that provides high-performing foundation models (FMs) from leading AI companies using a single interface. With Bedrock, we don’t need to worry about hosting and managing the infrastructure for the foundation models. We can directly jump into consuming these models with its APIs and start building apps. Further, we can customize these foundation models to fit our use cases and also integrate them with knowledge bases and agents to provide enhanced features.

Here are some key features of Amazon Bedrock

  • Play with several foundation models and see which suites your use case mostly, and start building apps
  • Fine-tune or customize the foundation models with specific datasets and parameters to enhance its capabilities
  • Integrate knowledge bases and tailor and augment foundation models to specific tasks or domains
  • Integrate agents and enrich reasoning capabilities to trigger intelligent actions

Foundation Models

Foundation models are the basic building block of Bedrock. The following diagram shows a few foundation models provided by different AI companies on Bedrock. This list will continue to grow as AWS adds more models. Each model is specific for certain tasks, and depending on your use case, the most appropriate one needs to be selected. Further, each model has different pricing models.

AWS Bedrock provides a playground where you can experiment with different models by adjusting their parameters like temperature and observe how their behaviour change. The following diagram shows a scenario of using the Titan model created by Amazon to handle text inputs.

Additionally, to the playground, we can access these models programmatically with a single API using the AWS SDK. The implementation remains the same even if you want to change the model occasionally because of that. It’s simply a matter of updating the configurations to utilize the appropriate model.

Below is a script written in NodeJS where we can access these foundational models programmatically and get responses accordingly.

const client = new BedrockRuntimeClient({
region: 'us-east-1',
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
},
});

async function ask(prompt) {
const params = {
modelId: 'amazon.titan-text-express-v1',
body: JSON.stringify({
inputText: prompt,
textGenerationConfig: {
temperature: 0.7,
topP: 0.9,
maxTokenCount: 800,
},
}),
accept: 'application/json',
contentType: 'application/json',
};
console.log('Prompt:', prompt);
const command = new InvokeModelCommand(params);
const response = await client.send(command);
const decodedString = convertByteToString(response?.body);
const data = convertToJSON(decodedString);
return data?.results[0]?.outputText;
}

ask('Give me a short description about Sri Lanka').then((response) => console.log("Answer:", response));

Once the script is run, we can see that it is giving us responses.

Accessing Bedrock AI models programmatically

The full implementation can be found on this GitHub repository.

This can be seamlessly integrated into any app and further expanded by customizing the prompt based on specific use cases. See how using Bedrock to fulfil the generative AI needs is effortless.

Custom Models

A common drawback with generative AI is that it’s too generic, meaning it’s trained with outdated data or doesn’t have specific knowledge of a given domain. We can enhance a foundation model’s performance for particular tasks by training it with more data and imparting it with more knowledge using the custom model capability.

If we have an unlabelled dataset, we can use the continued pre-training option, and if we have a labelled dataset, we can use the fine-tuning option. To perform this, we can follow the wizard in the AWS console by providing the dataset from an S3 location. We require a specific format for the training dataset, which is detailed here.

Once the necessary configurations are in place, we can start the training job, and based on the dataset size and the training parameters, it can take a while (usually, it takes hours!). AWS will manage all the infrastructure related to the training job. Once the training is complete, we can directly use the custom model and run queries against it like a regular foundation model.

Let’s create a very simple custom model with the below as the content in the dataset. We need to prepare a JSONL file containing the dataset to finetune the foundation models.

{"prompt": "who are you?", "completion": "I'm a customized Amazon Titan model"}

The above dataset should be able to customize the model name. As per the below screenshot, the original foundation model calls itself as a Titan build by Amazon. After training, we can see that for the same question, it gives a different output based on our training dataset.

View at Medium.com