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.
...@@ -79,6 +79,18 @@ class SearchItem(BaseModel): ...@@ -79,6 +79,18 @@ class SearchItem(BaseModel):
price_min: float | None = Field(..., description="Giá thấp nhất (VD: 100000)") price_min: float | None = Field(..., description="Giá thấp nhất (VD: 100000)")
price_max: float | None = Field(..., description="Giá cao nhất (VD: 500000)") 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)") 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): class MultiSearchParams(BaseModel):
...@@ -87,6 +99,23 @@ class MultiSearchParams(BaseModel): ...@@ -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") 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) @tool(args_schema=MultiSearchParams)
# @traceable(run_type="tool", name="data_retrieval_tool") # @traceable(run_type="tool", name="data_retrieval_tool")
...@@ -101,16 +130,26 @@ async def data_retrieval_tool(searches: list[SearchItem]) -> str: ...@@ -101,16 +130,26 @@ async def data_retrieval_tool(searches: list[SearchItem]) -> str:
""" """
logger.info("data_retrieval_tool started, searches=%s", len(searches)) logger.info("data_retrieval_tool started, searches=%s", len(searches))
try: 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) # 0. Log input tổng quan (không log chi tiết dài)
for idx, item in enumerate(searches): for idx, item in enumerate(searches):
short_query = (item.query[:60] + "...") if item.query and len(item.query) > 60 else item.query short_query = (item.query[:60] + "...") if item.query and len(item.query) > 60 else item.query
logger.debug( 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, idx,
short_query, short_query,
item.magento_ref_code, item.magento_ref_code,
item.price_min, item.price_min,
item.price_max, 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