cloud.google.com/go/aiplatform@v1.106.0/apiv1/prediction_client_example_test.go (about) 1 // Copyright 2025 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 // Code generated by protoc-gen-go_gapic. DO NOT EDIT. 16 17 package aiplatform_test 18 19 import ( 20 "context" 21 "io" 22 23 aiplatform "cloud.google.com/go/aiplatform/apiv1" 24 aiplatformpb "cloud.google.com/go/aiplatform/apiv1/aiplatformpb" 25 iampb "cloud.google.com/go/iam/apiv1/iampb" 26 longrunningpb "cloud.google.com/go/longrunning/autogen/longrunningpb" 27 "google.golang.org/api/iterator" 28 locationpb "google.golang.org/genproto/googleapis/cloud/location" 29 ) 30 31 func ExampleNewPredictionClient() { 32 ctx := context.Background() 33 // This snippet has been automatically generated and should be regarded as a code template only. 34 // It will require modifications to work: 35 // - It may require correct/in-range values for request initialization. 36 // - It may require specifying regional endpoints when creating the service client as shown in: 37 // https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options 38 c, err := aiplatform.NewPredictionClient(ctx) 39 if err != nil { 40 // TODO: Handle error. 41 } 42 defer c.Close() 43 44 // TODO: Use client. 45 _ = c 46 } 47 48 func ExamplePredictionClient_DirectPredict() { 49 ctx := context.Background() 50 // This snippet has been automatically generated and should be regarded as a code template only. 51 // It will require modifications to work: 52 // - It may require correct/in-range values for request initialization. 53 // - It may require specifying regional endpoints when creating the service client as shown in: 54 // https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options 55 c, err := aiplatform.NewPredictionClient(ctx) 56 if err != nil { 57 // TODO: Handle error. 58 } 59 defer c.Close() 60 61 req := &aiplatformpb.DirectPredictRequest{ 62 // TODO: Fill request struct fields. 63 // See https://pkg.go.dev/cloud.google.com/go/aiplatform/apiv1/aiplatformpb#DirectPredictRequest. 64 } 65 resp, err := c.DirectPredict(ctx, req) 66 if err != nil { 67 // TODO: Handle error. 68 } 69 // TODO: Use resp. 70 _ = resp 71 } 72 73 func ExamplePredictionClient_DirectRawPredict() { 74 ctx := context.Background() 75 // This snippet has been automatically generated and should be regarded as a code template only. 76 // It will require modifications to work: 77 // - It may require correct/in-range values for request initialization. 78 // - It may require specifying regional endpoints when creating the service client as shown in: 79 // https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options 80 c, err := aiplatform.NewPredictionClient(ctx) 81 if err != nil { 82 // TODO: Handle error. 83 } 84 defer c.Close() 85 86 req := &aiplatformpb.DirectRawPredictRequest{ 87 // TODO: Fill request struct fields. 88 // See https://pkg.go.dev/cloud.google.com/go/aiplatform/apiv1/aiplatformpb#DirectRawPredictRequest. 89 } 90 resp, err := c.DirectRawPredict(ctx, req) 91 if err != nil { 92 // TODO: Handle error. 93 } 94 // TODO: Use resp. 95 _ = resp 96 } 97 98 func ExamplePredictionClient_Explain() { 99 ctx := context.Background() 100 // This snippet has been automatically generated and should be regarded as a code template only. 101 // It will require modifications to work: 102 // - It may require correct/in-range values for request initialization. 103 // - It may require specifying regional endpoints when creating the service client as shown in: 104 // https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options 105 c, err := aiplatform.NewPredictionClient(ctx) 106 if err != nil { 107 // TODO: Handle error. 108 } 109 defer c.Close() 110 111 req := &aiplatformpb.ExplainRequest{ 112 // TODO: Fill request struct fields. 113 // See https://pkg.go.dev/cloud.google.com/go/aiplatform/apiv1/aiplatformpb#ExplainRequest. 114 } 115 resp, err := c.Explain(ctx, req) 116 if err != nil { 117 // TODO: Handle error. 118 } 119 // TODO: Use resp. 120 _ = resp 121 } 122 123 func ExamplePredictionClient_GenerateContent() { 124 ctx := context.Background() 125 // This snippet has been automatically generated and should be regarded as a code template only. 126 // It will require modifications to work: 127 // - It may require correct/in-range values for request initialization. 128 // - It may require specifying regional endpoints when creating the service client as shown in: 129 // https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options 130 c, err := aiplatform.NewPredictionClient(ctx) 131 if err != nil { 132 // TODO: Handle error. 133 } 134 defer c.Close() 135 136 req := &aiplatformpb.GenerateContentRequest{ 137 // TODO: Fill request struct fields. 138 // See https://pkg.go.dev/cloud.google.com/go/aiplatform/apiv1/aiplatformpb#GenerateContentRequest. 139 } 140 resp, err := c.GenerateContent(ctx, req) 141 if err != nil { 142 // TODO: Handle error. 143 } 144 // TODO: Use resp. 145 _ = resp 146 } 147 148 func ExamplePredictionClient_Predict() { 149 ctx := context.Background() 150 // This snippet has been automatically generated and should be regarded as a code template only. 151 // It will require modifications to work: 152 // - It may require correct/in-range values for request initialization. 153 // - It may require specifying regional endpoints when creating the service client as shown in: 154 // https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options 155 c, err := aiplatform.NewPredictionClient(ctx) 156 if err != nil { 157 // TODO: Handle error. 158 } 159 defer c.Close() 160 161 req := &aiplatformpb.PredictRequest{ 162 // TODO: Fill request struct fields. 163 // See https://pkg.go.dev/cloud.google.com/go/aiplatform/apiv1/aiplatformpb#PredictRequest. 164 } 165 resp, err := c.Predict(ctx, req) 166 if err != nil { 167 // TODO: Handle error. 168 } 169 // TODO: Use resp. 170 _ = resp 171 } 172 173 func ExamplePredictionClient_RawPredict() { 174 ctx := context.Background() 175 // This snippet has been automatically generated and should be regarded as a code template only. 176 // It will require modifications to work: 177 // - It may require correct/in-range values for request initialization. 178 // - It may require specifying regional endpoints when creating the service client as shown in: 179 // https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options 180 c, err := aiplatform.NewPredictionClient(ctx) 181 if err != nil { 182 // TODO: Handle error. 183 } 184 defer c.Close() 185 186 req := &aiplatformpb.RawPredictRequest{ 187 // TODO: Fill request struct fields. 188 // See https://pkg.go.dev/cloud.google.com/go/aiplatform/apiv1/aiplatformpb#RawPredictRequest. 189 } 190 resp, err := c.RawPredict(ctx, req) 191 if err != nil { 192 // TODO: Handle error. 193 } 194 // TODO: Use resp. 195 _ = resp 196 } 197 198 func ExamplePredictionClient_StreamDirectPredict() { 199 ctx := context.Background() 200 // This snippet has been automatically generated and should be regarded as a code template only. 201 // It will require modifications to work: 202 // - It may require correct/in-range values for request initialization. 203 // - It may require specifying regional endpoints when creating the service client as shown in: 204 // https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options 205 c, err := aiplatform.NewPredictionClient(ctx) 206 if err != nil { 207 // TODO: Handle error. 208 } 209 defer c.Close() 210 stream, err := c.StreamDirectPredict(ctx) 211 if err != nil { 212 // TODO: Handle error. 213 } 214 go func() { 215 reqs := []*aiplatformpb.StreamDirectPredictRequest{ 216 // TODO: Create requests. 217 } 218 for _, req := range reqs { 219 if err := stream.Send(req); err != nil { 220 // TODO: Handle error. 221 } 222 } 223 stream.CloseSend() 224 }() 225 for { 226 resp, err := stream.Recv() 227 if err == io.EOF { 228 break 229 } 230 if err != nil { 231 // TODO: handle error. 232 } 233 // TODO: Use resp. 234 _ = resp 235 } 236 } 237 238 func ExamplePredictionClient_StreamDirectRawPredict() { 239 ctx := context.Background() 240 // This snippet has been automatically generated and should be regarded as a code template only. 241 // It will require modifications to work: 242 // - It may require correct/in-range values for request initialization. 243 // - It may require specifying regional endpoints when creating the service client as shown in: 244 // https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options 245 c, err := aiplatform.NewPredictionClient(ctx) 246 if err != nil { 247 // TODO: Handle error. 248 } 249 defer c.Close() 250 stream, err := c.StreamDirectRawPredict(ctx) 251 if err != nil { 252 // TODO: Handle error. 253 } 254 go func() { 255 reqs := []*aiplatformpb.StreamDirectRawPredictRequest{ 256 // TODO: Create requests. 257 } 258 for _, req := range reqs { 259 if err := stream.Send(req); err != nil { 260 // TODO: Handle error. 261 } 262 } 263 stream.CloseSend() 264 }() 265 for { 266 resp, err := stream.Recv() 267 if err == io.EOF { 268 break 269 } 270 if err != nil { 271 // TODO: handle error. 272 } 273 // TODO: Use resp. 274 _ = resp 275 } 276 } 277 278 func ExamplePredictionClient_StreamingPredict() { 279 ctx := context.Background() 280 // This snippet has been automatically generated and should be regarded as a code template only. 281 // It will require modifications to work: 282 // - It may require correct/in-range values for request initialization. 283 // - It may require specifying regional endpoints when creating the service client as shown in: 284 // https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options 285 c, err := aiplatform.NewPredictionClient(ctx) 286 if err != nil { 287 // TODO: Handle error. 288 } 289 defer c.Close() 290 stream, err := c.StreamingPredict(ctx) 291 if err != nil { 292 // TODO: Handle error. 293 } 294 go func() { 295 reqs := []*aiplatformpb.StreamingPredictRequest{ 296 // TODO: Create requests. 297 } 298 for _, req := range reqs { 299 if err := stream.Send(req); err != nil { 300 // TODO: Handle error. 301 } 302 } 303 stream.CloseSend() 304 }() 305 for { 306 resp, err := stream.Recv() 307 if err == io.EOF { 308 break 309 } 310 if err != nil { 311 // TODO: handle error. 312 } 313 // TODO: Use resp. 314 _ = resp 315 } 316 } 317 318 func ExamplePredictionClient_StreamingRawPredict() { 319 ctx := context.Background() 320 // This snippet has been automatically generated and should be regarded as a code template only. 321 // It will require modifications to work: 322 // - It may require correct/in-range values for request initialization. 323 // - It may require specifying regional endpoints when creating the service client as shown in: 324 // https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options 325 c, err := aiplatform.NewPredictionClient(ctx) 326 if err != nil { 327 // TODO: Handle error. 328 } 329 defer c.Close() 330 stream, err := c.StreamingRawPredict(ctx) 331 if err != nil { 332 // TODO: Handle error. 333 } 334 go func() { 335 reqs := []*aiplatformpb.StreamingRawPredictRequest{ 336 // TODO: Create requests. 337 } 338 for _, req := range reqs { 339 if err := stream.Send(req); err != nil { 340 // TODO: Handle error. 341 } 342 } 343 stream.CloseSend() 344 }() 345 for { 346 resp, err := stream.Recv() 347 if err == io.EOF { 348 break 349 } 350 if err != nil { 351 // TODO: handle error. 352 } 353 // TODO: Use resp. 354 _ = resp 355 } 356 } 357 358 func ExamplePredictionClient_GetLocation() { 359 ctx := context.Background() 360 // This snippet has been automatically generated and should be regarded as a code template only. 361 // It will require modifications to work: 362 // - It may require correct/in-range values for request initialization. 363 // - It may require specifying regional endpoints when creating the service client as shown in: 364 // https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options 365 c, err := aiplatform.NewPredictionClient(ctx) 366 if err != nil { 367 // TODO: Handle error. 368 } 369 defer c.Close() 370 371 req := &locationpb.GetLocationRequest{ 372 // TODO: Fill request struct fields. 373 // See https://pkg.go.dev/google.golang.org/genproto/googleapis/cloud/location#GetLocationRequest. 374 } 375 resp, err := c.GetLocation(ctx, req) 376 if err != nil { 377 // TODO: Handle error. 378 } 379 // TODO: Use resp. 380 _ = resp 381 } 382 383 func ExamplePredictionClient_ListLocations() { 384 ctx := context.Background() 385 // This snippet has been automatically generated and should be regarded as a code template only. 386 // It will require modifications to work: 387 // - It may require correct/in-range values for request initialization. 388 // - It may require specifying regional endpoints when creating the service client as shown in: 389 // https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options 390 c, err := aiplatform.NewPredictionClient(ctx) 391 if err != nil { 392 // TODO: Handle error. 393 } 394 defer c.Close() 395 396 req := &locationpb.ListLocationsRequest{ 397 // TODO: Fill request struct fields. 398 // See https://pkg.go.dev/google.golang.org/genproto/googleapis/cloud/location#ListLocationsRequest. 399 } 400 it := c.ListLocations(ctx, req) 401 for { 402 resp, err := it.Next() 403 if err == iterator.Done { 404 break 405 } 406 if err != nil { 407 // TODO: Handle error. 408 } 409 // TODO: Use resp. 410 _ = resp 411 412 // If you need to access the underlying RPC response, 413 // you can do so by casting the `Response` as below. 414 // Otherwise, remove this line. Only populated after 415 // first call to Next(). Not safe for concurrent access. 416 _ = it.Response.(*locationpb.ListLocationsResponse) 417 } 418 } 419 420 func ExamplePredictionClient_GetIamPolicy() { 421 ctx := context.Background() 422 // This snippet has been automatically generated and should be regarded as a code template only. 423 // It will require modifications to work: 424 // - It may require correct/in-range values for request initialization. 425 // - It may require specifying regional endpoints when creating the service client as shown in: 426 // https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options 427 c, err := aiplatform.NewPredictionClient(ctx) 428 if err != nil { 429 // TODO: Handle error. 430 } 431 defer c.Close() 432 433 req := &iampb.GetIamPolicyRequest{ 434 // TODO: Fill request struct fields. 435 // See https://pkg.go.dev/cloud.google.com/go/iam/apiv1/iampb#GetIamPolicyRequest. 436 } 437 resp, err := c.GetIamPolicy(ctx, req) 438 if err != nil { 439 // TODO: Handle error. 440 } 441 // TODO: Use resp. 442 _ = resp 443 } 444 445 func ExamplePredictionClient_SetIamPolicy() { 446 ctx := context.Background() 447 // This snippet has been automatically generated and should be regarded as a code template only. 448 // It will require modifications to work: 449 // - It may require correct/in-range values for request initialization. 450 // - It may require specifying regional endpoints when creating the service client as shown in: 451 // https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options 452 c, err := aiplatform.NewPredictionClient(ctx) 453 if err != nil { 454 // TODO: Handle error. 455 } 456 defer c.Close() 457 458 req := &iampb.SetIamPolicyRequest{ 459 // TODO: Fill request struct fields. 460 // See https://pkg.go.dev/cloud.google.com/go/iam/apiv1/iampb#SetIamPolicyRequest. 461 } 462 resp, err := c.SetIamPolicy(ctx, req) 463 if err != nil { 464 // TODO: Handle error. 465 } 466 // TODO: Use resp. 467 _ = resp 468 } 469 470 func ExamplePredictionClient_TestIamPermissions() { 471 ctx := context.Background() 472 // This snippet has been automatically generated and should be regarded as a code template only. 473 // It will require modifications to work: 474 // - It may require correct/in-range values for request initialization. 475 // - It may require specifying regional endpoints when creating the service client as shown in: 476 // https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options 477 c, err := aiplatform.NewPredictionClient(ctx) 478 if err != nil { 479 // TODO: Handle error. 480 } 481 defer c.Close() 482 483 req := &iampb.TestIamPermissionsRequest{ 484 // TODO: Fill request struct fields. 485 // See https://pkg.go.dev/cloud.google.com/go/iam/apiv1/iampb#TestIamPermissionsRequest. 486 } 487 resp, err := c.TestIamPermissions(ctx, req) 488 if err != nil { 489 // TODO: Handle error. 490 } 491 // TODO: Use resp. 492 _ = resp 493 } 494 495 func ExamplePredictionClient_CancelOperation() { 496 ctx := context.Background() 497 // This snippet has been automatically generated and should be regarded as a code template only. 498 // It will require modifications to work: 499 // - It may require correct/in-range values for request initialization. 500 // - It may require specifying regional endpoints when creating the service client as shown in: 501 // https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options 502 c, err := aiplatform.NewPredictionClient(ctx) 503 if err != nil { 504 // TODO: Handle error. 505 } 506 defer c.Close() 507 508 req := &longrunningpb.CancelOperationRequest{ 509 // TODO: Fill request struct fields. 510 // See https://pkg.go.dev/cloud.google.com/go/longrunning/autogen/longrunningpb#CancelOperationRequest. 511 } 512 err = c.CancelOperation(ctx, req) 513 if err != nil { 514 // TODO: Handle error. 515 } 516 } 517 518 func ExamplePredictionClient_DeleteOperation() { 519 ctx := context.Background() 520 // This snippet has been automatically generated and should be regarded as a code template only. 521 // It will require modifications to work: 522 // - It may require correct/in-range values for request initialization. 523 // - It may require specifying regional endpoints when creating the service client as shown in: 524 // https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options 525 c, err := aiplatform.NewPredictionClient(ctx) 526 if err != nil { 527 // TODO: Handle error. 528 } 529 defer c.Close() 530 531 req := &longrunningpb.DeleteOperationRequest{ 532 // TODO: Fill request struct fields. 533 // See https://pkg.go.dev/cloud.google.com/go/longrunning/autogen/longrunningpb#DeleteOperationRequest. 534 } 535 err = c.DeleteOperation(ctx, req) 536 if err != nil { 537 // TODO: Handle error. 538 } 539 } 540 541 func ExamplePredictionClient_GetOperation() { 542 ctx := context.Background() 543 // This snippet has been automatically generated and should be regarded as a code template only. 544 // It will require modifications to work: 545 // - It may require correct/in-range values for request initialization. 546 // - It may require specifying regional endpoints when creating the service client as shown in: 547 // https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options 548 c, err := aiplatform.NewPredictionClient(ctx) 549 if err != nil { 550 // TODO: Handle error. 551 } 552 defer c.Close() 553 554 req := &longrunningpb.GetOperationRequest{ 555 // TODO: Fill request struct fields. 556 // See https://pkg.go.dev/cloud.google.com/go/longrunning/autogen/longrunningpb#GetOperationRequest. 557 } 558 resp, err := c.GetOperation(ctx, req) 559 if err != nil { 560 // TODO: Handle error. 561 } 562 // TODO: Use resp. 563 _ = resp 564 } 565 566 func ExamplePredictionClient_ListOperations() { 567 ctx := context.Background() 568 // This snippet has been automatically generated and should be regarded as a code template only. 569 // It will require modifications to work: 570 // - It may require correct/in-range values for request initialization. 571 // - It may require specifying regional endpoints when creating the service client as shown in: 572 // https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options 573 c, err := aiplatform.NewPredictionClient(ctx) 574 if err != nil { 575 // TODO: Handle error. 576 } 577 defer c.Close() 578 579 req := &longrunningpb.ListOperationsRequest{ 580 // TODO: Fill request struct fields. 581 // See https://pkg.go.dev/cloud.google.com/go/longrunning/autogen/longrunningpb#ListOperationsRequest. 582 } 583 it := c.ListOperations(ctx, req) 584 for { 585 resp, err := it.Next() 586 if err == iterator.Done { 587 break 588 } 589 if err != nil { 590 // TODO: Handle error. 591 } 592 // TODO: Use resp. 593 _ = resp 594 595 // If you need to access the underlying RPC response, 596 // you can do so by casting the `Response` as below. 597 // Otherwise, remove this line. Only populated after 598 // first call to Next(). Not safe for concurrent access. 599 _ = it.Response.(*longrunningpb.ListOperationsResponse) 600 } 601 } 602 603 func ExamplePredictionClient_WaitOperation() { 604 ctx := context.Background() 605 // This snippet has been automatically generated and should be regarded as a code template only. 606 // It will require modifications to work: 607 // - It may require correct/in-range values for request initialization. 608 // - It may require specifying regional endpoints when creating the service client as shown in: 609 // https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options 610 c, err := aiplatform.NewPredictionClient(ctx) 611 if err != nil { 612 // TODO: Handle error. 613 } 614 defer c.Close() 615 616 req := &longrunningpb.WaitOperationRequest{ 617 // TODO: Fill request struct fields. 618 // See https://pkg.go.dev/cloud.google.com/go/longrunning/autogen/longrunningpb#WaitOperationRequest. 619 } 620 resp, err := c.WaitOperation(ctx, req) 621 if err != nil { 622 // TODO: Handle error. 623 } 624 // TODO: Use resp. 625 _ = resp 626 }