Commit 0983572f authored by Vũ Hoàng Anh's avatar Vũ Hoàng Anh

feat: optimize prompt + fix vay lien than mapping

- Trim prompt 22% (118K->92K chars): condensed thao mai, urgency, merged examples
- Add khach sop section (mua nhieu/don lon)
- Add trend/viral phrases (Gen Z, tu do sang tao)
- Fix product_mapping: add 'vay lien than','dam lien','vay dam' synonyms for Vay lien
- Add CÂM PHAN BODY rule in thao mai section
parent 453ef792
...@@ -150,6 +150,7 @@ async def chat_controller( ...@@ -150,6 +150,7 @@ async def chat_controller(
ai_text_response = "" ai_text_response = ""
final_product_ids = [] final_product_ids = []
final_question_recommend = [] final_question_recommend = []
final_auto_cart = False
all_accumulated_messages = [] all_accumulated_messages = []
seen_message_ids = set() seen_message_ids = set()
...@@ -287,6 +288,8 @@ async def chat_controller( ...@@ -287,6 +288,8 @@ async def chat_controller(
final_product_ids = [str(s) for s in ai_json["product_ids"]] final_product_ids = [str(s) for s in ai_json["product_ids"]]
if not final_question_recommend and isinstance(ai_json.get("question_recommend"), list): if not final_question_recommend and isinstance(ai_json.get("question_recommend"), list):
final_question_recommend = ai_json["question_recommend"] final_question_recommend = ai_json["question_recommend"]
if ai_json.get("auto_cart"):
final_auto_cart = True
except json.JSONDecodeError: except json.JSONDecodeError:
ai_match = re.search(r'"ai_response"\s*:\s*"((?:[^"\\]|\\.)*)"', raw_content) ai_match = re.search(r'"ai_response"\s*:\s*"((?:[^"\\]|\\.)*)"', raw_content)
if ai_match: if ai_match:
...@@ -320,6 +323,8 @@ async def chat_controller( ...@@ -320,6 +323,8 @@ async def chat_controller(
final_product_ids = [str(s) for s in ai_json["product_ids"]] final_product_ids = [str(s) for s in ai_json["product_ids"]]
if not final_question_recommend and isinstance(ai_json.get("question_recommend"), list): if not final_question_recommend and isinstance(ai_json.get("question_recommend"), list):
final_question_recommend = ai_json["question_recommend"] final_question_recommend = ai_json["question_recommend"]
if ai_json.get("auto_cart"):
final_auto_cart = True
except json.JSONDecodeError: except json.JSONDecodeError:
# Regex fallback for Codex {{/}} braces that break JSON parse # Regex fallback for Codex {{/}} braces that break JSON parse
ai_match = re.search(r'"ai_response"\s*:\s*"((?:[^"\\]|\\.)*)"\s*,\s*"product_ids"', ai_text_response, re.DOTALL) ai_match = re.search(r'"ai_response"\s*:\s*"((?:[^"\\]|\\.)*)"\s*,\s*"product_ids"', ai_text_response, re.DOTALL)
...@@ -416,6 +421,7 @@ async def chat_controller( ...@@ -416,6 +421,7 @@ async def chat_controller(
"ai_response": ai_text_response, "ai_response": ai_text_response,
"product_ids": enriched_products, "product_ids": enriched_products,
"question_recommend": final_question_recommend, "question_recommend": final_question_recommend,
"auto_cart": final_auto_cart,
} }
if user_insight_dict is not None: if user_insight_dict is not None:
response_payload["user_insight"] = user_insight_dict response_payload["user_insight"] = user_insight_dict
......
...@@ -56,6 +56,10 @@ class ChatResponse(BaseModel): ...@@ -56,6 +56,10 @@ class ChatResponse(BaseModel):
default_factory=list, default_factory=list,
description="Danh sách mã SKU sản phẩm được nhắc đến (chỉ string, không object)" description="Danh sách mã SKU sản phẩm được nhắc đến (chỉ string, không object)"
) )
auto_cart: bool = Field(
default=False,
description="True khi khách thể hiện ý định mua/order rõ ràng (VD: 'mua cho tao', 'order cái này', 'thêm vào giỏ', 'chốt đơn'). False khi chỉ hỏi/tìm/xem."
)
user_insight: UserInsight = Field( user_insight: UserInsight = Field(
default_factory=UserInsight, default_factory=UserInsight,
description="Insight 6 tầng về khách hàng, cập nhật sau mỗi turn" description="Insight 6 tầng về khách hàng, cập nhật sau mỗi turn"
......
This diff is collapsed.
...@@ -32,7 +32,7 @@ PRODUCT_LINE_MAP: dict[str, list[str]] = { ...@@ -32,7 +32,7 @@ PRODUCT_LINE_MAP: dict[str, list[str]] = {
"Áo bra active": ["áo bra active", "áo bra", "bra"], "Áo bra active": ["áo bra active", "áo bra", "bra"],
"Áo Body": ["áo body", "áo croptop", "croptop", "baby tee", "áo lửng", "áo dáng ngắn"], "Áo Body": ["áo body", "áo croptop", "croptop", "baby tee", "áo lửng", "áo dáng ngắn"],
"Áo ba lỗ": ["áo ba lỗ", "áo sát nách", "tanktop", "tank top", "áo dây", "áo 2 dây", "áo hai dây"], "Áo ba lỗ": ["áo ba lỗ", "áo sát nách", "tanktop", "tank top", "áo dây", "áo 2 dây", "áo hai dây"],
"Váy liền": ["váy liền", "đầm"], "Váy liền": ["váy liền thân", "váy liền", "đầm", "đầm liền", "váy đầm"],
"Tất": ["tất", "vớ"], "Tất": ["tất", "vớ"],
"Túi xách": ["túi xách"], "Túi xách": ["túi xách"],
"Quần giả váy": ["quần giả váy", "quần váy"], "Quần giả váy": ["quần giả váy", "quần váy"],
......
...@@ -62,6 +62,7 @@ async def fashion_qa_chat(request: Request, req: QueryRequest, background_tasks: ...@@ -62,6 +62,7 @@ async def fashion_qa_chat(request: Request, req: QueryRequest, background_tasks:
"status": "success", "status": "success",
"ai_response": result["ai_response"], "ai_response": result["ai_response"],
"product_ids": result.get("product_ids", []), "product_ids": result.get("product_ids", []),
"auto_cart": result.get("auto_cart", False),
"question_recommend": result.get("question_recommend", []), "question_recommend": result.get("question_recommend", []),
"trace_id": result.get("trace_id", ""), "trace_id": result.get("trace_id", ""),
"limit_info": { "limit_info": {
...@@ -121,6 +122,7 @@ async def fashion_qa_chat_dev(request: Request, req: QueryRequest, background_ta ...@@ -121,6 +122,7 @@ async def fashion_qa_chat_dev(request: Request, req: QueryRequest, background_ta
"status": "success", "status": "success",
"ai_response": result["ai_response"], "ai_response": result["ai_response"],
"product_ids": result.get("product_ids", []), "product_ids": result.get("product_ids", []),
"auto_cart": result.get("auto_cart", False),
"question_recommend": result.get("question_recommend", []), "question_recommend": result.get("question_recommend", []),
"trace_id": result.get("trace_id", ""), "trace_id": result.get("trace_id", ""),
"insight_status": "success" if result.get("user_insight") else "pending", "insight_status": "success" if result.get("user_insight") else "pending",
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
"""
Tách nội dung từng Điều trong hợp đồng - giữ nguyên text.
"""
import re
def split_contract_by_dieu(text: str) -> list[dict]:
DIEU_RE = re.compile(r'^ĐIỀU\s+(\d+)\.\s*(.+)$')
segments = []
current_dieu = None
current_title = ""
current_content = []
for line in text.splitlines():
line_stripped = line.strip()
if not line_stripped:
continue
m = DIEU_RE.match(line_stripped)
if m:
if current_dieu is not None:
segments.append({
"dieu": current_dieu,
"tieu_de": current_title,
"noi_dung": "\n".join(current_content),
})
current_dieu = int(m.group(1))
current_title = m.group(2).strip()
current_content = []
continue
if current_dieu is not None:
current_content.append(line_stripped)
if current_dieu is not None:
segments.append({
"dieu": current_dieu,
"tieu_de": current_title,
"noi_dung": "\n".join(current_content),
})
return segments
if __name__ == "__main__":
with open("data.txt", "r", encoding="utf-8") as f:
text = f.read()
results = split_contract_by_dieu(text)
# Ghi ra file text, mỗi Điều cách nhau bằng dấu ===
output_lines = []
for seg in results:
output_lines.append(f"ĐIỀU {seg['dieu']}. {seg['tieu_de']}")
output_lines.append(seg["noi_dung"])
output_lines.append("\n" + "=" * 60 + "\n")
output_text = "\n".join(output_lines)
with open("output_dieu.txt", "w", encoding="utf-8") as f:
f.write(output_text)
print(f"Done! Tach duoc {len(results)} Dieu -> output_dieu.txt")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment