feat:修改nl2sql功能
This commit is contained in:
@@ -15,6 +15,8 @@ from config.database.mysql import get_session_factory
|
||||
from config.settings import settings
|
||||
from nl2sql.metadata import build_metadata_chunks
|
||||
from nl2sql.milvus_collections import NL2SQL_COLLECTION
|
||||
from model.base import Base
|
||||
from scripts.sync_nl2sql_metadata import collect_orm_tables, merge_orm_metadata_rows
|
||||
|
||||
|
||||
async def collect_consistency() -> dict:
|
||||
@@ -29,7 +31,21 @@ async def collect_consistency() -> dict:
|
||||
async with get_session_factory()() as session:
|
||||
tables = [dict(row) for row in (await session.execute(table_sql, {"db": settings.mysql.database})).mappings()]
|
||||
columns = [dict(row) for row in (await session.execute(column_sql, {"db": settings.mysql.database})).mappings()]
|
||||
expected = len(build_metadata_chunks(tables, columns))
|
||||
# 与同步脚本保持同一口径:ORM 表是唯一权威名单,数据库中存在但
|
||||
# 未被 model/ 定义的表不应影响 NL2SQL 元数据一致性判断。
|
||||
orm_tables = collect_orm_tables()
|
||||
db_tables = {
|
||||
str(row.get("TABLE_NAME") or row.get("table_name") or "").strip()
|
||||
for row in tables
|
||||
}
|
||||
missing_tables = sorted(orm_tables - db_tables)
|
||||
expected_tables, expected_columns = merge_orm_metadata_rows(
|
||||
tables,
|
||||
columns,
|
||||
allowed_tables=orm_tables,
|
||||
missing_table_objects=(Base.metadata.tables[name] for name in missing_tables),
|
||||
)
|
||||
expected = len(build_metadata_chunks(expected_tables, expected_columns))
|
||||
rows = await client().query(
|
||||
collection_name=NL2SQL_COLLECTION,
|
||||
filter="is_valid == true and is_deprecated == false",
|
||||
|
||||
@@ -25,6 +25,8 @@ from tool.llm import llm
|
||||
async def run_real_query(user_id: int, question: str, *, query_id: str | None = None) -> dict:
|
||||
"""使用已有员工权限执行一条真实查询,只输出脱敏统计。"""
|
||||
from config.database.mysql import get_session_factory
|
||||
from common.common_const import CUSTOMER_REL_STATUS_SIGNED, CUSTOMER_REL_STATUS_UNSIGNED
|
||||
from repositories.customer_relation import CustomerRelationRepo
|
||||
|
||||
query_id = query_id or uuid4().hex
|
||||
try:
|
||||
@@ -33,6 +35,22 @@ async def run_real_query(user_id: int, question: str, *, query_id: str | None =
|
||||
if not permission.get("can_query"):
|
||||
return {"query_status": "permission_denied", "user_id": user_id}
|
||||
|
||||
data_scope = None
|
||||
if any(
|
||||
scope.get("type") == "customer_ids"
|
||||
for scope in (permission.get("row_scopes") or {}).values()
|
||||
):
|
||||
relations = await CustomerRelationRepo(db).list_by_advisor(user_id)
|
||||
customer_ids = [
|
||||
relation.customer_id
|
||||
for relation in relations
|
||||
if relation.status in {
|
||||
CUSTOMER_REL_STATUS_UNSIGNED,
|
||||
CUSTOMER_REL_STATUS_SIGNED,
|
||||
}
|
||||
]
|
||||
data_scope = {"customer_ids": customer_ids}
|
||||
|
||||
async def permission_loader(_user_id: int):
|
||||
return permission
|
||||
|
||||
@@ -51,6 +69,7 @@ async def run_real_query(user_id: int, question: str, *, query_id: str | None =
|
||||
question=question,
|
||||
user_id=user_id,
|
||||
trace_id=f"nl2sql-e2e-{query_id}",
|
||||
data_scope=data_scope,
|
||||
include_sql=False,
|
||||
),
|
||||
session=db,
|
||||
|
||||
Reference in New Issue
Block a user