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,600 @@
|
|
|
1
|
+
> **Source (`statmike/bigquery-data-science`):** **Notebook:** [`bigquery-ai/functions/ml_translate/ml_translate.ipynb`](https://github.com/statmike/bigquery-data-science/blob/main/bigquery-ai/functions/ml_translate/ml_translate.ipynb) · **SQL:** [`bigquery-ai/functions/ml_translate/ml_translate.sql`](https://github.com/statmike/bigquery-data-science/blob/main/bigquery-ai/functions/ml_translate/ml_translate.sql)
|
|
2
|
+
|
|
3
|
+
# ML.TRANSLATE — BigQuery AI Functions
|
|
4
|
+
|
|
5
|
+
`ML.TRANSLATE` sends text from a BigQuery table to the [Cloud Translation API](https://cloud.google.com/translate/docs) and returns the translation — or, in its other mode, the detected source language — as a JSON column. It is a table-valued function that runs through a **remote model**, so there is a `CREATE MODEL` step, but nothing trains: the model object is a handle on a pre-trained Google API.
|
|
6
|
+
|
|
7
|
+
**One of five Cloud AI service models.** `ML.TRANSLATE` belongs to a small family of BigQuery ML functions that call a pre-trained Cloud AI service through `CREATE MODEL … REMOTE WITH CONNECTION … OPTIONS(REMOTE_SERVICE_TYPE = …)`. The five members and their service types:
|
|
8
|
+
|
|
9
|
+
| Function | `REMOTE_SERVICE_TYPE` | Service | Input |
|
|
10
|
+
|---|---|---|---|
|
|
11
|
+
| `ML.TRANSLATE` *(this notebook)* | `CLOUD_AI_TRANSLATE_V3` | Cloud Translation | A table or query with a `text_content` column |
|
|
12
|
+
| `functions/ml_understand_text` (`ML.UNDERSTAND_TEXT`) | `CLOUD_AI_NATURAL_LANGUAGE_V1` | Cloud Natural Language | A table or query with a `text_content` column |
|
|
13
|
+
| `functions/ml_annotate_image` (`ML.ANNOTATE_IMAGE`) | `CLOUD_AI_VISION_V1` | Cloud Vision | An object table of images |
|
|
14
|
+
| `functions/ml_transcribe` (`ML.TRANSCRIBE`) | `CLOUD_AI_SPEECH_TO_TEXT_V2` | Speech-to-Text | An object table of audio files |
|
|
15
|
+
| `functions/ml_process_document` (`ML.PROCESS_DOCUMENT`) | `CLOUD_AI_DOCUMENT_V1` | Document AI | An object table of documents |
|
|
16
|
+
|
|
17
|
+
The shared setup — connection, service account roles, `CREATE MODEL` — is written once in `reference/cloud-ai-service-models.md` (Cloud AI Service Models). This notebook repeats the parts you need to run it standalone.
|
|
18
|
+
|
|
19
|
+
**When to use it:**
|
|
20
|
+
- You need stable, per-language machine translation over a column of text, at a published per-character price
|
|
21
|
+
- You want the source language detected rather than declared
|
|
22
|
+
- You want translation behavior that does not change when a model default moves
|
|
23
|
+
|
|
24
|
+
**Alternative:** `functions/ai_generate` (`AI.GENERATE`) with a translation prompt. Example 8 runs both over the same rows. The tradeoff is real in both directions and the notebook takes a position at the end.
|
|
25
|
+
|
|
26
|
+
**Two modes, one function:** `TRANSLATE_TEXT` (requires `target_language_code`) and `DETECT_LANGUAGE` (takes no target). The mode is a string inside the `STRUCT` argument.
|
|
27
|
+
|
|
28
|
+
**The input-shape rule to know before you start:** the function reads a column named exactly `text_content`. A table without one errors. Example 3 shows the error and the one-line fix.
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
## Setup
|
|
32
|
+
|
|
33
|
+
This function needs two resources: a **Cloud Resource connection** whose service account may call the Cloud Translation API, and a **remote model** that names the service. Both are created below and both are idempotent.
|
|
34
|
+
|
|
35
|
+
> See the `setup` (Setup Reference) for connections and remote models in general, and `reference/cloud-ai-service-models.md` (Cloud AI Service Models) for this family in particular.
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
PROJECT_ID = 'statmike-mlops-349915' # <-- Replace with your project ID
|
|
39
|
+
LOCATION = 'US' # BigQuery dataset location — this family runs in US or EU
|
|
40
|
+
DATASET_ID = 'bq_ai_functions' # Shared dataset across all notebooks
|
|
41
|
+
CONNECTION_ID = 'bq_ai_functions' # Shared connection
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Environment
|
|
45
|
+
|
|
46
|
+
> **Already set up the project environment?** The cell below is a no-op — packages are already in your kernel. See the `setup` (Setup Reference) for details.
|
|
47
|
+
>
|
|
48
|
+
> **Running standalone** (Colab, Colab Enterprise, Vertex AI Workbench)? The cell below installs required packages into your current kernel.
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from google.cloud import bigquery
|
|
52
|
+
import pandas as pd
|
|
53
|
+
|
|
54
|
+
client = bigquery.Client(project=PROJECT_ID)
|
|
55
|
+
pd.set_option('display.max_colwidth', None)
|
|
56
|
+
|
|
57
|
+
# Create the shared dataset (idempotent)
|
|
58
|
+
dataset_ref = bigquery.DatasetReference(PROJECT_ID, DATASET_ID)
|
|
59
|
+
dataset = bigquery.Dataset(dataset_ref)
|
|
60
|
+
dataset.location = LOCATION
|
|
61
|
+
client.create_dataset(dataset, exists_ok=True)
|
|
62
|
+
print(f'Dataset {PROJECT_ID}.{DATASET_ID} ready')
|
|
63
|
+
|
|
64
|
+
# Register %%bigquery cell magic (auto-loaded in Colab, needed elsewhere)
|
|
65
|
+
%load_ext bigquery_magics
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### The APIs
|
|
69
|
+
|
|
70
|
+
Three services are involved: BigQuery itself, the BigQuery Connection service that owns the connection, and the Cloud Translation API that does the work. The cell enables any that are off.
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
import subprocess as _sp
|
|
74
|
+
|
|
75
|
+
REQUIRED_APIS = [
|
|
76
|
+
'bigquery.googleapis.com',
|
|
77
|
+
'bigqueryconnection.googleapis.com',
|
|
78
|
+
'translate.googleapis.com',
|
|
79
|
+
]
|
|
80
|
+
|
|
81
|
+
enabled = set(_sp.run(['gcloud', 'services', 'list', '--enabled',
|
|
82
|
+
'--format=value(config.name)', f'--project={PROJECT_ID}'],
|
|
83
|
+
capture_output=True, text=True, check=True).stdout.split())
|
|
84
|
+
missing = [api for api in REQUIRED_APIS if api not in enabled]
|
|
85
|
+
for api in missing:
|
|
86
|
+
_sp.run(['gcloud', 'services', 'enable', api, f'--project={PROJECT_ID}'],
|
|
87
|
+
capture_output=True, text=True, check=True)
|
|
88
|
+
|
|
89
|
+
print(f'Already enabled: {", ".join(sorted(set(REQUIRED_APIS) & enabled)) or "none"}')
|
|
90
|
+
print(f'Enabled now: {", ".join(missing) or "none"}')
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### The connection, and the role that is easy to miss
|
|
94
|
+
|
|
95
|
+
The connection's service account — not your user account — is what calls the Cloud Translation API. `ML.TRANSLATE` itself needs three grants at the project level, and [this function's setup page](https://cloud.google.com/bigquery/docs/translate-text) names only the first two. A fourth belongs to the `AI.GENERATE` comparisons later on, not to this function:
|
|
96
|
+
|
|
97
|
+
| Role | Why | Named in the setup docs |
|
|
98
|
+
|---|---|---|
|
|
99
|
+
| `roles/serviceusage.serviceUsageConsumer` | Lets the service account consume an API in this project. **Without it every call fails**, even though `CREATE MODEL` succeeds. | Yes |
|
|
100
|
+
| `roles/bigquery.connectionUser` | Lets the service account use the connection it belongs to. | Yes |
|
|
101
|
+
| `roles/cloudtranslate.user` | Lets it call the Cloud Translation API itself. | **No — found by running it** |
|
|
102
|
+
| `roles/aiplatform.user` | Only for Examples 7 and 8, which call `AI.GENERATE` through the same connection to compare against Cloud Translation. | Not part of `ML.TRANSLATE` |
|
|
103
|
+
|
|
104
|
+
> **`CREATE MODEL` succeeding tells you nothing about permissions.** Model creation for this family does not contact the service, so it completes against a connection that cannot call anything. Measured on this project, with the model created and the `serviceusage` role withheld, the first `ML.TRANSLATE` call failed with:
|
|
105
|
+
>
|
|
106
|
+
> ```
|
|
107
|
+
> bqcx-...@gcp-sa-bigquery-condel.iam.gserviceaccount.com does not have required permission to use
|
|
108
|
+
> the project. Please grant it the roles/serviceusage.serviceUsageConsumer role, or a custom role
|
|
109
|
+
> with the serviceusage.services.use permission and retry.
|
|
110
|
+
> ```
|
|
111
|
+
>
|
|
112
|
+
> The message names the role, which is more than most IAM failures give you — but it arrives at call time, not at setup time.
|
|
113
|
+
>
|
|
114
|
+
> **Granting both documented roles is not enough.** With `serviceUsageConsumer` and `connectionUser` in place, the next call failed differently:
|
|
115
|
+
>
|
|
116
|
+
> ```
|
|
117
|
+
> Permission denied: Cloud IAM permission 'cloudtranslate.languageDetectionModels.predict' denied.
|
|
118
|
+
> ```
|
|
119
|
+
>
|
|
120
|
+
> That is the Cloud Translation API refusing the caller, and clearing it takes a third role — `roles/cloudtranslate.user` — that the setup page does not mention. The shape generalizes across this family: the BigQuery-side roles get the request to the service, and then the service applies its own IAM.
|
|
121
|
+
|
|
122
|
+
The cell below prints which roles the service account already had and which it granted, so the run is auditable. It needs project-level IAM admin rights; if you do not have them, hand the four role names to someone who does.
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
import subprocess as _sp, json as _json
|
|
126
|
+
|
|
127
|
+
# Create the Cloud Resource connection (idempotent — an existing one is left alone)
|
|
128
|
+
_sp.run(['bq', 'mk', '--connection', '--location', LOCATION,
|
|
129
|
+
'--connection_type', 'CLOUD_RESOURCE',
|
|
130
|
+
'--project_id', PROJECT_ID, CONNECTION_ID],
|
|
131
|
+
capture_output=True, text=True)
|
|
132
|
+
|
|
133
|
+
# The connection's auto-provisioned service account is the identity that calls the Cloud AI service
|
|
134
|
+
r = _sp.run(['bq', 'show', '--connection', '--format=json',
|
|
135
|
+
'--project_id', PROJECT_ID, '--location', LOCATION, CONNECTION_ID],
|
|
136
|
+
capture_output=True, text=True, check=True)
|
|
137
|
+
sa = _json.loads(r.stdout)['cloudResource']['serviceAccountId']
|
|
138
|
+
print(f'Connection service account: {sa}')
|
|
139
|
+
|
|
140
|
+
REQUIRED_ROLES = [
|
|
141
|
+
'roles/serviceusage.serviceUsageConsumer',
|
|
142
|
+
'roles/bigquery.connectionUser',
|
|
143
|
+
'roles/cloudtranslate.user',
|
|
144
|
+
'roles/aiplatform.user', # only for the AI.GENERATE comparisons in Examples 7 and 8
|
|
145
|
+
]
|
|
146
|
+
|
|
147
|
+
def roles_held():
|
|
148
|
+
out = _sp.run(['gcloud', 'projects', 'get-iam-policy', PROJECT_ID,
|
|
149
|
+
'--flatten=bindings[].members',
|
|
150
|
+
f'--filter=bindings.members:{sa}',
|
|
151
|
+
'--format=value(bindings.role)'],
|
|
152
|
+
capture_output=True, text=True, check=True)
|
|
153
|
+
return set(out.stdout.split())
|
|
154
|
+
|
|
155
|
+
held = roles_held()
|
|
156
|
+
missing = [role for role in REQUIRED_ROLES if role not in held]
|
|
157
|
+
for role in missing:
|
|
158
|
+
_sp.run(['gcloud', 'projects', 'add-iam-policy-binding', PROJECT_ID,
|
|
159
|
+
f'--member=serviceAccount:{sa}', f'--role={role}', '--condition=None', '--quiet'],
|
|
160
|
+
capture_output=True, text=True, check=True)
|
|
161
|
+
|
|
162
|
+
print(f'Already held: {", ".join(sorted(set(REQUIRED_ROLES) & held)) or "none"}')
|
|
163
|
+
print(f'Granted now: {", ".join(missing) or "none"}')
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### The remote model
|
|
167
|
+
|
|
168
|
+
Nothing trains here. `CREATE MODEL` writes a model object that records the connection and the service type; the Cloud Translation API is called later, per row, by `ML.TRANSLATE`. There is no `ML.EVALUATE`, no weights, and no training data — the model's own metadata says so, and Example 9 reads it.
|
|
169
|
+
|
|
170
|
+
```python
|
|
171
|
+
client.query(f'''
|
|
172
|
+
CREATE OR REPLACE MODEL `{PROJECT_ID}.{DATASET_ID}.ml_translate_model`
|
|
173
|
+
REMOTE WITH CONNECTION `{PROJECT_ID}.{LOCATION}.{CONNECTION_ID}`
|
|
174
|
+
OPTIONS (REMOTE_SERVICE_TYPE = 'CLOUD_AI_TRANSLATE_V3')
|
|
175
|
+
''').result()
|
|
176
|
+
print('Model ml_translate_model ready')
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### The data — eight product reviews in eight languages
|
|
180
|
+
|
|
181
|
+
Deliberately built with a column named `comment`, not `text_content`, because that is what a real table looks like.
|
|
182
|
+
|
|
183
|
+
```python
|
|
184
|
+
client.query(f'''
|
|
185
|
+
CREATE OR REPLACE TABLE `{PROJECT_ID}.{DATASET_ID}.ml_translate_reviews` AS
|
|
186
|
+
SELECT * FROM UNNEST([
|
|
187
|
+
STRUCT(1 AS review_id, 'La batería dura todo el día, pero el cargador se calienta demasiado.' AS comment),
|
|
188
|
+
STRUCT(2, 'Livraison rapide et emballage soigné, je recommande ce vendeur.'),
|
|
189
|
+
STRUCT(3, '画面はとても綺麗ですが、アプリの動作が少し遅いです。'),
|
|
190
|
+
STRUCT(4, 'Die Verarbeitung wirkt hochwertig, allerdings ist das Kabel zu kurz.'),
|
|
191
|
+
STRUCT(5, 'O produto chegou com um arranhão na tampa, mas o suporte resolveu rápido.'),
|
|
192
|
+
STRUCT(6, 'Great sound quality for the price, though the case feels flimsy.'),
|
|
193
|
+
STRUCT(7, 'Il montaggio è stato semplice, le istruzioni però sono poco chiare.'),
|
|
194
|
+
STRUCT(8, '가격 대비 성능은 훌륭하지만 배송이 너무 오래 걸렸습니다.')
|
|
195
|
+
])
|
|
196
|
+
''').result()
|
|
197
|
+
|
|
198
|
+
client.query(f'''
|
|
199
|
+
SELECT review_id, comment
|
|
200
|
+
FROM `{PROJECT_ID}.{DATASET_ID}.ml_translate_reviews`
|
|
201
|
+
ORDER BY review_id
|
|
202
|
+
''').to_dataframe()
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### Wait for the grants to take effect
|
|
206
|
+
|
|
207
|
+
A role binding is not usable the instant `gcloud` returns. On a fresh grant the first call still fails with the permission error above, so this cell probes with the smallest possible translation and waits on evidence rather than on a clock. It prints how long it took — on a project where the roles were already in place, that is zero seconds.
|
|
208
|
+
|
|
209
|
+
```python
|
|
210
|
+
import time
|
|
211
|
+
from google.api_core.exceptions import BadRequest, Forbidden
|
|
212
|
+
|
|
213
|
+
probe_sql = f'''
|
|
214
|
+
SELECT STRING(ml_translate_result.translations[0].translated_text) AS out
|
|
215
|
+
FROM ML.TRANSLATE(
|
|
216
|
+
MODEL `{PROJECT_ID}.{DATASET_ID}.ml_translate_model`,
|
|
217
|
+
(SELECT 'hello' AS text_content),
|
|
218
|
+
STRUCT('TRANSLATE_TEXT' AS translate_mode, 'es' AS target_language_code)
|
|
219
|
+
)
|
|
220
|
+
'''
|
|
221
|
+
|
|
222
|
+
waited = 0
|
|
223
|
+
while True:
|
|
224
|
+
try:
|
|
225
|
+
print(f"Probe succeeded after {waited}s: hello -> {list(client.query(probe_sql).result())[0]['out']}")
|
|
226
|
+
break
|
|
227
|
+
except (BadRequest, Forbidden) as e:
|
|
228
|
+
if 'denied' not in str(e) and 'permission' not in str(e):
|
|
229
|
+
raise
|
|
230
|
+
if waited >= 300:
|
|
231
|
+
raise RuntimeError('IAM grants did not become usable within 5 minutes')
|
|
232
|
+
print(f' not usable yet ({waited}s elapsed)')
|
|
233
|
+
time.sleep(15)
|
|
234
|
+
waited += 15
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
---
|
|
238
|
+
## Examples — SQL
|
|
239
|
+
|
|
240
|
+
### 1. `DETECT_LANGUAGE` — what language is this?
|
|
241
|
+
|
|
242
|
+
The simpler of the two modes: no `target_language_code`, and the JSON result carries the detected language and a confidence. The `comment` column is aliased to `text_content` in the subquery, which is the calling convention for every table that does not happen to have that column name.
|
|
243
|
+
|
|
244
|
+
```python
|
|
245
|
+
query = f'''
|
|
246
|
+
SELECT
|
|
247
|
+
review_id,
|
|
248
|
+
text_content,
|
|
249
|
+
ml_translate_result,
|
|
250
|
+
ml_translate_status
|
|
251
|
+
FROM ML.TRANSLATE(
|
|
252
|
+
MODEL `{PROJECT_ID}.{DATASET_ID}.ml_translate_model`,
|
|
253
|
+
(SELECT review_id, comment AS text_content
|
|
254
|
+
FROM `{PROJECT_ID}.{DATASET_ID}.ml_translate_reviews`),
|
|
255
|
+
STRUCT('DETECT_LANGUAGE' AS translate_mode)
|
|
256
|
+
)
|
|
257
|
+
ORDER BY review_id
|
|
258
|
+
'''
|
|
259
|
+
detected = client.query(query).to_dataframe()
|
|
260
|
+
detected
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
The result is a JSON blob, not columns. Unpack it with the JSON accessor functions — `STRING()` and `FLOAT64()` — the same way the documentation's examples do.
|
|
264
|
+
|
|
265
|
+
```python
|
|
266
|
+
query = f'''
|
|
267
|
+
SELECT
|
|
268
|
+
review_id,
|
|
269
|
+
text_content,
|
|
270
|
+
STRING(ml_translate_result.languages[0].language_code) AS detected_language,
|
|
271
|
+
FLOAT64(ml_translate_result.languages[0].confidence) AS confidence
|
|
272
|
+
FROM ML.TRANSLATE(
|
|
273
|
+
MODEL `{PROJECT_ID}.{DATASET_ID}.ml_translate_model`,
|
|
274
|
+
(SELECT review_id, comment AS text_content
|
|
275
|
+
FROM `{PROJECT_ID}.{DATASET_ID}.ml_translate_reviews`),
|
|
276
|
+
STRUCT('DETECT_LANGUAGE' AS translate_mode)
|
|
277
|
+
)
|
|
278
|
+
ORDER BY review_id
|
|
279
|
+
'''
|
|
280
|
+
client.query(query).to_dataframe()
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
### 2. `TRANSLATE_TEXT` — translate everything to English
|
|
284
|
+
|
|
285
|
+
`target_language_code` is required in this mode. The detected source language comes back alongside the translation, so a `TRANSLATE_TEXT` call answers the `DETECT_LANGUAGE` question too.
|
|
286
|
+
|
|
287
|
+
```python
|
|
288
|
+
query = f'''
|
|
289
|
+
SELECT
|
|
290
|
+
review_id,
|
|
291
|
+
text_content AS original,
|
|
292
|
+
STRING(ml_translate_result.translations[0].detected_language_code) AS detected,
|
|
293
|
+
STRING(ml_translate_result.translations[0].translated_text) AS english,
|
|
294
|
+
ml_translate_status
|
|
295
|
+
FROM ML.TRANSLATE(
|
|
296
|
+
MODEL `{PROJECT_ID}.{DATASET_ID}.ml_translate_model`,
|
|
297
|
+
(SELECT review_id, comment AS text_content
|
|
298
|
+
FROM `{PROJECT_ID}.{DATASET_ID}.ml_translate_reviews`),
|
|
299
|
+
STRUCT('TRANSLATE_TEXT' AS translate_mode, 'en' AS target_language_code)
|
|
300
|
+
)
|
|
301
|
+
ORDER BY review_id
|
|
302
|
+
'''
|
|
303
|
+
to_english = client.query(query).to_dataframe()
|
|
304
|
+
to_english
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
`ml_translate_status` is empty on success. It is a **per-row** column, not a job-level one: the query job can finish successfully while individual rows carry an error string, which is why every example here selects it or checks it.
|
|
308
|
+
|
|
309
|
+
### 3. The `text_content` rule, and what breaks without it
|
|
310
|
+
|
|
311
|
+
Pass the table directly — `TABLE \`…ml_translate_reviews\`` — and the call fails, because the table's text column is named `comment`. The error is caught and printed rather than allowed to stop the notebook.
|
|
312
|
+
|
|
313
|
+
```python
|
|
314
|
+
try:
|
|
315
|
+
client.query(f'''
|
|
316
|
+
SELECT *
|
|
317
|
+
FROM ML.TRANSLATE(
|
|
318
|
+
MODEL `{PROJECT_ID}.{DATASET_ID}.ml_translate_model`,
|
|
319
|
+
TABLE `{PROJECT_ID}.{DATASET_ID}.ml_translate_reviews`,
|
|
320
|
+
STRUCT('TRANSLATE_TEXT' AS translate_mode, 'en' AS target_language_code)
|
|
321
|
+
)
|
|
322
|
+
''').result()
|
|
323
|
+
print('No error — this table has a text_content column after all.')
|
|
324
|
+
except BadRequest as e:
|
|
325
|
+
print('Failed, as expected:\n')
|
|
326
|
+
print(str(e).split(';')[0])
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
Two fixes, and they are not equivalent:
|
|
330
|
+
|
|
331
|
+
1. **Alias in a subquery** — `(SELECT comment AS text_content FROM …)`. What every other example here does. Carry along any other columns you want in the output; they pass through untouched.
|
|
332
|
+
2. **Name the column `text_content` in the table itself.** Then `TABLE …` works directly. This is worth doing only if the table exists to be translated.
|
|
333
|
+
|
|
334
|
+
Anything the subquery selects is returned beside the result columns, which is how `review_id` survives the call.
|
|
335
|
+
|
|
336
|
+
### 4. The raw JSON, both modes
|
|
337
|
+
|
|
338
|
+
The unpacking in Examples 1 and 2 skipped past the full response. Here it is, one row, both modes — including the two fields that are always present and always empty unless you use a glossary or a translation memory.
|
|
339
|
+
|
|
340
|
+
```python
|
|
341
|
+
query = f'''
|
|
342
|
+
SELECT 'TRANSLATE_TEXT' AS mode, TO_JSON_STRING(ml_translate_result) AS raw_json
|
|
343
|
+
FROM ML.TRANSLATE(
|
|
344
|
+
MODEL `{PROJECT_ID}.{DATASET_ID}.ml_translate_model`,
|
|
345
|
+
(SELECT comment AS text_content
|
|
346
|
+
FROM `{PROJECT_ID}.{DATASET_ID}.ml_translate_reviews` WHERE review_id = 1),
|
|
347
|
+
STRUCT('TRANSLATE_TEXT' AS translate_mode, 'en' AS target_language_code)
|
|
348
|
+
)
|
|
349
|
+
UNION ALL
|
|
350
|
+
SELECT 'DETECT_LANGUAGE', TO_JSON_STRING(ml_translate_result)
|
|
351
|
+
FROM ML.TRANSLATE(
|
|
352
|
+
MODEL `{PROJECT_ID}.{DATASET_ID}.ml_translate_model`,
|
|
353
|
+
(SELECT comment AS text_content
|
|
354
|
+
FROM `{PROJECT_ID}.{DATASET_ID}.ml_translate_reviews` WHERE review_id = 1),
|
|
355
|
+
STRUCT('DETECT_LANGUAGE' AS translate_mode)
|
|
356
|
+
)
|
|
357
|
+
'''
|
|
358
|
+
for row in client.query(query).result():
|
|
359
|
+
print(f"{row['mode']}:")
|
|
360
|
+
print(f" {row['raw_json']}\n")
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
### 5. One target language per call
|
|
364
|
+
|
|
365
|
+
`target_language_code` is a scalar inside the `STRUCT`, evaluated once for the whole call — not per row. Passing a column reference is a compile-time error, shown first. To fan out over several target languages you issue several calls, which is what the loop below does.
|
|
366
|
+
|
|
367
|
+
```python
|
|
368
|
+
# (a) A per-row target language is not available
|
|
369
|
+
try:
|
|
370
|
+
client.query(f'''
|
|
371
|
+
SELECT review_id, ml_translate_status
|
|
372
|
+
FROM ML.TRANSLATE(
|
|
373
|
+
MODEL `{PROJECT_ID}.{DATASET_ID}.ml_translate_model`,
|
|
374
|
+
(SELECT review_id, comment AS text_content, 'fr' AS want
|
|
375
|
+
FROM `{PROJECT_ID}.{DATASET_ID}.ml_translate_reviews`),
|
|
376
|
+
STRUCT('TRANSLATE_TEXT' AS translate_mode, want AS target_language_code)
|
|
377
|
+
)
|
|
378
|
+
''').result()
|
|
379
|
+
print('(a) Accepted a column as target_language_code.')
|
|
380
|
+
except BadRequest as e:
|
|
381
|
+
print('(a) Rejected, as expected:\n')
|
|
382
|
+
print(str(e).split(';')[0], '\n')
|
|
383
|
+
|
|
384
|
+
# (b) Fan out: one call per target language, unioned into a single result
|
|
385
|
+
targets = ['es', 'fr', 'ja']
|
|
386
|
+
union = '\nUNION ALL\n'.join(f'''
|
|
387
|
+
SELECT '{t}' AS target, review_id,
|
|
388
|
+
STRING(ml_translate_result.translations[0].translated_text) AS translated
|
|
389
|
+
FROM ML.TRANSLATE(
|
|
390
|
+
MODEL `{PROJECT_ID}.{DATASET_ID}.ml_translate_model`,
|
|
391
|
+
(SELECT review_id, comment AS text_content
|
|
392
|
+
FROM `{PROJECT_ID}.{DATASET_ID}.ml_translate_reviews` WHERE review_id = 6),
|
|
393
|
+
STRUCT('TRANSLATE_TEXT' AS translate_mode, '{t}' AS target_language_code)
|
|
394
|
+
)''' for t in targets)
|
|
395
|
+
|
|
396
|
+
print('(b) One English review, three target languages:')
|
|
397
|
+
client.query(union).to_dataframe().sort_values('target').reset_index(drop=True)
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
### 6. Round trip — English → Japanese → English
|
|
401
|
+
|
|
402
|
+
Calls nest: the output of one `ML.TRANSLATE` can be the input relation of the next, as long as the inner query aliases its translated column to `text_content`. A round trip is the cheapest sanity check on a translation pipeline you cannot read — the meaning should survive even though the wording will not.
|
|
403
|
+
|
|
404
|
+
```python
|
|
405
|
+
query = f'''
|
|
406
|
+
WITH out_and_back AS (
|
|
407
|
+
SELECT
|
|
408
|
+
review_id,
|
|
409
|
+
STRING(ml_translate_result.translations[0].translated_text) AS text_content
|
|
410
|
+
FROM ML.TRANSLATE(
|
|
411
|
+
MODEL `{PROJECT_ID}.{DATASET_ID}.ml_translate_model`,
|
|
412
|
+
(SELECT review_id, comment AS text_content
|
|
413
|
+
FROM `{PROJECT_ID}.{DATASET_ID}.ml_translate_reviews` WHERE review_id IN (6)),
|
|
414
|
+
STRUCT('TRANSLATE_TEXT' AS translate_mode, 'ja' AS target_language_code)
|
|
415
|
+
)
|
|
416
|
+
)
|
|
417
|
+
SELECT
|
|
418
|
+
text_content AS japanese,
|
|
419
|
+
STRING(ml_translate_result.translations[0].translated_text) AS back_to_english
|
|
420
|
+
FROM ML.TRANSLATE(
|
|
421
|
+
MODEL `{PROJECT_ID}.{DATASET_ID}.ml_translate_model`,
|
|
422
|
+
TABLE out_and_back,
|
|
423
|
+
STRUCT('TRANSLATE_TEXT' AS translate_mode, 'en' AS target_language_code)
|
|
424
|
+
)
|
|
425
|
+
'''
|
|
426
|
+
try:
|
|
427
|
+
trip = client.query(query).to_dataframe()
|
|
428
|
+
except BadRequest as e:
|
|
429
|
+
print('TABLE on a CTE was rejected — using a subquery instead:\n')
|
|
430
|
+
print(str(e).split(';')[0], '\n')
|
|
431
|
+
query = query.replace('TABLE out_and_back', '(SELECT * FROM out_and_back)')
|
|
432
|
+
trip = client.query(query).to_dataframe()
|
|
433
|
+
|
|
434
|
+
original = client.query(f'''
|
|
435
|
+
SELECT comment FROM `{PROJECT_ID}.{DATASET_ID}.ml_translate_reviews` WHERE review_id = 6
|
|
436
|
+
''').to_dataframe()['comment'][0]
|
|
437
|
+
|
|
438
|
+
print(f'original: {original}')
|
|
439
|
+
print(f'japanese: {trip["japanese"][0]}')
|
|
440
|
+
print(f'back to english: {trip["back_to_english"][0]}')
|
|
441
|
+
print(f'\nidentical to the original: {trip["back_to_english"][0] == original}')
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
### 7. Is it deterministic?
|
|
445
|
+
|
|
446
|
+
Worth knowing before you build a pipeline on it, and not documented either way. The same call runs twice with the query cache **off** — a cached result would answer the wrong question — and the two sets of strings are compared. For contrast the same comparison runs against `AI.GENERATE`, which does the same job with an LLM.
|
|
447
|
+
|
|
448
|
+
```python
|
|
449
|
+
no_cache = bigquery.QueryJobConfig(use_query_cache=False)
|
|
450
|
+
|
|
451
|
+
translate_sql = f'''
|
|
452
|
+
SELECT review_id, STRING(ml_translate_result.translations[0].translated_text) AS out
|
|
453
|
+
FROM ML.TRANSLATE(
|
|
454
|
+
MODEL `{PROJECT_ID}.{DATASET_ID}.ml_translate_model`,
|
|
455
|
+
(SELECT review_id, comment AS text_content
|
|
456
|
+
FROM `{PROJECT_ID}.{DATASET_ID}.ml_translate_reviews`),
|
|
457
|
+
STRUCT('TRANSLATE_TEXT' AS translate_mode, 'en' AS target_language_code)
|
|
458
|
+
)
|
|
459
|
+
ORDER BY review_id
|
|
460
|
+
'''
|
|
461
|
+
|
|
462
|
+
generate_sql = f'''
|
|
463
|
+
SELECT review_id,
|
|
464
|
+
AI.GENERATE(
|
|
465
|
+
('Translate this product review to English. Return only the translation: ', comment)
|
|
466
|
+
).result AS out
|
|
467
|
+
FROM `{PROJECT_ID}.{DATASET_ID}.ml_translate_reviews`
|
|
468
|
+
ORDER BY review_id
|
|
469
|
+
'''
|
|
470
|
+
|
|
471
|
+
def run_twice(sql, label):
|
|
472
|
+
a = client.query(sql, job_config=no_cache).to_dataframe()['out'].tolist()
|
|
473
|
+
b = client.query(sql, job_config=no_cache).to_dataframe()['out'].tolist()
|
|
474
|
+
same = sum(x == y for x, y in zip(a, b))
|
|
475
|
+
print(f'{label}: {same} of {len(a)} rows identical across two cache-disabled runs')
|
|
476
|
+
return a, b
|
|
477
|
+
|
|
478
|
+
ml_a, ml_b = run_twice(translate_sql, 'ML.TRANSLATE ')
|
|
479
|
+
ai_a, ai_b = run_twice(generate_sql, 'AI.GENERATE ')
|
|
480
|
+
|
|
481
|
+
pd.DataFrame({'ML.TRANSLATE run 1': ml_a, 'ML.TRANSLATE run 2': ml_b,
|
|
482
|
+
'AI.GENERATE run 1': ai_a, 'AI.GENERATE run 2': ai_b})
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
### 8. The same job, side by side
|
|
486
|
+
|
|
487
|
+
`ML.TRANSLATE` and `AI.GENERATE` over the same eight rows, in one query, so the difference is visible rather than argued.
|
|
488
|
+
|
|
489
|
+
```python
|
|
490
|
+
query = f'''
|
|
491
|
+
WITH api AS (
|
|
492
|
+
SELECT review_id,
|
|
493
|
+
text_content AS original,
|
|
494
|
+
STRING(ml_translate_result.translations[0].translated_text) AS translation_api
|
|
495
|
+
FROM ML.TRANSLATE(
|
|
496
|
+
MODEL `{PROJECT_ID}.{DATASET_ID}.ml_translate_model`,
|
|
497
|
+
(SELECT review_id, comment AS text_content
|
|
498
|
+
FROM `{PROJECT_ID}.{DATASET_ID}.ml_translate_reviews`),
|
|
499
|
+
STRUCT('TRANSLATE_TEXT' AS translate_mode, 'en' AS target_language_code)
|
|
500
|
+
)
|
|
501
|
+
),
|
|
502
|
+
llm AS (
|
|
503
|
+
SELECT review_id,
|
|
504
|
+
AI.GENERATE(
|
|
505
|
+
('Translate this product review to English. Return only the translation: ', comment)
|
|
506
|
+
).result AS gemini
|
|
507
|
+
FROM `{PROJECT_ID}.{DATASET_ID}.ml_translate_reviews`
|
|
508
|
+
)
|
|
509
|
+
SELECT api.review_id, api.original, api.translation_api, llm.gemini
|
|
510
|
+
FROM api JOIN llm USING (review_id)
|
|
511
|
+
ORDER BY review_id
|
|
512
|
+
'''
|
|
513
|
+
client.query(query).to_dataframe()
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
### 9. What the model object actually is
|
|
517
|
+
|
|
518
|
+
Models are not part of `INFORMATION_SCHEMA` — the `TABLES` view below lists the dataset's tables and the model is not among them — so the model's metadata is read through the client API. What comes back is the clearest statement that nothing trained: `model_type` is unspecified, the single training run carries an empty `trainingOptions` and an empty `evaluationMetrics`, and the only substance in the record is `remoteModelInfo`, holding the service type and the connection. `ML.EVALUATE`, the function every trained BigQuery ML model answers, refuses outright.
|
|
519
|
+
|
|
520
|
+
```python
|
|
521
|
+
import json
|
|
522
|
+
|
|
523
|
+
# INFORMATION_SCHEMA covers tables — models are not listed here
|
|
524
|
+
display(client.query(f'''
|
|
525
|
+
SELECT table_name, table_type
|
|
526
|
+
FROM `{PROJECT_ID}.{DATASET_ID}`.INFORMATION_SCHEMA.TABLES
|
|
527
|
+
ORDER BY table_name
|
|
528
|
+
''').to_dataframe())
|
|
529
|
+
|
|
530
|
+
# Model metadata comes from the client API instead
|
|
531
|
+
model = client.get_model(f'{PROJECT_ID}.{DATASET_ID}.ml_translate_model')
|
|
532
|
+
print(json.dumps(model.to_api_repr(), indent=2))
|
|
533
|
+
print()
|
|
534
|
+
|
|
535
|
+
try:
|
|
536
|
+
client.query(f'''
|
|
537
|
+
SELECT * FROM ML.EVALUATE(MODEL `{PROJECT_ID}.{DATASET_ID}.ml_translate_model`)
|
|
538
|
+
''').result()
|
|
539
|
+
print('ML.EVALUATE returned a result.')
|
|
540
|
+
except Exception as e:
|
|
541
|
+
print('ML.EVALUATE on a Cloud AI service model:\n')
|
|
542
|
+
print(str(e).split(';')[0])
|
|
543
|
+
```
|
|
544
|
+
|
|
545
|
+
---
|
|
546
|
+
## Examples — `%%bigquery` Magics
|
|
547
|
+
|
|
548
|
+
`%%bigquery` cannot interpolate Python variables into the SQL body, so model and table references are written out in full.
|
|
549
|
+
|
|
550
|
+
```sql
|
|
551
|
+
%%bigquery --project {PROJECT_ID}
|
|
552
|
+
|
|
553
|
+
SELECT
|
|
554
|
+
review_id,
|
|
555
|
+
STRING(ml_translate_result.translations[0].detected_language_code) AS detected,
|
|
556
|
+
STRING(ml_translate_result.translations[0].translated_text) AS english
|
|
557
|
+
FROM ML.TRANSLATE(
|
|
558
|
+
MODEL `statmike-mlops-349915.bq_ai_functions.ml_translate_model`,
|
|
559
|
+
(SELECT review_id, comment AS text_content
|
|
560
|
+
FROM `statmike-mlops-349915.bq_ai_functions.ml_translate_reviews`),
|
|
561
|
+
STRUCT('TRANSLATE_TEXT' AS translate_mode, 'en' AS target_language_code)
|
|
562
|
+
)
|
|
563
|
+
ORDER BY review_id
|
|
564
|
+
```
|
|
565
|
+
|
|
566
|
+
---
|
|
567
|
+
## Examples — BigFrames
|
|
568
|
+
|
|
569
|
+
BigFrames has no `ML.TRANSLATE` wrapper. Run the SQL through `read_gbq_query()` and get a BigFrames DataFrame back.
|
|
570
|
+
|
|
571
|
+
```python
|
|
572
|
+
import bigframes.pandas as bpd
|
|
573
|
+
|
|
574
|
+
bpd.options.bigquery.project = PROJECT_ID
|
|
575
|
+
bpd.options.bigquery.location = LOCATION
|
|
576
|
+
|
|
577
|
+
session = bpd.get_global_session()
|
|
578
|
+
df_bf = session.read_gbq_query(f'''
|
|
579
|
+
SELECT
|
|
580
|
+
review_id,
|
|
581
|
+
STRING(ml_translate_result.translations[0].detected_language_code) AS detected,
|
|
582
|
+
STRING(ml_translate_result.translations[0].translated_text) AS english
|
|
583
|
+
FROM ML.TRANSLATE(
|
|
584
|
+
MODEL `{PROJECT_ID}.{DATASET_ID}.ml_translate_model`,
|
|
585
|
+
(SELECT review_id, comment AS text_content
|
|
586
|
+
FROM `{PROJECT_ID}.{DATASET_ID}.ml_translate_reviews`),
|
|
587
|
+
STRUCT('TRANSLATE_TEXT' AS translate_mode, 'en' AS target_language_code)
|
|
588
|
+
)
|
|
589
|
+
''')
|
|
590
|
+
df_bf.to_pandas().sort_values('review_id').reset_index(drop=True)
|
|
591
|
+
```
|
|
592
|
+
|
|
593
|
+
---
|
|
594
|
+
## Which one should you reach for?
|
|
595
|
+
|
|
596
|
+
**Reach for `ML.TRANSLATE`** when translation is the whole job and you want it to behave the same way tomorrow: a fixed API, a published per-character price, a detected source language for free, and no prompt to maintain or re-test when a model default moves.
|
|
597
|
+
|
|
598
|
+
**Reach for `AI.GENERATE`** when the job is not really translation — translate *and* summarize, translate *and* keep the brand voice, translate *and* extract the complaint — or when you need to steer register, formality or terminology. One call does what would otherwise be a translation plus a second pass.
|
|
599
|
+
|
|
600
|
+
**What the notebook measured, not argued:** Example 7 compares run-to-run stability of the two under a disabled query cache, and Example 8 puts their outputs on the same eight rows so you can read them against each other.
|