-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(bigframes): add AI TVFs to the pandas bq accessor #17402
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ | |
| import pandas as pd | ||
|
|
||
| import bigframes.dataframe | ||
| import bigframes.ml.base as ml_base | ||
| import bigframes.series | ||
| import bigframes.session | ||
|
|
||
|
|
@@ -214,6 +215,107 @@ def generate_double( | |
| ) | ||
| return self._to_series(result) | ||
|
|
||
| def generate_embedding( | ||
| self, | ||
| model: ml_base.BaseEstimator | str | pd.Series, | ||
|
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( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
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( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
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]): | ||
| """ | ||
|
|
||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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