1. 簡介

在本程式碼研究室中,您將學習如何使用 Google Antigravity 設計、建構及部署無伺服器應用程式至 Google Cloud。我們將建構無伺服器且事件驅動的文件管道,從 Google Cloud Storage (GCS) 擷取檔案、使用 Cloud Run 和 Gemini 處理檔案,並將中繼資料串流至 BigQuery。
課程內容
- 如何使用 Antigravity 規劃和設計建築。
- 使用 AI 代理生成基礎架構即程式碼 (Shell 指令碼)。
- 建構及部署以 Python 為基礎的 Cloud Run 服務。
- 整合 Vertex AI 的 Gemini,進行多模態文件分析。
- 使用 Antigravity 的 Walkthrough 構件,驗證端對端管道。
軟硬體需求
- 已安裝 Google Antigravity。
- 已啟用計費功能的 Google Cloud 專案。
- 已安裝並驗證 gcloud CLI。
2. 應用程式總覽
在開始使用 Antigravity 架構及實作應用程式之前,請先為自己要建構的應用程式擬定大綱。
我們想建構無伺服器且事件驅動的文件管道,從 Google Cloud Storage (GCS) 擷取檔案、使用 Cloud Run 和 Gemini 處理檔案,並將中繼資料串流至 BigQuery。
這個應用程式的大致架構圖可能如下所示:

這項資訊不必精確,Antigravity 可協助我們逐步瞭解架構詳細資料。不過,最好先瞭解自己想建構什麼。您提供的詳細資料越多,Antigravity 在架構和程式碼方面提供的結果就越精準。
3. 規劃架構
我們已準備好使用 Antigravity 開始規劃架構詳細資料!
Antigravity 擅長規劃複雜系統,我們不必立即撰寫程式碼,而是可以先定義高階架構,並使用其中一項功能,協助 Antigravity 評估要求、提出後續問題,然後繼續規劃及實作。
假設您已啟動 Antigravity,我們將為這個程式碼研究室建立新專案。
按一下「實驗室」Projects 旁邊的新專案圖示,然後按一下 New Project,如下所示:

系統會顯示 Add Folder 選項,如下所示:

按一下「新增資料夾」按鈕,將資料夾新增至專案。我在電腦上建立了 google-cloud-serverless-app 資料夾,並將該資料夾新增至這個專案。
系統隨即會在 google-cloud-serverless-app 工作區中開啟對話。
按一下畫面左下角的主要設定圖示 ⚙️,然後前往專案專屬設定。將 Agent Settings / Security Preset 設為 Default,並將 Agent Behaviour / Artifact Review Policy 設為 Always Ask,如下所示:

