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

fix(backend): Enforce strict age/gender filtering in prompt and tool schema

parent c66892a7
This diff is collapsed.
......@@ -80,6 +80,18 @@ class SearchItem(BaseModel):
price_max: float | None = Field(..., description="Giá cao nhất (VD: 500000)")
action: str = Field(..., description="Hành động: 'search' (tìm kiếm) hoặc 'visual_search' (phân tích ảnh)")
# Metadata Fields for Filtering
gender_by_product: str | None = None
age_by_product: str | None = None
product_name: str | None = None
style: str | None = None
master_color: str | None = None
season: str | None = None
material_group: str | None = None
fitting: str | None = None
form_neckline: str | None = None
form_sleeve: str | None = None
class MultiSearchParams(BaseModel):
"""Tham số cho Parallel Multi-Search."""
......@@ -87,6 +99,23 @@ class MultiSearchParams(BaseModel):
searches: list[SearchItem] = Field(..., description="Danh sách các truy vấn tìm kiếm chạy song song")
def _parse_query_metadata(query_str: str) -> dict:
"""Parse structured query string explicitly generated by the Agent."""
import re
metadata = {}
if not query_str:
return metadata
# Matches "key: value" at the start of lines (handling indentation)
pattern = re.compile(r"^\s*([a-z_]+):\s*(.+?)\s*$", re.MULTILINE)
matches = pattern.findall(query_str)
for key, value in matches:
if value and value.lower() != 'none':
metadata[key] = value.strip()
return metadata
@tool(args_schema=MultiSearchParams)
# @traceable(run_type="tool", name="data_retrieval_tool")
......@@ -101,16 +130,26 @@ async def data_retrieval_tool(searches: list[SearchItem]) -> str:
"""
logger.info("data_retrieval_tool started, searches=%s", len(searches))
try:
# Pre-process: Parse metadata from query string
for item in searches:
if item.query:
meta = _parse_query_metadata(item.query)
for k, v in meta.items():
if hasattr(item, k):
setattr(item, k, v)
# 0. Log input tổng quan (không log chi tiết dài)
for idx, item in enumerate(searches):
short_query = (item.query[:60] + "...") if item.query and len(item.query) > 60 else item.query
logger.debug(
"search[%s] query=%r, code=%r, price_min=%r, price_max=%r",
"search[%s] query=%r, code=%r, price_min=%r, price_max=%r, gender=%r, age=%r",
idx,
short_query,
item.magento_ref_code,
item.price_min,
item.price_max,
item.gender_by_product,
item.age_by_product
)
......
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