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