這樣一來,您就能在每個步驟中審查並核准計畫,再由專員執行。
提示詞
現在,我們準備好向 Antigravity 提供第一個提示。我們將使用斜線指令 /grill-me 評估要求。
輸入 /grill-me,然後輸入下列提示並點選「提交」按鈕:
/grill-me
I want to build a serverless event-driven document processing pipeline on Google Cloud.
Architecture:
- Ingestion: Users upload files to a Cloud Storage bucket.
- Trigger: File uploads trigger a Pub/Sub message.
- Processor: A Python-based Cloud Run service receives the message, processes the file (simulated OCR), and extracts metadata.
- Storage: Stream the metadata (filename, date, tags, word_count) into a BigQuery dataset.
/grill-me 指令會詢問一系列後續查詢,請盡可能回答。此外,系統也會提供推薦的答案,你可以視需要選用。
以下是 /grill-me 指令的執行範例:
How would you provision and manage the Google Cloud infrastructure resources (Cloud Storage buckets, Pub/Sub topics, BigQuery datasets, Cloud Run service)?
gcloud CLI Setup Script - Shell scripts running gcloud CLI commands to create resources step-by-step
How should the Cloud Storage upload events trigger and reach your Python Cloud Run service?
(Recommended) Native Cloud Storage Pub/Sub Notifications + Pub/Sub Push subscription to Cloud Run (direct, lightweight, standard event-driven approach)
Which Python web framework would you prefer for the Cloud Run processing service?
Flask (with Gunicorn) - Standard, lightweight, and very common for simple Cloud Run services
How should the OCR and metadata extraction logic be implemented in the Cloud Run service?
(Recommended) Full local simulation - If it's a .txt file, read the contents, count words, and extract tags. For other files, generate mock OCR metadata and simulated word count. No external API calls.
Which BigQuery insertion method should the Cloud Run service use to store metadata?
(Recommended) BigQuery table.insert_rows() (Legacy Streaming API) - Extremely simple to code, clean error handling, perfect for simulation and low-to-medium volumes.
How should security/authentication be configured for the Cloud Run service?
Unauthenticated Cloud Run - Allow public requests to the Cloud Run service URL (simpler setup, but insecure for production).
What schema would you like to define for the BigQuery metadata table?
(Recommended) Extended Schema - Include filename, bucket, size, content_type, word_count, tags (as a REPEATED STRING array), ocr_text_preview, and process_timestamp.
How should the Cloud Run service handle processing failures (e.g., file not found, BigQuery write error)?
(Recommended) Fail-Fast with Retry - Log error to standard output (Cloud Logging) and return HTTP 500 to Pub/Sub, so that Pub/Sub automatically retries the message delivery.
What testing tools should we generate to verify the pipeline's functionality?
(Recommended) Both - Include a local test script (sending mock Pub/Sub POST requests to the local Flask server) and a Cloud-integrated test script (uploading a real file to GCS and verifying BigQuery).
請注意,我要求 Antigravity 執行以下操作:
- 用於佈建資源的簡單 gcloud CLI 指令碼
- 原生 Cloud Storage Pub/Sub 通知 + Pub/Sub 推送訂閱項目至 Cloud Run
- 使用 Flask (搭配 Gunicorn) 做為架構
- 只要使用本機模擬功能,並以文字檔提供資料,不必使用即時 OCR 資料即可。
- 使用 BigQuery table.insert_rows() 將資料列插入 BigQuery
- 未經驗證的 Cloud Run 部署作業
以及其他建議選項。
實作計畫和工作清單
Antigravity 現在會開始運作,並生成實作計畫。並顯示類似下方的訊息,供您審查:

您可以點選視窗右上方的「輔助窗格」切換鈕,查看產生的構件,目前只有「實作計畫」。

這項計畫涵蓋下列內容:
- 基礎架構:GCS Bucket、Pub/Sub 主題、BigQuery 資料集。
- 處理器:Python/Flask 應用程式、Dockerfile、需求。
- 整合:GCS 通知 → Pub/Sub → Cloud Run。
畫面應如下所示:以下是我們機器上的部分實作計畫清單:
Event-Driven Document Processing Pipeline Implementation Plan
This implementation plan describes the components and setup scripts required to build a serverless event-driven document processing pipeline on Google Cloud.
User Review Required
Please review the proposed architecture, components, and default configuration. If you agree, please approve the plan so we can proceed with creating the files and implementation.
IMPORTANT
Security Notice: As requested, the Cloud Run service is configured to allow unauthenticated invocations (--allow-unauthenticated) for simpler testing and development.
Error Handling: The service returns an HTTP 500 error code for failures to trigger Pub/Sub retries.
GCP Configuration: The provisioning scripts will use standard environment variables (e.g., GCP_PROJECT, GCP_REGION) that default to the active configuration of your local gcloud CLI.
Proposed Components and Files
The project will be organized as follows:
google-cloud-serverless-app/
├── src/
│ ├── __init__.py
│ ├── app.py # Flask app entrypoint and routes
│ ├── processor.py # Simulated OCR and metadata extraction engine
│ ├── gcs_helper.py # Helper functions to read files from Cloud Storage
│ └── bq_helper.py # Helper functions to write metadata to BigQuery
├── requirements.txt # Python dependencies
├── Dockerfile # Docker configuration for Cloud Run
├── deploy.sh # gcloud CLI provisioning and deployment script
├── test_local.sh # Script to test the Flask app locally with mock Pub/Sub events
├── test_cloud.sh # Script to upload a real file to GCS and query BigQuery
└── README.md # Setup and execution guide
請仔細閱讀,這是您提供導入作業意見回饋的機會。您可以點選導入計畫的任何部分並新增註解。新增註解後,請務必提交您希望看到的任何變更以供審查,特別是命名、Google Cloud 專案 ID、區域等。
確認一切無誤後,按一下 Proceed 按鈕,授權代理商繼續執行導入計畫。
現在,系統會繼續建立另一個構件「工作計畫」,其中包含 Antigravity 建立的一組工作。代理程式會逐一檢查並執行這些動作。以下是範例工作清單:

