|
13 | 13 | # limitations under the License.
|
14 | 14 | #
|
15 | 15 |
|
16 |
| -from google.cloud import discoveryengine_v1alpha as discoveryengine |
| 16 | +from google.cloud import discoveryengine_v1 as discoveryengine |
17 | 17 |
|
18 | 18 |
|
19 | 19 | def check_grounding_sample(
|
20 | 20 | project_id: str,
|
21 | 21 | ) -> discoveryengine.CheckGroundingResponse:
|
22 | 22 | # [START genappbuilder_check_grounding]
|
23 |
| - from google.cloud import discoveryengine_v1alpha as discoveryengine |
| 23 | + from google.cloud import discoveryengine_v1 as discoveryengine |
24 | 24 |
|
25 | 25 | # TODO(developer): Uncomment these variables before running the sample.
|
26 | 26 | # project_id = "YOUR_PROJECT_ID"
|
@@ -78,7 +78,7 @@ def rank_sample(
|
78 | 78 | project_id: str,
|
79 | 79 | ) -> discoveryengine.RankResponse:
|
80 | 80 | # [START genappbuilder_rank]
|
81 |
| - from google.cloud import discoveryengine_v1alpha as discoveryengine |
| 81 | + from google.cloud import discoveryengine_v1 as discoveryengine |
82 | 82 |
|
83 | 83 | # TODO(developer): Uncomment these variables before running the sample.
|
84 | 84 | # project_id = "YOUR_PROJECT_ID"
|
@@ -123,3 +123,183 @@ def rank_sample(
|
123 | 123 | # [END genappbuilder_rank]
|
124 | 124 |
|
125 | 125 | return response
|
| 126 | + |
| 127 | + |
| 128 | +def grounded_generation_inline_vais_sample( |
| 129 | + project_number: str, |
| 130 | + engine_id: str, |
| 131 | +) -> discoveryengine.GenerateGroundedContentResponse: |
| 132 | + # [START genappbuilder_grounded_generation_inline_vais] |
| 133 | + from google.cloud import discoveryengine_v1 as discoveryengine |
| 134 | + |
| 135 | + # TODO(developer): Uncomment these variables before running the sample. |
| 136 | + # project_number = "YOUR_PROJECT_NUMBER" |
| 137 | + # engine_id = "YOUR_ENGINE_ID" |
| 138 | + |
| 139 | + client = discoveryengine.GroundedGenerationServiceClient() |
| 140 | + |
| 141 | + request = discoveryengine.GenerateGroundedContentRequest( |
| 142 | + # The full resource name of the location. |
| 143 | + # Format: projects/{project_number}/locations/{location} |
| 144 | + location=client.common_location_path(project=project_number, location="global"), |
| 145 | + generation_spec=discoveryengine.GenerateGroundedContentRequest.GenerationSpec( |
| 146 | + model_id="gemini-1.5-flash", |
| 147 | + ), |
| 148 | + # Conversation between user and model |
| 149 | + contents=[ |
| 150 | + discoveryengine.GroundedGenerationContent( |
| 151 | + role="user", |
| 152 | + parts=[ |
| 153 | + discoveryengine.GroundedGenerationContent.Part( |
| 154 | + text="How did Google do in 2020? Where can I find BigQuery docs?" |
| 155 | + ) |
| 156 | + ], |
| 157 | + ) |
| 158 | + ], |
| 159 | + system_instruction=discoveryengine.GroundedGenerationContent( |
| 160 | + parts=[ |
| 161 | + discoveryengine.GroundedGenerationContent.Part( |
| 162 | + text="Add a smiley emoji after the answer." |
| 163 | + ) |
| 164 | + ], |
| 165 | + ), |
| 166 | + # What to ground on. |
| 167 | + grounding_spec=discoveryengine.GenerateGroundedContentRequest.GroundingSpec( |
| 168 | + grounding_sources=[ |
| 169 | + discoveryengine.GenerateGroundedContentRequest.GroundingSource( |
| 170 | + inline_source=discoveryengine.GenerateGroundedContentRequest.GroundingSource.InlineSource( |
| 171 | + grounding_facts=[ |
| 172 | + discoveryengine.GroundingFact( |
| 173 | + fact_text=( |
| 174 | + "The BigQuery documentation can be found at https://cloud.google.com/bigquery/docs/introduction" |
| 175 | + ), |
| 176 | + attributes={ |
| 177 | + "title": "BigQuery Overview", |
| 178 | + "uri": "https://cloud.google.com/bigquery/docs/introduction", |
| 179 | + }, |
| 180 | + ), |
| 181 | + ] |
| 182 | + ), |
| 183 | + ), |
| 184 | + discoveryengine.GenerateGroundedContentRequest.GroundingSource( |
| 185 | + search_source=discoveryengine.GenerateGroundedContentRequest.GroundingSource.SearchSource( |
| 186 | + # The full resource name of the serving config for a Vertex AI Search App |
| 187 | + serving_config=f"projects/{project_number}/locations/global/collections/default_collection/engines/{engine_id}/servingConfigs/default_search", |
| 188 | + ), |
| 189 | + ), |
| 190 | + ] |
| 191 | + ), |
| 192 | + ) |
| 193 | + response = client.generate_grounded_content(request) |
| 194 | + |
| 195 | + # Handle the response |
| 196 | + print(response) |
| 197 | + # [END genappbuilder_grounded_generation_inline_vais] |
| 198 | + |
| 199 | + return response |
| 200 | + |
| 201 | + |
| 202 | +def grounded_generation_google_search_sample( |
| 203 | + project_number: str, |
| 204 | +) -> discoveryengine.GenerateGroundedContentResponse: |
| 205 | + # [START genappbuilder_grounded_generation_google_search] |
| 206 | + from google.cloud import discoveryengine_v1 as discoveryengine |
| 207 | + |
| 208 | + # TODO(developer): Uncomment these variables before running the sample. |
| 209 | + # project_number = "YOUR_PROJECT_NUMBER" |
| 210 | + |
| 211 | + client = discoveryengine.GroundedGenerationServiceClient() |
| 212 | + |
| 213 | + request = discoveryengine.GenerateGroundedContentRequest( |
| 214 | + # The full resource name of the location. |
| 215 | + # Format: projects/{project_number}/locations/{location} |
| 216 | + location=client.common_location_path(project=project_number, location="global"), |
| 217 | + generation_spec=discoveryengine.GenerateGroundedContentRequest.GenerationSpec( |
| 218 | + model_id="gemini-1.5-flash", |
| 219 | + ), |
| 220 | + # Conversation between user and model |
| 221 | + contents=[ |
| 222 | + discoveryengine.GroundedGenerationContent( |
| 223 | + role="user", |
| 224 | + parts=[ |
| 225 | + discoveryengine.GroundedGenerationContent.Part( |
| 226 | + text="How much is Google stock?" |
| 227 | + ) |
| 228 | + ], |
| 229 | + ) |
| 230 | + ], |
| 231 | + system_instruction=discoveryengine.GroundedGenerationContent( |
| 232 | + parts=[ |
| 233 | + discoveryengine.GroundedGenerationContent.Part(text="Be comprehensive.") |
| 234 | + ], |
| 235 | + ), |
| 236 | + # What to ground on. |
| 237 | + grounding_spec=discoveryengine.GenerateGroundedContentRequest.GroundingSpec( |
| 238 | + grounding_sources=[ |
| 239 | + discoveryengine.GenerateGroundedContentRequest.GroundingSource( |
| 240 | + google_search_source=discoveryengine.GenerateGroundedContentRequest.GroundingSource.GoogleSearchSource( |
| 241 | + # Optional: For Dynamic Retrieval |
| 242 | + dynamic_retrieval_config=discoveryengine.GenerateGroundedContentRequest.DynamicRetrievalConfiguration( |
| 243 | + predictor=discoveryengine.GenerateGroundedContentRequest.DynamicRetrievalConfiguration.DynamicRetrievalPredictor( |
| 244 | + threshold=0.7 |
| 245 | + ) |
| 246 | + ) |
| 247 | + ) |
| 248 | + ), |
| 249 | + ] |
| 250 | + ), |
| 251 | + ) |
| 252 | + response = client.generate_grounded_content(request) |
| 253 | + |
| 254 | + # Handle the response |
| 255 | + print(response) |
| 256 | + # [END genappbuilder_grounded_generation_google_search] |
| 257 | + |
| 258 | + return response |
| 259 | + |
| 260 | + |
| 261 | +def grounded_generation_streaming_sample( |
| 262 | + project_number: str, |
| 263 | +) -> discoveryengine.GenerateGroundedContentResponse: |
| 264 | + # [START genappbuilder_grounded_generation_streaming] |
| 265 | + from google.cloud import discoveryengine_v1 as discoveryengine |
| 266 | + |
| 267 | + # TODO(developer): Uncomment these variables before running the sample. |
| 268 | + # project_id = "YOUR_PROJECT_ID" |
| 269 | + |
| 270 | + client = discoveryengine.GroundedGenerationServiceClient() |
| 271 | + |
| 272 | + request = discoveryengine.GenerateGroundedContentRequest( |
| 273 | + # The full resource name of the location. |
| 274 | + # Format: projects/{project_number}/locations/{location} |
| 275 | + location=client.common_location_path(project=project_number, location="global"), |
| 276 | + generation_spec=discoveryengine.GenerateGroundedContentRequest.GenerationSpec( |
| 277 | + model_id="gemini-1.5-flash", |
| 278 | + ), |
| 279 | + # Conversation between user and model |
| 280 | + contents=[ |
| 281 | + discoveryengine.GroundedGenerationContent( |
| 282 | + role="user", |
| 283 | + parts=[ |
| 284 | + discoveryengine.GroundedGenerationContent.Part( |
| 285 | + text="Summarize how to delete a data store in Vertex AI Agent Builder?" |
| 286 | + ) |
| 287 | + ], |
| 288 | + ) |
| 289 | + ], |
| 290 | + grounding_spec=discoveryengine.GenerateGroundedContentRequest.GroundingSpec( |
| 291 | + grounding_sources=[ |
| 292 | + discoveryengine.GenerateGroundedContentRequest.GroundingSource( |
| 293 | + google_search_source=discoveryengine.GenerateGroundedContentRequest.GroundingSource.GoogleSearchSource() |
| 294 | + ), |
| 295 | + ] |
| 296 | + ), |
| 297 | + ) |
| 298 | + responses = client.stream_generate_grounded_content(iter([request])) |
| 299 | + |
| 300 | + for response in responses: |
| 301 | + # Handle the response |
| 302 | + print(response) |
| 303 | + # [END genappbuilder_grounded_generation_streaming] |
| 304 | + |
| 305 | + return response |
0 commit comments