Skip to content

Commit 2ed7299

Browse files
Thoughtseize1holtskinner
authored andcommitted
refactor: (GenAI) Reorganized Grounding Samples (Group B) (GoogleCloudPlatform#12594)
* New files for "Grounding" folder * Added marks for files to delete * Fix lint * Fix filename * Update generative_ai/grounding/test_grounding.py * Update generative_ai/grounding/test_grounding.py --------- Co-authored-by: Holt Skinner <13262395+holtskinner@users.noreply.github.com>
1 parent 7978d23 commit 2ed7299

File tree

7 files changed

+248
-0
lines changed

7 files changed

+248
-0
lines changed

generative_ai/gemini_grounding_example.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 approval /grounding/web_example.py & /grounding/vais_example.py
1415
import os
1516

1617
from vertexai.generative_models import GenerationResponse

generative_ai/grounding.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 approval /grounding/palm_example.py
1415
import os
1516

1617
from typing import Optional
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 Optional
17+
18+
from vertexai.language_models import TextGenerationResponse
19+
20+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
21+
22+
23+
def grounding(
24+
data_store_location: Optional[str] = None,
25+
data_store_id: Optional[str] = None,
26+
) -> TextGenerationResponse:
27+
"""Grounding example with a Large Language Model"""
28+
# [START generativeaionvertexai_grounding]
29+
import vertexai
30+
31+
from vertexai.language_models import GroundingSource, TextGenerationModel
32+
33+
# TODO(developer): Update and un-comment below line
34+
# PROJECT_ID = "your-project-id"
35+
vertexai.init(project=PROJECT_ID, location="us-central1")
36+
37+
# TODO developer - override these parameters as needed:
38+
parameters = {
39+
"temperature": 0.1, # Temperature controls the degree of randomness in token selection.
40+
"max_output_tokens": 256, # Token limit determines the maximum amount of text output.
41+
"top_p": 0.8, # Tokens are selected from most probable to least until the sum of their probabilities equals the top_p value.
42+
"top_k": 20, # A top_k of 1 means the selected token is the most probable among all tokens.
43+
}
44+
45+
model = TextGenerationModel.from_pretrained("text-bison@002")
46+
47+
# TODO(developer): Update and un-comment below lines
48+
# data_store_id = "datastore_123456789012345"
49+
# data_store_location = "global"
50+
if data_store_id and data_store_location:
51+
# Use Vertex AI Search data store
52+
grounding_source = GroundingSource.VertexAISearch(
53+
data_store_id=data_store_id, location=data_store_location
54+
)
55+
else:
56+
# Use Google Search for grounding (Private Preview)
57+
grounding_source = GroundingSource.WebSearch()
58+
59+
response = model.predict(
60+
"What are the price, available colors, and storage size options of a Pixel Tablet?",
61+
grounding_source=grounding_source,
62+
**parameters,
63+
)
64+
print(f"Response from Model: {response.text}")
65+
print(f"Grounding Metadata: {response.grounding_metadata}")
66+
# [END generativeaionvertexai_grounding]
67+
68+
return response
69+
70+
71+
if __name__ == "__main__":
72+
grounding(data_store_id="data-store_1234567890123", data_store_location="global")
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 backoff
16+
17+
from google.api_core.exceptions import ResourceExhausted
18+
19+
import palm_example
20+
import vais_example
21+
import web_example
22+
23+
24+
@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10)
25+
def test_grounding() -> None:
26+
data_store_id = "test-search-engine_1689960780551"
27+
response = palm_example.grounding(
28+
data_store_location="global",
29+
data_store_id=data_store_id,
30+
)
31+
assert response
32+
assert response.text
33+
assert response.grounding_metadata
34+
35+
36+
def test_gemini_grounding_vais_example() -> None:
37+
response = vais_example.generate_text_with_grounding_vertex_ai_search(
38+
"grounding-test-datastore"
39+
)
40+
assert response
41+
42+
43+
def test_gemini_grounding_web_example() -> None:
44+
response = web_example.generate_text_with_grounding_web()
45+
assert response
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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_text_with_grounding_vertex_ai_search(
22+
data_store_id: str,
23+
) -> GenerationResponse:
24+
# [START generativeaionvertexai_gemini_grounding_with_vais]
25+
import vertexai
26+
27+
from vertexai.preview.generative_models import (
28+
GenerationConfig,
29+
GenerativeModel,
30+
Tool,
31+
grounding,
32+
)
33+
34+
# TODO(developer): Update and un-comment below lines
35+
# PROJECT_ID = "your-project-id"
36+
# data_store_id = "your-data-store-id"
37+
38+
vertexai.init(project=PROJECT_ID, location="us-central1")
39+
40+
model = GenerativeModel("gemini-1.5-flash-001")
41+
42+
tool = Tool.from_retrieval(
43+
grounding.Retrieval(
44+
grounding.VertexAISearch(
45+
datastore=data_store_id,
46+
project=PROJECT_ID,
47+
location="global",
48+
)
49+
)
50+
)
51+
52+
prompt = "How do I make an appointment to renew my driver's license?"
53+
response = model.generate_content(
54+
prompt,
55+
tools=[tool],
56+
generation_config=GenerationConfig(
57+
temperature=0.0,
58+
),
59+
)
60+
61+
print(response.text)
62+
63+
# [END generativeaionvertexai_gemini_grounding_with_vais]
64+
return response
65+
66+
67+
if __name__ == "__main__":
68+
generate_text_with_grounding_vertex_ai_search("data-store_1234567890123")
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+
from vertexai.generative_models import GenerationResponse
17+
18+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
19+
20+
21+
def generate_text_with_grounding_web() -> GenerationResponse:
22+
# [START generativeaionvertexai_gemini_grounding_with_web]
23+
import vertexai
24+
25+
from vertexai.generative_models import (
26+
GenerationConfig,
27+
GenerativeModel,
28+
Tool,
29+
grounding,
30+
)
31+
32+
# TODO(developer): Update and un-comment below line
33+
# PROJECT_ID = "your-project-id"
34+
vertexai.init(project=PROJECT_ID, location="us-central1")
35+
36+
model = GenerativeModel("gemini-1.5-flash-001")
37+
38+
# Use Google Search for grounding
39+
tool = Tool.from_google_search_retrieval(grounding.GoogleSearchRetrieval())
40+
41+
prompt = "When is the next total solar eclipse in US?"
42+
response = model.generate_content(
43+
prompt,
44+
tools=[tool],
45+
generation_config=GenerationConfig(
46+
temperature=0.0,
47+
),
48+
)
49+
50+
print(response.text)
51+
# Example response:
52+
# The next total solar eclipse visible from the contiguous United States will be on **August 23, 2044**.
53+
54+
# [END generativeaionvertexai_gemini_grounding_with_web]
55+
return response
56+
57+
58+
if __name__ == "__main__":
59+
generate_text_with_grounding_web()

generative_ai/test_gemini_examples.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ def test_gemini_chat_example() -> None:
127127
assert any([_ in text for _ in ("hi", "hello", "greeting")])
128128

129129

130+
# TODO: Delete this file after approval /grounding/web_example.py
130131
@pytest.mark.skip(
131132
"Unable to test Google Search grounding due to allowlist restrictions."
132133
)
@@ -135,6 +136,7 @@ def test_gemini_grounding_web_example() -> None:
135136
assert response
136137

137138

139+
# TODO: Delete this file after approval /grounding/vais_example.py
138140
def test_gemini_grounding_vais_example() -> None:
139141
response = gemini_grounding_example.generate_text_with_grounding_vertex_ai_search(
140142
"grounding-test-datastore"

0 commit comments

Comments
 (0)