Skip to content

Commit

Permalink
feat: implement knowledge base loading from JSON file and adjust resp…
Browse files Browse the repository at this point in the history
…onse thresholds
  • Loading branch information
CrzyHAX91 committed Jan 9, 2025
1 parent afb033f commit fe3268a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 27 deletions.
27 changes: 12 additions & 15 deletions ai_helpdesk.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from fuzzywuzzy import fuzz
import random
from textblob import TextBlob
import json

class AdvancedAIHelpdesk:
def __init__(self):
Expand All @@ -11,19 +12,15 @@ def __init__(self):
self.knowledge_base = self.load_knowledge_base()

def load_knowledge_base(self):
# Placeholder for loading a more comprehensive knowledge base
return {
"track|tracking|find|locate order": "To track your order, please follow these steps:\n1. Log in to your account on our website.\n2. Go to the 'Order History' section.\n3. Find your order and click on 'Track Order'.\n4. You'll see the current status and estimated delivery date of your order.",
"delay|delayed|late order": "If your order is delayed, please follow these steps:\n1. Check your order status on our website.\n2. If it's been more than 7 days past the estimated delivery date, please contact our customer support team.\n3. Provide your order number when contacting us for faster assistance.",
"cancel|cancellation|stop order": "To cancel your order:\n1. Log in to your account on our website.\n2. Go to 'Order History' and find the order you want to cancel.\n3. If the order hasn't been shipped yet, you should see a 'Cancel Order' button.\n4. Click the button and confirm the cancellation.\n5. If you don't see the cancel button, the order may have already been shipped. In this case, please contact our customer support team for assistance.",
"return policy|returns|refund": "Our return policy allows returns within 30 days of receiving your order. Please ensure the item is unused and in its original packaging. To initiate a return, log in to your account and go to the 'Returns' section.",
"shipping time|delivery time|how long|when will I receive": "Shipping times vary depending on your location and the shipping method chosen. Typically, orders are processed within 1-2 business days, and shipping can take 3-7 business days for standard shipping, or 1-3 business days for express shipping.",
"international shipping|ship to other countries": "Yes, we offer international shipping to many countries. Shipping costs and delivery times may vary depending on the destination. Please check our shipping information page or contact customer support for specific details about shipping to your country.",
"payment methods|how to pay": "We accept various payment methods including credit/debit cards (Visa, MasterCard, American Express), PayPal, and Apple Pay. You can select your preferred payment method during checkout.",
"size guide|sizing": "You can find our size guide on the product page of each item. Click on the 'Size Guide' link to view detailed measurements and find the best fit for you.",
"product availability|in stock": "Product availability is shown on each item's page. If an item is out of stock, you can sign up for email notifications to be alerted when it's back in stock.",
"discount|coupon|promo code": "To use a discount or promo code, enter it in the designated field during checkout. Make sure to check the terms and conditions of the coupon, as some may have restrictions or expiration dates."
}
try:
with open('knowledge_base.json', 'r') as file:
return json.load(file)
except FileNotFoundError:
print("Knowledge base file not found. Please ensure it exists.")
return {}
except json.JSONDecodeError:
print("Error decoding the knowledge base JSON file. Please check the file format.")
return {}

def generate_response(self, prompt, user_id):
prompt_lower = prompt.lower()
Expand All @@ -37,7 +34,7 @@ def generate_response(self, prompt, user_id):
best_score = score
best_match = response

if best_score >= 80:
if best_score >= 70: # Adjusted threshold
self.context.append(prompt)
response = self.personalize_response(best_match, user_id)
follow_up = self.generate_follow_up(prompt_lower)
Expand Down Expand Up @@ -84,7 +81,7 @@ def average_feedback(self):

def analyze_sentiment(self, prompt):
analysis = TextBlob(prompt)
if analysis.sentiment.polarity < -0.3: # Lowered threshold
if analysis.sentiment.polarity < -0.2: # Adjusted threshold
return "I apologize for any inconvenience. Would you like me to connect you with a human customer service representative?"
return None

Expand Down
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.8'

