|
| 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") |
0 commit comments