Amazon_Support / app.py
shubham680's picture
Update app.py
c638051 verified
import streamlit as st
import json
from langchain_core.output_parsers import StrOutputParser, JsonOutputParser
from langchain_groq import ChatGroq
from langchain_core.runnables import RunnablePassthrough
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate
# -----------------------------
# Parsers
# -----------------------------
str_parser = StrOutputParser()
json_parser = JsonOutputParser()
# -----------------------------
# API KEY INPUT (for Hugging Face Spaces)
# -----------------------------
api_key = st.text_input("Enter GROQ API Key", type="password")
# -----------------------------
# PROMPTS (UNCHANGED)
# -----------------------------
thought_prompt = ChatPromptTemplate.from_messages([
SystemMessagePromptTemplate.from_template("You are a strict reasoning AI that follows policy provided exactly"),
HumanMessagePromptTemplate.from_template("""
Query:{query}
Policy:{policy}
Generate 3 different tree of reasoning paths.
Rules:
- Use ONLY the given policy
- Do NOT assume anything
- Each path must lead to a conclusion
Return JSON:
{{
"thoughts": [
{{"path": "Path 1", "reasoning": "...", "conclusion": "..."}},
{{"path": "Path 2", "reasoning": "...", "conclusion": "..."}},
{{"path": "Path 3", "reasoning": "...", "conclusion": "..."}}
]
}}
""")
])
best_path_prompt = ChatPromptTemplate.from_messages([
("system", "You are a strict evaluator that selects exactly ONE best answer based on policy and relevance."),
("human", """
Query: "{query}"
Policy: "{policy}"
Thoughts:
{thoughts}
Instructions:
1. Evaluate ALL reasoning paths.
2. First, eliminate any path that violates policy.
3. If multiple paths are policy-compliant:
- Compare them based on:
a) Direct relevance to the query
b) Completeness of reasoning
c) Clarity and specificity
4. You MUST select ONLY ONE best answer.
5. Do NOT return multiple answers.
6. Do NOT say "all are correct".
Final Rule:
Even if all options are correct, pick the MOST relevant and precise one.
Return JSON:
{{
"answer": "...",
"reason": "Explain why this was chosen over others"
}}
""")
])
verify_prompt = ChatPromptTemplate.from_messages([
("system", "You are a strict policy verifier. Reject anything that violates policy."),
("human", """
Query: "{query}"
Policy: "{policy}"
Answer: "{answer}"
Reason: "{reason}"
Verification Rules:
- Must strictly follow policy
- No assumptions allowed
- Must be logically consistent
Return JSON:
{{
"verified": true/false,
"confidence": "high/medium/low",
"final_answer": "..."
}}
""")
])
# -----------------------------
# Streamlit UI
# -----------------------------
st.title("🛒 Amazon Customer Support AI")
query = st.text_area("Enter Query")
policy = st.text_area("Enter Policy")
if st.button("Run"):
if not api_key:
st.warning("Please enter GROQ API Key")
elif not query or not policy:
st.warning("Please enter both query and policy")
else:
llm = ChatGroq(model="openai/gpt-oss-120b", api_key=api_key)
# Chains (UNCHANGED LOGIC)
final_thought_chain = thought_prompt | llm | json_parser
final_bp = best_path_prompt | llm | json_parser
final_verification = verify_prompt | llm | json_parser
final_chain = (
RunnablePassthrough.assign(
thoughts=final_thought_chain
)
| RunnablePassthrough.assign(
bp_output=final_bp
)
| {
"query": lambda x: x["query"],
"policy": lambda x: x["policy"],
"answer": lambda x: x["bp_output"]["answer"],
"reason": lambda x: x["bp_output"]["reason"],
}
| final_verification
)
with st.spinner("Processing..."):
result = final_chain.invoke({"query": query, "policy": policy})
st.subheader("✅ Final Output")
st.json(result)