Skip to content

chore: include source columns in semantics.top_k #1531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 25, 2025
Merged
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
24 changes: 11 additions & 13 deletions bigframes/operations/semantics.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,13 +807,17 @@ def top_k(
>>> import bigframes.ml.llm as llm
>>> model = llm.GeminiTextGenerator(model_name="gemini-1.5-flash-001")

>>> df = bpd.DataFrame({"Animals": ["Dog", "Bird", "Cat", "Horse"]})
>>> df = bpd.DataFrame(
... {
... "Animals": ["Dog", "Bird", "Cat", "Horse"],
... "Sounds": ["Woof", "Chirp", "Meow", "Neigh"],
... })
>>> df.semantics.top_k("{Animals} are more popular as pets", model=model, k=2)
Animals
0 Dog
2 Cat
Animals Sounds
0 Dog Woof
2 Cat Meow
<BLANKLINE>
[2 rows x 1 columns]
[2 rows x 2 columns]

Args:
instruction (str):
Expand Down Expand Up @@ -911,14 +915,8 @@ def top_k(
)
num_selected += num_new_selected

df = (
df[df[status_column] > 0]
.drop(["index", status_column], axis=1)
.rename(columns={"old_index": "index"})
.set_index("index")
)
df.index.name = None
return df
result_df: bigframes.dataframe.DataFrame = self._df.copy()
return result_df[df.set_index("old_index")[status_column] > 0.0]

@staticmethod
def _topk_partition(
Expand Down