Upload App.py
Browse files
App.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
import gradio as gr
|
3 |
+
import vanna as vn
|
4 |
+
from groq import Groq
|
5 |
+
from vanna.remote import VannaDefault
|
6 |
+
|
7 |
+
# Set up Vanna.ai and Groq client as before
|
8 |
+
MY_VANNA_MODEL = "llama3-8b"
|
9 |
+
|
10 |
+
vn = VannaDefault(model=MY_VANNA_MODEL, api_key='30efac58cfee46d1967e28b8d6bdf5db')
|
11 |
+
|
12 |
+
vn.connect_to_mssql(odbc_conn_str=r'DRIVER={ODBC Driver 17 for SQL Server};SERVER=YISC1100715LT\SQLEXPRESS;DATABASE=master;Trusted_Connection=yes;') # Connect to your database
|
13 |
+
|
14 |
+
# Set up Groq client
|
15 |
+
groq_client = Groq(api_key="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")
|
16 |
+
|
17 |
+
def get_order_status(order_number):
|
18 |
+
sql = f"SELECT know_history.status FROM know_history WHERE know_history.erpordernumber = {order_number};"
|
19 |
+
result = vn.run_sql(sql)
|
20 |
+
# if result and len(result) > 0:
|
21 |
+
# return result['STATUS']
|
22 |
+
return result['status']
|
23 |
+
|
24 |
+
def generate_response(user_input):
|
25 |
+
# Check for order number in the input
|
26 |
+
order_match = re.search(r'#?(\d{5})', user_input)
|
27 |
+
|
28 |
+
if order_match:
|
29 |
+
order_number = order_match.group(1)
|
30 |
+
status = get_order_status(order_number)
|
31 |
+
|
32 |
+
# if status:
|
33 |
+
# Use Groq to generate a conversational response
|
34 |
+
prompt = f"""Given an order status '{status}' for order number {order_number},
|
35 |
+
generate a friendly, conversational response to the customer's query: "{user_input}".
|
36 |
+
The response should be informative and reassuring."""
|
37 |
+
|
38 |
+
chat_completion = groq_client.chat.completions.create(
|
39 |
+
messages=[
|
40 |
+
{
|
41 |
+
"role": "system",
|
42 |
+
"content": "You are a helpful customer service chatbot for an e-commerce company."
|
43 |
+
},
|
44 |
+
{
|
45 |
+
"role": "user",
|
46 |
+
"content": prompt,
|
47 |
+
}
|
48 |
+
],
|
49 |
+
model="llama3-8b-8192",
|
50 |
+
max_tokens=150,
|
51 |
+
temperature=0.7,
|
52 |
+
)
|
53 |
+
|
54 |
+
return chat_completion.choices[0].message.content.strip()
|
55 |
+
# else:
|
56 |
+
# return f"I'm sorry, but I couldn't find any information for order #{order_number}. Could you please check if the order number is correct?"
|
57 |
+
else:
|
58 |
+
# Handle general queries
|
59 |
+
prompt = f"""As a customer service chatbot for an e-commerce company,
|
60 |
+
provide a helpful response to the following customer query: "{user_input}"."""
|
61 |
+
|
62 |
+
chat_completion = groq_client.chat.completions.create(
|
63 |
+
messages=[
|
64 |
+
{
|
65 |
+
"role": "system",
|
66 |
+
"content": "You are a helpful customer service chatbot for an e-commerce company."
|
67 |
+
},
|
68 |
+
{
|
69 |
+
"role": "user",
|
70 |
+
"content": prompt,
|
71 |
+
}
|
72 |
+
],
|
73 |
+
model="llama3-8b-8192",
|
74 |
+
max_tokens=150,
|
75 |
+
temperature=0.7,
|
76 |
+
)
|
77 |
+
|
78 |
+
return chat_completion.choices[0].message.content.strip()
|
79 |
+
|
80 |
+
def chat_interface(message, history):
|
81 |
+
response = generate_response(message)
|
82 |
+
return response
|
83 |
+
|
84 |
+
iface = gr.ChatInterface(
|
85 |
+
chat_interface,
|
86 |
+
title="E-Commerce Customer Service Chatbot",
|
87 |
+
description="Ask about your order status or any other questions!",
|
88 |
+
examples=[
|
89 |
+
"Where is my order #12345?",
|
90 |
+
"What is the status of my order #67890?",
|
91 |
+
"How can I track my order?",
|
92 |
+
"Can I change my shipping address?",
|
93 |
+
"What's your return policy?"
|
94 |
+
]
|
95 |
+
)
|
96 |
+
|
97 |
+
if __name__ == "__main__":
|
98 |
+
iface.launch()
|