GeezerVison is showing github.com — feat: Update bigquery.ai.generate_table output_schema to allow Mappin… · google/bigframes@f7fd189
File treeExpand file treeCollapse file tree Open diff view settings bigframes/bigquery/_operations
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: bigframes/bigquery/_operations/ai.py +15-4Lines changed: 15 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -606,7 +606,7 @@ def generate_table( | 606 | 606 | model: Union[bigframes.ml.base.BaseEstimator, str, pd.Series], | 607 | 607 | data: Union[dataframe.DataFrame, series.Series, pd.DataFrame, pd.Series], | 608 | 608 | *, | 609 | | - output_schema: str, | | 609 | + output_schema: Union[str, Mapping[str, str]], | 610 | 610 | temperature: Optional[float] = None, | 611 | 611 | top_p: Optional[float] = None, | 612 | 612 | max_output_tokens: Optional[int] = None, | @@ -642,8 +642,10 @@ def generate_table( | 642 | 642 | treated as the 'prompt' column. If a DataFrame is provided, it | 643 | 643 | must contain a 'prompt' column, or you must rename the column you | 644 | 644 | wish to generate table to 'prompt'. | 645 | | - output_schema (str): | 646 | | - A string defining the output schema (e.g., "col1 STRING, col2 INT64"). | | 645 | + output_schema (str | Mapping[str, str]): | | 646 | + A string defining the output schema (e.g., "col1 STRING, col2 INT64"), | | 647 | + or a mapping value that specifies the schema of the output, in the form {field_name: data_type}. | | 648 | + Supported data types include `STRING`, `INT64`, `FLOAT64`, `BOOL`, `ARRAY`, and `STRUCT`. | 647 | 649 | temperature (float, optional): | 648 | 650 | A FLOAT64 value that is used for sampling promiscuity. The value | 649 | 651 | must be in the range ``[0.0, 1.0]``. | @@ -666,8 +668,17 @@ def generate_table( | 666 | 668 | model_name, session = bq_utils.get_model_name_and_session(model, data) | 667 | 669 | table_sql = bq_utils.to_sql(data) | 668 | 670 |
| | 671 | + if isinstance(output_schema, Mapping): | | 672 | + output_schema_str = ", ".join( | | 673 | + [f"{name} {sql_type}" for name, sql_type in output_schema.items()] | | 674 | + ) | | 675 | + # Validate user input | | 676 | + output_schemas.parse_sql_fields(output_schema_str) | | 677 | + else: | | 678 | + output_schema_str = output_schema | | 679 | + | 669 | 680 | struct_fields_bq: Dict[str, bigframes.core.sql.literals.STRUCT_VALUES] = { | 670 | | - "output_schema": output_schema | | 681 | + "output_schema": output_schema_str | 671 | 682 | } | 672 | 683 | if temperature is not None: | 673 | 684 | struct_fields_bq["temperature"] = temperature | |
Collapse file Copy file name to clipboardExpand all lines: tests/system/large/bigquery/test_ai.py +17Lines changed: 17 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -111,3 +111,20 @@ def test_generate_table(text_model): | 111 | 111 | assert "creator" in result.columns | 112 | 112 | # The model may not always return the exact number of rows requested. | 113 | 113 | assert len(result) > 0 | | 114 | + | | 115 | + | | 116 | +def test_generate_table_with_mapping_schema(text_model): | | 117 | + df = bpd.DataFrame( | | 118 | + {"prompt": ["Generate a table of 2 programming languages and their creators."]} | | 119 | + ) | | 120 | + | | 121 | + result = ai.generate_table( | | 122 | + text_model, | | 123 | + df, | | 124 | + output_schema={"language": "STRING", "creator": "STRING"}, | | 125 | + ) | | 126 | + | | 127 | + assert "language" in result.columns | | 128 | + assert "creator" in result.columns | | 129 | + # The model may not always return the exact number of rows requested. | | 130 | + assert len(result) > 0 |
Collapse file Copy file name to clipboardExpand all lines: tests/unit/bigquery/test_ai.py +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -269,6 +269,32 @@ def test_generate_table_with_options(mock_dataframe, mock_session): | 269 | 269 | ) | 270 | 270 |
| 271 | 271 |
| | 272 | +def test_generate_table_with_mapping_schema(mock_dataframe, mock_session): | | 273 | + model_name = "project.dataset.model" | | 274 | + | | 275 | + bbq.ai.generate_table( | | 276 | + model_name, | | 277 | + mock_dataframe, | | 278 | + output_schema={"col1": "STRING", "col2": "INT64"}, | | 279 | + ) | | 280 | + | | 281 | + mock_session.read_gbq_query.assert_called_once() | | 282 | + query = mock_session.read_gbq_query.call_args[0][0] | | 283 | + | | 284 | + # Normalize whitespace for comparison | | 285 | + query = " ".join(query.split()) | | 286 | + | | 287 | + expected_part_1 = "SELECT * FROM AI.GENERATE_TABLE(" | | 288 | + expected_part_2 = f"MODEL `{model_name}`," | | 289 | + expected_part_3 = "(SELECT * FROM my_table)," | | 290 | + expected_part_4 = "STRUCT('col1 STRING, col2 INT64' AS output_schema)" | | 291 | + | | 292 | + assert expected_part_1 in query | | 293 | + assert expected_part_2 in query | | 294 | + assert expected_part_3 in query | | 295 | + assert expected_part_4 in query | | 296 | + | | 297 | + | 272 | 298 | @mock.patch("bigframes.pandas.read_pandas") | 273 | 299 | def test_generate_text_with_pandas_dataframe( | 274 | 300 | read_pandas_mock, mock_dataframe, mock_session | |
|