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
102 changes: 102 additions & 0 deletions packages/bigframes/bigframes/extensions/core/dataframe_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import pandas as pd

import bigframes.dataframe
import bigframes.ml.base as ml_base
import bigframes.series
import bigframes.session

Expand Down Expand Up @@ -214,6 +215,107 @@ def generate_double(
)
return self._to_series(result)

def generate_embedding(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that this acts only on a single column (called "content"), I think the Series accessor would be a much better fit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call! Let me move the contents to the series accessor

self,
model: ml_base.BaseEstimator | str | pd.Series,
Comment thread
sycai marked this conversation as resolved.
*,
output_dimensionality: int | None = None,
task_type: str | None = None,
start_second: float | None = None,
end_second: float | None = None,
interval_seconds: float | None = None,
trial_id: int | None = None,
session: bigframes.session.Session | None = None,
) -> T:
"""
Creates embeddings that describe an entity — for example, a piece of text or an image.

This is an accessor for :func:`bigframes.bigquery.ai.generate_embedding`. See that
function's documentation for detailed parameter descriptions and examples.
"""
import bigframes.bigquery.ai

bf_df = self._bf_from_dataframe(session)
result = bigframes.bigquery.ai.generate_embedding(
model,
bf_df,
output_dimensionality=output_dimensionality,
task_type=task_type,
start_second=start_second,
end_second=end_second,
interval_seconds=interval_seconds,
trial_id=trial_id,
)
return self._to_dataframe(result)

def generate_text(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. This acts on a single "prompt" column. I think Series accessor would be a better fit.

To avoid conflicts with generated code, please use a "mixin" pattern to avoid conflicts between the generated Series code and these methods: https://adamj.eu/tech/2025/05/01/python-type-hints-mixin-classes/

self,
model: ml_base.BaseEstimator | str | pd.Series,
Comment thread
sycai marked this conversation as resolved.
*,
temperature: float | None = None,
max_output_tokens: int | None = None,
top_k: int | None = None,
top_p: float | None = None,
stop_sequences: List[str] | None = None,
ground_with_google_search: bool | None = None,
request_type: str | None = None,
session: bigframes.session.Session | None = None,
) -> T:
"""
Generates text using a BigQuery ML model.

This is an accessor for :func:`bigframes.bigquery.ai.generate_text`. See that
function's documentation for detailed parameter descriptions and examples.
"""
import bigframes.bigquery.ai

bf_df = self._bf_from_dataframe(session)
result = bigframes.bigquery.ai.generate_text(
model,
bf_df,
temperature=temperature,
max_output_tokens=max_output_tokens,
top_k=top_k,
top_p=top_p,
stop_sequences=stop_sequences,
ground_with_google_search=ground_with_google_search,
request_type=request_type,
)
return self._to_dataframe(result)

def generate_table(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. This acts on a single "prompt" column and would be better suited as a series accessor method.

self,
model: ml_base.BaseEstimator | str | pd.Series,
Comment thread
sycai marked this conversation as resolved.
*,
output_schema: str | Mapping[str, str],
temperature: float | None = None,
top_p: float | None = None,
max_output_tokens: int | None = None,
stop_sequences: List[str] | None = None,
request_type: str | None = None,
session: bigframes.session.Session | None = None,
) -> T:
"""
Generates a table using a BigQuery ML model.

This is an accessor for :func:`bigframes.bigquery.ai.generate_table`. See that
function's documentation for detailed parameter descriptions and examples.
"""
import bigframes.bigquery.ai

bf_df = self._bf_from_dataframe(session)
result = bigframes.bigquery.ai.generate_table(
model,
bf_df,
output_schema=output_schema,
temperature=temperature,
top_p=top_p,
max_output_tokens=max_output_tokens,
stop_sequences=stop_sequences,
request_type=request_type,
)
return self._to_dataframe(result)


class BigQueryDataFrameAccessor(AbstractBigQueryDataFrameAccessor[T, S]):
"""
Expand Down
Loading
Loading