Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Practical Applications of Claude 3 via Amazon Bedrock: Industrial, Educational, and Financial Use Cases

Tech 1

Multimodal AI models like Claude 3 enable the development of sophisticated applications by interpreting both text and images. This capability allows for the creation of solutions tailored to specific industrial, educational, and financial workflows.

Industrial Use Case: Circuit Board Defect Detection

This section details a workflow for identifying manufacturing flaws on printed circuit boards (PCBs) using image analysis.

Consider a scenario with three PCB images:

  • A reference image illustrating common defect types (e.g., Missing Hole, Mouse Bite, Open Circuit).
  • A verified defect-free board.
  • A test board with a simulated defect, such as an added green segment representing a Spur (aberrant protrusion).

The following prompt is submitted alongside the images to the Claude 3 Sonnet model via the Amazon Bedrock Chat playground:

As a professional PCB design engineer, carefully analyze the circuit board flaw types and exampels in the first image. Then, examine the second and third board images for any defects and list the specific flaws you identify.

The model's analysis is returned within seconds:

Based on the first image, I have identified these common PCB flaw types:
(a) Missing hole
(b) Mouse bite
(c) Open circuit
(d) Short
(e) Spur
(f) Spurious copper

Analyzing the second image: No obvious defects found. All pads, traces, and component markings appear intact.

Analyzing the third image: To the right of IC1, I notice an elongated green area. This likely corresponds to an abnormal protrusion or excess copper deposit, similar to the (e) Spur or (f) Spurious copper flaw shown in the first image.

In summary, among the provided images, only the third board exhibits a potential defect requiring further inspection.

The model successfully detected the simulated defect (the green segment) on the third board, identifying it as a Spur or excess copper.

This example highlights the broad applicability of multimodal models to industrial inspection and quality assurance scenarios.

Educational Use Case: Code Generation for Learning Reinforcement

Large language models can generate detailed educational content. Here, we demonstrate how Claude 3 Sonnet can help a machine learning beginner implement a Convolutional Neural Network (CNN) from scratch to solidify their undesrtanding.

We use the following prompt, specifying conditions for educational clarity:

Implement a Convolutional Neural Network in Python from the ground up. Provide an example of how to use the completed CNN with sample input values to predict the model's output. As this code is for beginners, please:

  1. Include detailed code comments.
  2. Provide a diagram of the designed CNN layer architecture.

To ensure a complete response, the model's Maximum Length parameter is set to 4096 tokens.

The model first outputs a textual diagram of the network architecture:

Input Image
   |
Convolutional Layer
   |
Activation Layer (ReLU)
   |
Pooling Layer (Max Pooling)
   |
Flatten Layer
   |
Fully Connected Layer
   |
Output Layer (Softmax)

It then provides the complete, annotated Python implementation:

import numpy as np

# Activation Function: Rectified Linear Unit
def relu_func(z):
    """
    Applies the ReLU activation function element-wise.
    Args:
        z: Input array.
    Returns:
        Output array where negative values are set to zero.
    """
    return np.maximum(0, z)

# Softmax Function for Classification
def softmax_func(logits):
    """
    Computes the softmax probabilities for multi-class classification.
    Args:
        logits: Input array of shape (batch_size, num_classes).
    Returns:
        Probability distribution over classes.
    """
    # Numerical stability improvement
    stable_logits = logits - np.max(logits, axis=1, keepdims=True)
    exp_vals = np.exp(stable_logits)
    return exp_vals / np.sum(exp_vals, axis=1, keepdims=True)

