File size: 3,545 Bytes
601e113
 
db4bb05
601e113
 
807d6fe
 
 
1d85e16
 
807d6fe
 
 
601e113
 
807d6fe
 
 
 
 
601e113
807d6fe
 
 
 
601e113
807d6fe
 
 
 
 
 
 
 
 
 
 
601e113
 
807d6fe
601e113
807d6fe
db4bb05
 
807d6fe
 
 
 
601e113
 
807d6fe
 
 
 
 
 
601e113
 
 
 
807d6fe
601e113
 
 
807d6fe
db4bb05
601e113
807d6fe
 
601e113
 
807d6fe
 
 
 
 
601e113
807d6fe
 
601e113
 
 
807d6fe
 
 
 
 
 
601e113
 
807d6fe
 
 
 
601e113
807d6fe
601e113
 
807d6fe
 
 
601e113
807d6fe
601e113
807d6fe
 
 
 
 
 
601e113
db4bb05
 
601e113
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
import os
import re
import gradio as gr

from langchain_groq import ChatGroq
from langchain_core.messages import SystemMessage

# ✅ Correct imports for SQLDatabase + Toolkit (HF-safe)
from langchain_community.utilities import SQLDatabase
from langchain_community.agent_toolkits import SQLDatabaseToolkit

# ✅ Correct import path for create_sql_agent (NOT langchain.agents)
from langchain_community.agent_toolkits.sql.base import create_sql_agent


# -----------------------------
# Config
# -----------------------------
MODEL_NAME = os.getenv("GROQ_MODEL", "meta-llama/llama-3.1-8b-instant")
GROQ_API_KEY = os.getenv("GROQ_API_KEY")

if not GROQ_API_KEY:
    raise ValueError(
        "❌ GROQ_API_KEY not found. Add it in Hugging Face: Settings → Secrets → GROQ_API_KEY"
    )

DB_PATH = "customer_orders.db"  # must be in same Space repo
DB_URI = f"sqlite:///{DB_PATH}"

if not os.path.exists(DB_PATH):
    raise FileNotFoundError(
        f"❌ Database file '{DB_PATH}' not found. Make sure customer_orders.db is committed to the Space repo."
    )

# -----------------------------
# LLM
# -----------------------------
llm = ChatGroq(
    model=MODEL_NAME,
    temperature=0,      # deterministic
    max_tokens=300,
    groq_api_key=GROQ_API_KEY
)

# -----------------------------
# Database + SQL Agent
# -----------------------------
db = SQLDatabase.from_uri(DB_URI)
toolkit = SQLDatabaseToolkit(db=db, llm=llm)

system_message = SystemMessage(content=
    "You are FoodHub customer support. "
    "You can query the database safely using SQL tools. "
    "Rules: (1) Never return bulk data. (2) Only answer for a single order_id. "
    "(3) If order_id missing, ask for it. (4) Keep replies short and professional."
)

sql_agent = create_sql_agent(
    llm=llm,
    toolkit=toolkit,
    verbose=False,  # set True only if you want logs
    system_message=system_message
)

ORDER_ID_PATTERN = re.compile(r"\bO\d{5}\b", re.IGNORECASE)

def extract_order_id(text: str):
    m = ORDER_ID_PATTERN.search(text or "")
    return m.group(0).upper() if m else None


# -----------------------------
# Chatbot logic
# -----------------------------
def respond(user_message, history):
    user_message = (user_message or "").strip()

    # 1) If order id not provided, ask for it
    order_id = extract_order_id(user_message)
    if not order_id:
        return "Please provide your Order ID (example: O12488)."

    # 2) Ask SQL agent for that one order only (safe query)
    # Keep query explicit to avoid the agent doing broad selects
    agent_query = (
        f"Retrieve all columns for order_id '{order_id}' from the orders table. "
        f"Then summarize the order status and payment status in 2-3 short sentences."
    )

    try:
        result = sql_agent.invoke({"input": agent_query})
        # result is usually dict with "output"
        output_text = result.get("output", str(result))
        return output_text
    except Exception as e:
        return f"Sorry — I couldn’t fetch that order right now. Please try again. (Error: {type(e).__name__})"


# -----------------------------
# Gradio UI
# -----------------------------
demo = gr.ChatInterface(
    fn=respond,
    title="FoodHub – AI Powered Food Delivery Chatbot",
    description="Ask order-related questions. Include your Order ID like O12488.",
    examples=[
        "Where is my order? O12488",
        "What is the payment status of order O12488?",
        "I want to cancel my order O12487"
    ],
)

if __name__ == "__main__":
    demo.launch()