DEV Community

Cover image for Building a Personalized Study Companion Using Amazon Bedrock
omar-steam
omar-steam

Posted on • Edited on

Building a Personalized Study Companion Using Amazon Bedrock

I'm in my master's degree program right now, and I've always wanted to find ways to reduce my learning hours every day with work and helping my family. Voila! Here's my solution: creating a study companion using Amazon Bedrock.

Using Amazon Bedrock, which we will incorporate into the system, we will be able to access the capabilities of foundation models such as GPT-4 or T5.

Such models will assist in generating the AI which will be able to answer user-generated questions on a number of topics in my master's course such as quantum physics, and machine learning among others. We will explain how to take the model and make it even better, use prompt engineering for smarter answers, and deploy Retrieval-Augmented Generation for precise answers to students like myself.

So, Let's get into it!

First step: Creating your AWS development Environment

In order to start this project, make sure your AWS account is created with the important permissions for Amazon S3, Lambda and Bedrock because those are the tools you'll be working with (I learned that the hard way after I found out I had to put in my debit card for verification :( ).

Using the URL navigate to the S3 Console and create a new bucket with an example name such as “study-materials”. S3 is used to upload content related to Education. In my case, I generated additional synthetic data that would be appropriate for my master’s program. You can make your own needs or even add other datasets from the Kaggle website for example.

[
    {
        "topic": "Advanced Economics",
        "question": "How does the Lucas Critique challenge traditional macroeconomic policy analysis?",
        "answer": "The Lucas Critique argues that traditional macroeconomic models' parameters are not policy-invariant because economic agents adjust their behavior based on expected policy changes, making historical relationships unreliable for policy evaluation."
    },
    {
        "topic": "Quantum Physics",
        "question": "Explain quantum entanglement and its implications for quantum computing.",
        "answer": "Quantum entanglement is a physical phenomenon where pairs of particles remain fundamentally connected regardless of distance. This property enables quantum computers to perform certain calculations exponentially faster than classical computers through quantum parallelism and superdense coding."
    },
    {
        "topic": "Advanced Statistics",
        "question": "What is the difference between frequentist and Bayesian approaches to statistical inference?",
        "answer": "Frequentist inference treats parameters as fixed and data as random, using probability to describe long-run frequency of events. Bayesian inference treats parameters as random variables with prior distributions, updated through data to form posterior distributions, allowing direct probability statements about parameters."
    },
    {
        "topic": "Machine Learning",
        "question": "How do transformers solve the long-range dependency problem in sequence modeling?",
        "answer": "Transformers use self-attention mechanisms to directly model relationships between all positions in a sequence, eliminating the need for recurrent connections. This allows parallel processing and better capture of long-range dependencies through multi-head attention and positional encodings."
    },
    {
        "topic": "Molecular Biology",
        "question": "What are the implications of epigenetic inheritance for evolutionary theory?",
        "answer": "Epigenetic inheritance challenges the traditional neo-Darwinian model by demonstrating that heritable changes in gene expression can occur without DNA sequence alterations, suggesting a Lamarckian component to evolution through environmentally-induced modifications."
    },
    {
        "topic": "Advanced Computer Architecture",
        "question": "How do non-volatile memory architectures impact traditional memory hierarchy design?",
        "answer": "Non-volatile memory architectures blur the traditional distinction between storage and memory, enabling persistent memory systems that combine storage durability with memory-like performance, requiring fundamental redesign of memory hierarchies and system software."
    }
]
Enter fullscreen mode Exit fullscreen mode

Step 2: Utilise Amazon Bedrock

Launch Amazon Bedrock then:

Click on Link, Amazon Bedrock, and go to the Amazon Bedrock Console.
Start a new project to open your text generator of choice or choose from one of the available foundation models (For example, GPT-3 or T5).
Select your use case, in this comfort, it’s a study companion. Then select the Fine-tuning option and input the dataset (your educational content from S3) for fine-tuning.

