Delete app (3).py
Browse files- app (3).py +0 -132
app (3).py
DELETED
|
@@ -1,132 +0,0 @@
|
|
| 1 |
-
{
|
| 2 |
-
"cells": [
|
| 3 |
-
{
|
| 4 |
-
"cell_type": "code",
|
| 5 |
-
"execution_count": null,
|
| 6 |
-
"id": "7351f47b-cce0-4e93-ad0c-8c0e43b1941f",
|
| 7 |
-
"metadata": {},
|
| 8 |
-
"outputs": [],
|
| 9 |
-
"source": [
|
| 10 |
-
"import re\n",
|
| 11 |
-
"import gradio as gr\n",
|
| 12 |
-
"import vanna as vn\n",
|
| 13 |
-
"from groq import Groq\n",
|
| 14 |
-
"from vanna.remote import VannaDefault\n",
|
| 15 |
-
"\n",
|
| 16 |
-
"# Set up Vanna.ai and Groq client as before\n",
|
| 17 |
-
"MY_VANNA_MODEL = \"llama3-8b\"\n",
|
| 18 |
-
"\n",
|
| 19 |
-
"vn = VannaDefault(model=MY_VANNA_MODEL, api_key='30efac58cfee46d1967e28b8d6bdf5db')\n",
|
| 20 |
-
"\n",
|
| 21 |
-
"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\n",
|
| 22 |
-
"\n",
|
| 23 |
-
"# Set up Groq client\n",
|
| 24 |
-
"groq_client = Groq(api_key=\"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\")\n",
|
| 25 |
-
"\n",
|
| 26 |
-
"def get_order_status(order_number):\n",
|
| 27 |
-
" sql = f\"SELECT know_history.status FROM know_history WHERE know_history.erpordernumber = {order_number};\"\n",
|
| 28 |
-
" result = vn.run_sql(sql)\n",
|
| 29 |
-
" # if result and len(result) > 0:\n",
|
| 30 |
-
" # return result['STATUS']\n",
|
| 31 |
-
" return result['status']\n",
|
| 32 |
-
"\n",
|
| 33 |
-
"def generate_response(user_input):\n",
|
| 34 |
-
" # Check for order number in the input\n",
|
| 35 |
-
" order_match = re.search(r'#?(\\d{5})', user_input)\n",
|
| 36 |
-
" \n",
|
| 37 |
-
" if order_match:\n",
|
| 38 |
-
" order_number = order_match.group(1)\n",
|
| 39 |
-
" status = get_order_status(order_number)\n",
|
| 40 |
-
" \n",
|
| 41 |
-
" # if status:\n",
|
| 42 |
-
" # Use Groq to generate a conversational response\n",
|
| 43 |
-
" prompt = f\"\"\"Given an order status '{status}' for order number {order_number}, \n",
|
| 44 |
-
" generate a friendly, conversational response to the customer's query: \"{user_input}\".\n",
|
| 45 |
-
" The response should be informative and reassuring.\"\"\"\n",
|
| 46 |
-
"\n",
|
| 47 |
-
" chat_completion = groq_client.chat.completions.create(\n",
|
| 48 |
-
" messages=[\n",
|
| 49 |
-
" {\n",
|
| 50 |
-
" \"role\": \"system\",\n",
|
| 51 |
-
" \"content\": \"You are a helpful customer service chatbot for an e-commerce company.\"\n",
|
| 52 |
-
" },\n",
|
| 53 |
-
" {\n",
|
| 54 |
-
" \"role\": \"user\",\n",
|
| 55 |
-
" \"content\": prompt,\n",
|
| 56 |
-
" }\n",
|
| 57 |
-
" ],\n",
|
| 58 |
-
" model=\"llama3-8b-8192\",\n",
|
| 59 |
-
" max_tokens=150,\n",
|
| 60 |
-
" temperature=0.7,\n",
|
| 61 |
-
" )\n",
|
| 62 |
-
" \n",
|
| 63 |
-
" return chat_completion.choices[0].message.content.strip()\n",
|
| 64 |
-
" # else:\n",
|
| 65 |
-
" # 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?\"\n",
|
| 66 |
-
" else:\n",
|
| 67 |
-
" # Handle general queries\n",
|
| 68 |
-
" prompt = f\"\"\"As a customer service chatbot for an e-commerce company, \n",
|
| 69 |
-
" provide a helpful response to the following customer query: \"{user_input}\".\"\"\"\n",
|
| 70 |
-
"\n",
|
| 71 |
-
" chat_completion = groq_client.chat.completions.create(\n",
|
| 72 |
-
" messages=[\n",
|
| 73 |
-
" {\n",
|
| 74 |
-
" \"role\": \"system\",\n",
|
| 75 |
-
" \"content\": \"You are a helpful customer service chatbot for an e-commerce company.\"\n",
|
| 76 |
-
" },\n",
|
| 77 |
-
" {\n",
|
| 78 |
-
" \"role\": \"user\",\n",
|
| 79 |
-
" \"content\": prompt,\n",
|
| 80 |
-
" }\n",
|
| 81 |
-
" ],\n",
|
| 82 |
-
" model=\"llama3-8b-8192\",\n",
|
| 83 |
-
" max_tokens=150,\n",
|
| 84 |
-
" temperature=0.7,\n",
|
| 85 |
-
" )\n",
|
| 86 |
-
" \n",
|
| 87 |
-
" return chat_completion.choices[0].message.content.strip()\n",
|
| 88 |
-
"\n",
|
| 89 |
-
"def chat_interface(message, history):\n",
|
| 90 |
-
" response = generate_response(message)\n",
|
| 91 |
-
" return response\n",
|
| 92 |
-
"\n",
|
| 93 |
-
"iface = gr.ChatInterface(\n",
|
| 94 |
-
" chat_interface,\n",
|
| 95 |
-
" title=\"E-Commerce Customer Service Chatbot\",\n",
|
| 96 |
-
" description=\"Ask about your order status or any other questions!\",\n",
|
| 97 |
-
" examples=[\n",
|
| 98 |
-
" \"Where is my order #12345?\",\n",
|
| 99 |
-
" \"What is the status of my order #67890?\",\n",
|
| 100 |
-
" \"How can I track my order?\",\n",
|
| 101 |
-
" \"Can I change my shipping address?\",\n",
|
| 102 |
-
" \"What's your return policy?\"\n",
|
| 103 |
-
" ]\n",
|
| 104 |
-
")\n",
|
| 105 |
-
"\n",
|
| 106 |
-
"if __name__ == \"__main__\":\n",
|
| 107 |
-
" iface.launch()"
|
| 108 |
-
]
|
| 109 |
-
}
|
| 110 |
-
],
|
| 111 |
-
"metadata": {
|
| 112 |
-
"kernelspec": {
|
| 113 |
-
"display_name": "Python 3 (ipykernel)",
|
| 114 |
-
"language": "python",
|
| 115 |
-
"name": "python3"
|
| 116 |
-
},
|
| 117 |
-
"language_info": {
|
| 118 |
-
"codemirror_mode": {
|
| 119 |
-
"name": "ipython",
|
| 120 |
-
"version": 3
|
| 121 |
-
},
|
| 122 |
-
"file_extension": ".py",
|
| 123 |
-
"mimetype": "text/x-python",
|
| 124 |
-
"name": "python",
|
| 125 |
-
"nbconvert_exporter": "python",
|
| 126 |
-
"pygments_lexer": "ipython3",
|
| 127 |
-
"version": "3.11.7"
|
| 128 |
-
}
|
| 129 |
-
},
|
| 130 |
-
"nbformat": 4,
|
| 131 |
-
"nbformat_minor": 5
|
| 132 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|