File size: 2,043 Bytes
3b362b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
def empathetic_delay_response(order_id):
    return (
        f"I understand your concern. Order {order_id} is currently in progress. "
        "We’re doing our best to ensure it reaches you soon. Thank you for your patience!"
    )

def polite_update_response(order_id, status):
    return f"Order {order_id} status has been updated to {status}. Thank you for checking in."

def empathetic_response(user_role: str, query: str, answer: str) -> str:
    if user_role == "customer":
        return f"😊 Dear customer, regarding your request: *{query}* → {answer}"
    else:  # admin
        return f"👨‍💼 Admin, here’s what I found for your query: *{query}* → {answer}"
    

def format_response(df: pd.DataFrame, role: str = "customer") -> str:
    """
    Convert a query result DataFrame into a conversational response.
    Role-specific formatting:
    - Customers: polite & personal
    - Admin: concise & professional
    """
    if df is None or df.empty:
        return "I couldn’t find any matching records."

    if "error" in df.columns:
        return f"⚠️ Query error: {df['error'][0]}"

    # If single scalar result
    if df.shape == (1, 1):
        value = df.iloc[0, 0]
        if role == "customer":
            return f"Here’s the information you requested: {value}."
        else:
            return f"Result: {value}"

    # If few rows (<=5)
    if len(df) <= 5:
        rows = df.to_dict(orient="records")
        if role == "customer":
            response = "Here are the details I found for you:\n"
        else:
            response = "Query returned the following records:\n"
        for r in rows:
            response += " • " + ", ".join([f"{k}: {v}" for k, v in r.items()]) + "\n"
        return response.strip()

    # If many rows
    if role == "customer":
        return f"I found {len(df)} records. Here are the first few:\n{df.head(5).to_string(index=False)}"
    else:
        return f"{len(df)} rows returned. Sample:\n{df.head(5).to_string(index=False)}"