Seamlessly Integrating Streamlit with AWS Bedrock: Building an Authenticated RAG App for Northwind Orders
Introduction
In this tutorial, you’ll explore how to integrate Streamlit with AWS Bedrock while using Django authentication to secure access for authorized users.
This approach ensures that only authenticated users from your Django web application can interact with your Streamlit app and ask insightful questions about Northwind orders using natural language.
We’ve previously covered building a RAG (Retrieval-Augmented Generation) system to analyze Northwind orders in this article. Now, we’ll take it a step further by enabling seamless integration of the Django-authenticated users with a Streamlit-based conversational interface powered by AWS Bedrock.
Objective
The goal is to:
1) Authenticate users from the existing Django app to provide secure access to the Streamlit interface.
2) Enable users to query Northwind orders using natural language via AWS Bedrock.
3) Build an agentic app that can answer insightful questions such as:
Architecture Overview
Here’s how the system works:
2. Streamlit Interface
3. AWS Bedrock for NLP Queries
Step 1: Secure Authentication with Django
To begin, ensure that your Django app is set up with authentication and uses PostgreSQL as the backend. Then, integrate Django authentication with your Streamlit app using Django’s session authentication.
Step 2: Building the Streamlit Interface and Setting up Bedrock KB Access
Once authenticated, users see a Streamlit dashboard where they can ask questions about Northwind orders.
领英推荐
import boto3
import streamlit as st
import os
boto3.setup_default_session(
aws_access_key_id= os.getenv("AWS_ACCESS_KEY"),
aws_secret_access_key= os.getenv("AWS_SECRET_ACCESS"),
region_name= os.getenv("AWS_REGION")
)
st.subheader('RAG using Amazon Bedrock', divider='rainbow')
st.markdown(f"<span style='color:#4700b3;font-weight:bold'>Hi. I am Nora, your AI assistant. How can I help you today. </span>", unsafe_allow_html=True)
if 'chat_history' not in st.session_state:
st.session_state.chat_history = []
for message in st.session_state.chat_history:
with st.chat_message(message['role']):
st.markdown(message['text'])
bedrockClient = boto3.client('bedrock-agent-runtime', os.getenv("AWS_REGION"))
def getAnswers(questions):
knowledgeBaseResponse = bedrockClient.retrieve_and_generate(
input={'text': questions},
retrieveAndGenerateConfiguration={
'knowledgeBaseConfiguration': {
'knowledgeBaseId': f'{os.getenv("AWS_BEDROCK_KBID")}',
'modelArn': 'arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-instant-v1'
},
'type': 'KNOWLEDGE_BASE'
})
return knowledgeBaseResponse
questions = st.chat_input('Enter you questions here...')
if questions:
with st.chat_message('user'):
st.markdown(questions)
st.session_state.chat_history.append({"role":'user', "text":questions})
response = getAnswers(questions)
#st.write(response)
st.write(response['output']['text'])
Step 3: Querying Northwind Orders Using AWS Bedrock
The user’s query is sent to AWS Bedrock, which processes the query and returns insights.
Step 4: Analyzing and Displaying Insights
Once the data is retrieved, different types of visualizations can be generated dynamically using Matplotlib, Plotly, or Altair..
Key Use Cases and Questions
Users can now ask insightful questions about Northwind orders, such as:
1) Which regions receive the most shipments?
2) What are the top 5 countries where shipments are sent?
3) Trends in order volume over time.
Benefits of this Approach
1) Security First: Only authenticated Django users can access the Streamlit interface.
2) Natural Language Interface: Users can query complex order data intuitively.
3) Agentic App Experience: Users receive contextual responses powered by AWS Bedrock.
Conclusion
By integrating Streamlit with AWS Bedrock and Django authentication, you can build a secure, intelligent, and interactive analytics platform for querying Northwind orders.
This approach empowers users to derive actionable insights with ease while maintaining strict access control.