Spaces:
Runtime error
Runtime error
Making changes to encounter visit filtering
Browse files160 unmodified lines1611621631641651661675 unmodified lines17317417517617717817918018118227 unmodified lines210211212213214215216217218219160 unmodified linesif "AGE_AT_VISIT_lte" in data:filters["$and"].append({"AGE_AT_VISIT": {"$lte": data["AGE_AT_VISIT_lte"]}})if "ENCOUNTERCLASS" in data:encounter_classes = data["ENCOUNTERCLASS"]if isinstance(encounter_classes, str):filters["$and"].append({"ENCOUNTERCLASS": {"$eq": encounter_classes}})5 unmodified linesfilters = {}elif not filters["$and"] and first_names and isinstance(first_names, str):filters = {"FIRST": {"$eq": first_names}}elif len(filters["$and"]) == 1 and isinstance(first_names, str):filters = filters["$and"][0]# print("Constructed Filters:", filters)results = []if isinstance(first_names, list):27 unmodified lineselse:results = vectordb.similarity_search(query=query,k=10,filter=filters)return results# --- RAG Enhanced Response Generation ---160 unmodified lines1611621631641651661671685 unmodified lines17417517617717817918018118218318418518627 unmodified lines214215216217218219220221222223224225226227228229230231232233234235236237238239240160 unmodified linesif "AGE_AT_VISIT_lte" in data:filters["$and"].append({"AGE_AT_VISIT": {"$lte": data["AGE_AT_VISIT_lte"]}})encounter_class_filter_present = "ENCOUNTERCLASS" in dataif encounter_class_filter_present:encounter_classes = data["ENCOUNTERCLASS"]if isinstance(encounter_classes, str):filters["$and"].append({"ENCOUNTERCLASS": {"$eq": encounter_classes}})5 unmodified linesfilters = {}elif not filters["$and"] and first_names and isinstance(first_names, str):filters = {"FIRST": {"$eq": first_names}}elif len(filters["$and"]) == 1 and isinstance(first_names, str) and not encounter_class_filter_present:filters = filters["$and"][0]elif len(filters["$and"]) == 1 and not first_names and encounter_class_filter_present:filters = filters["$and"][0]print("Constructed Filters:", filters)results = []if isinstance(first_names, list):27 unmodified lineselse:results = vectordb.similarity_search(query=query,k=20,filter=filters)# If no results and only encounter class filter was present, try searching without itif not results and encounter_class_filter_present and len(filters.get("$and", [])) >= 1:print("Retrying search without encounter class filter as no results were found.")filters_without_encounter_class = {"$and": [cond for cond in filters["$and"] if "ENCOUNTERCLASS" not in cond]}if not filters_without_encounter_class["$and"]:filters_without_encounter_class = {}elif len(filters_without_encounter_class["$and"]) == 1:filters_without_encounter_class = filters_without_encounter_class["$and"][0]results = vectordb.similarity_search(query=query,k=20,filter=filters_without_encounter_class)print(f"Retried search filters: {filters_without_encounter_class}")return results# --- RAG Enhanced Response Generation ---
app.py+26-5