更新工具方法 增加list数据兼容性
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import numpy as np
|
||||
from langchain_core.tools import tool
|
||||
import requests
|
||||
import pandas as pd
|
||||
@@ -80,12 +81,21 @@ def filter_data(domain: str, filters: list[dict] | None = None, search: list[str
|
||||
continue
|
||||
|
||||
col = df[field]
|
||||
|
||||
# 处理多值情况
|
||||
if col.apply(lambda x: isinstance(x, (list, tuple, set))).any():
|
||||
if op == "contains":
|
||||
is_multi = col.apply(lambda x: isinstance(x, (list, tuple, set))).any()
|
||||
|
||||
if is_multi:
|
||||
if op == "eq":
|
||||
col = col.apply(lambda x: value in x if isinstance(x, (list, tuple, set)) else False)
|
||||
elif op == "ne":
|
||||
col = col.apply(lambda x: value not in x if isinstance(x, (list, tuple, set)) else True)
|
||||
elif op == "contains":
|
||||
col = col.apply(lambda x: value in x if isinstance(x, (list, tuple, set)) else False)
|
||||
elif op == "not_contains":
|
||||
col = col.apply(lambda x: value not in x if isinstance(x, (list, tuple, set)) else True)
|
||||
else:
|
||||
continue
|
||||
df = df[col]
|
||||
continue
|
||||
|
||||
@@ -107,8 +117,17 @@ def filter_data(domain: str, filters: list[dict] | None = None, search: list[str
|
||||
for field in cfg.search_fields:
|
||||
if field not in df.columns:
|
||||
continue
|
||||
# 如果包含就表示命中
|
||||
mask |= df[field].astype(str).str.contains(kw, na=False)
|
||||
|
||||
# list 转成可检索的字符串
|
||||
col_str = df[field].apply(
|
||||
lambda x: " ".join(map(str, x))
|
||||
if isinstance(x, (list, tuple, set))
|
||||
else str(x)
|
||||
)
|
||||
|
||||
# 如果包含则命中
|
||||
mask |= col_str.str.contains(kw, na=False)
|
||||
|
||||
df = df[mask]
|
||||
|
||||
filtered_len = len(df)
|
||||
@@ -193,8 +212,19 @@ def inspect_data(domain: str, sort_by: str | None = None, ascending: bool = Fals
|
||||
if fld not in row:
|
||||
continue
|
||||
val = row[fld]
|
||||
if pd.isna(val) or val == "":
|
||||
|
||||
# 数组 / Series
|
||||
if isinstance(val, (np.ndarray, pd.Series)) or (isinstance(val, (list, tuple))):
|
||||
if len(val) == 0:
|
||||
val = "未填写"
|
||||
else:
|
||||
val = "、".join(map(str, val))
|
||||
elif pd.isna(val):
|
||||
val = "未填写"
|
||||
elif val == "":
|
||||
val = "未填写"
|
||||
else:
|
||||
val = str(val)
|
||||
lines.append(f"- {cfg.get_chinese_name(fld)}:{val}")
|
||||
print(f"[TOOL] inspect_data | rendered directly")
|
||||
return "\n".join(lines)
|
||||
|
||||
Reference in New Issue
Block a user