Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -25,53 +25,51 @@ class PDFChatbot:
|
|
25 |
relevant_chunks = [chunk.page_content for chunk in relevant_chunks]
|
26 |
return "\n\n".join(relevant_chunks)
|
27 |
def chat_with_pdf(self, user_question: str, pdf_content: str) -> str:
|
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 |
-
"content": f"""Insurance Document Content:
|
53 |
{relevant_context}
|
54 |
Customer Question: {user_question}
|
55 |
Please provide a helpful response based on the insurance document content above."""
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
return f"Error generating response: {str(e)}"
|
75 |
def main():
|
76 |
# st.set_page_config(page_title="Insurance PDF Chatbot", page_icon="🛡️", layout="wide")
|
77 |
st.title("🛡️ Insurance Policy Assistant")
|
@@ -88,7 +86,7 @@ def main():
|
|
88 |
st.session_state.chatbot.conversation_history = []
|
89 |
st.session_state.chat_history = []
|
90 |
st.rerun()
|
91 |
-
|
92 |
if st.session_state.pdf_processed:
|
93 |
st.header("💬 Ask About Your Insurance Policy")
|
94 |
# Display chat history
|
@@ -97,7 +95,7 @@ def main():
|
|
97 |
st.markdown(f"**You:** {question}")
|
98 |
st.markdown(f"**Insurance Assistant:** {answer}")
|
99 |
st.divider()
|
100 |
-
|
101 |
user_question = st.chat_input("Hãy đặt những câu hỏi về hợp đồng bảo hiểm cơ bản...")
|
102 |
if user_question:
|
103 |
with st.spinner("Analyzing your policy..."):
|
|
|
25 |
relevant_chunks = [chunk.page_content for chunk in relevant_chunks]
|
26 |
return "\n\n".join(relevant_chunks)
|
27 |
def chat_with_pdf(self, user_question: str, pdf_content: str) -> str:
|
28 |
+
"""Generate response using Azure OpenAI based on PDF content and user question."""
|
29 |
+
# Split PDF content into chunks
|
30 |
+
# Get relevant context for the question
|
31 |
+
relevant_context = self.get_relevant_context(user_question)
|
32 |
+
# Prepare messages for the chat
|
33 |
+
messages = [
|
34 |
+
{
|
35 |
+
"role": "system",
|
36 |
+
"content": """You are an experienced insurance agent assistant who helps customers understand their insurance policies and coverage details. Follow these guidelines:
|
37 |
+
1. Only provide information based on the PDF content provided
|
38 |
+
2. If the answer is not in the PDF, clearly state that the information is not available in the document
|
39 |
+
3. Provide clear, concise, and helpful responses in a professional manner
|
40 |
+
4. Always respond in Vietnamese using proper grammar and formatting
|
41 |
+
5. When possible, reference specific sections or clauses from the policy
|
42 |
+
6. Use insurance terminology appropriately but explain complex terms when necessary
|
43 |
+
7. Be empathetic and patient, as insurance can be confusing for customers
|
44 |
+
8. If asked about claims, coverage limits, deductibles, or policy terms, provide accurate information from the document
|
45 |
+
9. Always prioritize customer understanding and satisfaction
|
46 |
+
10. If multiple interpretations are possible, explain the different scenarios clearly
|
47 |
+
Remember: You are here to help customers understand their insurance coverage better."""
|
48 |
+
},
|
49 |
+
{
|
50 |
+
"role": "user",
|
51 |
+
"content": f"""Insurance Document Content:
|
|
|
52 |
{relevant_context}
|
53 |
Customer Question: {user_question}
|
54 |
Please provide a helpful response based on the insurance document content above."""
|
55 |
+
}
|
56 |
+
]
|
57 |
+
# Add conversation history
|
58 |
+
for msg in self.conversation_history[-2:]: # Keep last 6 messages for context
|
59 |
+
messages.append(msg)
|
60 |
+
# Get response from Azure OpenAI
|
61 |
+
response = self.azure_client.chat.completions.create(
|
62 |
+
model="gpt-4o-mini",
|
63 |
+
messages=messages,
|
64 |
+
max_tokens=300, #TODO
|
65 |
+
temperature=0.7
|
66 |
+
)
|
67 |
+
bot_response = response.choices[0].message.content
|
68 |
+
# Update conversation history
|
69 |
+
self.conversation_history.append({"role": "user", "content": user_question})
|
70 |
+
self.conversation_history.append({"role": "assistant", "content": bot_response})
|
71 |
+
return bot_response
|
72 |
+
|
|
|
73 |
def main():
|
74 |
# st.set_page_config(page_title="Insurance PDF Chatbot", page_icon="🛡️", layout="wide")
|
75 |
st.title("🛡️ Insurance Policy Assistant")
|
|
|
86 |
st.session_state.chatbot.conversation_history = []
|
87 |
st.session_state.chat_history = []
|
88 |
st.rerun()
|
89 |
+
# Main chat interface
|
90 |
if st.session_state.pdf_processed:
|
91 |
st.header("💬 Ask About Your Insurance Policy")
|
92 |
# Display chat history
|
|
|
95 |
st.markdown(f"**You:** {question}")
|
96 |
st.markdown(f"**Insurance Assistant:** {answer}")
|
97 |
st.divider()
|
98 |
+
# Chat input
|
99 |
user_question = st.chat_input("Hãy đặt những câu hỏi về hợp đồng bảo hiểm cơ bản...")
|
100 |
if user_question:
|
101 |
with st.spinner("Analyzing your policy..."):
|