|
|
@@ -1,10 +1,9 @@
|
|
|
import re
|
|
|
from fastapi import APIRouter , UploadFile
|
|
|
-from pydantic import BaseModel,Field
|
|
|
+from pydantic import BaseModel,Field, field_validator
|
|
|
from requests import request
|
|
|
from agent.core.sign_check import check
|
|
|
import agent.core.dao as dao
|
|
|
-from asyncer import asyncify
|
|
|
from agent.core.vector import get_embeddings
|
|
|
from agent.core.es import hybrid_search
|
|
|
from agent.agent import reflect_check
|
|
|
@@ -36,8 +35,8 @@ class TaggingRequest(BaseModel):
|
|
|
tag_category_id: Optional[str] = Field(None, description="指定标签类别ID,默认为空表示不指定")
|
|
|
esb_seq_no: Optional[str] = Field(None,description="ESB流水号")
|
|
|
instucde:Optional[str] = Field(None, description="法人行社代码")
|
|
|
- contract_no:Optional[str] = Field(None,description="合同编号")
|
|
|
instucde_nm:Optional[str] = Field(None,description="法人行社名称")
|
|
|
+ contract_no:Optional[str] = Field(None,description="合同编号")
|
|
|
company_nm:Optional[str] = Field(None,description="企业名称")
|
|
|
company_code:Optional[str]=Field(None,description="企业统一社会信用代码")
|
|
|
user_id:Optional[str] = Field(None,description="发起人ID")
|
|
|
@@ -45,6 +44,19 @@ class TaggingRequest(BaseModel):
|
|
|
user_org:Optional[str] = Field(None,description="发起人所属机构")
|
|
|
user_endpoint:Optional[str] = Field(None,description="发起人所属网点")
|
|
|
|
|
|
+ class Config:
|
|
|
+ extra = "ignore"
|
|
|
+ populate_by_name = True
|
|
|
+
|
|
|
+ @field_validator('business_attr', 'phrase', 'tag_category_id', 'esb_seq_no',
|
|
|
+ 'instucde', 'instucde_nm', 'contract_no', 'company_nm',
|
|
|
+ 'company_code', 'user_id', 'user_nm', 'user_org', 'user_endpoint',
|
|
|
+ mode='before')
|
|
|
+ def convert_to_str(cls, v):
|
|
|
+ if v is None:
|
|
|
+ return None
|
|
|
+ return str(v)
|
|
|
+
|
|
|
async def execute_reg(log_id:str,tag_category_id:str,phrase: str)-> list:
|
|
|
sql = f"""select
|
|
|
tti.id,
|
|
|
@@ -52,26 +64,26 @@ async def execute_reg(log_id:str,tag_category_id:str,phrase: str)-> list:
|
|
|
from aitag_tag_info tti left join aitag_tag_category ttc
|
|
|
on tti.category_id = ttc.id
|
|
|
where ttc.is_delete=0 and tti.is_delete=0 and ttc.state = 0 and tti.state = 0 and tti.tag_level = ttc.visibility_level
|
|
|
+ and '{phrase}' ~ tti.reg
|
|
|
"""
|
|
|
if tag_category_id:
|
|
|
sql += f""" and ttc.id = '{tag_category_id}'"""
|
|
|
labels = dao.query(sql)
|
|
|
# 循环调用reg匹配phrase,匹配成功则返回标签id
|
|
|
- result = []
|
|
|
- try:
|
|
|
- for label in labels:
|
|
|
- reg = label[1]
|
|
|
- if reg is not None:
|
|
|
- pattern = re.compile(reg, re.VERBOSE)
|
|
|
- # logger.info(f"Executing regex for label_id {label[0]}: {reg}")
|
|
|
- if pattern.match(phrase):
|
|
|
- logger.info(f"Executing regex for label_id {label[0]}: {reg} true")
|
|
|
- result.append(label[0])
|
|
|
- else:
|
|
|
- result.append(label[0])
|
|
|
- except Exception as e:
|
|
|
- logger.error(f"Regex execution failed: {e}")
|
|
|
- # logger.info(f"Regex filtering candidates: {result}")
|
|
|
+ result = [label[0] for label in labels]
|
|
|
+ # try:
|
|
|
+ # for label in labels:
|
|
|
+ # reg = label[1]
|
|
|
+ # if reg is not None:
|
|
|
+ # pattern = re.compile(reg, re.VERBOSE)
|
|
|
+ # # logger.info(f"Executing regex for label_id {label[0]}: {reg}")
|
|
|
+ # if pattern.match(phrase):
|
|
|
+ # logger.info(f"Executing regex for label_id {label[0]}: {reg} true")
|
|
|
+ # result.append(label[0])
|
|
|
+ # else:
|
|
|
+ # result.append(label[0])
|
|
|
+ # except Exception as e:
|
|
|
+ # logger.error(f"Regex execution failed: {e}")
|
|
|
dao.execute(
|
|
|
"""UPDATE aitag_tag_log SET reg_result = %s WHERE id = %s""",
|
|
|
(str(result), log_id)
|
|
|
@@ -131,22 +143,31 @@ def start_tagging(id:str, instucde: Optional[str] = None):
|
|
|
|
|
|
# 定义预设规则匹配函数
|
|
|
def defined_rule_match(phrase: str):
|
|
|
- sql = """select tag_type,tag_nm from ai_tagging.ai_tagging.aitag_predefined_rules where %s ~ defined_rule """
|
|
|
- rules = dao.query(sql, (phrase,))
|
|
|
- if rules and len(rules) > 0:
|
|
|
- matched = rules[0]
|
|
|
- logger.info(f"Predefined rule matched: {matched}")
|
|
|
- tag_info = dao.query("""select ati.id,ati.category_id, ati.tag_nm, ati.tag_path,ati.tag_code from aitag_tag_info ati left join aitag_tag_category atc on ati.category_id = atc.id where ati.tag_nm = %s and ati.is_delete = 0 and atc.category_code = %s""", (matched[1], matched[0]))
|
|
|
- return [{
|
|
|
- "id": tag_info[0][0],
|
|
|
- "desc": "",
|
|
|
- "passr": True,
|
|
|
- "tag_code": tag_info[0][4],
|
|
|
- "tag_name": tag_info[0][2],
|
|
|
- "tag_path": tag_info[0][3],
|
|
|
- "category_id": tag_info[0][1]
|
|
|
- }]
|
|
|
- return None
|
|
|
+ result = []
|
|
|
+ try:
|
|
|
+ sql = """select tag_type,tag_nm,defined_rule from aitag_predefined_rules where %s ~ defined_rule """
|
|
|
+ rules = dao.query(sql, (phrase,))
|
|
|
+ print(rules)
|
|
|
+ if rules and len(rules) > 0:
|
|
|
+ for matched in rules:
|
|
|
+ tag_info = dao.query("""select ati.id,ati.category_id, ati.tag_nm, ati.tag_path,ati.tag_code from aitag_tag_info ati left join aitag_tag_category atc on ati.category_id = atc.id where ati.tag_nm = %s and ati.is_delete = 0 and atc.category_code = %s""", (matched[1], matched[0]))
|
|
|
+ # 安全检查:只有当 tag_info 有数据时才加入结果
|
|
|
+ if tag_info and len(tag_info) > 0:
|
|
|
+ result.append({
|
|
|
+ "id": tag_info[0][0],
|
|
|
+ "desc": f"映射文本【{phrase}】与映射规则【{matched[2]}】匹配,映射为标签【{tag_info[0][2]}】",
|
|
|
+ "passr": True,
|
|
|
+ "tag_code": tag_info[0][4],
|
|
|
+ "tag_name": tag_info[0][2],
|
|
|
+ "tag_path": tag_info[0][3],
|
|
|
+ "category_id": tag_info[0][1]
|
|
|
+ })
|
|
|
+ else:
|
|
|
+ logger.warning(f"预设规则匹配成功,但找不到对应的标签记录: tag_nm={matched[1]}, category_code={matched[0]}")
|
|
|
+ except Exception as e:
|
|
|
+ logger.error(f"Defined rule match failed: {e}")
|
|
|
+ result = []
|
|
|
+ return result
|
|
|
|
|
|
def end_tagging_predefined_rule(id:str, result:str):
|
|
|
dao.execute(
|