Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import uuid
|
|
| 5 |
|
| 6 |
import pandas as pd
|
| 7 |
from datetime import datetime
|
|
|
|
| 8 |
|
| 9 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 10 |
from langchain_community.utilities.sql_database import SQLDatabase
|
|
@@ -290,20 +291,24 @@ Here’s a comprehensive system prompt for your e-commerce chatbot with clear in
|
|
| 290 |
You are an intelligent e-commerce chatbot designed to assist users with post-order queries. Gather necessary information from the user to help them with their query.
|
| 291 |
Use the following workflow to solve user queries. do not provide sql inputs to the sql tool - you only need to ask in natural language what information you need.
|
| 292 |
- If at any point you cannot determine the next steps - defer to human. you do not have clearance to go beyond the scope the following flow.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
### **Workflow:**
|
| 294 |
#### 1. **Cancel Order**
|
| 295 |
- Get necessary details and why customer wants to cancel.
|
| 296 |
- Retrieve cancellation fee, validity period, and amount for the product with `sql_tool`.
|
| 297 |
- Check If the cancellation meets the criteria - proceed to "Get Refund" flow.
|
| 298 |
#### 2. **Check Cancellation Fee**
|
| 299 |
-
-
|
| 300 |
#### 3. **Check Refund Policy**
|
| 301 |
-
- Provide the refund policy details
|
| 302 |
#### 4. **Get Invoice**
|
| 303 |
-
- Retrieve necessary details
|
| 304 |
- Provide the invoice information to the customer.
|
| 305 |
#### 5. **Get Refund**
|
| 306 |
-
- Retrieve necessary details using `sql_tool`.
|
| 307 |
- Retrieve cancellation fee, validity period, and amount for the product with `sql_tool`.
|
| 308 |
- Check If the cancellation meets the criteria: i.e,
|
| 309 |
if the order is within the validity period by checking days since purchase - use the days_since tool to calculate the number of days since delivery
|
|
@@ -311,15 +316,15 @@ Use the following workflow to solve user queries. do not provide sql inputs to t
|
|
| 311 |
if the customer is okay with the cancellation fee - do not proceed to cancel if the customer is not okay with the cancellation fee
|
| 312 |
- Call `cancel_order`.
|
| 313 |
- Process a refund using `process_refund` (output as JSON). refund amount should be product price - cancellation fee.
|
| 314 |
-
if it doesn't meet the criteria you do not have the clearance to process refund. defer to human.
|
|
|
|
| 315 |
- If at any point you cannot determine the next steps - defer to human.
|
| 316 |
#### 6. **Track Order/Track Refund**
|
| 317 |
-
-
|
| 318 |
-
- Retrieve the order or refund status using `sql_tool`.
|
| 319 |
-
- Provide the information to the customer.
|
| 320 |
#### 7. **Change Shipping Address**
|
| 321 |
-
- Retrieve necessary details
|
| 322 |
- Update the shipping address with `change_address`.
|
|
|
|
| 323 |
MANDATORY STEP:
|
| 324 |
After helping the customer with their concern,
|
| 325 |
- Ask if the customer needs help with anything else. If they ask for anything from the above list help them.
|
|
@@ -330,6 +335,7 @@ Once the customer confirms they do not need any further assistance:
|
|
| 330 |
- The category of the query (e.g., cancellation, refund, tracking, etc.).
|
| 331 |
- The feedback received.
|
| 332 |
- the rating given
|
|
|
|
| 333 |
---
|
| 334 |
### **Handling Out-of-Scope Queries:**
|
| 335 |
If the user's query, at any point is not covered by the workflows above:
|
|
@@ -338,6 +344,7 @@ If the user's query, at any point is not covered by the workflows above:
|
|
| 338 |
- End the conversation.
|
| 339 |
---
|
| 340 |
### **Important Notes for the Model:**
|
|
|
|
| 341 |
- Always aim to minimize the number of questions asked by retrieving as much information as possible from the database using `sql_tool` - retrieve customer information using any details provided and use it throughout the conversation.
|
| 342 |
- Follow the exact workflows for each query category.
|
| 343 |
- Ensure smooth transitions between steps, asking the customer if they need further assistance after resolving their issue.
|
|
@@ -369,17 +376,70 @@ agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
|
|
| 369 |
|
| 370 |
#================================LOGIN=========================================#
|
| 371 |
|
| 372 |
-
# Function to check login credentials
|
| 373 |
def check_login(email, phone):
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
|
| 384 |
# Function to process user input and generate a chatbot response
|
| 385 |
def chat_with_agent():
|
|
|
|
| 5 |
|
| 6 |
import pandas as pd
|
| 7 |
from datetime import datetime
|
| 8 |
+
import sqlite3
|
| 9 |
|
| 10 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 11 |
from langchain_community.utilities.sql_database import SQLDatabase
|
|
|
|
| 291 |
You are an intelligent e-commerce chatbot designed to assist users with post-order queries. Gather necessary information from the user to help them with their query.
|
| 292 |
Use the following workflow to solve user queries. do not provide sql inputs to the sql tool - you only need to ask in natural language what information you need.
|
| 293 |
- If at any point you cannot determine the next steps - defer to human. you do not have clearance to go beyond the scope the following flow.
|
| 294 |
+
|
| 295 |
+
Following is the information about the customer from the last 2 weeks:
|
| 296 |
+
{df}
|
| 297 |
+
If this information is not enough, fetch necessary information usign the sql_tool.
|
| 298 |
+
|
| 299 |
### **Workflow:**
|
| 300 |
#### 1. **Cancel Order**
|
| 301 |
- Get necessary details and why customer wants to cancel.
|
| 302 |
- Retrieve cancellation fee, validity period, and amount for the product with `sql_tool`.
|
| 303 |
- Check If the cancellation meets the criteria - proceed to "Get Refund" flow.
|
| 304 |
#### 2. **Check Cancellation Fee**
|
| 305 |
+
- Return the Cancellation Fee
|
| 306 |
#### 3. **Check Refund Policy**
|
| 307 |
+
- Provide the refund policy details.
|
| 308 |
#### 4. **Get Invoice**
|
| 309 |
+
- Retrieve necessary details.
|
| 310 |
- Provide the invoice information to the customer.
|
| 311 |
#### 5. **Get Refund**
|
|
|
|
| 312 |
- Retrieve cancellation fee, validity period, and amount for the product with `sql_tool`.
|
| 313 |
- Check If the cancellation meets the criteria: i.e,
|
| 314 |
if the order is within the validity period by checking days since purchase - use the days_since tool to calculate the number of days since delivery
|
|
|
|
| 316 |
if the customer is okay with the cancellation fee - do not proceed to cancel if the customer is not okay with the cancellation fee
|
| 317 |
- Call `cancel_order`.
|
| 318 |
- Process a refund using `process_refund` (output as JSON). refund amount should be product price - cancellation fee.
|
| 319 |
+
if it doesn't meet the criteria you do not have the clearance to process refund. defer to human.
|
| 320 |
+
Also, provide necessary information to the customer on why you took that decision. It is important to let the customer know why something is done.
|
| 321 |
- If at any point you cannot determine the next steps - defer to human.
|
| 322 |
#### 6. **Track Order/Track Refund**
|
| 323 |
+
- Return the order or refund status.
|
|
|
|
|
|
|
| 324 |
#### 7. **Change Shipping Address**
|
| 325 |
+
- Retrieve necessary details.
|
| 326 |
- Update the shipping address with `change_address`.
|
| 327 |
+
|
| 328 |
MANDATORY STEP:
|
| 329 |
After helping the customer with their concern,
|
| 330 |
- Ask if the customer needs help with anything else. If they ask for anything from the above list help them.
|
|
|
|
| 335 |
- The category of the query (e.g., cancellation, refund, tracking, etc.).
|
| 336 |
- The feedback received.
|
| 337 |
- the rating given
|
| 338 |
+
|
| 339 |
---
|
| 340 |
### **Handling Out-of-Scope Queries:**
|
| 341 |
If the user's query, at any point is not covered by the workflows above:
|
|
|
|
| 344 |
- End the conversation.
|
| 345 |
---
|
| 346 |
### **Important Notes for the Model:**
|
| 347 |
+
- Be empathetic to the customer but loyal to the instructions provided to you. Try to deescalate a situation before
|
| 348 |
- Always aim to minimize the number of questions asked by retrieving as much information as possible from the database using `sql_tool` - retrieve customer information using any details provided and use it throughout the conversation.
|
| 349 |
- Follow the exact workflows for each query category.
|
| 350 |
- Ensure smooth transitions between steps, asking the customer if they need further assistance after resolving their issue.
|
|
|
|
| 376 |
|
| 377 |
#================================LOGIN=========================================#
|
| 378 |
|
|
|
|
| 379 |
def check_login(email, phone):
|
| 380 |
+
try:
|
| 381 |
+
# Connect to the SQLite database
|
| 382 |
+
connection = sqlite3.connect("2911.db") # Replace with your .db file path
|
| 383 |
+
cursor = connection.cursor()
|
| 384 |
+
|
| 385 |
+
# Query to count matching customers
|
| 386 |
+
query = """
|
| 387 |
+
SELECT COUNT(*)
|
| 388 |
+
FROM customers
|
| 389 |
+
WHERE email = ? AND phone = ?;
|
| 390 |
+
"""
|
| 391 |
+
cursor.execute(query, (email, phone))
|
| 392 |
+
result = cursor.fetchone()
|
| 393 |
+
|
| 394 |
+
# Extract the count and return
|
| 395 |
+
customer_count = result[0] if result else 0
|
| 396 |
+
|
| 397 |
+
query = """
|
| 398 |
+
SELECT
|
| 399 |
+
c.customer_id,
|
| 400 |
+
c.first_name || ' ' || c.last_name AS customer_name,
|
| 401 |
+
c.email,
|
| 402 |
+
c.phone,
|
| 403 |
+
c.address AS customer_address,
|
| 404 |
+
o.order_id,
|
| 405 |
+
o.order_date,
|
| 406 |
+
o.status AS order_status,
|
| 407 |
+
o.price AS order_price,
|
| 408 |
+
p.name AS product_name,
|
| 409 |
+
p.price AS product_price,
|
| 410 |
+
i.invoice_date,
|
| 411 |
+
i.amount AS invoice_amount,
|
| 412 |
+
i.invoice_url,
|
| 413 |
+
s.delivery_date,
|
| 414 |
+
s.shipping_status,
|
| 415 |
+
s.shipping_address,
|
| 416 |
+
r.refund_amount,
|
| 417 |
+
r.refund_status
|
| 418 |
+
FROM Customers c
|
| 419 |
+
LEFT JOIN Orders o ON c.customer_id = o.customer_id
|
| 420 |
+
LEFT JOIN Products p ON o.product_id = p.product_id
|
| 421 |
+
LEFT JOIN Invoices i ON o.order_id = i.order_id
|
| 422 |
+
LEFT JOIN Shipping s ON o.order_id = s.order_id
|
| 423 |
+
LEFT JOIN Refund r ON o.order_id = r.order_id
|
| 424 |
+
WHERE o.order_date >= datetime('now', '-14 days')
|
| 425 |
+
ORDER BY o.order_date DESC;
|
| 426 |
+
"""
|
| 427 |
+
global df
|
| 428 |
+
# Execute the query and fetch data into a DataFrame
|
| 429 |
+
df = pd.read_sql_query(query, connection)
|
| 430 |
+
st.write(df)
|
| 431 |
+
|
| 432 |
+
return customer_count > 0 # True if at least 1 customer matches
|
| 433 |
|
| 434 |
+
except Exception as e:
|
| 435 |
+
print(f"Error: {e}")
|
| 436 |
+
return False
|
| 437 |
+
|
| 438 |
+
finally:
|
| 439 |
+
# Close the connection
|
| 440 |
+
if connection:
|
| 441 |
+
cursor.close()
|
| 442 |
+
connection.close()
|
| 443 |
|
| 444 |
# Function to process user input and generate a chatbot response
|
| 445 |
def chat_with_agent():
|