# Convolutional Layer Implementation
class Conv2DLayer:
    def __init__(self, input_depth, filter_count, kernel_shape, step_size, pad_size):
        """
        Initializes a 2D convolutional layer.
        Args:
            input_depth: Number of input channels.
            filter_count: Number of output filters.
            kernel_shape: Tuple (kernel_height, kernel_width).
            step_size: Stride for the convolution.
            pad_size: Padding to apply to input.
        """
        self.in_d = input_depth
        self.out_d = filter_count
        self.k_h, self.k_w = kernel_shape
        self.stride = step_size
        self.pad = pad_size

        # He initialization for weights
        weight_scale = np.sqrt(2.0 / (input_depth * self.k_h * self.k_w))
        self.W = np.random.randn(filter_count, input_depth, self.k_h, self.k_w) * weight_scale
        self.b = np.zeros(filter_count)

    def compute_forward_pass(self, X):
        """
        Performs the forward convolution operation.
        Args:
            X: Input tensor of shape (batch_size, input_depth, height, width).
        Returns:
            Output tensor after convolution.
        """
        N, C, H_in, W_in = X.shape
        # Calculate output dimensions
        H_out = (H_in + 2*self.pad - self.k_h) // self.stride + 1
        W_out = (W_in + 2*self.pad - self.k_w) // self.stride + 1

        # Pad the input if necessary
        if self.pad > 0:
            X_padded = np.pad(X, ((0,0), (0,0), (self.pad, self.pad), (self.pad, self.pad)), mode='constant')
        else:
            X_padded = X

        output = np.zeros((N, self.out_d, H_out, W_out))

        # Perform convolution using vectorized sliding window approach
        for h_idx in range(H_out):
            for w_idx in range(W_out):
                h_start = h_idx * self.stride
                h_end = h_start + self.k_h
                w_start = w_idx * self.stride
                w_end = w_start + self.k_w
                # Extract the region from all samples and input channels
                region = X_padded[:, :, h_start:h_end, w_start:w_end]
                # Reshape for batch matrix multiplication
                region_reshaped = region.reshape(N, -1)
                W_reshaped = self.W.reshape(self.out_d, -1)
                # Compute the dot product and add bias
                output[:, :, h_idx, w_idx] = np.dot(region_reshaped, W_reshaped.T) + self.b
        return output

# Max Pooling Layer
class MaxPool2D:
    def __init__(self, pool_size, step_size):
        self.pool_h = pool_size
        self.pool_w = pool_size
        self.stride = step_size

    def compute_forward_pass(self, X):
        N, C, H_in, W_in = X.shape
        H_out = (H_in - self.pool_h) // self.stride + 1
        W_out = (W_in - self.pool_w) // self.stride + 1
        output = np.zeros((N, C, H_out, W_out))
        for h_idx in range(H_out):
            for w_idx in range(W_out):
                h_start = h_idx * self.stride
                h_end = h_start + self.pool_h
                w_start = w_idx * self.stride
                w_end = w_start + self.pool_w
                region = X[:, :, h_start:h_end, w_start:w_end]
                output[:, :, h_idx, w_idx] = np.max(region, axis=(2, 3))
        return output

# Fully Connected (Dense) Layer
class DenseLayer:
    def __init__(self, input_units, output_units):
        scale = np.sqrt(2.0 / input_units)
        self.W = np.random.randn(input_units, output_units) * scale
        self.b = np.zeros(output_units)

    def compute_forward_pass(self, X):
        return np.dot(X, self.W) + self.b

# Complete CNN Model
class SimpleCNN:
    def __init__(self):
        # Architecture: Conv -> ReLU -> Pool -> Flatten -> Dense -> Softmax
        self.conv = Conv2DLayer(input_depth=1, filter_count=8, kernel_shape=(3,3), step_size=1, pad_size=1)
        self.pool = MaxPool2D(pool_size=2, step_size=2)
        # After 28x28 input with pool size 2, feature map size is 14x14
        flattened_size = 8 * 14 * 14
        self.dense = DenseLayer(flattened_size, 10) # 10 output classes

    def predict(self, X):
        """Performs a forward pass for prediction."""
        conv_out = self.conv.compute_forward_pass(X)
        activated = relu_func(conv_out)
        pooled = self.pool.compute_forward_pass(activated)
        flattened = pooled.reshape(pooled.shape[0], -1)
        logits = self.dense.compute_forward_pass(flattened)
        return softmax_func(logits)

# Example Usage
if __name__ == "__main__":
    # Instantiate the model
    model = SimpleCNN()
    # Create dummy input: 5 grayscale images of 28x28 pixels
    dummy_input = np.random.randn(5, 1, 28, 28).astype(np.float32)
    # Get predictions
    predictions = model.predict(dummy_input)
    print("Predicted class probabilities:\n", predictions)
    print("Shape of predictions:", predictions.shape)

The code provides a complete, runnable CNN with educational comments. This illustrates how LLMs can flatten the learning curve by generating foundational code for complex topics.

Financial Use Case: Augmented Search for Public Market Information

Retrieval-Augmented Generation (RAG) enhances LLM responses with external, relevant data. This example builds a financial analysis tool using Amazon Bedrock's Knowledge Bases and the Claude 3 Sonnet model to query Amazon's annual shareholder letters from 2019-2022.

The solution involves:

  1. Storing PDF documents in an S3 bucket and ingesting them into a Knowledge Base, which chunks the text and stores embeddings in a vector store (e.g., Amazon OpenSearch).
  2. Using the Bedrock Retrieve API to find document chunks relevant to a user query.
  3. Augmenting a prompt with the retrieved context and sending it to Claude 3 for a grounded response.

1. Using the Bedrock Retrieve API with Foundation Models

