Skip to content

Commit 6ae0b7e

Browse files
refactor: (GenAI) Reorganized Text Generation Samples (Group D) (#12673)
* Refactored "Text Generation" Category * Refactored Text Generation files * Moved openAI Samples
1 parent 9432a78 commit 6ae0b7e

39 files changed

+1675
-22
lines changed

generative_ai/function_calling/parallel_function_calling_example.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def parallel_function_calling_example() -> ChatSession:
4444
"properties": {
4545
"location": {
4646
"type": "string",
47-
"description": "The location to whih to get the weather. Can be a city name, a city name and state, or a zip code. Examples: 'San Francisco', 'San Francisco, CA', '95616', etc."
47+
"description": "The location to whih to get the weather. Can be a city name, a city name and state, or a zip code. Examples: 'San Francisco', 'San Francisco, CA', '95616', etc.",
4848
},
4949
},
5050
},
@@ -77,22 +77,26 @@ def mock_weather_api_service(location: str) -> str:
7777
api_responses = []
7878
for func in function_calls:
7979
if func.name == function_name:
80-
api_responses.append({
81-
"content": mock_weather_api_service(location=func.args["location"])
82-
})
80+
api_responses.append(
81+
{
82+
"content": mock_weather_api_service(
83+
location=func.args["location"]
84+
)
85+
}
86+
)
8387

8488
# Return the API response to Gemini
8589
response = chat.send_message(
86-
[
87-
Part.from_function_response(
88-
name="get_current_weather",
89-
response=api_responses[0],
90-
),
91-
Part.from_function_response(
92-
name="get_current_weather",
93-
response=api_responses[1],
94-
),
95-
],
90+
[
91+
Part.from_function_response(
92+
name="get_current_weather",
93+
response=api_responses[0],
94+
),
95+
Part.from_function_response(
96+
name="get_current_weather",
97+
response=api_responses[1],
98+
),
99+
],
96100
)
97101

98102
print(response.text)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2023 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 generate_text() -> str:
20+
# [START generativeaionvertexai_gemini_get_started]
21+
import vertexai
22+
23+
from vertexai.generative_models import GenerativeModel, Part
24+
25+
# TODO(developer): Update and un-comment below line
26+
# PROJECT_ID = "your-project-id"
27+
vertexai.init(project=PROJECT_ID, location="us-central1")
28+
29+
model = GenerativeModel("gemini-1.5-flash-002")
30+
31+
response = model.generate_content(
32+
[
33+
Part.from_uri(
34+
"gs://cloud-samples-data/generative-ai/image/scones.jpg",
35+
mime_type="image/jpeg",
36+
),
37+
"What is shown in this image?",
38+
]
39+
)
40+
41+
print(response.text)
42+
# That's a lovely overhead shot of a rustic-style breakfast or brunch spread.
43+
# Here's what's in the image:
44+
# * **Blueberry scones:** Several freshly baked blueberry scones are arranged on parchment paper.
45+
# They look crumbly and delicious.
46+
# ...
47+
48+
# [END generativeaionvertexai_gemini_get_started]
49+
return response.text
50+
51+
52+
if __name__ == "__main__":
53+
generate_text()
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 generate_text() -> None:
20+
# [START generativeaionvertexai_gemini_pro_example]
21+
import vertexai
22+
23+
from vertexai.generative_models import GenerativeModel, Part
24+
25+
# TODO(developer): Update and un-comment below line
26+
# PROJECT_ID = "your-project-id"
27+
vertexai.init(project=PROJECT_ID, location="us-central1")
28+
29+
model = GenerativeModel("gemini-1.5-flash-002")
30+
31+
image_file = Part.from_uri(
32+
"gs://cloud-samples-data/generative-ai/image/scones.jpg", "image/jpeg"
33+
)
34+
35+
# Query the model
36+
response = model.generate_content([image_file, "what is this image?"])
37+
print(response.text)
38+
# Example response:
39+
# That's a lovely overhead flatlay photograph of blueberry scones.
40+
# The image features:
41+
# * **Several blueberry scones:** These are the main focus,
42+
# arranged on parchment paper with some blueberry juice stains.
43+
# ...
44+
45+
# [END generativeaionvertexai_gemini_pro_example]
46+
return response.text
47+
48+
49+
if __name__ == "__main__":
50+
generate_text()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 image_example01
16+
import image_example02
17+
18+
19+
def test_gemini_guide_example() -> None:
20+
text = image_example01.generate_text()
21+
text = text.lower()
22+
assert len(text) > 0
23+
24+
25+
def test_gemini_pro_basic_example() -> None:
26+
text = image_example02.generate_text()
27+
assert len(text) > 0

generative_ai/inference/inference_api_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
import stream_text_basic
1919

2020

21+
# TODO: Delete this test after approval generative_ai/text_generation/text_example03.py
2122
def test_non_stream_text_basic() -> None:
2223
response = non_stream_text_basic.generate_content()
2324
assert response
2425

2526

