Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,44 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import pandas as pd
|
4 |
-
|
5 |
from dotenv import load_dotenv
|
|
|
6 |
|
7 |
# Load environment variables
|
8 |
load_dotenv()
|
9 |
|
10 |
-
#
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# Simple database using pandas DataFrames
|
14 |
class SimpleDatabase:
|
@@ -83,26 +113,24 @@ class QueryRouter:
|
|
83 |
def _classify_query(self, query):
|
84 |
"""Classify the query to determine which agent should handle it"""
|
85 |
# Use OpenAI to classify the query
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
temperature=0
|
102 |
-
)
|
103 |
|
104 |
# Extract the query type from the response
|
105 |
-
query_type = response
|
106 |
return query_type
|
107 |
|
108 |
def _extract_parameters(self, query, query_type):
|
@@ -122,15 +150,19 @@ class QueryRouter:
|
|
122 |
Example: {{"product_name": "laptop"}} or {{"date": "2025-04-29"}}
|
123 |
"""
|
124 |
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
|
|
|
|
|
|
|
|
134 |
|
135 |
# Parse the JSON response
|
136 |
import json
|
@@ -142,20 +174,17 @@ class QueryRouter:
|
|
142 |
|
143 |
def _handle_general_knowledge(self, query):
|
144 |
"""Handle general knowledge queries using OpenAI"""
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
)
|
157 |
-
|
158 |
-
return response.choices[0].message.content
|
159 |
|
160 |
def _format_response(self, query_type, data):
|
161 |
"""Format the response based on query type and data"""
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import pandas as pd
|
4 |
+
import json
|
5 |
from dotenv import load_dotenv
|
6 |
+
import httpx
|
7 |
|
8 |
# Load environment variables
|
9 |
load_dotenv()
|
10 |
|
11 |
+
# Get OpenAI API key
|
12 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
13 |
+
|
14 |
+
# Helper function for OpenAI API calls
|
15 |
+
def openai_chat_completion(messages, temperature=0, response_format=None, max_tokens=None):
|
16 |
+
"""Make a direct API call to OpenAI without using the client library"""
|
17 |
+
url = "https://api.openai.com/v1/chat/completions"
|
18 |
+
|
19 |
+
headers = {
|
20 |
+
"Content-Type": "application/json",
|
21 |
+
"Authorization": f"Bearer {api_key}"
|
22 |
+
}
|
23 |
+
|
24 |
+
payload = {
|
25 |
+
"model": "gpt-3.5-turbo",
|
26 |
+
"messages": messages,
|
27 |
+
"temperature": temperature
|
28 |
+
}
|
29 |
+
|
30 |
+
if response_format:
|
31 |
+
payload["response_format"] = response_format
|
32 |
+
|
33 |
+
if max_tokens:
|
34 |
+
payload["max_tokens"] = max_tokens
|
35 |
+
|
36 |
+
response = httpx.post(url, json=payload, headers=headers)
|
37 |
+
|
38 |
+
if response.status_code != 200:
|
39 |
+
raise Exception(f"OpenAI API error: {response.status_code} - {response.text}")
|
40 |
+
|
41 |
+
return response.json()
|
42 |
|
43 |
# Simple database using pandas DataFrames
|
44 |
class SimpleDatabase:
|
|
|
113 |
def _classify_query(self, query):
|
114 |
"""Classify the query to determine which agent should handle it"""
|
115 |
# Use OpenAI to classify the query
|
116 |
+
messages = [
|
117 |
+
{"role": "system", "content": """
|
118 |
+
You are a query classifier for a shop assistant system.
|
119 |
+
Classify customer queries into one of these categories:
|
120 |
+
- max_revenue_product: Questions about which product generated the most revenue (today or on a specific date)
|
121 |
+
- inventory_check: Questions about product availability or stock levels
|
122 |
+
- product_info: Questions about product details, pricing, etc.
|
123 |
+
- general_knowledge: Questions that require general knowledge not related to specific shop data
|
124 |
+
|
125 |
+
Return ONLY the category as a single word without any explanation.
|
126 |
+
"""},
|
127 |
+
{"role": "user", "content": query}
|
128 |
+
]
|
129 |
+
|
130 |
+
response = openai_chat_completion(messages, temperature=0)
|
|
|
|
|
131 |
|
132 |
# Extract the query type from the response
|
133 |
+
query_type = response["choices"][0]["message"]["content"].strip().lower()
|
134 |
return query_type
|
135 |
|
136 |
def _extract_parameters(self, query, query_type):
|
|
|
150 |
Example: {{"product_name": "laptop"}} or {{"date": "2025-04-29"}}
|
151 |
"""
|
152 |
|
153 |
+
messages = [
|
154 |
+
{"role": "system", "content": "You extract parameters from customer queries for a shop assistant."},
|
155 |
+
{"role": "user", "content": prompt_content}
|
156 |
+
]
|
157 |
+
|
158 |
+
response = openai_chat_completion(messages, temperature=0, response_format={"type": "json_object"})
|
159 |
+
|
160 |
+
# Parse the JSON response
|
161 |
+
try:
|
162 |
+
parameters = json.loads(response["choices"][0]["message"]["content"])
|
163 |
+
return parameters
|
164 |
+
except json.JSONDecodeError:
|
165 |
+
return {}
|
166 |
|
167 |
# Parse the JSON response
|
168 |
import json
|
|
|
174 |
|
175 |
def _handle_general_knowledge(self, query):
|
176 |
"""Handle general knowledge queries using OpenAI"""
|
177 |
+
messages = [
|
178 |
+
{"role": "system", "content": """
|
179 |
+
You are a helpful assistant for a shop. Answer the customer's question
|
180 |
+
using your general knowledge. Keep answers brief and focused.
|
181 |
+
"""},
|
182 |
+
{"role": "user", "content": query}
|
183 |
+
]
|
184 |
+
|
185 |
+
response = openai_chat_completion(messages, temperature=0.7, max_tokens=150)
|
186 |
+
|
187 |
+
return response["choices"][0]["message"]["content"]
|
|
|
|
|
|
|
188 |
|
189 |
def _format_response(self, query_type, data):
|
190 |
"""Format the response based on query type and data"""
|