bigquery-data-science 0.7.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bigquery_data_science/__init__.py +58 -0
- bigquery_data_science/_skills/PLANS.md +76 -0
- bigquery_data_science/_skills/bigquery-ai/SKILL.md +66 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_agg.md +528 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_classify.md +450 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_count_tokens.md +180 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_detect_anomalies.md +322 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_embed.md +425 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_evaluate.md +368 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_forecast.md +437 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_generate.md +693 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_generate_bool.md +248 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_generate_double.md +254 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_generate_embedding.md +402 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_generate_int.md +252 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_generate_table.md +447 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_generate_text.md +356 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_if.md +373 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_key_drivers.md +331 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_parse_document.md +416 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_predict.md +559 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_score.md +310 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_search.md +465 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_similarity.md +371 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/catalog_search.md +951 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/content_analysis.md +284 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/content_moderation.md +369 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/data_enrichment.md +426 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/document_intelligence.md +470 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/document_rag.md +453 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/image_deduplication.md +770 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/log_analysis.md +668 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/metric_diagnostics.md +568 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ml_annotate_image.md +680 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ml_generate_embedding.md +217 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ml_generate_text.md +223 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ml_process_document.md +392 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ml_transcribe.md +815 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ml_translate.md +600 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ml_understand_text.md +673 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/multimodal_analysis.md +266 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/rag_pipeline.md +593 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/semantic_search.md +510 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/tabular_prediction.md +485 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/time_series_intelligence.md +489 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/vector_search.md +672 -0
- bigquery_data_science/_skills/bigquery-ai/reference/augmented-analytics.md +77 -0
- bigquery_data_science/_skills/bigquery-ai/reference/cloud-ai-service-models.md +88 -0
- bigquery_data_science/_skills/bigquery-ai/reference/document-processing.md +95 -0
- bigquery_data_science/_skills/bigquery-ai/reference/embeddings-and-search.md +166 -0
- bigquery_data_science/_skills/bigquery-ai/reference/general-purpose-generation.md +87 -0
- bigquery_data_science/_skills/bigquery-ai/reference/managed-functions.md +72 -0
- bigquery_data_science/_skills/bigquery-ai/reference/predictive-ai.md +113 -0
- bigquery_data_science/_skills/bigquery-ai/reference/workflows.md +56 -0
- bigquery_data_science/_skills/bigquery-ai/skill.manifest.json +109 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/INDEX.md +71 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aiagg.md +67 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aicausal_effect-covered-in-bigquery-ml.md +17 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aiclassify.md +72 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aicount_tokens.md +53 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aidetect_anomalies.md +63 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aiembed.md +85 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aievaluate.md +114 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aiforecast.md +123 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aigenerate.md +69 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aigenerate_bool.md +53 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aigenerate_double.md +45 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aigenerate_embedding.md +108 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aigenerate_int.md +45 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aigenerate_table.md +64 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aigenerate_text.md +167 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aiif.md +56 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aikey_drivers.md +76 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aiparse_document.md +91 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aipredict.md +74 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aiscore.md +52 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aisearch.md +66 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aisimilarity.md +79 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/choosing-between-these-and-the-ai-functions.md +11 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/hybrid-search-capability.md +165 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/mlannotate_image.md +24 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/mlgenerate_embedding.md +34 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/mlgenerate_text.md +65 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/mlprocess_document.md +125 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/mltranscribe.md +26 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/mltranslate.md +27 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/mlunderstand_text.md +32 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/multimodal-input-patterns-by-function.md +128 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/object-tables.md +109 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/objectref-and-objectrefruntime-schema-reference.md +50 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/objfetch_metadata.md +49 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/objget_access_url.md +66 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/objget_read_url.md +40 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/objlist.md +34 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/objmake_ref.md +58 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/per-row-failures-and-the-retry-pattern.md +19 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/setup-apis-connection-roles.md +39 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/two-input-shapes.md +22 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/vector_search.md +78 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/what-the-five-have-in-common.md +27 -0
- bigquery_data_science/_skills/bigquery-data-science/SKILL.md +69 -0
- bigquery_data_science/_skills/bigquery-data-science/reference/namespace-matrix.md +112 -0
- bigquery_data_science/_skills/bigquery-data-science/skill.manifest.json +13 -0
- bigquery_data_science/_skills/bigquery-ml/SKILL.md +70 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/airflow_with_kfp.md +379 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/anomaly_fraud_detection.md +195 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/arima_plus.md +635 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/arima_plus_xreg.md +453 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/autoencoder.md +470 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/automl_classifier.md +291 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/automl_regressor.md +274 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/boosted_tree_classifier.md +449 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/boosted_tree_regressor.md +400 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/bucketizing.md +201 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/causal_effect.md +773 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/churn_retention.md +271 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/cloud_workflows.md +370 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/composer_airflow.md +475 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/contribution_analysis.md +346 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/cross_validation.md +185 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/customer_segmentation.md +233 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/dataform.md +372 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/dbt.md +283 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/difference_in_differences.md +280 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/distance.md +195 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/dnn_classifier.md +326 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/dnn_regressor.md +308 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/embeddings_classification.md +426 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/encoding.md +238 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/ensembling.md +278 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/evaluation.md +445 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/exploration.md +779 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/export.md +321 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/feature_engineering.md +280 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/feature_store.md +652 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/feature_store_workflow.md +426 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/ga4_churn_prediction.md +285 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/hierarchical_forecasting.md +441 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/image.md +791 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/imported.md +426 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/kmeans.md +348 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/linear_regression.md +372 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/logistic_regression.md +390 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/matrix_factorization.md +404 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/model_monitoring.md +609 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/pca.md +357 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/price_elasticity_dml.md +286 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/propensity_score_matching.md +350 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/random_forest_classifier.md +445 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/random_forest_regressor.md +409 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/recommendation.md +281 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/regression_based_forecasting.md +491 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/remote.md +428 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/scalers.md +325 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/scheduled_queries.md +284 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/sql_scripting.md +243 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/survival_analysis.md +363 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/synthetic_control.md +221 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/text.md +215 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/time_series.md +669 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/transform_only.md +273 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/uplift_cate.md +241 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/vertex_kfp.md +360 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/wide_and_deep_classifier.md +316 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/wide_and_deep_regressor.md +298 -0
- bigquery_data_science/_skills/bigquery-ml/reference/classification.md +80 -0
- bigquery_data_science/_skills/bigquery-ml/reference/model-management.md +90 -0
- bigquery_data_science/_skills/bigquery-ml/reference/preprocessing-functions.md +234 -0
- bigquery_data_science/_skills/bigquery-ml/reference/regression.md +79 -0
- bigquery_data_science/_skills/bigquery-ml/reference/unsupervised-and-specialized.md +101 -0
- bigquery_data_science/_skills/bigquery-ml/reference/workflows-and-pipelines.md +111 -0
- bigquery_data_science/_skills/bigquery-ml/skill.manifest.json +177 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/1-model_type-catalog.md +28 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/2-task-mlevaluate-metric-columns.md +26 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/3-capability-matrix-per-model-type.md +21 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/4-explainability-weights-functions.md +14 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/5-connection-matrix.md +11 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/6-which-model-free-function-no-create-model-no-connection.md +15 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/7-point-in-time-feature-retrieval-which-of-the-two.md +11 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/INDEX.md +111 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/aicausal_effect.md +85 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/arima_plus-forecasting-lifecycle-functions.md +14 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/arima_plus-univariate-time-series-forecasting.md +132 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/arima_plus_xreg-multivariate-time-series-with-external-regressors.md +103 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/autoencoder.md +102 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/automl_classifier-automl_regressor.md +88 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/boosted_tree_classifier-boosted_tree_regressor.md +117 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/contribution_analysis.md +113 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/dnn_classifier-dnn_regressor.md +107 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/dnn_linear_combined_classifier-dnn_linear_combined_regressor-wide-and-deep.md +98 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/explainability-functions-mlexplain_predict-mlglobal_explain-mlfeature_importance.md +7 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/exploratory-data-analysis-mldescribe_data-mlcorrelation.md +11 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/export-model.md +92 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/feature-engineering-standalone-preprocessing-functions.md +13 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/image-preprocessing-functions-mldecode_image-mlresize_image-mlconvert_image_type-mlconvert_color_space.md +63 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/imported-models-tensorflow-tensorflow_lite-onnx-xgboost.md +36 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/kmeans.md +87 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/linear_reg-logistic_reg-generalized-linear-models.md +101 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/matrix_factorization.md +162 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mladvanced_weights.md +76 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlarima_coefficients.md +30 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlarima_evaluate.md +49 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlbag_of_words.md +47 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlbucketize.md +47 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlcentroids.md +46 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlconfusion_matrix.md +56 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlconvert_color_space.md +53 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlconvert_image_type.md +39 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlcorrelation.md +78 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mldecode_image.md +44 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mldescribe_data.md +74 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mldetect_anomalies.md +86 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mldistance.md +48 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlentity_features_at_time.md +56 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlevaluate.md +72 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlexplain_forecast.md +50 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlexplain_predict.md +66 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlfeature_cross.md +46 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlfeature_importance.md +47 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlfeature_info.md +50 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlfeatures_at_time.md +51 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlforecast.md +76 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlgenerate_embedding-from-a-bqml-pca-autoencoder-matrix_factorization-model.md +64 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlglobal_explain.md +63 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlhash_bucketize.md +47 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlholiday_info.md +46 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlimputer.md +46 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mllp_norm.md +47 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlmetrics.md +80 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlngrams.md +37 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlone_hot_encoder-mlmulti_hot_encoder-mllabel_encoder.md +67 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlpolynomial_expand.md +47 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlpredict.md +67 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlprincipal_component_info.md +42 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlprincipal_components.md +40 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlquantile_bucketize.md +43 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlrecommend.md +60 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlreconstruction_loss.md +48 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlresize_image.md +45 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlroc_curve.md +63 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mltf_idf.md +37 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mltfdv_describe.md +34 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mltfdv_validate.md +48 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mltraining_info.md +78 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mltransform-applying-a-saved-transform.md +52 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mltranspose-not-a-function-pointer.md +32 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mltrial_info.md +60 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlvalidate_data_drift.md +99 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlvalidate_data_skew.md +50 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlweights.md +84 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/model-free-time-series-decomposition-mltrend-mlseasonality-mldetect_change_points.md +100 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/model-introspection-mlfeature_info-mltraining_info-mltrial_info.md +7 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/model-monitoring-data-validation.md +23 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/numerical-scalers-mlstandard_scaler-mlmin_max_scaler-mlmax_abs_scaler-mlrobust_scaler-mlnormalizer.md +77 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/onnx.md +55 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/pca.md +91 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/point-in-time-feature-retrieval-mlfeatures_at_time-mlentity_features_at_time.md +25 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/quick-comparison.md +14 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/random_forest_classifier-random_forest_regressor.md +123 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/remote-remote-model-over-a-vertex-ai-endpoint-the-mechanism.md +87 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/tensorflow.md +48 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/tensorflow_lite.md +29 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/text-preprocessing-functions-mlngrams-mltf_idf-mlbag_of_words.md +18 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/timesfm-built-in-foundation-forecaster-see-bigquery-ai.md +28 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/transform_only.md +91 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/xgboost.md +72 -0
- bigquery_data_science/_skills/pinned-models.json +28 -0
- bigquery_data_science/catalog.py +1107 -0
- bigquery_data_science/cli.py +786 -0
- bigquery_data_science/installer.py +282 -0
- bigquery_data_science/mcp_server.py +317 -0
- bigquery_data_science/tooling/__init__.py +15 -0
- bigquery_data_science/tooling/convert_notebook.py +176 -0
- bigquery_data_science/tooling/link_policy.py +247 -0
- bigquery_data_science/tooling/manifest.py +152 -0
- bigquery_data_science/tooling/markdown_anchors.py +84 -0
- bigquery_data_science/tooling/narrative_drift.py +163 -0
- bigquery_data_science/tooling/pinned_models.py +216 -0
- bigquery_data_science/tooling/reference_structure.py +85 -0
- bigquery_data_science/tooling/sync_syntax.py +407 -0
- bigquery_data_science/tooling/validate.py +208 -0
- bigquery_data_science-0.7.0.dist-info/METADATA +306 -0
- bigquery_data_science-0.7.0.dist-info/RECORD +286 -0
- bigquery_data_science-0.7.0.dist-info/WHEEL +4 -0
- bigquery_data_science-0.7.0.dist-info/entry_points.txt +5 -0
- bigquery_data_science-0.7.0.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
> **Source (`statmike/bigquery-data-science`):** **Notebook:** [`bigquery-ai/functions/ai_agg/ai_agg.ipynb`](https://github.com/statmike/bigquery-data-science/blob/main/bigquery-ai/functions/ai_agg/ai_agg.ipynb) · **SQL:** [`bigquery-ai/functions/ai_agg/ai_agg.sql`](https://github.com/statmike/bigquery-data-science/blob/main/bigquery-ai/functions/ai_agg/ai_agg.sql)
|
|
2
|
+
|
|
3
|
+
# AI.AGG — BigQuery AI Functions
|
|
4
|
+
|
|
5
|
+
`AI.AGG` is an aggregate function that uses a Gemini model to aggregate data based on natural language instructions. It automatically handles multi-level batching, so it can analyze data that exceeds the Gemini context window.
|
|
6
|
+
|
|
7
|
+
**When to use it:**
|
|
8
|
+
- You need to summarize, analyze, or find patterns across groups of rows
|
|
9
|
+
- Your data may exceed the Gemini context window (AI.AGG auto-batches)
|
|
10
|
+
- You want one result per GROUP BY group, not one per row
|
|
11
|
+
|
|
12
|
+
**Alternatives:**
|
|
13
|
+
- `functions/ai_generate` (`AI.GENERATE`) — Per-row generation. For aggregation, you'd need to manually batch data into a single prompt (e.g., with `STRING_AGG` or `ARRAY_AGG` + `TO_JSON_STRING`)
|
|
14
|
+
- `functions/ai_generate_text` (`AI.GENERATE_TEXT`) — Same manual batching issue, but supports non-Gemini models
|
|
15
|
+
|
|
16
|
+
**Key differences from AI.GENERATE:**
|
|
17
|
+
- `AI.AGG` is an **aggregate** function (like `SUM`, `COUNT`) — use with `GROUP BY`
|
|
18
|
+
- `AI.GENERATE` is a **scalar** function — returns one result per input row
|
|
19
|
+
- `AI.AGG` automatically batches large datasets; `AI.GENERATE` requires manual batching
|
|
20
|
+
|
|
21
|
+
**Featured in:** `workflows/content_analysis` (Content Analysis Pipeline) | `workflows/content_moderation` (Content Moderation) | `workflows/document_intelligence` (Document Intelligence) | `workflows/log_analysis` (Log Analysis)
|
|
22
|
+
|
|
23
|
+
**Multimodal:** Supports image input via `reference/unstructured-data-infrastructure.md#objectref-and-objectrefruntime-schema-reference` (ObjectRef). Pass a STRUCT input with `ObjectRef` fields to aggregate over images.
|
|
24
|
+
|
|
25
|
+
**References:** `RESOURCES.md` (Full syntax reference) | [Official documentation](https://cloud.google.com/bigquery/docs/reference/standard-sql/bigqueryml-syntax-ai-agg) | `setup` (Setup guide)
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
## Setup
|
|
29
|
+
|
|
30
|
+
Set your project and location, authenticate, and create a temporary dataset for this notebook.
|
|
31
|
+
|
|
32
|
+
> This function doesn't require a connection or model for basic SQL usage — it uses end-user credentials automatically. A connection is needed for the `connection_id` parameter and multimodal examples. See the `setup` (Setup Reference) for details.
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
PROJECT_ID = 'statmike-mlops-349915' # <-- Replace with your project ID
|
|
36
|
+
LOCATION = 'US' # BigQuery dataset location
|
|
37
|
+
DATASET_ID = 'bq_ai_functions' # Shared dataset across all notebooks
|
|
38
|
+
CONNECTION_ID = 'bq_ai_functions' # Shared connection (for connection_id and multimodal examples)
|
|
39
|
+
BUCKET = PROJECT_ID # GCS bucket for multimodal examples
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Environment
|
|
43
|
+
|
|
44
|
+
> **Already set up the project environment?** Skip to [Examples](#examples--sql).
|
|
45
|
+
> **Running standalone** (Colab, Colab Enterprise, Vertex AI Workbench)? Run the cells below to install packages, authenticate, and create the shared dataset. See the `setup` (Setup Reference) for details.
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
from google.cloud import bigquery
|
|
49
|
+
import pandas as pd
|
|
50
|
+
|
|
51
|
+
client = bigquery.Client(project=PROJECT_ID)
|
|
52
|
+
pd.set_option('display.max_colwidth', None)
|
|
53
|
+
|
|
54
|
+
# Create the shared dataset (idempotent)
|
|
55
|
+
dataset_ref = bigquery.DatasetReference(PROJECT_ID, DATASET_ID)
|
|
56
|
+
dataset = bigquery.Dataset(dataset_ref)
|
|
57
|
+
dataset.location = LOCATION
|
|
58
|
+
client.create_dataset(dataset, exists_ok=True)
|
|
59
|
+
print(f'Dataset {PROJECT_ID}.{DATASET_ID} ready')
|
|
60
|
+
|
|
61
|
+
# Register %%bigquery cell magic (auto-loaded in Colab, needed elsewhere)
|
|
62
|
+
%load_ext bigquery_magics
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
import subprocess as _sp, json as _json
|
|
67
|
+
|
|
68
|
+
# Create connection (idempotent)
|
|
69
|
+
_sp.run(['bq', 'mk', '--connection', '--location', LOCATION,
|
|
70
|
+
'--connection_type', 'CLOUD_RESOURCE',
|
|
71
|
+
'--project_id', PROJECT_ID, CONNECTION_ID],
|
|
72
|
+
capture_output=True, text=True)
|
|
73
|
+
|
|
74
|
+
# Get service account and grant required roles
|
|
75
|
+
r = _sp.run(['bq', 'show', '--connection', '--format=json',
|
|
76
|
+
'--project_id', PROJECT_ID, '--location', LOCATION, CONNECTION_ID],
|
|
77
|
+
capture_output=True, text=True, check=True)
|
|
78
|
+
sa = _json.loads(r.stdout)['cloudResource']['serviceAccountId']
|
|
79
|
+
for role in ['roles/aiplatform.user', 'roles/storage.objectViewer']:
|
|
80
|
+
_sp.run(['gcloud', 'projects', 'add-iam-policy-binding', PROJECT_ID,
|
|
81
|
+
f'--member=serviceAccount:{sa}', f'--role={role}', '--quiet'],
|
|
82
|
+
capture_output=True, text=True)
|
|
83
|
+
print(f'Connection {CONNECTION_ID} ready (SA: {sa})')
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
## Examples — SQL
|
|
88
|
+
|
|
89
|
+
Progressive examples from simplest to most advanced. Each cell adds one new concept.
|
|
90
|
+
|
|
91
|
+
`AI.AGG` is an **aggregate function** — it works like `SUM()` or `COUNT()`. Without `GROUP BY`, it aggregates all rows into one result. With `GROUP BY`, it returns one result per group.
|
|
92
|
+
|
|
93
|
+
### 1. Simplest call — aggregate without GROUP BY
|
|
94
|
+
|
|
95
|
+
Without `GROUP BY`, `AI.AGG` aggregates all input rows into a single summary.
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
query = """
|
|
99
|
+
SELECT
|
|
100
|
+
AI.AGG(
|
|
101
|
+
review,
|
|
102
|
+
'What is the overall sentiment of these reviews?'
|
|
103
|
+
) AS sentiment_summary
|
|
104
|
+
FROM UNNEST([
|
|
105
|
+
'Absolutely love this product! Best purchase ever.',
|
|
106
|
+
'Terrible quality, broke after one week.',
|
|
107
|
+
'Pretty decent for the price. Nothing fancy.',
|
|
108
|
+
'Would not recommend. Waste of money.',
|
|
109
|
+
'Exceeded my expectations in every way!'
|
|
110
|
+
]) AS review
|
|
111
|
+
"""
|
|
112
|
+
df = client.query(query).to_dataframe()
|
|
113
|
+
print(df.iloc[0]['sentiment_summary'])
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### 2. GROUP BY — one summary per group
|
|
117
|
+
|
|
118
|
+
`AI.AGG` returns one STRING per group, making it ideal for summarizing data by category.
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
query = """
|
|
122
|
+
SELECT
|
|
123
|
+
category,
|
|
124
|
+
AI.AGG(
|
|
125
|
+
feedback,
|
|
126
|
+
'Summarize the main themes in this feedback.'
|
|
127
|
+
) AS themes
|
|
128
|
+
FROM UNNEST([
|
|
129
|
+
STRUCT('product' AS category, 'The battery life is incredible' AS feedback),
|
|
130
|
+
STRUCT('product', 'Screen quality is outstanding'),
|
|
131
|
+
STRUCT('product', 'Too heavy to carry around comfortably'),
|
|
132
|
+
STRUCT('shipping', 'Arrived two days early, great packaging'),
|
|
133
|
+
STRUCT('shipping', 'Package was damaged during transit'),
|
|
134
|
+
STRUCT('shipping', 'Delivery took three weeks instead of five days'),
|
|
135
|
+
STRUCT('support', 'Agent resolved my issue in minutes'),
|
|
136
|
+
STRUCT('support', 'Was on hold for over an hour before getting help'),
|
|
137
|
+
STRUCT('support', 'Very knowledgeable and patient support team')
|
|
138
|
+
]) AS t
|
|
139
|
+
GROUP BY category
|
|
140
|
+
"""
|
|
141
|
+
client.query(query).to_dataframe()
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### 3. DISTINCT — deduplicate before aggregating
|
|
145
|
+
|
|
146
|
+
Use `DISTINCT` to remove duplicate inputs before sending them to the model.
|
|
147
|
+
|
|
148
|
+
```python
|
|
149
|
+
query = """
|
|
150
|
+
SELECT
|
|
151
|
+
AI.AGG(
|
|
152
|
+
DISTINCT comment,
|
|
153
|
+
'What topics do these comments discuss?'
|
|
154
|
+
) AS topics
|
|
155
|
+
FROM UNNEST([
|
|
156
|
+
'Great performance on the new update',
|
|
157
|
+
'Great performance on the new update',
|
|
158
|
+
'Battery drain is still an issue',
|
|
159
|
+
'Battery drain is still an issue',
|
|
160
|
+
'Love the new camera features',
|
|
161
|
+
'UI feels sluggish after the patch'
|
|
162
|
+
]) AS comment
|
|
163
|
+
"""
|
|
164
|
+
df = client.query(query).to_dataframe()
|
|
165
|
+
print(df.iloc[0]['topics'])
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### 4. TO_JSON_STRING — aggregate structured rows
|
|
169
|
+
|
|
170
|
+
Use `TO_JSON_STRING` to pass multiple columns as context. The model sees each row's full structure.
|
|
171
|
+
|
|
172
|
+
```python
|
|
173
|
+
query = """
|
|
174
|
+
WITH tickets AS (
|
|
175
|
+
SELECT *
|
|
176
|
+
FROM UNNEST([
|
|
177
|
+
STRUCT('Alice' AS agent, 'password reset' AS issue, 2 AS resolution_minutes),
|
|
178
|
+
STRUCT('Alice', 'account locked', 5),
|
|
179
|
+
STRUCT('Bob', 'billing error', 15),
|
|
180
|
+
STRUCT('Bob', 'refund request', 30),
|
|
181
|
+
STRUCT('Bob', 'subscription cancel', 8),
|
|
182
|
+
STRUCT('Carol', 'login failure', 3),
|
|
183
|
+
STRUCT('Carol', 'two-factor setup', 7)
|
|
184
|
+
])
|
|
185
|
+
)
|
|
186
|
+
SELECT
|
|
187
|
+
agent,
|
|
188
|
+
AI.AGG(
|
|
189
|
+
TO_JSON_STRING(STRUCT(agent, issue, resolution_minutes)),
|
|
190
|
+
'Analyze this support agent performance. What types of issues do they handle and how quickly?'
|
|
191
|
+
) AS performance_summary
|
|
192
|
+
FROM tickets
|
|
193
|
+
GROUP BY agent
|
|
194
|
+
"""
|
|
195
|
+
client.query(query).to_dataframe()
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### 5. Finding patterns across data
|
|
199
|
+
|
|
200
|
+
`AI.AGG` excels at root cause analysis and pattern identification across structured log data.
|
|
201
|
+
|
|
202
|
+
```python
|
|
203
|
+
query = """
|
|
204
|
+
WITH error_logs AS (
|
|
205
|
+
SELECT *
|
|
206
|
+
FROM UNNEST([
|
|
207
|
+
STRUCT('2025-01-15 08:23:01' AS timestamp, 'auth-service' AS service, 'Connection timeout to user DB' AS message),
|
|
208
|
+
STRUCT('2025-01-15 08:23:05', 'auth-service', 'Failed to validate token: DB unavailable'),
|
|
209
|
+
STRUCT('2025-01-15 08:23:12', 'api-gateway', 'Upstream auth-service returned 503'),
|
|
210
|
+
STRUCT('2025-01-15 08:23:15', 'api-gateway', 'Circuit breaker opened for auth-service'),
|
|
211
|
+
STRUCT('2025-01-15 08:24:00', 'payment-service', 'Transaction failed: auth token invalid'),
|
|
212
|
+
STRUCT('2025-01-15 08:24:02', 'notification-service', 'Unable to send email: rate limit exceeded'),
|
|
213
|
+
STRUCT('2025-01-15 08:25:00', 'auth-service', 'Connection to user DB restored'),
|
|
214
|
+
STRUCT('2025-01-15 08:25:01', 'api-gateway', 'Circuit breaker closed for auth-service')
|
|
215
|
+
])
|
|
216
|
+
)
|
|
217
|
+
SELECT
|
|
218
|
+
AI.AGG(
|
|
219
|
+
TO_JSON_STRING(STRUCT(timestamp, service, message)),
|
|
220
|
+
'Analyze these error logs. What was the root cause, which services were affected, and what was the sequence of events?'
|
|
221
|
+
) AS incident_analysis
|
|
222
|
+
FROM error_logs
|
|
223
|
+
"""
|
|
224
|
+
df = client.query(query).to_dataframe()
|
|
225
|
+
print(df.iloc[0]['incident_analysis'])
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### 6. Specifying an endpoint
|
|
229
|
+
|
|
230
|
+
Override the default model with any Gemini model that doesn't require thinking budget.
|
|
231
|
+
|
|
232
|
+
```python
|
|
233
|
+
query = """
|
|
234
|
+
SELECT
|
|
235
|
+
AI.AGG(
|
|
236
|
+
review,
|
|
237
|
+
'What do customers like and dislike? Be concise.',
|
|
238
|
+
endpoint => 'gemini-2.5-flash'
|
|
239
|
+
) AS summary
|
|
240
|
+
FROM UNNEST([
|
|
241
|
+
'Fast delivery but the box was crushed.',
|
|
242
|
+
'Product works great, very happy with it.',
|
|
243
|
+
'Instructions were confusing but the item itself is solid.'
|
|
244
|
+
]) AS review
|
|
245
|
+
"""
|
|
246
|
+
df = client.query(query).to_dataframe()
|
|
247
|
+
print(df.iloc[0]['summary'])
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### 7. Using a public dataset
|
|
251
|
+
|
|
252
|
+
Analyze real IMDB reviews grouped by movie — demonstrates AI.AGG's ability to handle large groups with automatic batching.
|
|
253
|
+
|
|
254
|
+
```python
|
|
255
|
+
query = """
|
|
256
|
+
SELECT
|
|
257
|
+
title,
|
|
258
|
+
movie_id,
|
|
259
|
+
AI.AGG(
|
|
260
|
+
review,
|
|
261
|
+
'Summarize the overall sentiment towards this movie.'
|
|
262
|
+
) AS sentiment
|
|
263
|
+
FROM `bigquery-public-data.imdb.reviews`
|
|
264
|
+
WHERE movie_id IN ('tt0339384', 'tt0084787', 'tt0029850')
|
|
265
|
+
GROUP BY movie_id, title
|
|
266
|
+
"""
|
|
267
|
+
client.query(query).to_dataframe()
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
### 8. Using connection_id — service account credentials
|
|
271
|
+
|
|
272
|
+
By default, `AI.AGG` uses end-user credentials. The `connection_id` parameter lets you use a Cloud resource connection's service account instead — useful for shared environments, production pipelines, or when end-user credentials don't have Vertex AI access.
|
|
273
|
+
|
|
274
|
+
```python
|
|
275
|
+
query = f"""
|
|
276
|
+
SELECT
|
|
277
|
+
AI.AGG(
|
|
278
|
+
review,
|
|
279
|
+
'What is the overall sentiment of these reviews?',
|
|
280
|
+
connection_id => '{PROJECT_ID}.{LOCATION}.{CONNECTION_ID}'
|
|
281
|
+
) AS sentiment_summary
|
|
282
|
+
FROM UNNEST([
|
|
283
|
+
'Absolutely love this product! Best purchase ever.',
|
|
284
|
+
'Terrible quality, broke after one week.',
|
|
285
|
+
'Pretty decent for the price. Nothing fancy.',
|
|
286
|
+
'Would not recommend. Waste of money.',
|
|
287
|
+
'Exceeded my expectations in every way!'
|
|
288
|
+
]) AS review
|
|
289
|
+
"""
|
|
290
|
+
df = client.query(query).to_dataframe()
|
|
291
|
+
print(df.iloc[0]['sentiment_summary'])
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
### 9. Quantitative aggregation — counting and ranking
|
|
295
|
+
|
|
296
|
+
`AI.AGG` isn't limited to text summaries. It can count, rank, compare quantities, and extract structured insights from data — treating the model as an analyst, not just a summarizer.
|
|
297
|
+
|
|
298
|
+
```python
|
|
299
|
+
query = """
|
|
300
|
+
WITH tasks AS (
|
|
301
|
+
SELECT *
|
|
302
|
+
FROM UNNEST([
|
|
303
|
+
STRUCT('backend' AS team, 'critical' AS priority, 'Fix auth token expiry' AS task),
|
|
304
|
+
STRUCT('backend', 'critical', 'Patch SQL injection vulnerability'),
|
|
305
|
+
STRUCT('backend', 'high', 'Optimize query performance'),
|
|
306
|
+
STRUCT('backend', 'medium', 'Add request logging'),
|
|
307
|
+
STRUCT('frontend', 'critical', 'Fix checkout form validation'),
|
|
308
|
+
STRUCT('frontend', 'high', 'Implement dark mode'),
|
|
309
|
+
STRUCT('frontend', 'high', 'Add accessibility labels'),
|
|
310
|
+
STRUCT('frontend', 'low', 'Update footer copyright year'),
|
|
311
|
+
STRUCT('data', 'critical', 'Fix broken ETL pipeline'),
|
|
312
|
+
STRUCT('data', 'high', 'Add data quality checks'),
|
|
313
|
+
STRUCT('data', 'medium', 'Document schema changes'),
|
|
314
|
+
STRUCT('data', 'medium', 'Archive old partitions')
|
|
315
|
+
])
|
|
316
|
+
)
|
|
317
|
+
SELECT
|
|
318
|
+
AI.AGG(
|
|
319
|
+
TO_JSON_STRING(STRUCT(team, priority, task)),
|
|
320
|
+
'Count the tasks by priority level for each team. Which team has the most critical items? Present as a concise breakdown.'
|
|
321
|
+
) AS task_analysis
|
|
322
|
+
FROM tasks
|
|
323
|
+
"""
|
|
324
|
+
df = client.query(query).to_dataframe()
|
|
325
|
+
print(df.iloc[0]['task_analysis'])
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
### 10. AI.AGG vs manual STRING_AGG + AI.GENERATE
|
|
329
|
+
|
|
330
|
+
Before `AI.AGG`, aggregating data required manually concatenating rows into a single prompt with `STRING_AGG` + `AI.GENERATE`. This works for small datasets but breaks when data exceeds the context window. `AI.AGG` handles batching automatically.
|
|
331
|
+
|
|
332
|
+
Compare both approaches on the same data:
|
|
333
|
+
|
|
334
|
+
```python
|
|
335
|
+
query = """
|
|
336
|
+
WITH feedback AS (
|
|
337
|
+
SELECT *
|
|
338
|
+
FROM UNNEST([
|
|
339
|
+
STRUCT('product' AS category, 'The battery life is incredible' AS comment),
|
|
340
|
+
STRUCT('product', 'Screen quality is outstanding'),
|
|
341
|
+
STRUCT('product', 'Too heavy to carry around comfortably'),
|
|
342
|
+
STRUCT('shipping', 'Arrived two days early, great packaging'),
|
|
343
|
+
STRUCT('shipping', 'Package was damaged during transit'),
|
|
344
|
+
STRUCT('shipping', 'Delivery took three weeks instead of five days')
|
|
345
|
+
])
|
|
346
|
+
),
|
|
347
|
+
|
|
348
|
+
-- Approach 1: Manual STRING_AGG + AI.GENERATE (the old way)
|
|
349
|
+
manual_approach AS (
|
|
350
|
+
SELECT
|
|
351
|
+
category,
|
|
352
|
+
(AI.GENERATE(
|
|
353
|
+
CONCAT(
|
|
354
|
+
'Summarize the main themes in this feedback: ',
|
|
355
|
+
STRING_AGG(comment, ' | ')
|
|
356
|
+
)
|
|
357
|
+
)).result AS themes
|
|
358
|
+
FROM feedback
|
|
359
|
+
GROUP BY category
|
|
360
|
+
),
|
|
361
|
+
|
|
362
|
+
-- Approach 2: AI.AGG (the better way)
|
|
363
|
+
agg_approach AS (
|
|
364
|
+
SELECT
|
|
365
|
+
category,
|
|
366
|
+
AI.AGG(comment, 'Summarize the main themes in this feedback.') AS themes
|
|
367
|
+
FROM feedback
|
|
368
|
+
GROUP BY category
|
|
369
|
+
)
|
|
370
|
+
|
|
371
|
+
SELECT
|
|
372
|
+
'STRING_AGG + AI.GENERATE' AS approach,
|
|
373
|
+
category, themes
|
|
374
|
+
FROM manual_approach
|
|
375
|
+
UNION ALL
|
|
376
|
+
SELECT
|
|
377
|
+
'AI.AGG' AS approach,
|
|
378
|
+
category, themes
|
|
379
|
+
FROM agg_approach
|
|
380
|
+
ORDER BY category, approach
|
|
381
|
+
"""
|
|
382
|
+
client.query(query).to_dataframe()
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
### 11. Multimodal — aggregate documents with ObjectRef
|
|
386
|
+
|
|
387
|
+
`AI.AGG` takes multimodal input as a `STRUCT` wrapping an `ObjectRef`. An object table's `ref` column goes in directly.
|
|
388
|
+
|
|
389
|
+
> **`AI.AGG` is the one multimodal function whose reference page still specifies `ObjectRefRuntime`** — every other `AI.*` page documents the input type as `ObjectRef` or `ARRAY<ObjectRef>`. The bare column works here anyway, and the page's own Known Issues argue for it: rows carrying arrays of `ObjectRefRuntime` objects "might be skipped," and some image objects created by `OBJ.GET_ACCESS_URL` "might fail to process." Passing `ref` sidesteps both.
|
|
390
|
+
|
|
391
|
+
```python
|
|
392
|
+
from google.cloud import storage
|
|
393
|
+
from pathlib import Path
|
|
394
|
+
|
|
395
|
+
gcs = storage.Client(project=PROJECT_ID)
|
|
396
|
+
bucket = gcs.bucket(BUCKET)
|
|
397
|
+
prefix = 'bq_ai_functions/ai_agg_docs'
|
|
398
|
+
|
|
399
|
+
data_dir = Path('../../data/documents')
|
|
400
|
+
if not data_dir.exists():
|
|
401
|
+
data_dir = Path('data/documents')
|
|
402
|
+
|
|
403
|
+
files_to_upload = [
|
|
404
|
+
('invoices/invoice_001.pdf', 'invoice_001.pdf'),
|
|
405
|
+
('invoices/invoice_002.pdf', 'invoice_002.pdf'),
|
|
406
|
+
('invoices/invoice_003.pdf', 'invoice_003.pdf'),
|
|
407
|
+
('receipts/receipt_001.pdf', 'receipt_001.pdf'),
|
|
408
|
+
('receipts/receipt_002.pdf', 'receipt_002.pdf'),
|
|
409
|
+
('receipts/receipt_003.pdf', 'receipt_003.pdf'),
|
|
410
|
+
]
|
|
411
|
+
|
|
412
|
+
for src, dst in files_to_upload:
|
|
413
|
+
blob = bucket.blob(f'{prefix}/{dst}')
|
|
414
|
+
if not blob.exists():
|
|
415
|
+
blob.upload_from_filename(str(data_dir / src))
|
|
416
|
+
print(f'{len(files_to_upload)} documents uploaded to gs://{BUCKET}/{prefix}/')
|
|
417
|
+
|
|
418
|
+
client.query(f"""
|
|
419
|
+
CREATE OR REPLACE EXTERNAL TABLE `{PROJECT_ID}.{DATASET_ID}.ai_agg_docs`
|
|
420
|
+
WITH CONNECTION `{PROJECT_ID}.{LOCATION}.{CONNECTION_ID}`
|
|
421
|
+
OPTIONS (
|
|
422
|
+
object_metadata = 'SIMPLE',
|
|
423
|
+
uris = ['gs://{BUCKET}/{prefix}/*.pdf']
|
|
424
|
+
)
|
|
425
|
+
""").result()
|
|
426
|
+
print('Object table ai_agg_docs ready')
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
```python
|
|
430
|
+
query = f"""
|
|
431
|
+
SELECT
|
|
432
|
+
AI.AGG(
|
|
433
|
+
STRUCT(ref),
|
|
434
|
+
'These are financial documents (invoices and receipts). Summarize the types of documents, the vendors/stores involved, and the typical transaction amounts.',
|
|
435
|
+
connection_id => '{PROJECT_ID}.{LOCATION}.{CONNECTION_ID}'
|
|
436
|
+
) AS document_summary
|
|
437
|
+
FROM
|
|
438
|
+
`{PROJECT_ID}.{DATASET_ID}.ai_agg_docs`
|
|
439
|
+
"""
|
|
440
|
+
df = client.query(query).to_dataframe()
|
|
441
|
+
print(df.iloc[0]['document_summary'])
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
---
|
|
445
|
+
## Examples — `%%bigquery` Magics
|
|
446
|
+
|
|
447
|
+
The same examples using IPython magic commands. Magics let you write SQL directly in notebook cells without Python string wrapping.
|
|
448
|
+
|
|
449
|
+
Key patterns:
|
|
450
|
+
- `%%bigquery` — run SQL, display results inline
|
|
451
|
+
- `%%bigquery df` — run SQL, capture results into a pandas DataFrame
|
|
452
|
+
|
|
453
|
+
### Simple aggregation
|
|
454
|
+
|
|
455
|
+
```sql
|
|
456
|
+
%%bigquery --project {PROJECT_ID}
|
|
457
|
+
|
|
458
|
+
SELECT
|
|
459
|
+
AI.AGG(
|
|
460
|
+
review,
|
|
461
|
+
'What is the overall sentiment?'
|
|
462
|
+
) AS sentiment
|
|
463
|
+
FROM UNNEST([
|
|
464
|
+
'Love it!', 'Terrible.', 'It was okay.', 'Best purchase ever!'
|
|
465
|
+
]) AS review
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
### Aggregation with GROUP BY — capture to DataFrame
|
|
469
|
+
|
|
470
|
+
```sql
|
|
471
|
+
%%bigquery df_agg --project {PROJECT_ID}
|
|
472
|
+
|
|
473
|
+
SELECT
|
|
474
|
+
category,
|
|
475
|
+
AI.AGG(
|
|
476
|
+
feedback,
|
|
477
|
+
'Summarize the main themes.'
|
|
478
|
+
) AS themes
|
|
479
|
+
FROM UNNEST([
|
|
480
|
+
STRUCT('product' AS category, 'Battery life is great' AS feedback),
|
|
481
|
+
STRUCT('product', 'Too heavy'),
|
|
482
|
+
STRUCT('shipping', 'Fast delivery'),
|
|
483
|
+
STRUCT('shipping', 'Box was damaged')
|
|
484
|
+
]) AS t
|
|
485
|
+
GROUP BY category
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
```python
|
|
489
|
+
df_agg
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
---
|
|
493
|
+
## Examples — BigFrames
|
|
494
|
+
|
|
495
|
+
There is no native BigFrames API for `AI.AGG` yet. Use `session.read_gbq_query()` to execute AI.AGG queries and get results as a BigFrames DataFrame.
|
|
496
|
+
|
|
497
|
+
```python
|
|
498
|
+
import bigframes.pandas as bpd
|
|
499
|
+
|
|
500
|
+
bpd.close_session() # Reset session to apply connection settings
|
|
501
|
+
bpd.options.bigquery.project = PROJECT_ID
|
|
502
|
+
bpd.options.bigquery.location = LOCATION
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
### AI.AGG via `read_gbq_query()`
|
|
506
|
+
|
|
507
|
+
```python
|
|
508
|
+
query = """
|
|
509
|
+
SELECT
|
|
510
|
+
category,
|
|
511
|
+
AI.AGG(
|
|
512
|
+
feedback,
|
|
513
|
+
'Summarize the key themes in this feedback. Keep it concise.'
|
|
514
|
+
) AS themes
|
|
515
|
+
FROM UNNEST([
|
|
516
|
+
STRUCT('product' AS category, 'Battery life is incredible' AS feedback),
|
|
517
|
+
STRUCT('product', 'Screen quality is outstanding'),
|
|
518
|
+
STRUCT('product', 'Too heavy to carry'),
|
|
519
|
+
STRUCT('shipping', 'Arrived early, great packaging'),
|
|
520
|
+
STRUCT('shipping', 'Package was damaged'),
|
|
521
|
+
STRUCT('support', 'Quick and helpful response'),
|
|
522
|
+
STRUCT('support', 'Long hold time before getting help')
|
|
523
|
+
]) AS t
|
|
524
|
+
GROUP BY category
|
|
525
|
+
"""
|
|
526
|
+
bf_df = bpd.read_gbq_query(query)
|
|
527
|
+
bf_df.to_pandas()
|
|
528
|
+
```
|