-
Notifications
You must be signed in to change notification settings - Fork 6.6k
refactor: (GenAI) Reorganized Grounding Samples (Group B) #12594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
75160f6
New files for "Grounding" folder
Thoughtseize1 c08cbb0
Added marks for files to delete
Thoughtseize1 ba0ce79
Fix lint
Thoughtseize1 b5037b1
Fix filename
Thoughtseize1 f3a7a13
Update generative_ai/grounding/test_grounding.py
holtskinner bb34656
Update generative_ai/grounding/test_grounding.py
holtskinner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # Copyright 2024 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| import os | ||
|
|
||
| from typing import Optional | ||
|
|
||
| from vertexai.language_models import TextGenerationResponse | ||
|
|
||
| PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") | ||
|
|
||
|
|
||
| def grounding( | ||
| data_store_location: Optional[str] = None, | ||
| data_store_id: Optional[str] = None, | ||
| ) -> TextGenerationResponse: | ||
| """Grounding example with a Large Language Model""" | ||
| # [START generativeaionvertexai_grounding] | ||
| import vertexai | ||
|
|
||
| from vertexai.language_models import GroundingSource, TextGenerationModel | ||
|
|
||
| # TODO(developer): Update and un-comment below line | ||
| # PROJECT_ID = "your-project-id" | ||
| vertexai.init(project=PROJECT_ID, location="us-central1") | ||
|
|
||
| # TODO developer - override these parameters as needed: | ||
| parameters = { | ||
| "temperature": 0.1, # Temperature controls the degree of randomness in token selection. | ||
| "max_output_tokens": 256, # Token limit determines the maximum amount of text output. | ||
| "top_p": 0.8, # Tokens are selected from most probable to least until the sum of their probabilities equals the top_p value. | ||
| "top_k": 20, # A top_k of 1 means the selected token is the most probable among all tokens. | ||
| } | ||
|
|
||
| model = TextGenerationModel.from_pretrained("text-bison@002") | ||
|
|
||
| # TODO(developer): Update and un-comment below lines | ||
| # data_store_id = "datastore_123456789012345" | ||
| # data_store_location = "global" | ||
| if data_store_id and data_store_location: | ||
| # Use Vertex AI Search data store | ||
| grounding_source = GroundingSource.VertexAISearch( | ||
| data_store_id=data_store_id, location=data_store_location | ||
| ) | ||
| else: | ||
| # Use Google Search for grounding (Private Preview) | ||
| grounding_source = GroundingSource.WebSearch() | ||
|
|
||
| response = model.predict( | ||
| "What are the price, available colors, and storage size options of a Pixel Tablet?", | ||
| grounding_source=grounding_source, | ||
| **parameters, | ||
| ) | ||
| print(f"Response from Model: {response.text}") | ||
| print(f"Grounding Metadata: {response.grounding_metadata}") | ||
| # [END generativeaionvertexai_grounding] | ||
|
|
||
| return response | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| grounding(data_store_id="data-store_1234567890123", data_store_location="global") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # Copyright 2024 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import backoff | ||
|
|
||
| from google.api_core.exceptions import ResourceExhausted | ||
|
|
||
| import palm_example | ||
| import pytest | ||
|
|
||
| import vais_example | ||
| import web_example | ||
|
|
||
|
|
||
| @backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10) | ||
| def test_grounding() -> None: | ||
| data_store_id = "test-search-engine_1689960780551" | ||
| response = palm_example.grounding( | ||
| data_store_location="global", | ||
| data_store_id=data_store_id, | ||
| ) | ||
| assert response | ||
| assert response.text | ||
| assert response.grounding_metadata | ||
|
|
||
|
|
||
| def test_gemini_grounding_vais_example() -> None: | ||
| response = vais_example.generate_text_with_grounding_vertex_ai_search( | ||
| "grounding-test-datastore" | ||
| ) | ||
| assert response | ||
|
|
||
|
|
||
| @pytest.mark.skip( | ||
| "Unable to test Google Search grounding due to allowlist restrictions." | ||
| ) | ||
holtskinner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| def test_gemini_grounding_web_example() -> None: | ||
| response = web_example.generate_text_with_grounding_web() | ||
| assert response | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # Copyright 2024 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| import os | ||
|
|
||
| from vertexai.generative_models import GenerationResponse | ||
|
|
||
| PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") | ||
|
|
||
|
|
||
| def generate_text_with_grounding_vertex_ai_search( | ||
| data_store_id: str, | ||
| ) -> GenerationResponse: | ||
| # [START generativeaionvertexai_gemini_grounding_with_vais] | ||
| import vertexai | ||
|
|
||
| from vertexai.preview.generative_models import ( | ||
| GenerationConfig, | ||
| GenerativeModel, | ||
| Tool, | ||
| grounding, | ||
| ) | ||
|
|
||
| # TODO(developer): Update and un-comment below lines | ||
| # PROJECT_ID = "your-project-id" | ||
| # data_store_id = "your-data-store-id" | ||
|
|
||
| vertexai.init(project=PROJECT_ID, location="us-central1") | ||
|
|
||
| model = GenerativeModel("gemini-1.5-flash-001") | ||
|
|
||
| tool = Tool.from_retrieval( | ||
| grounding.Retrieval( | ||
| grounding.VertexAISearch( | ||
| datastore=data_store_id, | ||
| project=PROJECT_ID, | ||
| location="global", | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| prompt = "How do I make an appointment to renew my driver's license?" | ||
| response = model.generate_content( | ||
| prompt, | ||
| tools=[tool], | ||
| generation_config=GenerationConfig( | ||
| temperature=0.0, | ||
| ), | ||
| ) | ||
|
|
||
| print(response.text) | ||
|
|
||
| # [END generativeaionvertexai_gemini_grounding_with_vais] | ||
| return response | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| generate_text_with_grounding_vertex_ai_search("data-store_1234567890123") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # Copyright 2024 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| import os | ||
|
|
||
| from vertexai.generative_models import GenerationResponse | ||
|
|
||
| PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") | ||
|
|
||
|
|
||
| def generate_text_with_grounding_web() -> GenerationResponse: | ||
| # [START generativeaionvertexai_gemini_grounding_with_web] | ||
| import vertexai | ||
|
|
||
| from vertexai.generative_models import ( | ||
| GenerationConfig, | ||
| GenerativeModel, | ||
| Tool, | ||
| grounding, | ||
| ) | ||
|
|
||
| # TODO(developer): Update and un-comment below line | ||
| # PROJECT_ID = "your-project-id" | ||
| vertexai.init(project=PROJECT_ID, location="us-central1") | ||
|
|
||
| model = GenerativeModel("gemini-1.5-flash-001") | ||
|
|
||
| # Use Google Search for grounding | ||
| tool = Tool.from_google_search_retrieval(grounding.GoogleSearchRetrieval()) | ||
|
|
||
| prompt = "When is the next total solar eclipse in US?" | ||
| response = model.generate_content( | ||
| prompt, | ||
| tools=[tool], | ||
| generation_config=GenerationConfig( | ||
| temperature=0.0, | ||
| ), | ||
| ) | ||
|
|
||
| print(response.text) | ||
| # Example response: | ||
| # The next total solar eclipse visible from the contiguous United States will be on **August 23, 2044**. | ||
|
|
||
| # [END generativeaionvertexai_gemini_grounding_with_web] | ||
| return response | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| generate_text_with_grounding_web() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.