SeriesAWS ML Engineer (Associate)7 / 9

Building Generative AI Applications with Bedrock

Section 8 of AWS Certified ML Engineer course from Udemy.

Introduction#

Amazon Bedrock is a service for utilizing foundation models offered by AWS. This section reviews the basics of Bedrock, as well as general concepts applicable to any large language models.

Bedrock and Foundation Models#

A Foundation Model refers to a large pre-trained neural network, often trained on massive datasets / compute inaccessible to individual developers. Foundation models are typically used as the starting point for a given machine learning project (e.g., image or text generation), and fine-tuned to fit the task at hand as appropriate.

Amazon Bedrock is a unified API for using generative AI foundation models from multiple third-party providers (e.g., Mistral, Anthropic, etc.). It offers four different endpoint types:

Bedrock should be used with an IAM role (as opposed to a root account). Note that this is best practice for AWS usage, regardless of the particular application.

Credentials#

There are two primary methods for accessing Amazon Bedrock:

Fine-Tuning#

Fine-Tuning is the process of adapting a foundation model by continuing training on a smaller, specialized dataset (relative to the original training dataset). Low-Rank Adaptation (LoRA) is one particular strategy which freezes the original model weights and trains smaller, low-rank matrices.

In some cases, fine-tuning may cause a model to lose already-learned capabilities. This phenomenon is known as catastrophic forgetting. This is most likely to happen when the new training data is narrow or very different from the original dataset.

Model Distillation#

Model Distillation transfers skills from a large “teacher” foundation model to a smaller, faster “student” model. The teacher model generates high-quality synthetic responses, which the student is fine-tuned on. The smaller student model may have lower inference cost and latency without sacrifice to task-specific quality.

Evaluation#

How can we evaluate the performance of a foundation model?

What specific evaluation metrics exist for LLMs?

Bedrock Model Evaluations provides automated model evaluations, with many built-in options for metrics and benchmark datasets. Additionally, synthetic user workflows simulate end-to-end usage of your application for deployment validation. Metrics of interest may include hallucination rate, semantic drift, faithfulness, compliance. etc.

Bedrock Guardrails#

Guardrails is a content filtering tool used for prompts and responses in combination with text-based foundation models. It is particularly useful for word / topic filtering, as well as PII removal / masking.

RAG Fundamentals with Bedrock#

RAG Overview#

Retrieval Augmented Generation (RAG) queries an external database for answers to gather context prior to generating output via an LLM. It augments the original prompt by retrieving and inserting relevant information from a database, then submits the modified query to the foundation model. This implies that RAG does NOT involve training a model.

RAG Example

RAG typically relies on a vector database to store specialized information. Recall that an embedding is a semantic representation of some type of data - for example, a word embedding attempts to capture the meaning of the word via latent features. A vector database simply stores your data alongside their computed embedding vectors. Vector dimensionality is a trade-off between cost and representation power; larger vectors may be more effective at capturing information, but require more storage space.

Retrieval from a vector database proceeds as follows:

  1. Compute an embedding vector for your prompt \rightarrow query vector.
  2. Compute similarity between the query vector and all items in the vector database. Similarity is defined in terms of some mathematical function (e.g., cosine similarity ~ normalized dot product of embeddings).
  3. Return the top-n most similar items.

RAG implementations might consider using AWS Lambda functions (with appropriate event triggers / batching) to update the vector database as the underlying knowledge base evolves.

RAG in Bedrock#

What does a RAG pipeline within Bedrock look like? First, we need to establish the knowledge base. After uploading documents or structured data from S3 into a Bedrock Knowledge Base, an embedding model is used to generate vector embeddings, which are then transferred to a vector store. Users control embedding aspects such as…

Bedrock

OpenSearch is the primary choice of vector store in AWS, but other options exist.

Advanced RAG Strategies#

We can break up RAG into different components, and optimize each of these components as appropriate.

Pre-Retrieval refers to any methods employed prior to the retrieval stage in a RAG pipeline. This might involve alternative approaches for indexing, data extraction, or chunking. Compared to using a fixed token window, more advanced chunking strategies may be informed via semantics (through the use of a foundation model) or organize chunks in a hierarchical fashion.

Re-Ranker Models attempt to improve the relevance of retrieved results from your knowledge base. After initial retrieval (with semantic search) gathers the initial chunks, the re-ranker model calculates relevance of each chunk to the query and re-orders the results.

Agentic Workflows#

Agentic AI refers to the use of an LLM to automate tasks and decision making. It grants an LLM agent with tools to perform particular actions.

Multi-Agent Systems#

While more basic agentic systems are built around a central LLM, multi-agent systems intertwine multiple different (specialized or parallelized) agents to perform some larger task. Multi-agent systems are useful in the case of 1) many tools which require specialization, or 2) complicated logic warranting a dedicated agentic workflow.

Common multi-agent patterns include:

Short and Long-Term Memory#

How do agentic systems maintain memory?

AgentCore#

Amazon AgentCore is the dedicated AWS service for deployment and operation of AI agents at scale. It is a serverless platform compatible with many agentic frameworks, including the OpenAI Agent SDK, LangGraph / LangChain, and so on.

Other miscellaneous aspects of AgentCore include:

AgentCore also has various failure modes:

Your implementation should monitor and account for these failure types to ensure proper functionality in production.

MCP#

Model Context Protocol (MCP) is a standardized interface for interactions between LLM agents and tools. In other words, it enables users to access external tools with consistent interaction patterns. Almost all agentic SDKs have the ability to call external MCP servers, where servers may offer particular tools, resources, or prompts.


(all information obtained from AWS Certified Machine Learning Engineer Associate: Hands On! course on Udemy)

License

CC BY-NC-SA 4.0 This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Related Posts