Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -162,45 +162,96 @@ def cancel_order(order_id, customer_id):
|
|
| 162 |
"customer_id": customer_id,
|
| 163 |
"action": "Order Canceled"
|
| 164 |
}
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
| 166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
-
@tool
|
| 169 |
-
def process_refund(order_id, refund_amount, reason):
|
| 170 |
-
"""
|
| 171 |
-
Logs the action of processing a refund for an order.
|
| 172 |
-
Args:
|
| 173 |
-
order_id (int): The unique ID of the order for which the refund is processed.
|
| 174 |
-
refund_amount (float): The amount to be refunded.
|
| 175 |
-
reason (str): The reason for initiating the refund.
|
| 176 |
-
Returns:
|
| 177 |
-
None: The function logs the action and does not return any value.
|
| 178 |
-
"""
|
| 179 |
-
details = {
|
| 180 |
-
"order_id": order_id,
|
| 181 |
-
"refund_amount": refund_amount,
|
| 182 |
-
"reason": reason,
|
| 183 |
-
"action": "Refund Processed"
|
| 184 |
-
}
|
| 185 |
-
log_action("process_refund", details)
|
| 186 |
|
| 187 |
|
| 188 |
-
@tool
|
| 189 |
def change_address(order_id, new_address):
|
| 190 |
"""
|
| 191 |
-
|
| 192 |
Args:
|
| 193 |
order_id (int): The unique ID of the order for which the address is being changed.
|
| 194 |
new_address (str): The new shipping address to be updated.
|
| 195 |
Returns:
|
| 196 |
-
|
| 197 |
"""
|
| 198 |
details = {
|
| 199 |
"order_id": order_id,
|
| 200 |
"new_address": new_address,
|
| 201 |
"action": "Address Changed"
|
| 202 |
}
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
@tool
|
| 206 |
def register_feedback(intent, customer_id, feedback, rating):
|
|
@@ -444,7 +495,7 @@ def chatbot_interface():
|
|
| 444 |
prompt = build_prompt(details)
|
| 445 |
|
| 446 |
|
| 447 |
-
tools = [sql_tool,defer_to_human,
|
| 448 |
|
| 449 |
chatbot = AzureChatOpenAI(
|
| 450 |
model_name=model_name,
|
|
|
|
| 162 |
"customer_id": customer_id,
|
| 163 |
"action": "Order Canceled"
|
| 164 |
}
|
| 165 |
+
try:
|
| 166 |
+
# Connect to the database
|
| 167 |
+
connection = sqlite3.connect("2911.db") # Replace with your .db file path
|
| 168 |
+
cursor = connection.cursor()
|
| 169 |
|
| 170 |
+
# Update the order status
|
| 171 |
+
query_order = """
|
| 172 |
+
UPDATE Orders
|
| 173 |
+
SET status = 'Canceled'
|
| 174 |
+
WHERE order_id = ? AND customer_id = ?;
|
| 175 |
+
"""
|
| 176 |
+
cursor.execute(query_order, (order_id, customer_id))
|
| 177 |
+
|
| 178 |
+
if cursor.rowcount == 0:
|
| 179 |
+
return f"No matching order found for Order ID {order_id} and Customer ID {customer_id}."
|
| 180 |
+
|
| 181 |
+
# Update the shipping status
|
| 182 |
+
query_shipping = """
|
| 183 |
+
UPDATE Shipping
|
| 184 |
+
SET shipping_status = 'Canceled'
|
| 185 |
+
WHERE order_id = ? AND customer_id = ?;
|
| 186 |
+
"""
|
| 187 |
+
cursor.execute(query_shipping, (order_id, customer_id))
|
| 188 |
+
|
| 189 |
+
cursor.execute("SELECT cancel_fee FROM Orders WHERE order_id = ?", (order_id,))
|
| 190 |
+
row = cursor.fetchone()
|
| 191 |
+
if not row:
|
| 192 |
+
cancel_fee = 0.0
|
| 193 |
+
|
| 194 |
+
cancel_fee = float(row[0])
|
| 195 |
+
|
| 196 |
+
# Insert refund details
|
| 197 |
+
query_refund = """
|
| 198 |
+
INSERT INTO Refund (order_id, cancellation_date, cancel_fee, refund_amount, refund_status)
|
| 199 |
+
VALUES (?, datetime('now'), ?, ?, 'Under Process');
|
| 200 |
+
"""
|
| 201 |
+
cursor.execute(query_refund, (order_id, cancel_fee, refund_amount))
|
| 202 |
+
|
| 203 |
+
connection.commit()
|
| 204 |
+
log_action("cancel_order", details) # Log the cancellation
|
| 205 |
+
return f"Order ID {order_id} for Customer ID {customer_id} has been canceled successfully. Shipping status updated."
|
| 206 |
+
|
| 207 |
+
except sqlite3.Error as e:
|
| 208 |
+
return f"An error occurred: {e}"
|
| 209 |
+
|
| 210 |
+
finally:
|
| 211 |
+
if connection:
|
| 212 |
+
connection.close()
|
| 213 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
|
|
|
|
| 216 |
def change_address(order_id, new_address):
|
| 217 |
"""
|
| 218 |
+
Updates the shipping address for a specific order in the database.
|
| 219 |
Args:
|
| 220 |
order_id (int): The unique ID of the order for which the address is being changed.
|
| 221 |
new_address (str): The new shipping address to be updated.
|
| 222 |
Returns:
|
| 223 |
+
str: A confirmation message indicating the update status.
|
| 224 |
"""
|
| 225 |
details = {
|
| 226 |
"order_id": order_id,
|
| 227 |
"new_address": new_address,
|
| 228 |
"action": "Address Changed"
|
| 229 |
}
|
| 230 |
+
|
| 231 |
+
try:
|
| 232 |
+
# Connect to the SQLite database
|
| 233 |
+
connection = sqlite3.connect("2911_updates.db") # Replace with your .db file path
|
| 234 |
+
cursor = connection.cursor()
|
| 235 |
+
|
| 236 |
+
# SQL query to update the address
|
| 237 |
+
query = """
|
| 238 |
+
UPDATE shipping
|
| 239 |
+
SET shipping_address = ?
|
| 240 |
+
WHERE order_id = ?;
|
| 241 |
+
"""
|
| 242 |
+
|
| 243 |
+
# Execute the query with placeholders
|
| 244 |
+
cursor.execute(query, (new_address, st.session_state.email, order_id))
|
| 245 |
+
connection.commit() # Commit the changes
|
| 246 |
+
|
| 247 |
+
return f"Update successful for Order ID {order_id} with new address: {new_address}"
|
| 248 |
+
|
| 249 |
+
except sqlite3.Error as e:
|
| 250 |
+
return f"An error occurred: {e}"
|
| 251 |
+
|
| 252 |
+
finally:
|
| 253 |
+
if connection:
|
| 254 |
+
connection.close()
|
| 255 |
|
| 256 |
@tool
|
| 257 |
def register_feedback(intent, customer_id, feedback, rating):
|
|
|
|
| 495 |
prompt = build_prompt(details)
|
| 496 |
|
| 497 |
|
| 498 |
+
tools = [sql_tool,defer_to_human, cancel_order, change_address, register_feedback, days_since]
|
| 499 |
|
| 500 |
chatbot = AzureChatOpenAI(
|
| 501 |
model_name=model_name,
|