Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Setting the project name in cloud function using vertex ai sdk

Hi Team, 

I am trying the below code in cloud function to call and it fails while Building function sources

ERROR: failed to build: executing lifecycle. This may be the result of using an untrusted builder: failed with status code: 62

If I  remove following one line  

vertexai.init(project='project', location='us-central1')

it builds but fails at runtime due to not finding the google cloud project set
Please help me to fix this.

 

import functions_framework
import vertexai
from vertexai.preview.generative_models import GenerativeModel, Part
from google.cloud import aiplatform

@functions_framework.http
def hello_http(request):
    """HTTP Cloud Function.
    Args:
        request (flask.Request): The request object.
        <https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data>
    Returns:
        The response text, or any set of values that can be turned into a
        Response object using `make_response`
        <https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response>.
    """
    print("Request received")
    vertexai.init(project='project', location='us-central1')
    request_json = request.get_json(silent=True)
    request_args = request.args
    model = GenerativeModel("gemini-pro-vision")
    responses = model.generate_content(
    "What is life?",
    generation_config={
        "max_output_tokens": 2048,
        "temperature": 0.4,
        "top_p": 1,
        "top_k": 32
    },
    )

    print(responses)
    return 'Hello {}!'.format("name")

 

Solved Solved
0 2 2,007
1 ACCEPTED SOLUTION

 It seems like you're facing issues with the setup of your Cloud Function due to the Vertex AI initialization. To resolve this, make sure your Cloud Function environment has the necessary permissions and configurations to access the Google Cloud project. Additionally, ensure that the service account associated with the function has the required permissions to interact with Vertex AI and other Google Cloud services. Double-check your project ID and location settings to ensure they're accurate. If the issue persists, reviewing the Cloud Function logs might provide more specific error details.

View solution in original post

2 REPLIES 2

 It seems like you're facing issues with the setup of your Cloud Function due to the Vertex AI initialization. To resolve this, make sure your Cloud Function environment has the necessary permissions and configurations to access the Google Cloud project. Additionally, ensure that the service account associated with the function has the required permissions to interact with Vertex AI and other Google Cloud services. Double-check your project ID and location settings to ensure they're accurate. If the issue persists, reviewing the Cloud Function logs might provide more specific error details.

Hi @Sharath_Kumar_K , Thanks a lot for the guidance. A Service Account with Vertex AI Administrator role helped to resolve the issue. Thanks again