Skip to content

Commit 3ab394d

Browse files
committed
docs(generative-ai): Add sample for set labels in generate_content()
1 parent b9b28d7 commit 3ab394d

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
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 vertexai.generative_models import GenerationResponse
17+
18+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
19+
20+
21+
def generate_content() -> GenerationResponse:
22+
# [START generativeaionvertexai_gemini_set_labels]
23+
import vertexai
24+
25+
from vertexai.generative_models import GenerativeModel
26+
27+
# TODO(developer): Update and un-comment below line
28+
# PROJECT_ID = "your-project-id"
29+
vertexai.init(project=PROJECT_ID, location="us-central1")
30+
31+
model = GenerativeModel("gemini-1.5-flash-001")
32+
33+
prompt = "What is Generative AI?"
34+
response = model.generate_content(
35+
prompt,
36+
# Example Labels
37+
labels={
38+
"team": "research",
39+
"component": "frontend",
40+
"environment": "production",
41+
},
42+
)
43+
44+
print(response.text)
45+
# Example response:
46+
# Generative AI is a type of Artificial Intelligence focused on **creating new content** based on existing data.
47+
48+
# [END generativeaionvertexai_gemini_set_labels]
49+
return response
50+
51+
52+
if __name__ == "__main__":
53+
generate_content()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
15+
import labels_example
16+
17+
18+
def test_set_labels() -> None:
19+
response = labels_example.generate_content()
20+
assert response

0 commit comments

Comments
 (0)