Skip to content

Commit cc0039d

Browse files
refactor: (GenAI) Reorganized Extensions Samples (Group A) (GoogleCloudPlatform#12587)
* New examples for Extensions (Group A) and tests * Files to delete * Changed "Example output" -> "Example response"
1 parent d0d9fa1 commit cc0039d

8 files changed

Lines changed: 282 additions & 1 deletion

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import os
15+
16+
from vertexai.preview import extensions
17+
18+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
19+
20+
21+
def create_extension() -> extensions.Extension:
22+
# [START generativeaionvertexai_create_extension]
23+
import vertexai
24+
from vertexai.preview import extensions
25+
26+
# TODO(developer): Update and un-comment below line
27+
# PROJECT_ID = "your-project-id"
28+
vertexai.init(project=PROJECT_ID, location="us-central1")
29+
30+
extension = extensions.Extension.create(
31+
display_name="Code Interpreter",
32+
description="This extension generates and executes code in the specified language",
33+
manifest={
34+
"name": "code_interpreter_tool",
35+
"description": "Google Code Interpreter Extension",
36+
"api_spec": {
37+
"open_api_gcs_uri": "gs://vertex-extension-public/code_interpreter.yaml"
38+
},
39+
"auth_config": {
40+
"google_service_account_config": {},
41+
"auth_type": "GOOGLE_SERVICE_ACCOUNT_AUTH",
42+
},
43+
},
44+
)
45+
print(extension.resource_name)
46+
# Example response:
47+
# projects/123456789012/locations/us-central1/extensions/12345678901234567
48+
49+
# [END generativeaionvertexai_create_extension]
50+
return extension
51+
52+
53+
if __name__ == "__main__":
54+
create_extension()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import os
15+
16+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
17+
18+
19+
def delete_extension(extension_id: str) -> None:
20+
# [START generativeaionvertexai_delete_extension]
21+
import vertexai
22+
from vertexai.preview import extensions
23+
24+
# TODO(developer): Update and un-comment below lines
25+
# PROJECT_ID = "your-project-id"
26+
# extension_id = "extension_id"
27+
vertexai.init(project=PROJECT_ID, location="us-central1")
28+
29+
extension = extensions.Extension(extension_id)
30+
extension.delete()
31+
# Example response:
32+
# ...
33+
# Extension resource projects/[PROJECT_ID]/locations/us-central1/extensions/[extension_id] deleted.
34+
35+
# [END generativeaionvertexai_delete_extension]
36+
37+
38+
if __name__ == "__main__":
39+
delete_extension("9138454554818379776")
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import os
15+
16+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
17+
18+
19+
def execute_extension(extension_id: str) -> object:
20+
# [START generativeaionvertexai_execute_extension]
21+
import vertexai
22+
from vertexai.preview import extensions
23+
24+
# TODO(developer): Update and un-comment below lines
25+
# PROJECT_ID = "your-project-id"
26+
# extension_id = "your-extension-id"
27+
vertexai.init(project=PROJECT_ID, location="us-central1")
28+
29+
extension = extensions.Extension(extension_id)
30+
31+
response = extension.execute(
32+
operation_id="generate_and_execute",
33+
operation_params={"query": "find the max value in the list: [1,2,3,4,-5]"},
34+
)
35+
print(response)
36+
# Example response:
37+
# {
38+
# "generated_code": "```python\n# Find the maximum value in the list\ndata = [1, 2,..", ..
39+
# "execution_result": "The maximum value in the list is: 4\n",
40+
# "execution_error": "",
41+
# "output_files": [],
42+
# }
43+
44+
# [END generativeaionvertexai_execute_extension]
45+
46+
return response
47+
48+
49+
if __name__ == "__main__":
50+
execute_extension("12345678901234567")
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import os
15+
16+
from typing import Generator
17+
18+
import create_example
19+
import delete_example
20+
import execute_example
21+
import get_example
22+
import list_example
23+
24+
import pytest
25+
26+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
27+
28+
29+
@pytest.fixture(scope="module")
30+
def extension_id() -> Generator[str, None, None]:
31+
extension = create_example.create_extension()
32+
yield extension.resource_name
33+
print("Deleting Extension...")
34+
delete_example.delete_extension(extension.resource_name)
35+
36+
37+
def test_create_extension_basic(extension_id: str) -> None:
38+
assert extension_id
39+
40+
41+
def test_execute_extension(extension_id: str) -> None:
42+
response = execute_example.execute_extension(extension_id)
43+
assert "generated_code" in response
44+
45+
46+
def test_get_extension(extension_id: str) -> None:
47+
response = get_example.get_extension(extension_id)
48+
assert "/extensions/" in response.resource_name
49+
50+
51+
def test_list_extensions() -> None:
52+
response = list_example.list_extensions()
53+
assert "/extensions/" in response[1].resource_name
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import os
15+
16+
from vertexai.preview import extensions
17+
18+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
19+
20+
21+
def get_extension(extension_id: str) -> extensions.Extension:
22+
# [START generativeaionvertexai_get_extension]
23+
import vertexai
24+
from vertexai.preview import extensions
25+
26+
# TODO(developer): Update and un-comment below lines
27+
# PROJECT_ID = "your-project-id"
28+
# extension_id = "your-extension-id"
29+
vertexai.init(project=PROJECT_ID, location="us-central1")
30+
31+
extension = extensions.Extension(extension_id)
32+
print(extension.resource_name)
33+
# Example response:
34+
# projects/[PROJECT_ID]/locations/us-central1/extensions/12345678901234567
35+
36+
# [END generativeaionvertexai_get_extension]
37+
return extension
38+
39+
40+
if __name__ == "__main__":
41+
get_extension("12345678901234567")
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import os
15+
16+
from typing import List
17+
18+
from vertexai.preview import extensions
19+
20+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
21+
22+
23+
def list_extensions() -> List[extensions.Extension]:
24+
# [START generativeaionvertexai_list_extensions]
25+
import vertexai
26+
from vertexai.preview import extensions
27+
28+
# TODO (developer):Update project_id
29+
# PROJECT_ID = "your-project-id"
30+
vertexai.init(project=PROJECT_ID, location="us-central1")
31+
32+
extensions = extensions.Extension.list()
33+
print(extensions)
34+
# Example response:
35+
# [<vertexai.extensions._extensions.Extension object at 0x76e8ced37af0>
36+
# resource name: projects/[PROJECT_ID]/locations/us-central1/extensions/1234567890123456]
37+
38+
# [END generativeaionvertexai_list_extensions]
39+
return extensions
40+
41+
42+
if __name__ == "__main__":
43+
list_extensions()

generative_ai/gemini_extensions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
# TODO: Delete this file after aproving /extensions folder
1415
import os
1516

1617
from typing import List

generative_ai/gemini_extensions_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
14+
# TODO: Delete this file after aproving /extensions folder
1515

1616
import os
1717

0 commit comments

Comments
 (0)