|
| 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 | +# http://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 | + |
| 16 | +from google.cloud import discoveryengine_v1alpha as discoveryengine |
| 17 | + |
| 18 | + |
| 19 | +def check_grounding_sample( |
| 20 | + project_id: str, |
| 21 | +) -> discoveryengine.CheckGroundingResponse: |
| 22 | + # [START genappbuilder_check_grounding] |
| 23 | + from google.cloud import discoveryengine_v1alpha as discoveryengine |
| 24 | + |
| 25 | + # TODO(developer): Uncomment these variables before running the sample. |
| 26 | + # project_id = "YOUR_PROJECT_ID" |
| 27 | + |
| 28 | + client = discoveryengine.GroundedGenerationServiceClient() |
| 29 | + |
| 30 | + # The full resource name of the grounding config. |
| 31 | + # Format: projects/{project_id}/locations/{location}/groundingConfigs/default_grounding_config |
| 32 | + grounding_config = client.grounding_config_path( |
| 33 | + project=project_id, |
| 34 | + location="global", |
| 35 | + grounding_config="default_grounding_config", |
| 36 | + ) |
| 37 | + |
| 38 | + request = discoveryengine.CheckGroundingRequest( |
| 39 | + grounding_config=grounding_config, |
| 40 | + answer_candidate="Titanic was directed by James Cameron. It was released in 1997.", |
| 41 | + facts=[ |
| 42 | + discoveryengine.GroundingFact( |
| 43 | + fact_text=( |
| 44 | + "Titanic is a 1997 American epic romantic disaster movie. It was directed, written," |
| 45 | + " and co-produced by James Cameron. The movie is about the 1912 sinking of the" |
| 46 | + " RMS Titanic. It stars Kate Winslet and Leonardo DiCaprio. The movie was released" |
| 47 | + " on December 19, 1997. It received positive critical reviews. The movie won 11 Academy" |
| 48 | + " Awards, and was nominated for fourteen total Academy Awards." |
| 49 | + ), |
| 50 | + attributes={"author": "Simple Wikipedia"}, |
| 51 | + ), |
| 52 | + discoveryengine.GroundingFact( |
| 53 | + fact_text=( |
| 54 | + 'James Cameron\'s "Titanic" is an epic, action-packed romance' |
| 55 | + "set against the ill-fated maiden voyage of the R.M.S. Titanic;" |
| 56 | + "the pride and joy of the White Star Line and, at the time," |
| 57 | + "the largest moving object ever built. " |
| 58 | + 'She was the most luxurious liner of her era -- the "ship of dreams" -- ' |
| 59 | + "which ultimately carried over 1,500 people to their death in the " |
| 60 | + "ice cold waters of the North Atlantic in the early hours of April 15, 1912." |
| 61 | + ), |
| 62 | + attributes={"author": "Simple Wikipedia"}, |
| 63 | + ), |
| 64 | + ], |
| 65 | + grounding_spec=discoveryengine.CheckGroundingSpec(citation_threshold=0.6), |
| 66 | + ) |
| 67 | + |
| 68 | + response = client.check_grounding(request=request) |
| 69 | + |
| 70 | + # Handle the response |
| 71 | + print(response) |
| 72 | + # [END genappbuilder_check_grounding] |
| 73 | + |
| 74 | + return response |
| 75 | + |
| 76 | + |
| 77 | +def rank_sample( |
| 78 | + project_id: str, |
| 79 | +) -> discoveryengine.RankResponse: |
| 80 | + # [START genappbuilder_rank] |
| 81 | + from google.cloud import discoveryengine_v1alpha as discoveryengine |
| 82 | + |
| 83 | + # TODO(developer): Uncomment these variables before running the sample. |
| 84 | + # project_id = "YOUR_PROJECT_ID" |
| 85 | + |
| 86 | + client = discoveryengine.RankServiceClient() |
| 87 | + |
| 88 | + # The full resource name of the ranking config. |
| 89 | + # Format: projects/{project_id}/locations/{location}/rankingConfigs/default_ranking_config |
| 90 | + ranking_config = client.ranking_config_path( |
| 91 | + project=project_id, |
| 92 | + location="global", |
| 93 | + ranking_config="default_ranking_config", |
| 94 | + ) |
| 95 | + request = discoveryengine.RankRequest( |
| 96 | + ranking_config=ranking_config, |
| 97 | + model="semantic-ranker-512@latest", |
| 98 | + top_n=10, |
| 99 | + query="What is Google Gemini?", |
| 100 | + records=[ |
| 101 | + discoveryengine.RankingRecord( |
| 102 | + id="1", |
| 103 | + title="Gemini", |
| 104 | + content="The Gemini zodiac symbol often depicts two figures standing side-by-side.", |
| 105 | + ), |
| 106 | + discoveryengine.RankingRecord( |
| 107 | + id="2", |
| 108 | + title="Gemini", |
| 109 | + content="Gemini is a cutting edge large language model created by Google.", |
| 110 | + ), |
| 111 | + discoveryengine.RankingRecord( |
| 112 | + id="3", |
| 113 | + title="Gemini Constellation", |
| 114 | + content="Gemini is a constellation that can be seen in the night sky.", |
| 115 | + ), |
| 116 | + ], |
| 117 | + ) |
| 118 | + |
| 119 | + response = client.rank(request=request) |
| 120 | + |
| 121 | + # Handle the response |
| 122 | + print(response) |
| 123 | + # [END genappbuilder_rank] |
| 124 | + |
| 125 | + return response |
0 commit comments