4. 生成應用程式
計畫獲准後,Antigravity 就會開始生成應用程式所需的檔案,包括佈建指令碼和應用程式程式碼。
Antigravity 會建立資料夾,並開始建立專案所需的檔案。如果您檢查構件,會發現系統產生了多個檔案 (原始碼、指令碼檔案等)。

完成工作後,這項工具會提及此事,並建立「逐步解說」文件供您查看。因為當中提到使用者的後續步驟,範例如下:
- 部署管道:請確認您已登入 GCP CLI 並設定目標專案,然後執行:
./deploy.sh - 執行端對端測試:執行 Cloud 整合測試腳本,確認檔案上傳作業會觸發 Cloud Run 處理程序,並將中繼資料串流至 BigQuery:
./test_cloud.sh - 清除:測試完成後,請參閱
README.md清除指令,移除建立的資源,以免產生費用。
系統會產生 deploy.sh 或類似名稱的殼層指令碼檔案,自動建立資源。這個檔案會處理下列事項:
- 啟用 API (
run、pubsub、bigquery、storage)。 - 建立 Google Cloud Storage bucket (
document-processing-ingest-{project-id})。 - 建立 BigQuery 資料集和資料表 (
document_processing.processed_metadata)。 - 設定 Pub/Sub 主題和通知。
5. 部署應用程式
讓我們透過 ./ deploy.sh 指令,部署上述應用程式。我們可以要求 Antigravity 為我們執行這項作業,但在此之前,請先確認 gcloud CLI 是否存在,以及是否已為 Google Cloud 專案設定。
我們可以向 Antigravity 發出「Run the deploy.sh for me」(為我執行 deploy.sh) 提示。系統會提示您授予權限,請繼續操作並授予權限。

系統會以背景工作形式啟動程序,如下方訊息所述:
我已在背景執行 ./deploy.sh,開始部署程序。我會監控執行記錄,並在部署完成時通知你!
6. 驗證應用程式
管道部署完成後,Antigravity 會繼續驗證應用程式是否正常運作,並詢問是否可以執行 test_cloud.sh 檔案。我們可以繼續並授予權限。

