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

feat: add Langfuse @observe tracing for DB queries + event-based search prompts

parent 12c89dc6
......@@ -8,6 +8,8 @@ import json
import logging
import time
from langfuse.decorators import observe
from langchain_core.tools import tool
from pydantic import BaseModel, Field
......@@ -101,6 +103,7 @@ class MultiSearchParams(BaseModel):
searches: list[SearchItem] = Field(description="Danh sách các truy vấn tìm kiếm")
@observe(name="db-query-search")
async def _execute_single_search(
db, item: SearchItem, query_vector: list[float] | None = None
) -> tuple[list[dict], dict]:
......@@ -150,6 +153,28 @@ async def _execute_single_search(
first_p.get("original_price"),
first_p.get("sale_price"),
)
logger.info(
"🔍 [DEBUG] size_scale: %r, description_text: %r",
first_p.get("size_scale"),
(first_p.get("description_text") or "")[:100],
)
from langfuse.decorators import langfuse_context
langfuse_context.update_current_observation(
input={
"description": item.description[:200] if item.description else None,
"product_name": item.product_name,
"gender": item.gender_by_product,
"age": item.age_by_product,
"product_line_vn": item.product_line_vn,
"magento_ref_code": item.magento_ref_code,
},
output={
"raw_count": len(products),
"build_ms": round(query_build_time, 2),
"db_ms": round(db_time, 2),
},
)
return format_product_results(products), {"fallback_used": False}
except Exception as e:
......
......@@ -2,6 +2,8 @@ import logging
import os
import time
from langfuse.decorators import observe
from common.embedding_service import create_embedding_async
......@@ -101,6 +103,7 @@ def _get_metadata_clauses(params, sql_params: list) -> list[str]:
return clauses
@observe(name="build-starrocks-query")
async def build_starrocks_query(params, query_vector: list[float] | None = None) -> tuple[str, list]:
"""
Build SQL query với Parameterized Query để tránh SQL Injection.
......@@ -121,7 +124,7 @@ async def build_starrocks_query(params, query_vector: list[float] | None = None)
master_color,
product_image_url_thumbnail,
product_web_url,
description_text_full,
description_text,
sale_price,
original_price,
discount_amount,
......@@ -129,7 +132,8 @@ async def build_starrocks_query(params, query_vector: list[float] | None = None)
age_by_product,
gender_by_product,
product_line_vn,
product_line_en,
quantity_sold,
size_scale,
1.0 as similarity_score
FROM shared_source.magento_product_dimension_with_text_embedding
WHERE internal_ref_code = %s OR magento_ref_code = %s
......
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