Sush commited on
Commit ·
c98c28c
1
Parent(s): c5af905
Added reasoning to response
Browse files- agents/rag_agent.py +24 -6
- app.py +19 -1
agents/rag_agent.py
CHANGED
|
@@ -49,7 +49,11 @@ def load_rag_agent(vectorstore_path: str = "vectorstore/"):
|
|
| 49 |
|
| 50 |
# Load FAISS index
|
| 51 |
if os.path.exists("vectorstore/index.faiss"):
|
| 52 |
-
vectorstore = FAISS.load_local(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
else:
|
| 54 |
documents = load_documents() #PDF loader
|
| 55 |
|
|
@@ -75,9 +79,22 @@ def load_rag_agent(vectorstore_path: str = "vectorstore/"):
|
|
| 75 |
|
| 76 |
# Grounded prompt
|
| 77 |
prompt_template = """You are a helpful HDFC Bank policy assistant.
|
|
|
|
| 78 |
Use ONLY the context below to answer the customer's question.
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
Context:
|
| 83 |
{context}
|
|
@@ -93,18 +110,19 @@ Answer:"""
|
|
| 93 |
|
| 94 |
def format_docs(docs):
|
| 95 |
formatted = []
|
| 96 |
-
sources =
|
| 97 |
|
| 98 |
for doc in docs:
|
| 99 |
source = doc.metadata.get("source", "Unknown")
|
| 100 |
filename = os.path.basename(source)
|
| 101 |
|
| 102 |
-
sources.
|
| 103 |
formatted.append(doc.page_content)
|
| 104 |
|
| 105 |
context = "\n\n".join(formatted)
|
| 106 |
|
| 107 |
-
|
|
|
|
| 108 |
|
| 109 |
return context + source_text
|
| 110 |
|
|
|
|
| 49 |
|
| 50 |
# Load FAISS index
|
| 51 |
if os.path.exists("vectorstore/index.faiss"):
|
| 52 |
+
vectorstore = FAISS.load_local(
|
| 53 |
+
"vectorstore",
|
| 54 |
+
embeddings,
|
| 55 |
+
allow_dangerous_deserialization=True
|
| 56 |
+
)
|
| 57 |
else:
|
| 58 |
documents = load_documents() #PDF loader
|
| 59 |
|
|
|
|
| 79 |
|
| 80 |
# Grounded prompt
|
| 81 |
prompt_template = """You are a helpful HDFC Bank policy assistant.
|
| 82 |
+
|
| 83 |
Use ONLY the context below to answer the customer's question.
|
| 84 |
+
|
| 85 |
+
IMPORTANT:
|
| 86 |
+
- Always include the sources at the end of your answer.
|
| 87 |
+
- Also include a short explanation titled "Why this answer?"
|
| 88 |
+
- Explain briefly how the answer was derived from context
|
| 89 |
+
- The sources are provided in the context.
|
| 90 |
+
- Format sources exactly like this:
|
| 91 |
+
|
| 92 |
+
Sources:
|
| 93 |
+
- file1.pdf
|
| 94 |
+
- file2.pdf
|
| 95 |
+
|
| 96 |
+
If the answer is not in the context, say:
|
| 97 |
+
"I don't have enough information in the policy documents to answer this. Please contact HDFC Bank directly."
|
| 98 |
|
| 99 |
Context:
|
| 100 |
{context}
|
|
|
|
| 110 |
|
| 111 |
def format_docs(docs):
|
| 112 |
formatted = []
|
| 113 |
+
sources = []
|
| 114 |
|
| 115 |
for doc in docs:
|
| 116 |
source = doc.metadata.get("source", "Unknown")
|
| 117 |
filename = os.path.basename(source)
|
| 118 |
|
| 119 |
+
sources.append(filename)
|
| 120 |
formatted.append(doc.page_content)
|
| 121 |
|
| 122 |
context = "\n\n".join(formatted)
|
| 123 |
|
| 124 |
+
unique_sources = list(set(sources))
|
| 125 |
+
source_text = "\n\nSources:\n" + "\n".join(f"- {s}" for s in unique_sources)
|
| 126 |
|
| 127 |
return context + source_text
|
| 128 |
|
app.py
CHANGED
|
@@ -87,7 +87,25 @@ if query := st.chat_input("Ask your banking question here..."):
|
|
| 87 |
else:
|
| 88 |
st.caption(" Answered by: Data Agent (SQL)")
|
| 89 |
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
# Save assistant message
|
| 93 |
st.session_state.messages.append({
|
|
|
|
| 87 |
else:
|
| 88 |
st.caption(" Answered by: Data Agent (SQL)")
|
| 89 |
|
| 90 |
+
if "Sources:" in response:
|
| 91 |
+
answer, sources = response.split("Sources:")
|
| 92 |
+
|
| 93 |
+
# Show answer
|
| 94 |
+
st.markdown(answer)
|
| 95 |
+
|
| 96 |
+
# Show sources nicely
|
| 97 |
+
st.markdown("### Sources")
|
| 98 |
+
for s in sources.strip().split("\n"):
|
| 99 |
+
if s.strip():
|
| 100 |
+
st.markdown(f"- {s.replace('-', '').strip()}")
|
| 101 |
+
else:
|
| 102 |
+
st.markdown(response)
|
| 103 |
+
|
| 104 |
+
if "Why this answer?" in response:
|
| 105 |
+
explanation = response.split("Why this answer?")[1]
|
| 106 |
+
|
| 107 |
+
st.markdown("### Why this answer?")
|
| 108 |
+
st.markdown(explanation)
|
| 109 |
|
| 110 |
# Save assistant message
|
| 111 |
st.session_state.messages.append({
|