To call your Vertex AI endpoint using HTTP, you’ll need to construct a POST request with the correct authorization and data format. Here’s a breakdown and an example using curl
:
1. Prerequisites
- Endpoint ID: You’ll need the ID of your Vertex AI endpoint. You can find this in the Google Cloud Console or by using the Vertex AI Python SDK (as shown in the previous response).
- Google Cloud Credentials: You’ll need credentials to authorize the request. The easiest way to do this from your local machine is to have the Google Cloud SDK (
gcloud
) installed and configured. - Project ID and Region: You will need your Google Cloud Project ID and the region where you deployed the endpoint.
2. Authorization
Vertex AI API requests require an authorization header with a valid access token. If you have the Google Cloud SDK installed, you can obtain an access token using the following command:
gcloud auth print-access-token
3. Construct the HTTP Request
You’ll make a POST request to the Vertex AI API endpoint. The URL will look like this:
https://{region}-aiplatform.googleapis.com/v1/projects/{project_id}/locations/{region}/endpoints/{endpoint_id}:predict
{project_id}
: Your Google Cloud Project ID.{region}
: The region where your endpoint is deployed (e.g., “us-central1”).{endpoint_id}
: The ID of your Vertex AI endpoint.
The request body should be a JSON object with an “instances” key. The value of “instances” is a list of data instances. In your case, each instance represents the features of a house for which you want to predict the price.
4. Example using curl
Here’s an example of how to call your endpoint using curl
:
ACCESS_TOKEN=$(gcloud auth print-access-token)
PROJECT_ID="your-project-id" # Replace with your Project ID
REGION="us-central1" # Replace with your Region
ENDPOINT_ID="your-endpoint-id" # Replace with your Endpoint ID
# Sample data (same as in the Python SDK example)
DATA='{
"instances": [
{
"Size_LivingArea_SqFt": 2000,
"Size_Lot_SqFt": 8000,
"Size_TotalArea_SqFt": 2800,
"Rooms_Total": 7,
"Bedrooms": 3,
"Bathrooms_Full": 2,
"Bathrooms_Half": 1,
"Basement_Area_SqFt": 800,
"Basement_Finished": 1,
"Garage_Cars": 2,
"Fireplaces": 1,
"Porch_Area_SqFt": 100,
"Year_Built": 2000,
"Year_Remodeled": 2010,
"Condition_Overall": 7,
"Quality_Overall": 7,
"Building_Type": "House",
"House_Style": "Ranch",
"Foundation_Type": "Slab",
"Roof_Material": "Composition Shingle",
"Exterior_Material": "Brick",
"Heating_Type": "Forced Air",
"Cooling_Type": "Central AC",
"Kitchen_Quality": "Good",
"Bathroom_Quality": "Good",
"Fireplace_Quality": "Average",
"Basement_Quality": "Average",
"Stories": 1,
"Floor_Material": "Hardwood",
"Neighborhood": "Bentonville Central",
"Proximity_Schools_Miles": 0.5,
"Proximity_Parks_Miles": 1.2,
"Proximity_PublicTransport_Miles": 0.8,
"Proximity_Shopping_Miles": 1.5,
"Proximity_Hospitals_Miles": 2.0,
"Safety_CrimeRate_Index": 65,
"Environmental_NoiseLevel_dB": 45,
"Environmental_AirQuality_Index": 35,
"Flood_Zone": "No",
"View": "None",
"Time_of_Sale": "2024-08",
"Interest_Rate": 6.2,
"Inflation_Rate": 3.5,
"Unemployment_Rate": 4.2,
"Housing_Inventory": 0.05,
"Economic_Growth_Rate": 2.5
}
]
}'
# Construct the URL
URL="https://${REGION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${REGION}/endpoints/${ENDPOINT_ID}:predict"
# Make the POST request
curl -X POST \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "Content-Type: application/json" \
-d "${DATA}" \
"${URL}"
Explanation:
ACCESS_TOKEN=$(gcloud auth print-access-token)
: Gets your current access token.PROJECT_ID
,REGION
,ENDPOINT_ID
: Replace these with your actual values.DATA
: A JSON string containing the input data. Crucially, it’s wrapped in an “instances” list.URL
: The Vertex AI API endpoint URL.- The
curl
command:-X POST
: Specifies the POST request method.-H "Authorization: Bearer ${ACCESS_TOKEN}"
: Adds the authorization header.-H "Content-Type: application/json"
: Sets the content type to JSON.-d "${DATA}"
: Sends the JSON data in the request body.${URL}
: The URL to send the request to.
5. Response
The response from the Vertex AI endpoint will be a JSON object with a “predictions” key. The value of “predictions” will be a list, where each element corresponds to the prediction for an instance in your input. In this case, you’ll get a list with a single element: the predicted house price.