@@ -601,6 +601,101 @@ def generate_text(
601601 return session .read_gbq_query (query )
602602
603603
604+ @log_adapter .method_logger (custom_base_name = "bigquery_ai" )
605+ def generate_table (
606+ model : Union [bigframes .ml .base .BaseEstimator , str , pd .Series ],
607+ data : Union [dataframe .DataFrame , series .Series , pd .DataFrame , pd .Series ],
608+ * ,
609+ output_schema : str ,
610+ temperature : Optional [float ] = None ,
611+ top_p : Optional [float ] = None ,
612+ max_output_tokens : Optional [int ] = None ,
613+ stop_sequences : Optional [List [str ]] = None ,
614+ request_type : Optional [str ] = None ,
615+ ) -> dataframe .DataFrame :
616+ """
617+ Generates a table using a BigQuery ML model.
618+
619+ See the `AI.GENERATE_TABLE function syntax
620+ <https://docs.cloud.google.com/bigquery/docs/reference/standard-sql/bigqueryml-syntax-generate-table>`_
621+ for additional reference.
622+
623+ **Examples:**
624+
625+ >>> import bigframes.pandas as bpd
626+ >>> import bigframes.bigquery as bbq
627+ >>> # The user is responsible for constructing a DataFrame that contains
628+ >>> # the necessary columns for the model's prompt. For example, a
629+ >>> # DataFrame with a 'prompt' column for text classification.
630+ >>> df = bpd.DataFrame({'prompt': ["some text to classify"]})
631+ >>> result = bbq.ai.generate_table(
632+ ... "project.dataset.model_name",
633+ ... data=df,
634+ ... output_schema="category STRING"
635+ ... ) # doctest: +SKIP
636+
637+ Args:
638+ model (bigframes.ml.base.BaseEstimator or str):
639+ The model to use for table generation.
640+ data (bigframes.pandas.DataFrame or bigframes.pandas.Series):
641+ The data to generate embeddings for. If a Series is provided, it is
642+ treated as the 'content' column. If a DataFrame is provided, it
643+ must contain a 'content' column, or you must rename the column you
644+ wish to embed to 'content'.
645+ output_schema (str):
646+ A string defining the output schema (e.g., "col1 STRING, col2 INT64").
647+ temperature (float, optional):
648+ A FLOAT64 value that is used for sampling promiscuity. The value
649+ must be in the range ``[0.0, 1.0]``.
650+ top_p (float, optional):
651+ A FLOAT64 value that changes how the model selects tokens for
652+ output.
653+ max_output_tokens (int, optional):
654+ An INT64 value that sets the maximum number of tokens in the
655+ generated table.
656+ stop_sequences (List[str], optional):
657+ An ARRAY<STRING> value that contains the stop sequences for the model.
658+ request_type (str, optional):
659+ A STRING value that contains the request type for the model.
660+
661+ Returns:
662+ bigframes.pandas.DataFrame:
663+ The generated table.
664+ """
665+ data = _to_dataframe (data , series_rename = "prompt" )
666+ model_name , session = bq_utils .get_model_name_and_session (model , data )
667+ table_sql = bq_utils .to_sql (data )
668+
669+ struct_fields_bq : Dict [str , bigframes .core .sql .literals .STRUCT_VALUES ] = {
670+ "output_schema" : output_schema
671+ }
672+ if temperature is not None :
673+ struct_fields_bq ["temperature" ] = temperature
674+ if top_p is not None :
675+ struct_fields_bq ["top_p" ] = top_p
676+ if max_output_tokens is not None :
677+ struct_fields_bq ["max_output_tokens" ] = max_output_tokens
678+ if stop_sequences is not None :
679+ struct_fields_bq ["stop_sequences" ] = stop_sequences
680+ if request_type is not None :
681+ struct_fields_bq ["request_type" ] = request_type
682+
683+ struct_sql = bigframes .core .sql .literals .struct_literal (struct_fields_bq )
684+ query = f"""
685+ SELECT *
686+ FROM AI.GENERATE_TABLE(
687+ MODEL `{ model_name } `,
688+ ({ table_sql } ),
689+ { struct_sql }
690+ )
691+ """
692+
693+ if session is None :
694+ return bpd .read_gbq_query (query )
695+ else :
696+ return session .read_gbq_query (query )
697+
698+
604699@log_adapter .method_logger (custom_base_name = "bigquery_ai" )
605700def if_ (
606701 prompt : PROMPT_TYPE ,
0 commit comments