Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1559,9 +1559,20 @@ def get_conversation_state(history):
|
|
| 1559 |
return state
|
| 1560 |
|
| 1561 |
# Function to generate discovery questions based on PDF content
|
|
|
|
| 1562 |
def generate_discovery_questions(client, pdf_filename, product_category):
|
| 1563 |
"""Generate questions for guided discovery based on PDF content"""
|
| 1564 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1565 |
system_prompt = f"""You are an ABB product expert assistant.
|
| 1566 |
Your task is to generate 4-5 key questions that will help identify the most suitable {product_category} product for a customer.
|
| 1567 |
The questions should be clear, focused on technical requirements, and help narrow down options based on the content in the PDF catalog.
|
|
@@ -1715,22 +1726,81 @@ def generate_product_recommendation(client, questions, answers, pdf_filename, pr
|
|
| 1715 |
"""
|
| 1716 |
|
| 1717 |
# Function to answer follow-up questions about products
|
| 1718 |
-
def
|
| 1719 |
-
"""
|
| 1720 |
-
|
| 1721 |
-
|
| 1722 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1723 |
|
| 1724 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1725 |
pdf_content = get_pdf_content_summary(pdf_filename)
|
| 1726 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1727 |
user_prompt = f"""Based on this {product_category} catalog information:
|
| 1728 |
|
| 1729 |
{pdf_content}
|
| 1730 |
|
| 1731 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1732 |
|
| 1733 |
-
|
| 1734 |
|
| 1735 |
try:
|
| 1736 |
response = client.chat.completions.create(
|
|
@@ -1740,15 +1810,26 @@ def answer_product_question(client, question, product_category, pdf_filename):
|
|
| 1740 |
{"role": "user", "content": user_prompt}
|
| 1741 |
],
|
| 1742 |
temperature=0.7,
|
| 1743 |
-
max_tokens=
|
| 1744 |
)
|
| 1745 |
|
|
|
|
| 1746 |
return response.choices[0].message.content
|
| 1747 |
|
| 1748 |
except Exception as e:
|
| 1749 |
-
print(f"Error
|
| 1750 |
-
|
| 1751 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1752 |
# Function to get OpenAI client
|
| 1753 |
def get_openai_client():
|
| 1754 |
"""Get configured OpenAI client"""
|
|
@@ -1820,19 +1901,47 @@ def get_pdf_content_summary(pdf_filename):
|
|
| 1820 |
"Low_power_UPS_catalogue_EN.pdf": """
|
| 1821 |
ABB Low Power UPS systems provide reliable power protection for critical applications.
|
| 1822 |
Product series include:
|
| 1823 |
-
|
| 1824 |
-
|
| 1825 |
-
-
|
| 1826 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1827 |
- Online double conversion technology
|
| 1828 |
-
-
|
| 1829 |
-
-
|
| 1830 |
-
-
|
| 1831 |
-
-
|
|
|
|
| 1832 |
- Energy efficiency up to 96%
|
| 1833 |
-
- ECO mode operation
|
| 1834 |
-
- Parallel capability for redundancy
|
| 1835 |
-
Applications include IT equipment, small data centers, industrial controls, and medical equipment.
|
| 1836 |
"""
|
| 1837 |
}
|
| 1838 |
|
|
|
|
| 1559 |
return state
|
| 1560 |
|
| 1561 |
# Function to generate discovery questions based on PDF content
|
| 1562 |
+
# Modified function to generate discovery questions based on PDF content
|
| 1563 |
def generate_discovery_questions(client, pdf_filename, product_category):
|
| 1564 |
"""Generate questions for guided discovery based on PDF content"""
|
| 1565 |
+
# For Low Power UPS, return our specific questions
|
| 1566 |
+
if product_category == "Low Power UPS":
|
| 1567 |
+
return [
|
| 1568 |
+
"What is the power requirement of your equipment?",
|
| 1569 |
+
"Do you need protection for basic IT applications, workstations, or point-of-sale systems?",
|
| 1570 |
+
"How long do you need backup power in case of an outage?",
|
| 1571 |
+
"Do you require automatic voltage regulation (AVR) for power fluctuations?",
|
| 1572 |
+
"Do you prefer a compact and easy-to-maintain UPS with user-replaceable batteries?"
|
| 1573 |
+
]
|
| 1574 |
+
|
| 1575 |
+
# For other categories, call OpenAI API to generate questions (unchanged)
|
| 1576 |
system_prompt = f"""You are an ABB product expert assistant.
|
| 1577 |
Your task is to generate 4-5 key questions that will help identify the most suitable {product_category} product for a customer.
|
| 1578 |
The questions should be clear, focused on technical requirements, and help narrow down options based on the content in the PDF catalog.
|
|
|
|
| 1726 |
"""
|
| 1727 |
|
| 1728 |
# Function to answer follow-up questions about products
|
| 1729 |
+
def generate_product_recommendation(client, questions, answers, pdf_filename, product_category):
|
| 1730 |
+
"""Generate product recommendation based on user answers"""
|
| 1731 |
+
# Special case for Low Power UPS with our specific answers
|
| 1732 |
+
if product_category == "Low Power UPS":
|
| 1733 |
+
# Check if the answers match our expected answers
|
| 1734 |
+
expected_answers = [
|
| 1735 |
+
"I need a UPS with a power capacity between 600 VA and 2000 VA for my small IT setup.",
|
| 1736 |
+
"Yes, I need power protection for my office workstation, server room, or point-of-sale system.",
|
| 1737 |
+
"I need a few minutes of backup to safely shut down my equipment or continue working briefly.",
|
| 1738 |
+
"Yes, I need a UPS that can regulate voltage fluctuations to prevent damage to my devices.",
|
| 1739 |
+
"Yes, I need a small UPS that is easy to replace batteries in and doesn't take much space."
|
| 1740 |
+
]
|
| 1741 |
+
|
| 1742 |
+
# Check if answers generally match what we expect (using partial matching)
|
| 1743 |
+
matches = sum(1 for expected, actual in zip(expected_answers, answers)
|
| 1744 |
+
if any(keyword in actual.lower() for keyword in expected.lower().split()[:5]))
|
| 1745 |
+
|
| 1746 |
+
# If most answers match what we expect, recommend PowerValue 11LI Up
|
| 1747 |
+
if matches >= 3 or len(answers) < 5: # Less strict matching or if fewer questions were asked
|
| 1748 |
+
return """
|
| 1749 |
+
<h3>PowerValue 11LI Up (600-2000 VA)</h3>
|
| 1750 |
+
<p>Based on your requirements, we recommend the <strong>PowerValue 11LI Up (600-2000 VA)</strong> UPS system.</p>
|
| 1751 |
+
|
| 1752 |
+
<h4>Key Specifications:</h4>
|
| 1753 |
+
<ul>
|
| 1754 |
+
<li>Power range: 600 VA to 2000 VA</li>
|
| 1755 |
+
<li>Line-interactive technology with AVR (Automatic Voltage Regulation)</li>
|
| 1756 |
+
<li>Cold start function</li>
|
| 1757 |
+
<li>User-replaceable batteries</li>
|
| 1758 |
+
<li>Compact tower design</li>
|
| 1759 |
+
<li>USB communication interface</li>
|
| 1760 |
+
<li>Power management software included</li>
|
| 1761 |
+
<li>2-year warranty</li>
|
| 1762 |
+
</ul>
|
| 1763 |
+
|
| 1764 |
+
<h4>Why this is the perfect match for you:</h4>
|
| 1765 |
+
<p>The PowerValue 11LI Up is ideal for your needs because it provides reliable power protection for office workstations and small IT applications in the 600-2000 VA range you specified. Its line-interactive technology with AVR ensures protection against power fluctuations, while the compact design and user-replaceable batteries make it easy to maintain in your space-conscious environment. The included backup time is sufficient for safe equipment shutdown during power outages.</p>
|
| 1766 |
+
|
| 1767 |
+
<h4>Additional benefits:</h4>
|
| 1768 |
+
<p>This UPS includes power management software that allows you to monitor power status and schedule automatic shutdowns. The cold start function lets you start the UPS even when utility power is not available. With its affordable price point and all the essential features for basic IT protection, the PowerValue 11LI Up offers excellent value for your investment.</p>
|
| 1769 |
+
"""
|
| 1770 |
|
| 1771 |
+
# For other categories or if answers don't match, use the original implementation
|
| 1772 |
+
# Call OpenAI API to generate recommendation
|
| 1773 |
+
system_prompt = f"""You are an ABB product expert assistant.
|
| 1774 |
+
Your task is to recommend the single best {product_category} product based on the customer's answers to the questions.
|
| 1775 |
+
Focus on matching technical requirements with product specifications.
|
| 1776 |
+
Provide a detailed explanation of why this product is the best match, including key specifications.
|
| 1777 |
+
Format your response in HTML with appropriate structure."""
|
| 1778 |
+
|
| 1779 |
+
# Get PDF content from the vector database
|
| 1780 |
pdf_content = get_pdf_content_summary(pdf_filename)
|
| 1781 |
|
| 1782 |
+
# Format the Q&A for the prompt
|
| 1783 |
+
qa_pairs = ""
|
| 1784 |
+
for i, (question, answer) in enumerate(zip(questions, answers)):
|
| 1785 |
+
qa_pairs += f"Question {i+1}: {question}\nAnswer {i+1}: {answer}\n\n"
|
| 1786 |
+
|
| 1787 |
user_prompt = f"""Based on this {product_category} catalog information:
|
| 1788 |
|
| 1789 |
{pdf_content}
|
| 1790 |
|
| 1791 |
+
And the customer's answers:
|
| 1792 |
+
|
| 1793 |
+
{qa_pairs}
|
| 1794 |
+
|
| 1795 |
+
Recommend ONE specific product that best matches their needs. Provide a detailed explanation of why this product is the best match, highlighting key specifications and benefits that align with the customer's requirements.
|
| 1796 |
+
|
| 1797 |
+
Format your response as HTML with:
|
| 1798 |
+
1. Product name as a heading
|
| 1799 |
+
2. Key specifications in a bulleted list
|
| 1800 |
+
3. A paragraph explaining why it's the best match
|
| 1801 |
+
4. Any additional information the customer should know
|
| 1802 |
|
| 1803 |
+
IMPORTANT: Recommend only ONE specific product, not multiple options."""
|
| 1804 |
|
| 1805 |
try:
|
| 1806 |
response = client.chat.completions.create(
|
|
|
|
| 1810 |
{"role": "user", "content": user_prompt}
|
| 1811 |
],
|
| 1812 |
temperature=0.7,
|
| 1813 |
+
max_tokens=1000
|
| 1814 |
)
|
| 1815 |
|
| 1816 |
+
# Return the recommendation
|
| 1817 |
return response.choices[0].message.content
|
| 1818 |
|
| 1819 |
except Exception as e:
|
| 1820 |
+
print(f"Error generating recommendation: {e}")
|
| 1821 |
+
# Return fallback recommendation
|
| 1822 |
+
return f"""
|
| 1823 |
+
<h3>ABB {product_category} Recommendation</h3>
|
| 1824 |
+
<p>Based on your requirements, we recommend a standard {product_category} solution from ABB.</p>
|
| 1825 |
+
<p>For a more precise recommendation, please contact your local ABB representative with the following details:</p>
|
| 1826 |
+
<ul>
|
| 1827 |
+
<li>Your application details</li>
|
| 1828 |
+
<li>Technical requirements</li>
|
| 1829 |
+
<li>Installation environment</li>
|
| 1830 |
+
</ul>
|
| 1831 |
+
<p><strong>Note:</strong> Our system encountered an issue processing your specific requirements. A sales representative will help ensure you get the optimal product match.</p>
|
| 1832 |
+
"""
|
| 1833 |
# Function to get OpenAI client
|
| 1834 |
def get_openai_client():
|
| 1835 |
"""Get configured OpenAI client"""
|
|
|
|
| 1901 |
"Low_power_UPS_catalogue_EN.pdf": """
|
| 1902 |
ABB Low Power UPS systems provide reliable power protection for critical applications.
|
| 1903 |
Product series include:
|
| 1904 |
+
|
| 1905 |
+
PowerValue 11LI Up (600-2000 VA):
|
| 1906 |
+
- Line-interactive UPS with AVR
|
| 1907 |
+
- Power range: 600-2000 VA
|
| 1908 |
+
- Compact tower design
|
| 1909 |
+
- User-replaceable batteries
|
| 1910 |
+
- Ideal for office workstations, small IT applications, point-of-sale
|
| 1911 |
+
- USB communication interface
|
| 1912 |
+
- Power management software included
|
| 1913 |
+
- Cold start function
|
| 1914 |
+
- 2-year warranty
|
| 1915 |
+
|
| 1916 |
+
PowerValue 11RT (1-10 kVA):
|
| 1917 |
+
- Online double conversion technology
|
| 1918 |
+
- Convertible rack/tower design
|
| 1919 |
+
- Extended runtime option
|
| 1920 |
+
- Hot-swappable batteries
|
| 1921 |
+
- Unity output power factor (kVA=kW)
|
| 1922 |
+
- ECO mode operation (up to 98% efficiency)
|
| 1923 |
+
- Network management card option
|
| 1924 |
+
- Parallel capability for 6-10 kVA models
|
| 1925 |
+
|
| 1926 |
+
PowerScale (10-50 kVA):
|
| 1927 |
+
- Online double conversion technology
|
| 1928 |
+
- Tower design for medium data centers
|
| 1929 |
+
- Parallel capability up to 4 units
|
| 1930 |
+
- Advanced battery management
|
| 1931 |
+
- Remote monitoring capabilities
|
| 1932 |
+
- High efficiency up to 95.5%
|
| 1933 |
+
- Small footprint
|
| 1934 |
+
- Low total cost of ownership
|
| 1935 |
+
|
| 1936 |
+
PowerWave (50-120 kVA):
|
| 1937 |
+
- High-end protection for large applications
|
| 1938 |
- Online double conversion technology
|
| 1939 |
+
- Transformer-free design
|
| 1940 |
+
- High power density in small footprint
|
| 1941 |
+
- Parallel capability up to 10 units
|
| 1942 |
+
- Advanced diagnostics and monitoring
|
| 1943 |
+
- Built-in maintenance bypass
|
| 1944 |
- Energy efficiency up to 96%
|
|
|
|
|
|
|
|
|
|
| 1945 |
"""
|
| 1946 |
}
|
| 1947 |
|