-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
152 lines (123 loc) · 5.11 KB
/
app.py
File metadata and controls
152 lines (123 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import streamlit as st
import boto3
import json
from typing import Dict, List, Any, Optional
# Configure Streamlit page
st.set_page_config(
page_title="Panther Pathfinder - AI Knowledge Assistant",
page_icon="🐾",
layout="wide"
)
# Bedrock client
bedrock_client = boto3.client('bedrock-agent-runtime', region_name='us-west-2')
def query_knowledge_base(client, query: str) -> Optional[Dict[str, Any]]:
"""Query the knowledge base using retrieve_and_generate"""
try:
response = bedrock_client.retrieve_and_generate(
input={
'text': query
},
retrieveAndGenerateConfiguration={
'knowledgeBaseConfiguration': {
'knowledgeBaseId': "Y4NJOU25DB",
'modelArn': 'arn:aws:bedrock:us-west-2::foundation-model/anthropic.claude-3-5-sonnet-20240620-v1:0',
},
'type': 'KNOWLEDGE_BASE'
}
)
return response
except Exception as e:
st.error(f"Error querying knowledge base: {str(e)}")
return None
def display_sources(citations: List[Dict]) -> None:
"""Display source citations in an expandable section"""
if not citations:
return
with st.expander("📚 Sources", expanded=False):
for i, citation in enumerate(citations):
st.write(f"**Source {i+1}:**")
# Display retrieved text
if 'retrievedReferences' in citation:
for ref in citation['retrievedReferences']:
if 'content' in ref and 'text' in ref['content']:
st.write(f"Content: {ref['content']['text'][:200]}...")
if 'location' in ref and 's3Location' in ref['location']:
st.write(f"Document: {ref['location']['s3Location']['uri']}")
st.write("---")
def initialize_session():
"""Initialize session state variables"""
if "messages" not in st.session_state:
st.session_state.messages = []
def display_chat_history():
"""Display the chat message history"""
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Show sources for assistant messages
if message["role"] == "assistant" and "citations" in message:
display_sources(message["citations"])
def main():
"""Main application function"""
# Page header
st.title("🐾 Panther Pathfinder")
st.subheader("AI-Powered Knowledge Assistant")
# Initialize session
initialize_session()
# Sidebar for configuration
with st.sidebar:
st.header("⚙️ Settings")
# Clear chat button
if st.button("🗑️ Clear Chat"):
st.session_state.messages = []
st.rerun()
# Configuration info
st.markdown("---")
st.markdown("**Current Settings:**")
st.write("Region: us-west-2")
st.write("Model: Claude 3.5 Sonnet")
st.write("Knowledge Base ID: Y4NJOU25DB")
# Display chat history
display_chat_history()
# Chat input
if user_input := st.chat_input("Ask me anything about your knowledge base..."):
# Add user message
st.session_state.messages.append({
"role": "user",
"content": user_input
})
# Display user message
with st.chat_message("user"):
st.markdown(user_input)
# Generate and display assistant response
with st.chat_message("assistant"):
with st.spinner("Searching knowledge base..."):
# Query the knowledge base
response = query_knowledge_base(
bedrock_client,
user_input
)
if response and 'output' in response and 'text' in response['output']:
# Extract the generated text
answer = response['output']['text']
# Display the response
st.markdown(answer)
# Get citations if available
citations = response.get('citations', [])
# Add to message history
st.session_state.messages.append({
"role": "assistant",
"content": answer,
"citations": citations
})
# Display sources
if citations:
display_sources(citations)
else:
error_msg = "I couldn't generate a response. Please try rephrasing your question."
st.error(error_msg)
st.session_state.messages.append({
"role": "assistant",
"content": error_msg
})
if __name__ == "__main__":
main()