File size: 4,015 Bytes
ad7b14b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)