27+
# TODO: Delete this test after approval generative_ai/text_generation/multimodal_example02.py
2628
def test_non_stream_multi_modality_basic() -> None:
2729
response = non_stream_multimodality_basic.generate_content()
2830
assert response
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 generate_text() -> object:
20+
# [START generativeaionvertexai_gemini_chat_completions_non_streaming]
21+
import vertexai
22+
import openai
23+
24+
from google.auth import default, transport
25+
26+
# TODO(developer): Update and un-comment below line
27+
# PROJECT_ID = "your-project-id"
28+
location = "us-central1"
29+
30+
vertexai.init(project=PROJECT_ID, location=location)
31+
32+
# Programmatically get an access token
33+
credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
34+
auth_request = transport.requests.Request()
35+
credentials.refresh(auth_request)
36+
37+
# # OpenAI Client
38+
client = openai.OpenAI(
39+
base_url=f"https://{location}-aiplatform.googleapis.com/v1beta1/projects/{PROJECT_ID}/locations/{location}/endpoints/openapi",
40+
api_key=credentials.token,
41+
)
42+
43+
response = client.chat.completions.create(
44+
model="google/gemini-1.5-flash-002",
45+
messages=[{"role": "user", "content": "Why is the sky blue?"}],
46+
)
47+
48+
print(response.choices[0].message.content)
49+
# Example response:
50+
# The sky is blue due to a phenomenon called **Rayleigh scattering**.
51+
# Sunlight is made up of all the colors of the rainbow.
52+
# As sunlight enters the Earth's atmosphere ...
53+
54+
# [END generativeaionvertexai_gemini_chat_completions_non_streaming]
55+
return response
56+
57+
58+
if __name__ == "__main__":
59+
generate_text()
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 generate_text() -> object:
20+
# [START generativeaionvertexai_gemini_chat_completions_non_streaming_image]
21+
import vertexai
22+
import openai
23+
24+
from google.auth import default, transport
25+
26+
# TODO(developer): Update and un-comment below lines
27+
# PROJECT_ID = "your-project-id"
28+
location = "us-central1"
29+
30+
vertexai.init(project=PROJECT_ID, location=location)
31+
32+
# Programmatically get an access token
33+
credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
34+
auth_request = transport.requests.Request()
35+
credentials.refresh(auth_request)
36+
37+
# OpenAI Client
38+
client = openai.OpenAI(
39+
base_url=f"https://{location}-aiplatform.googleapis.com/v1beta1/projects/{PROJECT_ID}/locations/{location}/endpoints/openapi",
40+
api_key=credentials.token,
41+
)
42+
43+
response = client.chat.completions.create(
44+
model="google/gemini-1.5-flash-002",
45+
messages=[
46+
{
47+
"role": "user",
48+
"content": [
49+
{"type": "text", "text": "Describe the following image:"},
50+
{
51+
"type": "image_url",
52+
"image_url": "gs://cloud-samples-data/generative-ai/image/scones.jpg",
53+
},
54+
],
55+
}
56+
],
57+
)
58+
59+
print(response.choices[0].message.content)
60+
# Example response:
61+
# Here's a description of the image:
62+
# High-angle, close-up view of a rustic arrangement featuring several blueberry scones
63+
# on a piece of parchment paper. The scones are golden-brown...
64+
65+
# [END generativeaionvertexai_gemini_chat_completions_non_streaming_image]
66+
return response
67+
68+
69+
if __name__ == "__main__":
70+
generate_text()
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 generate_text() -> object:
20+
# [START generativeaionvertexai_gemini_chat_completions_streaming_image]
21+
import vertexai
22+
import openai
23+
24+
from google.auth import default, transport
25+
26+
# TODO(developer): Update and un-comment below line
27+
# PROJECT_ID = "your-project-id"
28+
location = "us-central1"
29+
30+
vertexai.init(project=PROJECT_ID, location=location)
31+
32+
# Programmatically get an access token
33+
credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
34+
auth_request = transport.requests.Request()
35+
credentials.refresh(auth_request)
36+
37+
# OpenAI Client
38+
client = openai.OpenAI(
39+
base_url=f"https://{location}-aiplatform.googleapis.com/v1beta1/projects/{PROJECT_ID}/locations/{location}/endpoints/openapi",
40+
api_key=credentials.token,
41+
)
42+
43+
response = client.chat.completions.create(
44+
model="google/gemini-1.5-flash-002",
45+
messages=[
46+
{
47+
"role": "user",
48+
"content": [
49+
{"type": "text", "text": "Describe the following image:"},
50+
{
51+
"type": "image_url",
52+
"image_url": "gs://cloud-samples-data/generative-ai/image/scones.jpg",
53+
},
54+
],
55+
}
56+
],
57+
stream=True,
58+
)
59+
for chunk in response:
60+
print(chunk.choices[0].delta.content)
61+
# Example response:
62+
# Here's a description of the image:
63+
# High-angle, close-up view of a rustic scene featuring several blueberry
64+
# scones arranged on a piece of parchment paper...
65+
66+
# [END generativeaionvertexai_gemini_chat_completions_streaming_image]
67+
return response
68+
69+
70+
if __name__ == "__main__":
71+
generate_text()

0 commit comments

Comments
 (0)