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|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import json
|
| 3 |
+
from langchain_core.output_parsers import StrOutputParser, JsonOutputParser
|
| 4 |
+
from langchain_groq import ChatGroq
|
| 5 |
+
from langchain_core.runnables import RunnablePassthrough
|
| 6 |
+
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate
|
| 7 |
+
|
| 8 |
+
# -----------------------------
|
| 9 |
+
# Parsers
|
| 10 |
+
# -----------------------------
|
| 11 |
+
str_parser = StrOutputParser()
|
| 12 |
+
json_parser = JsonOutputParser()
|
| 13 |
+
|
| 14 |
+
# -----------------------------
|
| 15 |
+
# API KEY INPUT (for Hugging Face Spaces)
|
| 16 |
+
# -----------------------------
|
| 17 |
+
api_key = st.text_input("Enter GROQ API Key", type="password")
|
| 18 |
+
|
| 19 |
+
# -----------------------------
|
| 20 |
+
# PROMPTS (UNCHANGED)
|
| 21 |
+
# -----------------------------
|
| 22 |
+
thought_prompt = ChatPromptTemplate.from_messages([
|
| 23 |
+
SystemMessagePromptTemplate.from_template("You are a strict reasoning AI that follows policy provided exactly"),
|
| 24 |
+
HumanMessagePromptTemplate.from_template("""
|
| 25 |
+
Query:{query}
|
| 26 |
+
Policy:{policy}
|
| 27 |
+
|
| 28 |
+
Generate 3 different tree of reasoning paths.
|
| 29 |
+
|
| 30 |
+
Rules:
|
| 31 |
+
- Use ONLY the given policy
|
| 32 |
+
- Do NOT assume anything
|
| 33 |
+
- Each path must lead to a conclusion
|
| 34 |
+
|
| 35 |
+
Return JSON:
|
| 36 |
+
{{
|
| 37 |
+
"thoughts": [
|
| 38 |
+
{{"path": "Path 1", "reasoning": "...", "conclusion": "..."}},
|
| 39 |
+
{{"path": "Path 2", "reasoning": "...", "conclusion": "..."}},
|
| 40 |
+
{{"path": "Path 3", "reasoning": "...", "conclusion": "..."}}
|
| 41 |
+
]
|
| 42 |
+
}}
|
| 43 |
+
""")
|
| 44 |
+
])
|
| 45 |
+
|
| 46 |
+
best_path_prompt = ChatPromptTemplate.from_messages([
|
| 47 |
+
("system", "You are a strict evaluator that selects exactly ONE best answer based on policy and relevance."),
|
| 48 |
+
("human", """
|
| 49 |
+
Query: "{query}"
|
| 50 |
+
Policy: "{policy}"
|
| 51 |
+
|
| 52 |
+
Thoughts:
|
| 53 |
+
{thoughts}
|
| 54 |
+
|
| 55 |
+
Instructions:
|
| 56 |
+
1. Evaluate ALL reasoning paths.
|
| 57 |
+
2. First, eliminate any path that violates policy.
|
| 58 |
+
3. If multiple paths are policy-compliant:
|
| 59 |
+
- Compare them based on:
|
| 60 |
+
a) Direct relevance to the query
|
| 61 |
+
b) Completeness of reasoning
|
| 62 |
+
c) Clarity and specificity
|
| 63 |
+
4. You MUST select ONLY ONE best answer.
|
| 64 |
+
5. Do NOT return multiple answers.
|
| 65 |
+
6. Do NOT say "all are correct".
|
| 66 |
+
|
| 67 |
+
Final Rule:
|
| 68 |
+
Even if all options are correct, pick the MOST relevant and precise one.
|
| 69 |
+
|
| 70 |
+
Return JSON:
|
| 71 |
+
{{
|
| 72 |
+
"answer": "...",
|
| 73 |
+
"reason": "Explain why this was chosen over others"
|
| 74 |
+
}}
|
| 75 |
+
""")
|
| 76 |
+
])
|
| 77 |
+
|
| 78 |
+
verify_prompt = ChatPromptTemplate.from_messages([
|
| 79 |
+
("system", "You are a strict policy verifier. Reject anything that violates policy."),
|
| 80 |
+
("human", """
|
| 81 |
+
Query: "{query}"
|
| 82 |
+
Policy: "{policy}"
|
| 83 |
+
|
| 84 |
+
Answer: "{answer}"
|
| 85 |
+
Reason: "{reason}"
|
| 86 |
+
|
| 87 |
+
Verification Rules:
|
| 88 |
+
- Must strictly follow policy
|
| 89 |
+
- No assumptions allowed
|
| 90 |
+
- Must be logically consistent
|
| 91 |
+
|
| 92 |
+
Return JSON:
|
| 93 |
+
{{
|
| 94 |
+
"verified": true/false,
|
| 95 |
+
"confidence": "high/medium/low",
|
| 96 |
+
"final_answer": "..."
|
| 97 |
+
}}
|
| 98 |
+
""")
|
| 99 |
+
])
|
| 100 |
+
|
| 101 |
+
# -----------------------------
|
| 102 |
+
# Streamlit UI
|
| 103 |
+
# -----------------------------
|
| 104 |
+
|
| 105 |
+
st.title("🛒 Amazon Customer Support AI")
|
| 106 |
+
|
| 107 |
+
query = st.text_area("Enter Query")
|
| 108 |
+
policy = st.text_area("Enter Policy")
|
| 109 |
+
|
| 110 |
+
if st.button("Run"):
|
| 111 |
+
if not api_key:
|
| 112 |
+
st.warning("Please enter GROQ API Key")
|
| 113 |
+
elif not query or not policy:
|
| 114 |
+
st.warning("Please enter both query and policy")
|
| 115 |
+
else:
|
| 116 |
+
llm = ChatGroq(model="openai/gpt-oss-120b", api_key=api_key)
|
| 117 |
+
|
| 118 |
+
# Chains (UNCHANGED LOGIC)
|
| 119 |
+
final_thought_chain = thought_prompt | llm | json_parser
|
| 120 |
+
final_bp = best_path_prompt | llm | json_parser
|
| 121 |
+
final_verification = verify_prompt | llm | json_parser
|
| 122 |
+
|
| 123 |
+
final_chain = (
|
| 124 |
+
RunnablePassthrough.assign(
|
| 125 |
+
thoughts=final_thought_chain
|
| 126 |
+
)
|
| 127 |
+
| RunnablePassthrough.assign(
|
| 128 |
+
bp_output=final_bp
|
| 129 |
+
)
|
| 130 |
+
| {
|
| 131 |
+
"query": lambda x: x["query"],
|
| 132 |
+
"policy": lambda x: x["policy"],
|
| 133 |
+
"answer": lambda x: x["bp_output"]["answer"],
|
| 134 |
+
"reason": lambda x: x["bp_output"]["reason"],
|
| 135 |
+
}
|
| 136 |
+
| final_verification
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
with st.spinner("Processing..."):
|
| 140 |
+
result = final_chain.invoke({"query": query, "policy": policy})
|
| 141 |
+
|
| 142 |
+
st.subheader("✅ Final Output")
|
| 143 |
+
st.json(result)
|
| 144 |
+
|
| 145 |
+
# -----------------------------
|
| 146 |
+
# Hugging Face Instructions
|
| 147 |
+
# -----------------------------
|
| 148 |
+
|
| 149 |
+
st.markdown("""
|
| 150 |
+
---
|
| 151 |
+
### 🚀 Deploy on Hugging Face Spaces
|
| 152 |
+
|
| 153 |
+
1. Create Streamlit Space
|
| 154 |
+
2. Upload this as app.py
|
| 155 |
+
3. Add requirements.txt:
|
| 156 |
+
|
| 157 |
+
```
|
| 158 |
+
streamlit
|
| 159 |
+
langchain
|
| 160 |
+
langchain-groq
|
| 161 |
+
```
|
| 162 |
+
|
| 163 |
+
4. Add GROQ API key in UI while running
|
| 164 |
+
""")
|