I built a generative AI model in Vertex AI Studio and can successfully call it using CURL commands. However, when I try to implement the same functionality in a Google Cloud Function, I keep running into authentication issues.
I found documentation for accessing Vertex AI from client applications using various SDKs, but there’s not much guidance for server-to-server communication from Cloud Functions. Can someone help me figure out the proper authentication flow?
Here’s my current Cloud Function code:
import json
from google.auth.transport import requests as auth_requests
from google.oauth2 import service_account
import requests
def call_generative_model(request):
# Configuration
ENDPOINT = "us-central1-aiplatform.googleapis.com"
PROJECT = "my-project-id"
MODEL = "text-bison@001"
API_URL = f"https://{ENDPOINT}/v1/projects/{PROJECT}/locations/us-central1/publishers/google/models/{MODEL}:predict"
# Authentication setup
creds = service_account.Credentials.from_service_account_file(
'credentials.json'
)
auth_request = auth_requests.Request()
access_token = creds.refresh(auth_request).token
# Request configuration
request_headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json"
}
# Model parameters
request_data = {
"instances": [
{
"content": "Generate a response for this prompt..."
}
],
"parameters": {
"candidateCount": 1,
"maxOutputTokens": 256,
"temperature": 0.3,
"topP": 0.9,
"topK": 20
}
}
# Make API call
result = requests.post(API_URL, headers=request_headers, json=request_data)
print(result.json())
return 'Success', 200
The authentication part seems to be failing. What’s the correct way to handle this?