services:
web:
build: .
Expand Down
12 changes: 12 additions & 0 deletions knowledge_base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"track|tracking|find|locate order": "To track your order, please follow these steps:\n1. Log in to your account on our website.\n2. Go to the 'Order History' section.\n3. Find your order and click on 'Track Order'.\n4. You'll see the current status and estimated delivery date of your order.",
"delay|delayed|late order": "If your order is delayed, please follow these steps:\n1. Check your order status on our website.\n2. If it's been more than 7 days past the estimated delivery date, please contact our customer support team.\n3. Provide your order number when contacting us for faster assistance.",
"cancel|cancellation|stop order": "To cancel your order:\n1. Log in to your account on our website.\n2. Go to 'Order History' and find the order you want to cancel.\n3. If the order hasn't been shipped yet, you should see a 'Cancel Order' button.\n4. Click the button and confirm the cancellation.\n5. If you don't see the cancel button, the order may have already been shipped. In this case, please contact our customer support team for assistance.",
"return policy|returns|refund": "Our return policy allows returns within 30 days of receiving your order. Please ensure the item is unused and in its original packaging. To initiate a return, log in to your account and go to the 'Returns' section.",
"shipping time|delivery time|how long|when will I receive": "Shipping times vary depending on your location and the shipping method chosen. Typically, orders are processed within 1-2 business days, and shipping can take 3-7 business days for standard shipping, or 1-3 business days for express shipping.",
"international shipping|ship to other countries": "Yes, we offer international shipping to many countries. Shipping costs and delivery times may vary depending on the destination. Please check our shipping information page or contact customer support for specific details about shipping to your country.",
"payment methods|how to pay": "We accept various payment methods including credit/debit cards (Visa, MasterCard, American Express), PayPal, and Apple Pay. You can select your preferred payment method during checkout.",
"size guide|sizing": "You can find our size guide on the product page of each item. Click on the 'Size Guide' link to view detailed measurements and find the best fit for you.",
"product availability|in stock": "Product availability is shown on each item's page. If an item is out of stock, you can sign up for email notifications to be alerted when it's back in stock.",
"discount|coupon|promo code": "To use a discount or promo code, enter it in the designated field during checkout. Make sure to check the terms and conditions of the coupon, as some may have restrictions or expiration dates."
}
10 changes: 4 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
Django==5.1.4
Django==4.2.16
django-allauth==65.3.0
Pillow==11.0.0
python-dotenv==1.0.1
aliexpress-api-python==1.0.0

fuzzywuzzy
python-Levenshtein
textblob
fuzzywuzzy==0.18.0 # Pinning version
python-Levenshtein==0.12.2 # Pinning version
textblob==0.15.3 # Pinning version
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# Navigate to the project directory
cd /home/user/dropshipv2
cd "C:/Users/Behee/Desktop/Dropship dbug/dropshipv2"

# Activate virtual environment (if you're using one)
# source /path/to/your/virtualenv/bin/activate
Expand Down
14 changes: 11 additions & 3 deletions scheduler.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import time
import subprocess
import os
import sys
import logging
from datetime import datetime, timedelta

# Configure logging
logging.basicConfig(level=logging.INFO)

def run_sync_command():
manage_py_path = os.path.join(os.path.dirname(__file__), 'dropship_project', 'manage.py')
subprocess.run(['python3', manage_py_path, 'sync_aliexpress_products'])
try:
subprocess.run([sys.executable, manage_py_path, 'sync_aliexpress_products'], check=True)
except subprocess.CalledProcessError as e:
logging.error(f"Error running sync command: {e}")

def main():
while True:
now = datetime.now()
next_run = now.replace(hour=0, minute=0, second=0, microsecond=0) + timedelta(days=1)
time_to_wait = (next_run - now).total_seconds()

print(f"Next sync scheduled at {next_run}")
logging.info(f"Next sync scheduled at {next_run}")
time.sleep(time_to_wait)

print("Running AliExpress product sync...")
logging.info("Running AliExpress product sync...")
run_sync_command()

if __name__ == "__main__":
Expand Down

0 comments on commit fe3268a

Please sign in to comment.