最後,這項工具會提供以下資訊:
I have successfully run both ./deploy.sh and ./test_cloud.sh.
The pipeline is fully deployed to your GCP project PROJECT_ID and working end-to-end.
Please refer to the walkthrough.md for full verification details and the query output from BigQuery showing the successfully processed document metadata.
我們可以開啟 Walkthrough 構件來查看結果:
Cloud Verification Results
The deployment and end-to-end cloud integration test (./test_cloud.sh) succeeded perfectly!
Resources Created:
GCS Bucket: gs://document-processing-ingest-gcp-experiments-349209
Pub/Sub Topic: document-uploads-topic
Pub/Sub Subscription: document-uploads-sub (pushing to Cloud Run)
Cloud Run Service: document-processor (URL: https://document-processor-ido3ocn3pq-uc.a.run.app)
BigQuery Dataset: document_processing
BigQuery Table: processed_metadata
Integration Test Run: A test file cloud_test_sample.txt was uploaded to the bucket. The pipeline processed the file and streamed the metadata record into BigQuery:
🔍 Querying BigQuery to verify metadata insertion...
<BIQUERY DATA HERE>
The pipeline successfully detected the hashtags #gcp and #serverless as tags, correctly counted the 47 words, generated the OCR preview, and wrote the entry to BigQuery under the schema.
選用:手動驗證
即使 Antigravity 已驗證應用程式,您也可以視需要按照下列步驟,在 Google Cloud 控制台中手動檢查是否已建立所有資源。
Cloud Storage
目標:確認值區存在,並檢查上傳的檔案。
- 依序前往「Cloud Storage」>「Bucket」。
- 找出名為
document-processing-ingest-{project-id}的 bucket。 - 按一下 bucket 名稱即可瀏覽檔案。
- 確認:您應該會看到上傳的檔案 (例如
cloud_test_sample.txt)。
Pub/Sub
目標:確認主題存在且有推送訂閱項目。
- 依序前往「Pub/Sub」>「主題」。
- 找到 document-uploads-topic。
- 按一下主題 ID。
- 向下捲動至「訂閱項目」分頁標籤。
- 驗證:確認 doc-uploads-sub 列於「Push」傳送類型中。
Cloud Run
目標:檢查服務狀態和記錄。
- 前往 Cloud Run。
- 按一下「document-processor」服務。
- 驗證:
- 健康狀態:綠色勾號表示服務已啟用。
- 記錄:按一下「記錄」分頁標籤,尋找「Processing file: gs://...」(正在處理檔案:gs://...) 和「Successfully inserted...」(已成功插入...) 等項目。
BigQuery
目標:驗證資料是否確實已儲存。
- 前往「BigQuery」>「SQL 工作區」。
- 在「Explorer」窗格中,展開「專案」>「document_processing 資料集」。
- 點選「processed_metadata」processed_metadata資料表。
- 按一下「查詢」分頁標籤,然後透過 SELECT * 陳述式從資料表擷取所有資料列。
- 驗證:您應該會看到包含 filename、process_timestamp、tags 和 word_count 的資料列。
7. 探索應用程式
此時,您已佈建並執行基本應用程式。在進一步擴充這個應用程式之前,請先花點時間瞭解程式碼。您可以查看構件,當中應會顯示生成的程式碼檔案。
以下簡要說明您可能會看到的檔案:
deploy.sh:主要指令碼,用於佈建所有 Google Cloud 資源,並啟用必要的 API。appy.py:管道的主要進入點。這個 Python 應用程式會建立網路伺服器,接收 Pub/Sub 推送訊息、從 GCS 下載檔案、「處理」檔案 (模擬 OCR),並將中繼資料串流至 BigQuery。Dockerfile:定義如何將應用程式封裝至容器映像檔。requirements.txt:列出 Python 依附元件。
您也可能會看到測試和驗證所需的其他指令碼和文字檔。
8. 擴展應用程式
現在您已擁有可運作的基本應用程式,可以繼續疊代及擴充應用程式。以下提供幾個點子。
新增前端
建立簡單的網頁介面,查看處理過的文件。
請嘗試使用下列提示:Create a simple Streamlit or Flask web application that connects to BigQuery. It should display a table of the processed documents (filename, upload_date, tags, word_count) and allow me to filter the results by tag
與實際的 AI/ML 整合
改用 Gemini 模型擷取、分類及翻譯,而非模擬 OCR 處理程序。
- 取代虛擬 OCR 邏輯。將圖片/PDF 傳送至 Gemini,擷取實際文字和資料。分析擷取的文字,將文件分類 (發票、合約、履歷) 或擷取實體 (日期、姓名、地點)。
- 自動偵測文件語言,並先翻譯成英文再儲存。你也可以使用其他語言。
提升儲存空間和數據分析能力
您可以在 bucket 上設定生命週期規則,將舊檔案移至「Coldline」或「Archive」儲存空間,以節省費用。
穩定性與安全性
您可以採取下列措施,讓應用程式更穩定可靠且安全:
- 無效信件佇列 (DLQ):更新 Pub/Sub 訂閱項目,處理失敗情況。如果 Cloud Run 服務無法處理檔案 5 次,請將訊息傳送至另一個「Dead Letter」主題/bucket,供人工檢查。
- Secret Manager:如果應用程式需要 API 金鑰或機密設定,請將這些項目儲存在 Secret Manager 中,並從 Cloud Run 安全存取,而非將字串硬式編碼。
- Eventarc:從直接使用 Pub/Sub 升級至 Eventarc,即可享有更彈性的事件轉送功能,根據複雜的稽核記錄或其他 GCP 服務事件觸發動作。
當然,你也可以自行發想點子,並使用 Antigravity 協助實作!
9. 結語
您已使用 Google Antigravity,在幾分鐘內成功建構可擴充的無伺服器 AI 輔助文件管道。您學到了下列內容:
- 運用 AI 規劃架構。
- 指示及管理 Antigravity,讓它從程式碼生成到部署和驗證,逐步完成應用程式生成作業。
- 透過逐步解說驗證部署作業和驗證。