Execute a Extension in Vertex AI

This code sample demonstrates how to execute an extension.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

Python

Before trying this sample, follow the Python setup instructions in the Vertex AI quickstart using client libraries. For more information, see the Vertex AI Python API reference documentation.

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

importvertexai
fromvertexai.previewimport extensions
# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# extension_id = "your-extension-id"
vertexai .init(project=PROJECT_ID, location="us-central1")
extension = extensions.Extension(extension_id)
response = extension.execute(
 operation_id="generate_and_execute",
 operation_params={"query": "find the max value in the list: [1,2,3,4,-5]"},
)
print(response)
# Example response:
# {
# "generated_code": "```python\n# Find the maximum value in the list\ndata = [1, 2,..", ..
# "execution_result": "The maximum value in the list is: 4\n",
# "execution_error": "",
# "output_files": [],
# }

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.