Spaces:
Sleeping
Sleeping
Gộp một số endpoint vào /chat
Browse files
app.py
CHANGED
|
@@ -66,8 +66,13 @@ class StateManager:
|
|
| 66 |
"""Clear all conversation history and reset state via API"""
|
| 67 |
if state.session_id is not None and API_BASE_URL:
|
| 68 |
try:
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
except Exception as e:
|
| 72 |
print(f"Warning: clear_memory failed: {e}")
|
| 73 |
|
|
@@ -79,15 +84,7 @@ class StateManager:
|
|
| 79 |
@staticmethod
|
| 80 |
def change_model(state: ConversationState, model_name: str):
|
| 81 |
"""Change the selected model"""
|
| 82 |
-
|
| 83 |
-
try:
|
| 84 |
-
requests.post(f"{API_BASE_URL}/set_model",
|
| 85 |
-
json={"model_name": model_name})
|
| 86 |
-
state.selected_model = model_name
|
| 87 |
-
except Exception as e:
|
| 88 |
-
print(f"Warning: set_model failed: {e}")
|
| 89 |
-
else:
|
| 90 |
-
state.selected_model = model_name
|
| 91 |
|
| 92 |
@staticmethod
|
| 93 |
def toggle_product_model_search(state: ConversationState):
|
|
@@ -117,46 +114,64 @@ class ChatService:
|
|
| 117 |
# Call API
|
| 118 |
try:
|
| 119 |
if image_path:
|
|
|
|
| 120 |
with open(image_path, 'rb') as f:
|
| 121 |
files = {"image": f}
|
| 122 |
data = {
|
| 123 |
"message": message,
|
| 124 |
"product_model_search": str(state.product_model_search).lower(),
|
| 125 |
-
"session_id": state.session_id
|
|
|
|
|
|
|
| 126 |
}
|
| 127 |
resp = requests.post(
|
| 128 |
-
f"{API_BASE_URL}/chat_with_image",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
else:
|
| 130 |
-
|
|
|
|
| 131 |
"message": message,
|
| 132 |
"session_id": state.session_id,
|
| 133 |
"debug": "Normal",
|
| 134 |
-
"product_model_search": state.product_model_search
|
|
|
|
| 135 |
}
|
| 136 |
-
resp = requests.post(
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
if resp.status_code == 200:
|
| 140 |
j = resp.json()
|
| 141 |
response = j.get("response", "")
|
| 142 |
specs_advantages = j.get("specs_advantages")
|
| 143 |
solution_packages = j.get("solution_packages")
|
| 144 |
-
raw_documents = j.get("raw_documents")
|
| 145 |
outputs = j.get("outputs")
|
| 146 |
else:
|
|
|
|
| 147 |
response = f"Error: API status {resp.status_code}"
|
| 148 |
specs_advantages, solution_packages, raw_documents, outputs = None, None, None, None
|
| 149 |
except Exception as e:
|
|
|
|
| 150 |
response = f"Error calling API: {e}"
|
| 151 |
specs_advantages, solution_packages, raw_documents, outputs = None, None, None, None
|
| 152 |
|
| 153 |
end = time.perf_counter()
|
| 154 |
|
| 155 |
# Update state
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
# Filter products based on query
|
| 162 |
if state.specs_advantages is not None:
|
|
@@ -211,7 +226,7 @@ class DisplayService:
|
|
| 211 |
|
| 212 |
# Handle both products and solution packages
|
| 213 |
if url:
|
| 214 |
-
full_name = f"**[{data['name']} {model}]({url})**"
|
| 215 |
else:
|
| 216 |
full_name = f"**{data['name']} {model}**"
|
| 217 |
|
|
@@ -296,41 +311,6 @@ class DisplayService:
|
|
| 296 |
markdown_table = df.to_markdown(index=False)
|
| 297 |
return f"📄 **Thông số kỹ thuật**\n\n{markdown_table}"
|
| 298 |
|
| 299 |
-
# @staticmethod
|
| 300 |
-
# def show_advantages(state: ConversationState) -> str:
|
| 301 |
-
# """Generate advantages table"""
|
| 302 |
-
# specs_map = state.specs_advantages
|
| 303 |
-
# columns = ["Tên", "Ưu điểm nổi trội"]
|
| 304 |
-
# table_data = []
|
| 305 |
-
|
| 306 |
-
# if not specs_map:
|
| 307 |
-
# return "💡 **Ưu điểm nổi trội**\n\nKhông có ưu điểm nào."
|
| 308 |
-
|
| 309 |
-
# for prod_id, data in specs_map.items():
|
| 310 |
-
# adv = data.get("advantages", "Không có ưu điểm")
|
| 311 |
-
# model = data.get("model", "")
|
| 312 |
-
# url = data.get("url", "")
|
| 313 |
-
|
| 314 |
-
# if adv not in ["Không có ưu điểm", "", None]:
|
| 315 |
-
# adv = adv.replace("\n", "")
|
| 316 |
-
|
| 317 |
-
# # Handle both products and solution packages
|
| 318 |
-
# if url:
|
| 319 |
-
# full_name = f"**[{data['name']} {model}]({url})**"
|
| 320 |
-
# else:
|
| 321 |
-
# full_name = f"**{data['name']} {model}**"
|
| 322 |
-
|
| 323 |
-
# if adv not in ["Không có ưu điểm", "", None]:
|
| 324 |
-
# table_data.append([full_name, adv])
|
| 325 |
-
|
| 326 |
-
# if table_data:
|
| 327 |
-
# df = pd.DataFrame(table_data, columns=columns)
|
| 328 |
-
# else:
|
| 329 |
-
# df = pd.DataFrame([["Không có ưu điểm", ""]], columns=columns)
|
| 330 |
-
|
| 331 |
-
# markdown_table = df.to_markdown(index=False)
|
| 332 |
-
# return f"💡 **Ưu điểm nổi trội**\n\n{markdown_table}"
|
| 333 |
-
|
| 334 |
@staticmethod
|
| 335 |
def show_advantages(state: ConversationState) -> str:
|
| 336 |
"""Generate advantages as bullet list instead of table"""
|
|
@@ -377,21 +357,24 @@ class DisplayService:
|
|
| 377 |
@staticmethod
|
| 378 |
def show_all_products_table(state: ConversationState):
|
| 379 |
outputs = state.outputs or {}
|
| 380 |
-
tags = outputs.get("tags", [])
|
| 381 |
|
| 382 |
if not outputs:
|
| 383 |
return "Không có dữ liệu sản phẩm"
|
| 384 |
|
| 385 |
try:
|
|
|
|
|
|
|
| 386 |
resp = requests.post(f"{API_BASE_URL}/products_by_category",
|
| 387 |
-
json=
|
| 388 |
|
| 389 |
if resp.status_code == 200:
|
| 390 |
data = resp.json()
|
| 391 |
return data.get("markdown_table", "Không có dữ liệu sản phẩm")
|
| 392 |
else:
|
|
|
|
| 393 |
return "Không có dữ liệu sản phẩm"
|
| 394 |
except Exception as e:
|
|
|
|
| 395 |
return f"Error: {e}"
|
| 396 |
|
| 397 |
|
|
|
|
| 66 |
"""Clear all conversation history and reset state via API"""
|
| 67 |
if state.session_id is not None and API_BASE_URL:
|
| 68 |
try:
|
| 69 |
+
payload = {
|
| 70 |
+
"reset_cache": True,
|
| 71 |
+
"reset_model": False,
|
| 72 |
+
"session_id": state.session_id
|
| 73 |
+
}
|
| 74 |
+
response = requests.post(f"{API_BASE_URL}/clear_memory", json=payload)
|
| 75 |
+
print(f"Clear memory response: {response.status_code}")
|
| 76 |
except Exception as e:
|
| 77 |
print(f"Warning: clear_memory failed: {e}")
|
| 78 |
|
|
|
|
| 84 |
@staticmethod
|
| 85 |
def change_model(state: ConversationState, model_name: str):
|
| 86 |
"""Change the selected model"""
|
| 87 |
+
state.selected_model = model_name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
@staticmethod
|
| 90 |
def toggle_product_model_search(state: ConversationState):
|
|
|
|
| 114 |
# Call API
|
| 115 |
try:
|
| 116 |
if image_path:
|
| 117 |
+
# For image uploads, use form-data format as expected by API
|
| 118 |
with open(image_path, 'rb') as f:
|
| 119 |
files = {"image": f}
|
| 120 |
data = {
|
| 121 |
"message": message,
|
| 122 |
"product_model_search": str(state.product_model_search).lower(),
|
| 123 |
+
"session_id": state.session_id,
|
| 124 |
+
"llm_model": state.selected_model,
|
| 125 |
+
"debug": "Normal"
|
| 126 |
}
|
| 127 |
resp = requests.post(
|
| 128 |
+
f"{API_BASE_URL}/chat_with_image",
|
| 129 |
+
files=files,
|
| 130 |
+
data=data,
|
| 131 |
+
timeout=120
|
| 132 |
+
)
|
| 133 |
else:
|
| 134 |
+
# For text messages, use form-data format as expected by API
|
| 135 |
+
data = {
|
| 136 |
"message": message,
|
| 137 |
"session_id": state.session_id,
|
| 138 |
"debug": "Normal",
|
| 139 |
+
"product_model_search": state.product_model_search,
|
| 140 |
+
"llm_model": state.selected_model
|
| 141 |
}
|
| 142 |
+
resp = requests.post(
|
| 143 |
+
f"{API_BASE_URL}/chat",
|
| 144 |
+
data=data, # Changed from json to data for form format
|
| 145 |
+
timeout=120
|
| 146 |
+
)
|
| 147 |
|
| 148 |
if resp.status_code == 200:
|
| 149 |
j = resp.json()
|
| 150 |
response = j.get("response", "")
|
| 151 |
specs_advantages = j.get("specs_advantages")
|
| 152 |
solution_packages = j.get("solution_packages")
|
| 153 |
+
raw_documents = j.get("raw_documents") # This might be None from API
|
| 154 |
outputs = j.get("outputs")
|
| 155 |
else:
|
| 156 |
+
print(f"API Error: {resp.status_code} - {resp.text}")
|
| 157 |
response = f"Error: API status {resp.status_code}"
|
| 158 |
specs_advantages, solution_packages, raw_documents, outputs = None, None, None, None
|
| 159 |
except Exception as e:
|
| 160 |
+
print(f"Exception calling API: {e}")
|
| 161 |
response = f"Error calling API: {e}"
|
| 162 |
specs_advantages, solution_packages, raw_documents, outputs = None, None, None, None
|
| 163 |
|
| 164 |
end = time.perf_counter()
|
| 165 |
|
| 166 |
# Update state
|
| 167 |
+
if specs_advantages is not None:
|
| 168 |
+
state.specs_advantages = specs_advantages
|
| 169 |
+
if solution_packages is not None:
|
| 170 |
+
state.solution_packages = solution_packages
|
| 171 |
+
if raw_documents is not None:
|
| 172 |
+
state.raw_documents = raw_documents
|
| 173 |
+
if outputs is not None:
|
| 174 |
+
state.outputs = outputs
|
| 175 |
|
| 176 |
# Filter products based on query
|
| 177 |
if state.specs_advantages is not None:
|
|
|
|
| 226 |
|
| 227 |
# Handle both products and solution packages
|
| 228 |
if url:
|
| 229 |
+
full_name = f"**[{data['name']} {model}]({url + '.html'})**"
|
| 230 |
else:
|
| 231 |
full_name = f"**{data['name']} {model}**"
|
| 232 |
|
|
|
|
| 311 |
markdown_table = df.to_markdown(index=False)
|
| 312 |
return f"📄 **Thông số kỹ thuật**\n\n{markdown_table}"
|
| 313 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
@staticmethod
|
| 315 |
def show_advantages(state: ConversationState) -> str:
|
| 316 |
"""Generate advantages as bullet list instead of table"""
|
|
|
|
| 357 |
@staticmethod
|
| 358 |
def show_all_products_table(state: ConversationState):
|
| 359 |
outputs = state.outputs or {}
|
|
|
|
| 360 |
|
| 361 |
if not outputs:
|
| 362 |
return "Không có dữ liệu sản phẩm"
|
| 363 |
|
| 364 |
try:
|
| 365 |
+
# Updated to match API format - send outputs in request body
|
| 366 |
+
payload = {"outputs": outputs}
|
| 367 |
resp = requests.post(f"{API_BASE_URL}/products_by_category",
|
| 368 |
+
json=payload, timeout=60)
|
| 369 |
|
| 370 |
if resp.status_code == 200:
|
| 371 |
data = resp.json()
|
| 372 |
return data.get("markdown_table", "Không có dữ liệu sản phẩm")
|
| 373 |
else:
|
| 374 |
+
print(f"All products API error: {resp.status_code} - {resp.text}")
|
| 375 |
return "Không có dữ liệu sản phẩm"
|
| 376 |
except Exception as e:
|
| 377 |
+
print(f"Exception in show_all_products_table: {e}")
|
| 378 |
return f"Error: {e}"
|
| 379 |
|
| 380 |
|