First, define a retrieval function and initialize the Knowledge Base ID.

import boto3
import json
import pprint

bedrock_agent_client = boto3.client('bedrock-agent-runtime', region_name='us-east-1')
kb_id = 'YOUR_KNOWLEDGE_BASE_ID'

def retrieve_from_kb(query_text, kb_id, max_results=5):
    """Retrieves relevant chunks from the Knowledge Base."""
    retrieval_config = {
        'vectorSearchConfiguration': {
            'numberOfResults': max_results,
            'overrideSearchType': 'SEMANTIC'
        }
    }
    response = bedrock_agent_client.retrieve(
        knowledgeBaseId=kb_id,
        retrievalQuery={'text': query_text},
        retrievalConfiguration=retrieval_config
    )
    return response

query = "What is Amazon's strategy regarding Generative AI?"
retrieval_response = retrieve_from_kb(query, kb_id, 5)
results = retrieval_response.get('retrievalResults', [])

# Extract the text content from results
retrieved_contexts = [res['content']['text'] for res in results]
pprint.pprint(retrieved_contexts[:2])  # Print first two chunks

Next, construct an augmented prompt and invoke the Claude 3 model.

bedrock_client = boto3.client('bedrock-runtime', region_name='us-east-1')

# Build the prompt with retrieved context
context_block = '\n'.join(retrieved_contexts)

enhanced_prompt = f"""\
Human: You are an AI financial analyst. Answer the question using only the factual information provided in the context below. If the context does not contain the answer, state that you do not know. Use specific figures or statistics when available.

<context>
{context_block}
</context>

<question>
{query}
</question>

Provide a concise, evidence-based answer.

Assistant:"""

# Prepare and send the request to Claude 3 Sonnet
model_arn = 'anthropic.claude-3-sonnet-20240229-v1:0'

request_body = {
    "anthropic_version": "bedrock-2023-05-31",
    "max_tokens": 512,
    "messages": [{"role": "user", "content": [{"type": "text", "text": enhanced_prompt}]}],
    "temperature": 0.5
}

response = bedrock_client.invoke_model(
    modelId=model_arn,
    body=json.dumps(request_body)
)
response_data = json.loads(response['body'].read())
final_answer = response_data['content'][0]['text']
print("\nClaude 3's Answer:")
print(final_answer)

2. Integration with LangChain

The LangChain framework simplifies building such RAG pipelines.

from langchain_community.chat_models import BedrockChat
from langchain.retrievers import AmazonKnowledgeBasesRetriever
from langchain.chains import RetrievalQA
from langchain.prompts import PromptTemplate

# Initialize the LLM and Retriever
llm = BedrockChat(
    model_id='anthropic.claude-3-sonnet-20240229-v1:0',
    client=bedrock_client,
    model_kwargs={'temperature': 0.5}
)

retriever = AmazonKnowledgeBasesRetriever(
    knowledge_base_id=kb_id,
    retrieval_config={
        "vectorSearchConfiguration": {"numberOfResults": 4}
    }
)

# Define a custom prompt template
qa_prompt_template = """\
Human: Use the following pieces of context to answer the question at the end. If you don't know the answer, just say so.

{context}

Question: {question}

Provide a detailed answer citing the source context.
Assistant:"""

PROMPT = PromptTemplate(
    template=qa_prompt_template, input_variables=["context", "question"]
)

# Create the RetrievalQA chain
qa_chain = RetrievalQA.from_chain_type(
    llm=llm,
    chain_type="stuff",
    retriever=retriever,
    return_source_documents=True,
    chain_type_kwargs={"prompt": PROMPT}
)

# Execute the query
query_result = qa_chain.invoke({"query": query})
print("Answer:", query_result['result'])
print("\nSources:")
for doc in query_result['source_documents']:
    print(f"- {doc.metadata.get('source', 'N/A')}")

Example Output for the query: "What is Amazon doing in the field of Generative AI?"

Based on the provided shareholder letters, Amazon is making substantial investments in Generative AI (GenAI) and large language models (LLMs), viewing them as transformative for customer experiences. Key initiatives include:
1. Developing its own large-scale LLMs for use across its consumer, seller, brand, and creator ecosystems.
2. Democratizing access via AWS, offering cost-effective machine learning chips (Trainium, Inferentia) for training and running LLMs.
3. Launching GenAI-powered applications like CodeWhisperer to boost developer productivity.
The company believes GenAI will be significant for its customers, shareholders, and its own future.

This implementation demonstrates RAG's power to ground LLM responses in authoritative, proprietary data, reducing hallucinations and increasing answer relevance for domain-specific queries.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.