Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/classifai/indexers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ def reverse_search( # noqa: C901, PLR0912
# polars conversion
paired_query = pl.DataFrame(
{"id": query.id.astype(str).to_list(), "searched_doc_label": query.doc_label.astype(str).to_list()}
)
).with_row_index("_query_row_idx")

# rename vectors dataframe for reverse search return column names and joining
docs = self.vectors.rename({"label": "doc_label", "text": "doc_text"}).with_columns(
Expand All @@ -701,9 +701,14 @@ def reverse_search( # noqa: C901, PLR0912
how="inner",
).rename({"doc_label_copy": "doc_label"})

out = out.sort(by=["id", "searched_doc_label"], descending=[False, False])
if max_n_results != -1:
out = out.group_by("id").head(max_n_results)
out = out.sort(by=["_query_row_idx", "id", "searched_doc_label"], descending=[False, False, False])
out = out.group_by(["_query_row_idx", "id", "searched_doc_label"], maintain_order=True).head(
max_n_results
)

# drop the helper column before final select
out = out.drop("_query_row_idx")

# get formatted table
final_table = out.select(
Expand Down
12 changes: 6 additions & 6 deletions src/classifai/servers/pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ def convert_reverse_search_dataframe_to_pydantic_response(
for original_query in original_input:
input_id = original_query["id"]
input_doc_label = original_query["doc_label"]
# Get the subset of the DataFrame corresponding to the current `id`
group_df = df[df["id"] == input_id]
# Get the subset of the DataFrame corresponding to the current `id` AND `searched_doc_label`
group_df = df[(df["id"] == input_id) & (df["searched_doc_label"] == input_doc_label)]

if group_df.empty:
# If there are no matches for this input_id, we still want to include it in the response, with empty results
Expand Down Expand Up @@ -272,11 +272,11 @@ def convert_search_dataframe_to_pydantic_response(df: pd.DataFrame, meta_data: d
)
)

# Group rows by `query_id`
grouped = df.groupby("query_id")
# Group rows by `query_id` and 'query_text' in case the DataFrame contains multiple queries with the same 'qeuery_id'.
grouped = df.groupby(["query_id", "query_text"])

results_list = []
for query_id, group_df in grouped:
for (query_id, query_text), group_df in grouped:
# Convert group_df to a list of dictionaries
rows_as_dicts = group_df.to_dict(orient="records")

Expand Down Expand Up @@ -305,7 +305,7 @@ def convert_search_dataframe_to_pydantic_response(df: pd.DataFrame, meta_data: d
results_list.append(
SearchResponseSet(
query_id=query_id,
query_text=group_df["query_text"].iloc[0],
query_text=query_text,
entries=response_entries,
)
)
Expand Down