Bedrock will be fine-tuned to set up the foundation model on your dataset. For example, if you are using GPT-3, Amazon Bedrock modifies it to learn the context of educational material and come up with the right answers to certain issues.

Here is a code quick snippet of the fine-tuned model below

import boto3

# Initialize Bedrock client
client = boto3.client("bedrock-runtime")

# Define S3 path for your dataset
dataset_path = 's3://study-materials/my-educational-dataset.json'

# Fine-tune the model
response = client.start_training(
    modelName="GPT-3",
    datasetLocation=dataset_path,
    trainingParameters={"batch_size": 16, "epochs": 5}
)
print(response)

Enter fullscreen mode Exit fullscreen mode

Save Fine-tuned Model: In the post fine-tuning stage, the model is saved and in a position to be deployed. It can be located in your Amazon S3 bucket in a new directory that will be named as fine-tuning-model.

Step 3: Implement RAG or Retrieval-Augmented Generation.

1. Create the AWS lambda function:

Lambda will process the request to the natural language understanding model to give back the proper response. This, in turn, will involve the Lambda function using the information the user has entered to search for relevant study material on S3 and then use RAG in the creation of an accurate response.

The Lambda code for generating answers: The code Below is an example of how you might configure the lambda function in order to use the fine tuned model for generating the answers needed:

import json
import boto3
from transformers import GPT2LMHeadModel, GPT2Tokenizer

s3 = boto3.client('s3')
model_s3_path = 's3://study-materials/fine-tuned-model'

# Load model and tokenizer
def load_model():
    s3.download_file(model_s3_path, 'model.pth')
    tokenizer = GPT2Tokenizer.from_pretrained('model.pth')
    model = GPT2LMHeadModel.from_pretrained('model.pth')
    return tokenizer, model

tokenizer, model = load_model()

def lambda_handler(event, context):
    query = event['query']
    topic = event['topic']

    # Retrieve relevant documents from S3 (RAG)
    retrieved_docs = retrieve_documents_from_s3(topic)

    # Generate response
    prompt = f"Topic: {topic}\nQuestion: {query}\nAnswer:"
    inputs = tokenizer(prompt, return_tensors="pt")
    outputs = model.generate(inputs['input_ids'], max_length=150)
    answer = tokenizer.decode(outputs[0], skip_special_tokens=True)

    return {
        'statusCode': 200,
        'body': json.dumps({'answer': answer})
    }

def retrieve_documents_from_s3(topic):
    # Fetch study materials related to the topic from S3
    # Your logic for document retrieval goes here
    pass

Enter fullscreen mode Exit fullscreen mode

3. Launch/deploy the lambda function: Delpoy that lambda function on AWS. It will be invoked through API Gateway to deal with the provocation of real-time user queries.

Step 4: Expose the model via an API Gateway

Create an API gateway then:

  • Go to the API gateway console and generate a new REST API.
  • Create a POST endpoint for your Lambda function that generates the answers.

Deploy the actual API

Deploy the API itself to make it public by pointing it to a custom domain or using any default URL they provide in AWS.

Last step: Building the Streamlit app (aka a backend developer's favourite tool)

And finally, build the Streamlit app that allows the user to interact and ask questions.

import streamlit as st
import requests

st.title("Personalized Study Companion")

topic = st.text_input("Enter Study Topic:")
query = st.text_input("Enter Your Question:")

if st.button("Generate Answer"):
    response = requests.post("https://your-api-endpoint", json={"topic": topic, "query": query})
    answer = response.json().get("answer")
    st.write(answer)

Enter fullscreen mode Exit fullscreen mode

You can host the Streamlit application on AWS E2 or Elastic Beanstalk

If everything works well congratulations. You just made your study companion. If I had to evaluate this project, I could add some more examples for my synthetic data (duh??) or get another educational dataset that perfectly aligns with my goals.

Thanks for reading! Let me know what do you think!

Top comments (0)