From 52c85103e5bd6e95302494b391385d30e591dd3a Mon Sep 17 00:00:00 2001 From: Valeriy Burlaka Date: Wed, 5 Feb 2025 11:55:03 +0100 Subject: [PATCH 1/8] feat(genai sdk): generate text content from text prompt --- genai/go.mod | 58 ++++++++ genai/go.sum | 129 ++++++++++++++++++ genai/text_generation/text_generation_test.go | 45 ++++++ genai/text_generation/textgen_with_txt.go | 59 ++++++++ go.work | 1 + 5 files changed, 292 insertions(+) create mode 100644 genai/go.mod create mode 100644 genai/go.sum create mode 100644 genai/text_generation/text_generation_test.go create mode 100644 genai/text_generation/textgen_with_txt.go diff --git a/genai/go.mod b/genai/go.mod new file mode 100644 index 0000000000..9d2e3ccadb --- /dev/null +++ b/genai/go.mod @@ -0,0 +1,58 @@ +module github.com/GoogleCloudPlatform/golang-samples/genai + +go 1.23.0 + +require ( + github.com/GoogleCloudPlatform/golang-samples v0.0.0-20250201051611-5fb145d1e974 + google.golang.org/genai v0.1.0 +) + +require ( + cel.dev/expr v0.19.1 // indirect + cloud.google.com/go v0.118.0 // indirect + cloud.google.com/go/auth v0.14.0 // indirect + cloud.google.com/go/auth/oauth2adapt v0.2.7 // indirect + cloud.google.com/go/compute/metadata v0.6.0 // indirect + cloud.google.com/go/iam v1.3.1 // indirect + cloud.google.com/go/monitoring v1.23.0 // indirect + cloud.google.com/go/storage v1.50.0 // indirect + github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.25.0 // indirect + github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.49.0 // indirect + github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.49.0 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/cncf/xds/go v0.0.0-20250121191232-2f005788dc42 // indirect + github.com/envoyproxy/go-control-plane/envoy v1.32.3 // indirect + github.com/envoyproxy/protoc-gen-validate v1.1.0 // indirect + github.com/felixge/httpsnoop v1.0.4 // indirect + github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/google/go-cmp v0.6.0 // indirect + github.com/google/s2a-go v0.1.9 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect + github.com/googleapis/gax-go/v2 v2.14.1 // indirect + github.com/gorilla/websocket v1.5.3 // indirect + github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect + go.opentelemetry.io/auto/sdk v1.1.0 // indirect + go.opentelemetry.io/contrib/detectors/gcp v1.34.0 // indirect + go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.59.0 // indirect + go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.59.0 // indirect + go.opentelemetry.io/otel v1.34.0 // indirect + go.opentelemetry.io/otel/metric v1.34.0 // indirect + go.opentelemetry.io/otel/sdk v1.34.0 // indirect + go.opentelemetry.io/otel/sdk/metric v1.34.0 // indirect + go.opentelemetry.io/otel/trace v1.34.0 // indirect + golang.org/x/crypto v0.32.0 // indirect + golang.org/x/net v0.34.0 // indirect + golang.org/x/oauth2 v0.25.0 // indirect + golang.org/x/sync v0.10.0 // indirect + golang.org/x/sys v0.29.0 // indirect + golang.org/x/text v0.21.0 // indirect + golang.org/x/time v0.9.0 // indirect + google.golang.org/api v0.217.0 // indirect + google.golang.org/genproto v0.0.0-20250115164207-1a7da9e5054f // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20250115164207-1a7da9e5054f // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f // indirect + google.golang.org/grpc v1.69.4 // indirect + google.golang.org/protobuf v1.36.3 // indirect +) diff --git a/genai/go.sum b/genai/go.sum new file mode 100644 index 0000000000..31a2b86ab2 --- /dev/null +++ b/genai/go.sum @@ -0,0 +1,129 @@ +cel.dev/expr v0.19.1 h1:NciYrtDRIR0lNCnH1LFJegdjspNx9fI59O7TWcua/W4= +cel.dev/expr v0.19.1/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw= +cloud.google.com/go v0.118.0 h1:tvZe1mgqRxpiVa3XlIGMiPcEUbP1gNXELgD4y/IXmeQ= +cloud.google.com/go v0.118.0/go.mod h1:zIt2pkedt/mo+DQjcT4/L3NDxzHPR29j5HcclNH+9PM= +cloud.google.com/go/auth v0.14.0 h1:A5C4dKV/Spdvxcl0ggWwWEzzP7AZMJSEIgrkngwhGYM= +cloud.google.com/go/auth v0.14.0/go.mod h1:CYsoRL1PdiDuqeQpZE0bP2pnPrGqFcOkI0nldEQis+A= +cloud.google.com/go/auth/oauth2adapt v0.2.7 h1:/Lc7xODdqcEw8IrZ9SvwnlLX6j9FHQM74z6cBk9Rw6M= +cloud.google.com/go/auth/oauth2adapt v0.2.7/go.mod h1:NTbTTzfvPl1Y3V1nPpOgl2w6d/FjO7NNUQaWSox6ZMc= +cloud.google.com/go/compute/metadata v0.6.0 h1:A6hENjEsCDtC1k8byVsgwvVcioamEHvZ4j01OwKxG9I= +cloud.google.com/go/compute/metadata v0.6.0/go.mod h1:FjyFAW1MW0C203CEOMDTu3Dk1FlqW3Rga40jzHL4hfg= +cloud.google.com/go/iam v1.3.1 h1:KFf8SaT71yYq+sQtRISn90Gyhyf4X8RGgeAVC8XGf3E= +cloud.google.com/go/iam v1.3.1/go.mod h1:3wMtuyT4NcbnYNPLMBzYRFiEfjKfJlLVLrisE7bwm34= +cloud.google.com/go/logging v1.13.0 h1:7j0HgAp0B94o1YRDqiqm26w4q1rDMH7XNRU34lJXHYc= +cloud.google.com/go/logging v1.13.0/go.mod h1:36CoKh6KA/M0PbhPKMq6/qety2DCAErbhXT62TuXALA= +cloud.google.com/go/longrunning v0.6.4 h1:3tyw9rO3E2XVXzSApn1gyEEnH2K9SynNQjMlBi3uHLg= +cloud.google.com/go/longrunning v0.6.4/go.mod h1:ttZpLCe6e7EXvn9OxpBRx7kZEB0efv8yBO6YnVMfhJs= +cloud.google.com/go/monitoring v1.23.0 h1:M3nXww2gn9oZ/qWN2bZ35CjolnVHM3qnSbu6srCPgjk= +cloud.google.com/go/monitoring v1.23.0/go.mod h1:034NnlQPDzrQ64G2Gavhl0LUHZs9H3rRmhtnp7jiJgg= +cloud.google.com/go/storage v1.50.0 h1:3TbVkzTooBvnZsk7WaAQfOsNrdoM8QHusXA1cpk6QJs= +cloud.google.com/go/storage v1.50.0/go.mod h1:l7XeiD//vx5lfqE3RavfmU9yvk5Pp0Zhcv482poyafY= +cloud.google.com/go/trace v1.11.3 h1:c+I4YFjxRQjvAhRmSsmjpASUKq88chOX854ied0K/pE= +cloud.google.com/go/trace v1.11.3/go.mod h1:pt7zCYiDSQjC9Y2oqCsh9jF4GStB/hmjrYLsxRR27q8= +github.com/GoogleCloudPlatform/golang-samples v0.0.0-20250201051611-5fb145d1e974 h1:PS8ehvuB87wYj3/1LJk6x9T2W1wg9T1yqLsBXWvwELM= +github.com/GoogleCloudPlatform/golang-samples v0.0.0-20250201051611-5fb145d1e974/go.mod h1:qMsuU2f349s5vt0u1hUeU0Vu6JWLQ5dAoKM+0Lm7fkQ= +github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.25.0 h1:3c8yed4lgqTt+oTQ+JNMDo+F4xprBf+O/il4ZC0nRLw= +github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.25.0/go.mod h1:obipzmGjfSjam60XLwGfqUkJsfiheAl+TUjG+4yzyPM= +github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.49.0 h1:o90wcURuxekmXrtxmYWTyNla0+ZEHhud6DI1ZTxd1vI= +github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.49.0/go.mod h1:6fTWu4m3jocfUZLYF5KsZC1TUfRvEjs7lM4crme/irw= +github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/cloudmock v0.49.0 h1:jJKWl98inONJAr/IZrdFQUWcwUO95DLY1XMD1ZIut+g= +github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/cloudmock v0.49.0/go.mod h1:l2fIqmwB+FKSfvn3bAD/0i+AXAxhIZjTK2svT/mgUXs= +github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.49.0 h1:GYUJLfvd++4DMuMhCFLgLXvFwofIxh/qOwoGuS/LTew= +github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.49.0/go.mod h1:wRbFgBQUVm1YXrvWKofAEmq9HNJTDphbAaJSSX01KUI= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/cncf/xds/go v0.0.0-20250121191232-2f005788dc42 h1:Om6kYQYDUk5wWbT0t0q6pvyM49i9XZAv9dDrkDA7gjk= +github.com/cncf/xds/go v0.0.0-20250121191232-2f005788dc42/go.mod h1:W+zGtBO5Y1IgJhy4+A9GOqVhqLpfZi+vwmdNXUehLA8= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/envoyproxy/go-control-plane v0.13.4 h1:zEqyPVyku6IvWCFwux4x9RxkLOMUL+1vC9xUFv5l2/M= +github.com/envoyproxy/go-control-plane v0.13.4/go.mod h1:kDfuBlDVsSj2MjrLEtRWtHlsWIFcGyB2RMO44Dc5GZA= +github.com/envoyproxy/go-control-plane/envoy v1.32.3 h1:hVEaommgvzTjTd4xCaFd+kEQ2iYBtGxP6luyLrx6uOk= +github.com/envoyproxy/go-control-plane/envoy v1.32.3/go.mod h1:F6hWupPfh75TBXGKA++MCT/CZHFq5r9/uwt/kQYkZfE= +github.com/envoyproxy/go-control-plane/ratelimit v0.1.0 h1:/G9QYbddjL25KvtKTv3an9lx6VBE2cnb8wp1vEGNYGI= +github.com/envoyproxy/go-control-plane/ratelimit v0.1.0/go.mod h1:Wk+tMFAFbCXaJPzVVHnPgRKdUdwW/KdbRt94AzgRee4= +github.com/envoyproxy/protoc-gen-validate v1.1.0 h1:tntQDh69XqOCOZsDz0lVJQez/2L6Uu2PdjCQwWCJ3bM= +github.com/envoyproxy/protoc-gen-validate v1.1.0/go.mod h1:sXRDRVmzEbkM7CVcM06s9shE/m23dg3wzjl0UWqJ2q4= +github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= +github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 h1:f+oWsMOmNPc8JmEHVZIycC7hBoQxHH9pNKQORJNozsQ= +github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8/go.mod h1:wcDNUvekVysuuOpQKo3191zZyTpiI6se1N1ULghS0sw= +github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= +github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/martian/v3 v3.3.3 h1:DIhPTQrbPkgs2yJYdXU/eNACCG5DVQjySNRNlflZ9Fc= +github.com/google/martian/v3 v3.3.3/go.mod h1:iEPrYcgCF7jA9OtScMFQyAlZZ4YXTKEtJ1E6RWzmBA0= +github.com/google/s2a-go v0.1.9 h1:LGD7gtMgezd8a/Xak7mEWL0PjoTQFvpRudN895yqKW0= +github.com/google/s2a-go v0.1.9/go.mod h1:YA0Ei2ZQL3acow2O62kdp9UlnvMmU7kA6Eutn0dXayM= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/googleapis/enterprise-certificate-proxy v0.3.4 h1:XYIDZApgAnrN1c855gTgghdIA6Stxb52D5RnLI1SLyw= +github.com/googleapis/enterprise-certificate-proxy v0.3.4/go.mod h1:YKe7cfqYXjKGpGvmSg28/fFvhNzinZQm8DGnaburhGA= +github.com/googleapis/gax-go/v2 v2.14.1 h1:hb0FFeiPaQskmvakKu5EbCbpntQn48jyHuvrkurSS/Q= +github.com/googleapis/gax-go/v2 v2.14.1/go.mod h1:Hb/NubMaVM88SrNkvl8X/o8XWwDJEPqouaLeN2IUxoA= +github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= +github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 h1:GFCKgmp0tecUJ0sJuv4pzYCqS9+RGSn52M3FUwPs+uo= +github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10/go.mod h1:t/avpk3KcrXxUnYOhZhMXJlSEyie6gQbtLq5NM3loB8= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= +go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= +go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA= +go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A= +go.opentelemetry.io/contrib/detectors/gcp v1.34.0 h1:JRxssobiPg23otYU5SbWtQC//snGVIM3Tx6QRzlQBao= +go.opentelemetry.io/contrib/detectors/gcp v1.34.0/go.mod h1:cV4BMFcscUR/ckqLkbfQmF0PRsq8w/lMGzdbCSveBHo= +go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.59.0 h1:rgMkmiGfix9vFJDcDi1PK8WEQP4FLQwLDfhp5ZLpFeE= +go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.59.0/go.mod h1:ijPqXp5P6IRRByFVVg9DY8P5HkxkHE5ARIa+86aXPf4= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.59.0 h1:CV7UdSGJt/Ao6Gp4CXckLxVRRsRgDHoI8XjbL3PDl8s= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.59.0/go.mod h1:FRmFuRJfag1IZ2dPkHnEoSFVgTVPUd2qf5Vi69hLb8I= +go.opentelemetry.io/otel v1.34.0 h1:zRLXxLCgL1WyKsPVrgbSdMN4c0FMkDAskSTQP+0hdUY= +go.opentelemetry.io/otel v1.34.0/go.mod h1:OWFPOQ+h4G8xpyjgqo4SxJYdDQ/qmRH+wivy7zzx9oI= +go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.29.0 h1:WDdP9acbMYjbKIyJUhTvtzj601sVJOqgWdUxSdR/Ysc= +go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.29.0/go.mod h1:BLbf7zbNIONBLPwvFnwNHGj4zge8uTCM/UPIVW1Mq2I= +go.opentelemetry.io/otel/metric v1.34.0 h1:+eTR3U0MyfWjRDhmFMxe2SsW64QrZ84AOhvqS7Y+PoQ= +go.opentelemetry.io/otel/metric v1.34.0/go.mod h1:CEDrp0fy2D0MvkXE+dPV7cMi8tWZwX3dmaIhwPOaqHE= +go.opentelemetry.io/otel/sdk v1.34.0 h1:95zS4k/2GOy069d321O8jWgYsW3MzVV+KuSPKp7Wr1A= +go.opentelemetry.io/otel/sdk v1.34.0/go.mod h1:0e/pNiaMAqaykJGKbi+tSjWfNNHMTxoC9qANsCzbyxU= +go.opentelemetry.io/otel/sdk/metric v1.34.0 h1:5CeK9ujjbFVL5c1PhLuStg1wxA7vQv7ce1EK0Gyvahk= +go.opentelemetry.io/otel/sdk/metric v1.34.0/go.mod h1:jQ/r8Ze28zRKoNRdkjCZxfs6YvBTG1+YIqyFVFYec5w= +go.opentelemetry.io/otel/trace v1.34.0 h1:+ouXS2V8Rd4hp4580a8q23bg0azF2nI8cqLYnC8mh/k= +go.opentelemetry.io/otel/trace v1.34.0/go.mod h1:Svm7lSjQD7kG7KJ/MUHPVXSDGz2OX4h0M2jHBhmSfRE= +golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc= +golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc= +golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0= +golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k= +golang.org/x/oauth2 v0.25.0 h1:CY4y7XT9v0cRI9oupztF8AgiIu99L/ksR/Xp/6jrZ70= +golang.org/x/oauth2 v0.25.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= +golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= +golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= +golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= +golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= +golang.org/x/time v0.9.0 h1:EsRrnYcQiGH+5FfbgvV4AP7qEZstoyrHB0DzarOQ4ZY= +golang.org/x/time v0.9.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= +google.golang.org/api v0.217.0 h1:GYrUtD289o4zl1AhiTZL0jvQGa2RDLyC+kX1N/lfGOU= +google.golang.org/api v0.217.0/go.mod h1:qMc2E8cBAbQlRypBTBWHklNJlaZZJBwDv81B1Iu8oSI= +google.golang.org/genai v0.1.0 h1:hAwvRGt7Nd79ZwrwYYJ2FSxeF4Cu/zTcNjA0tIIf0Ws= +google.golang.org/genai v0.1.0/go.mod h1:yPyKKBezIg2rqZziLhHQ5CD62HWr7sLDLc2PDzdrNVs= +google.golang.org/genproto v0.0.0-20250115164207-1a7da9e5054f h1:387Y+JbxF52bmesc8kq1NyYIp33dnxCw6eiA7JMsTmw= +google.golang.org/genproto v0.0.0-20250115164207-1a7da9e5054f/go.mod h1:0joYwWwLQh18AOj8zMYeZLjzuqcYTU3/nC5JdCvC3JI= +google.golang.org/genproto/googleapis/api v0.0.0-20250115164207-1a7da9e5054f h1:gap6+3Gk41EItBuyi4XX/bp4oqJ3UwuIMl25yGinuAA= +google.golang.org/genproto/googleapis/api v0.0.0-20250115164207-1a7da9e5054f/go.mod h1:Ic02D47M+zbarjYYUlK57y316f2MoN0gjAwI3f2S95o= +google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f h1:OxYkA3wjPsZyBylwymxSHa7ViiW1Sml4ToBrncvFehI= +google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f/go.mod h1:+2Yz8+CLJbIfL9z73EW45avw8Lmge3xVElCP9zEKi50= +google.golang.org/grpc v1.69.4 h1:MF5TftSMkd8GLw/m0KM6V8CMOCY6NZ1NQDPGFgbTt4A= +google.golang.org/grpc v1.69.4/go.mod h1:vyjdE6jLBI76dgpDojsFGNaHlxdjXN9ghpnd2o7JGZ4= +google.golang.org/protobuf v1.36.3 h1:82DV7MYdb8anAVi3qge1wSnMDrnKK7ebr+I0hHRN1BU= +google.golang.org/protobuf v1.36.3/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/genai/text_generation/text_generation_test.go b/genai/text_generation/text_generation_test.go new file mode 100644 index 0000000000..289a400e4b --- /dev/null +++ b/genai/text_generation/text_generation_test.go @@ -0,0 +1,45 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package text_generation + +import ( + "bytes" + "testing" + + "github.com/GoogleCloudPlatform/golang-samples/internal/testutil" +) + +func TestTextGeneration(t *testing.T) { + tc := testutil.SystemTest(t) + + t.Setenv("GOOGLE_GENAI_USE_VERTEXAI", "1") + t.Setenv("GOOGLE_CLOUD_LOCATION", "us-central1") + t.Setenv("GOOGLE_CLOUD_PROJECT", tc.ProjectID) + + buf := new(bytes.Buffer) + + t.Run("generate text with text prompt", func(t *testing.T) { + buf.Reset() + err := generateWithText(buf) + if err != nil { + t.Fatalf("generateWithText failed: %v", err) + } + + output := buf.String() + if output == "" { + t.Error("expected non-empty output, got empty") + } + }) +} diff --git a/genai/text_generation/textgen_with_txt.go b/genai/text_generation/textgen_with_txt.go new file mode 100644 index 0000000000..e856c285ed --- /dev/null +++ b/genai/text_generation/textgen_with_txt.go @@ -0,0 +1,59 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package text_generation shows examples of generating text using the GenAI SDK. +package text_generation + +// [START googlegenaisdk_textgen_with_txt] +import ( + "context" + "fmt" + "io" + + "google.golang.org/genai" +) + +// generateWithText shows how to generate text using a text prompt. +func generateWithText(w io.Writer) error { + ctx := context.Background() + + client, err := genai.NewClient(ctx, &genai.ClientConfig{}) + if err != nil { + return fmt.Errorf("unable to create genai client: %w", err) + } + + resp, err := client.Models.GenerateContent(ctx, + "gemini-2.0-flash-001", + genai.Text("How does AI work?"), + nil, + ) + if err != nil { + return fmt.Errorf("unable to generate content: %w", err) + } + + respText, err := resp.Text() + if err != nil { + return fmt.Errorf("unable to convert model response to text: %w", err) + } + fmt.Fprintln(w, respText) + // Example response: + // That's a great question! Understanding how AI works can feel like ... + // ... + // **1. The Foundation: Data and Algorithms** + // ... + + return nil +} + +// [END googlegenaisdk_textgen_with_txt] diff --git a/go.work b/go.work index e289cefdb3..88e3df323c 100644 --- a/go.work +++ b/go.work @@ -84,6 +84,7 @@ use ( ./functions/tips ./functions/tips/infinite_retries ./functions/typed/greeting + ./genai ./getting-started ./getting-started/authenticating-users ./getting-started/background From c0f7370cd69b1b33d6b671229a9200e004edcd68 Mon Sep 17 00:00:00 2001 From: Valeriy Burlaka Date: Thu, 6 Feb 2025 13:24:02 +0100 Subject: [PATCH 2/8] feat: update genai sdk version to 0.2.0 --- genai/go.mod | 2 +- genai/go.sum | 4 ++-- go.work | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/genai/go.mod b/genai/go.mod index 9d2e3ccadb..fdef951aca 100644 --- a/genai/go.mod +++ b/genai/go.mod @@ -4,7 +4,7 @@ go 1.23.0 require ( github.com/GoogleCloudPlatform/golang-samples v0.0.0-20250201051611-5fb145d1e974 - google.golang.org/genai v0.1.0 + google.golang.org/genai v0.2.0 ) require ( diff --git a/genai/go.sum b/genai/go.sum index 31a2b86ab2..76a61e7f81 100644 --- a/genai/go.sum +++ b/genai/go.sum @@ -113,8 +113,8 @@ golang.org/x/time v0.9.0 h1:EsRrnYcQiGH+5FfbgvV4AP7qEZstoyrHB0DzarOQ4ZY= golang.org/x/time v0.9.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= google.golang.org/api v0.217.0 h1:GYrUtD289o4zl1AhiTZL0jvQGa2RDLyC+kX1N/lfGOU= google.golang.org/api v0.217.0/go.mod h1:qMc2E8cBAbQlRypBTBWHklNJlaZZJBwDv81B1Iu8oSI= -google.golang.org/genai v0.1.0 h1:hAwvRGt7Nd79ZwrwYYJ2FSxeF4Cu/zTcNjA0tIIf0Ws= -google.golang.org/genai v0.1.0/go.mod h1:yPyKKBezIg2rqZziLhHQ5CD62HWr7sLDLc2PDzdrNVs= +google.golang.org/genai v0.2.0 h1:lWKoc2bru2VW9zBIgmBXQTKdT1oQ44W5Gfk950CkTNc= +google.golang.org/genai v0.2.0/go.mod h1:yPyKKBezIg2rqZziLhHQ5CD62HWr7sLDLc2PDzdrNVs= google.golang.org/genproto v0.0.0-20250115164207-1a7da9e5054f h1:387Y+JbxF52bmesc8kq1NyYIp33dnxCw6eiA7JMsTmw= google.golang.org/genproto v0.0.0-20250115164207-1a7da9e5054f/go.mod h1:0joYwWwLQh18AOj8zMYeZLjzuqcYTU3/nC5JdCvC3JI= google.golang.org/genproto/googleapis/api v0.0.0-20250115164207-1a7da9e5054f h1:gap6+3Gk41EItBuyi4XX/bp4oqJ3UwuIMl25yGinuAA= diff --git a/go.work b/go.work index 88e3df323c..791d9a836a 100644 --- a/go.work +++ b/go.work @@ -1,4 +1,4 @@ -go 1.22.7 +go 1.23.0 toolchain go1.23.4 From 2a80e1399d606c32499e88b78caef7a855be9587 Mon Sep 17 00:00:00 2001 From: Valeriy Burlaka Date: Thu, 6 Feb 2025 13:24:18 +0100 Subject: [PATCH 3/8] feat: standardize error messages --- genai/text_generation/textgen_with_txt.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/genai/text_generation/textgen_with_txt.go b/genai/text_generation/textgen_with_txt.go index e856c285ed..37d59da16a 100644 --- a/genai/text_generation/textgen_with_txt.go +++ b/genai/text_generation/textgen_with_txt.go @@ -30,7 +30,7 @@ func generateWithText(w io.Writer) error { client, err := genai.NewClient(ctx, &genai.ClientConfig{}) if err != nil { - return fmt.Errorf("unable to create genai client: %w", err) + return fmt.Errorf("failed to create genai client: %w", err) } resp, err := client.Models.GenerateContent(ctx, @@ -39,12 +39,12 @@ func generateWithText(w io.Writer) error { nil, ) if err != nil { - return fmt.Errorf("unable to generate content: %w", err) + return fmt.Errorf("failed to generate content: %w", err) } respText, err := resp.Text() if err != nil { - return fmt.Errorf("unable to convert model response to text: %w", err) + return fmt.Errorf("failed to convert model response to text: %w", err) } fmt.Fprintln(w, respText) // Example response: From a8e5b52357f39fa894097c44547e9f9292da927e Mon Sep 17 00:00:00 2001 From: Valeriy Burlaka Date: Thu, 6 Feb 2025 14:05:09 +0100 Subject: [PATCH 4/8] feat: text generation: generate from mute video --- genai/text_generation/text_generation_test.go | 13 ++++ .../textgen_with_mute_video.go | 66 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 genai/text_generation/textgen_with_mute_video.go diff --git a/genai/text_generation/text_generation_test.go b/genai/text_generation/text_generation_test.go index 289a400e4b..a4ea4dd0db 100644 --- a/genai/text_generation/text_generation_test.go +++ b/genai/text_generation/text_generation_test.go @@ -42,4 +42,17 @@ func TestTextGeneration(t *testing.T) { t.Error("expected non-empty output, got empty") } }) + + t.Run("generate text with video file input (no sound)", func(t *testing.T) { + buf.Reset() + err := generateWithMuteVideo(buf) + if err != nil { + t.Fatalf("generateWithMuteVideo failed: %v", err) + } + + output := buf.String() + if output == "" { + t.Error("expected non-empty output, got empty") + } + }) } diff --git a/genai/text_generation/textgen_with_mute_video.go b/genai/text_generation/textgen_with_mute_video.go new file mode 100644 index 0000000000..1591907469 --- /dev/null +++ b/genai/text_generation/textgen_with_mute_video.go @@ -0,0 +1,66 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package text_generation shows examples of generating text using the GenAI SDK. +package text_generation + +// [START googlegenaisdk_textgen_with_mute_video] +import ( + "context" + "fmt" + "io" + + genai "google.golang.org/genai" +) + +// generateWithMuteVideo shows how to generate text using a video with no sound as the input. +func generateWithMuteVideo(w io.Writer) error { + ctx := context.Background() + + client, err := genai.NewClient(ctx, &genai.ClientConfig{ + HTTPOptions: genai.HTTPOptions{APIVersion: "v1"}, + }) + if err != nil { + return fmt.Errorf("failed to create genai client: %w", err) + } + + modelName := "gemini-2.0-flash-001" + contents := []*genai.Content{ + {Parts: []*genai.Part{ + {Text: "What is in the video?"}, + {FileData: &genai.FileData{ + FileURI: "gs://cloud-samples-data/generative-ai/video/ad_copy_from_video.mp4", + MIMEType: "video/mp4", + }}, + }}, + } + + resp, err := client.Models.GenerateContent(ctx, modelName, contents, nil) + if err != nil { + return fmt.Errorf("failed to generate content: %w", err) + } + + respText, err := resp.Text() + if err != nil { + return fmt.Errorf("failed to convert model response to text: %w", err) + } + fmt.Fprintln(w, respText) + + // Example response: + // The video shows several surfers riding waves in an ocean setting. The waves are ... + + return nil +} + +// [END googlegenaisdk_textgen_with_mute_video] From 9b1521d083284b53901055ae32e7232159e434bc Mon Sep 17 00:00:00 2001 From: Valeriy Burlaka Date: Thu, 6 Feb 2025 16:01:41 +0100 Subject: [PATCH 5/8] feat: text generation: generate from video w. sound --- genai/text_generation/text_generation_test.go | 17 +++- genai/text_generation/textgen_with_video.go | 77 +++++++++++++++++++ 2 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 genai/text_generation/textgen_with_video.go diff --git a/genai/text_generation/text_generation_test.go b/genai/text_generation/text_generation_test.go index a4ea4dd0db..e45f0270ec 100644 --- a/genai/text_generation/text_generation_test.go +++ b/genai/text_generation/text_generation_test.go @@ -30,7 +30,7 @@ func TestTextGeneration(t *testing.T) { buf := new(bytes.Buffer) - t.Run("generate text with text prompt", func(t *testing.T) { + t.Run("generate with text prompt", func(t *testing.T) { buf.Reset() err := generateWithText(buf) if err != nil { @@ -43,7 +43,7 @@ func TestTextGeneration(t *testing.T) { } }) - t.Run("generate text with video file input (no sound)", func(t *testing.T) { + t.Run("generate with video file input (no sound)", func(t *testing.T) { buf.Reset() err := generateWithMuteVideo(buf) if err != nil { @@ -55,4 +55,17 @@ func TestTextGeneration(t *testing.T) { t.Error("expected non-empty output, got empty") } }) + + t.Run("generate with video file input", func(t *testing.T) { + buf.Reset() + err := generateWithVideo(buf) + if err != nil { + t.Fatalf("generateWithVideo failed: %v", err) + } + + output := buf.String() + if output == "" { + t.Error("expected non-empty output, got empty") + } + }) } diff --git a/genai/text_generation/textgen_with_video.go b/genai/text_generation/textgen_with_video.go new file mode 100644 index 0000000000..352431e38f --- /dev/null +++ b/genai/text_generation/textgen_with_video.go @@ -0,0 +1,77 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package text_generation shows examples of generating text using the GenAI SDK. +package text_generation + +// [START googlegenaisdk_textgen_with_video] +import ( + "context" + "fmt" + "io" + + genai "google.golang.org/genai" +) + +// generateWithVideo shows how to generate text using a video input. +func generateWithVideo(w io.Writer) error { + ctx := context.Background() + + client, err := genai.NewClient(ctx, &genai.ClientConfig{ + HTTPOptions: genai.HTTPOptions{APIVersion: "v1"}, + }) + if err != nil { + return fmt.Errorf("failed to create genai client: %w", err) + } + + modelName := "gemini-2.0-flash-001" + contents := []*genai.Content{ + {Parts: []*genai.Part{ + {Text: `Analyze the provided video file, including its audio. +Summarize the main points of the video concisely. +Create a chapter breakdown with timestamps for key sections or topics discussed.`}, + {FileData: &genai.FileData{ + FileURI: "gs://cloud-samples-data/generative-ai/video/pixel8.mp4", + MIMEType: "video/mp4", + }}, + }}, + } + + resp, err := client.Models.GenerateContent(ctx, modelName, contents, nil) + if err != nil { + return fmt.Errorf("failed to generate content: %w", err) + } + + respText, err := resp.Text() + if err != nil { + return fmt.Errorf("failed to convert model response to text: %w", err) + } + fmt.Fprintln(w, respText) + + // Example response: + // Here's an analysis of the provided video file: + // + // **Summary** + // + // The video features Saeka Shimada, a photographer in Tokyo, who uses the new Pixel phone ... + // + // **Chapter Breakdown** + // + // * **0:00-0:05**: Introduction to Saeka Shimada and her work as a photographer in Tokyo. + // ... + + return nil +} + +// [END googlegenaisdk_textgen_with_video] From be6270d49f6e59ae5aac125ae023a1563232c710 Mon Sep 17 00:00:00 2001 From: Valeriy Burlaka Date: Thu, 6 Feb 2025 16:10:06 +0100 Subject: [PATCH 6/8] feat: text generation: generate from audio input --- genai/text_generation/text_generation_test.go | 13 ++++ .../text_generation/textgen_with_gcs_audio.go | 77 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 genai/text_generation/textgen_with_gcs_audio.go diff --git a/genai/text_generation/text_generation_test.go b/genai/text_generation/text_generation_test.go index e45f0270ec..f8e20853c2 100644 --- a/genai/text_generation/text_generation_test.go +++ b/genai/text_generation/text_generation_test.go @@ -68,4 +68,17 @@ func TestTextGeneration(t *testing.T) { t.Error("expected non-empty output, got empty") } }) + + t.Run("generate with audio file input", func(t *testing.T) { + buf.Reset() + err := generateWithAudio(buf) + if err != nil { + t.Fatalf("generateWithAudio failed: %v", err) + } + + output := buf.String() + if output == "" { + t.Error("expected non-empty output, got empty") + } + }) } diff --git a/genai/text_generation/textgen_with_gcs_audio.go b/genai/text_generation/textgen_with_gcs_audio.go new file mode 100644 index 0000000000..8b5dbc7d10 --- /dev/null +++ b/genai/text_generation/textgen_with_gcs_audio.go @@ -0,0 +1,77 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package text_generation shows examples of generating text using the GenAI SDK. +package text_generation + +// [START googlegenaisdk_textgen_with_gcs_audio] +import ( + "context" + "fmt" + "io" + + genai "google.golang.org/genai" +) + +// generateWithAudio shows how to generate text using an audio input. +func generateWithAudio(w io.Writer) error { + ctx := context.Background() + + client, err := genai.NewClient(ctx, &genai.ClientConfig{ + HTTPOptions: genai.HTTPOptions{APIVersion: "v1"}, + }) + if err != nil { + return fmt.Errorf("failed to create genai client: %w", err) + } + + modelName := "gemini-2.0-flash-001" + contents := []*genai.Content{ + {Parts: []*genai.Part{ + {Text: `Provide the summary of the audio file. +Summarize the main points of the audio concisely. +Create a chapter breakdown with timestamps for key sections or topics discussed.`}, + {FileData: &genai.FileData{ + FileURI: "gs://cloud-samples-data/generative-ai/audio/pixel.mp3", + MIMEType: "audio/mpeg", + }}, + }}, + } + + resp, err := client.Models.GenerateContent(ctx, modelName, contents, nil) + if err != nil { + return fmt.Errorf("failed to generate content: %w", err) + } + + respText, err := resp.Text() + if err != nil { + return fmt.Errorf("failed to convert model response to text: %w", err) + } + fmt.Fprintln(w, respText) + + // Example response: + // Here is a summary and chapter breakdown of the audio file: + // + // **Summary:** + // + // The audio file is a "Made by Google" podcast episode discussing the Pixel Feature Drops, ... + // + // **Chapter Breakdown:** + // + // * **0:00 - 0:54:** Introduction to the podcast and guests, Aisha Sharif and DeCarlos Love. + // ... + + return nil +} + +// [END googlegenaisdk_textgen_with_gcs_audio] From 9fa6588099f8e9896f88232de44a1f0cffc284ce Mon Sep 17 00:00:00 2001 From: Valeriy Burlaka Date: Thu, 6 Feb 2025 16:18:57 +0100 Subject: [PATCH 7/8] feat: text generation: generate an audio transcript --- genai/text_generation/text_generation_test.go | 13 ++++ .../textgen_transcript_with_gcs_audio.go | 69 +++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 genai/text_generation/textgen_transcript_with_gcs_audio.go diff --git a/genai/text_generation/text_generation_test.go b/genai/text_generation/text_generation_test.go index f8e20853c2..bfa249cc03 100644 --- a/genai/text_generation/text_generation_test.go +++ b/genai/text_generation/text_generation_test.go @@ -81,4 +81,17 @@ func TestTextGeneration(t *testing.T) { t.Error("expected non-empty output, got empty") } }) + + t.Run("generate an audio transcript", func(t *testing.T) { + buf.Reset() + err := generateAudioTranscript(buf) + if err != nil { + t.Fatalf("generateAudioTranscript failed: %v", err) + } + + output := buf.String() + if output == "" { + t.Error("expected non-empty output, got empty") + } + }) } diff --git a/genai/text_generation/textgen_transcript_with_gcs_audio.go b/genai/text_generation/textgen_transcript_with_gcs_audio.go new file mode 100644 index 0000000000..8b9f89aa11 --- /dev/null +++ b/genai/text_generation/textgen_transcript_with_gcs_audio.go @@ -0,0 +1,69 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package text_generation shows examples of generating text using the GenAI SDK. +package text_generation + +// [START googlegenaisdk_textgen_transcript_with_gcs_audio] +import ( + "context" + "fmt" + "io" + + genai "google.golang.org/genai" +) + +// generateAudioTranscript shows how to generate an audio transcript. +func generateAudioTranscript(w io.Writer) error { + ctx := context.Background() + + client, err := genai.NewClient(ctx, &genai.ClientConfig{ + HTTPOptions: genai.HTTPOptions{APIVersion: "v1"}, + }) + if err != nil { + return fmt.Errorf("failed to create genai client: %w", err) + } + + modelName := "gemini-2.0-flash-001" + contents := []*genai.Content{ + {Parts: []*genai.Part{ + {Text: `Transcribe the interview, in the format of timecode, speaker, caption. +Use speaker A, speaker B, etc. to identify speakers.`}, + {FileData: &genai.FileData{ + FileURI: "gs://cloud-samples-data/generative-ai/audio/pixel.mp3", + MIMEType: "audio/mpeg", + }}, + }}, + } + + resp, err := client.Models.GenerateContent(ctx, modelName, contents, nil) + if err != nil { + return fmt.Errorf("failed to generate content: %w", err) + } + + respText, err := resp.Text() + if err != nil { + return fmt.Errorf("failed to convert model response to text: %w", err) + } + fmt.Fprintln(w, respText) + + // Example response: + // 00:00:00, A: your devices are getting better over time. + // 00:01:13, A: And so we think about it across the entire portfolio from phones to watch, ... + // ... + + return nil +} + +// [END googlegenaisdk_textgen_transcript_with_gcs_audio] From 92b906c0655e702e8c7a0c088934bd53c79479ec Mon Sep 17 00:00:00 2001 From: Valeriy Burlaka Date: Thu, 13 Feb 2025 14:45:27 +0100 Subject: [PATCH 8/8] chore(genai sdk): update sdk version --- genai/go.mod | 2 +- genai/go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/genai/go.mod b/genai/go.mod index fdef951aca..feb4a76b5e 100644 --- a/genai/go.mod +++ b/genai/go.mod @@ -4,7 +4,7 @@ go 1.23.0 require ( github.com/GoogleCloudPlatform/golang-samples v0.0.0-20250201051611-5fb145d1e974 - google.golang.org/genai v0.2.0 + google.golang.org/genai v0.3.0 ) require ( diff --git a/genai/go.sum b/genai/go.sum index 76a61e7f81..0d6ca71543 100644 --- a/genai/go.sum +++ b/genai/go.sum @@ -113,8 +113,8 @@ golang.org/x/time v0.9.0 h1:EsRrnYcQiGH+5FfbgvV4AP7qEZstoyrHB0DzarOQ4ZY= golang.org/x/time v0.9.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= google.golang.org/api v0.217.0 h1:GYrUtD289o4zl1AhiTZL0jvQGa2RDLyC+kX1N/lfGOU= google.golang.org/api v0.217.0/go.mod h1:qMc2E8cBAbQlRypBTBWHklNJlaZZJBwDv81B1Iu8oSI= -google.golang.org/genai v0.2.0 h1:lWKoc2bru2VW9zBIgmBXQTKdT1oQ44W5Gfk950CkTNc= -google.golang.org/genai v0.2.0/go.mod h1:yPyKKBezIg2rqZziLhHQ5CD62HWr7sLDLc2PDzdrNVs= +google.golang.org/genai v0.3.0 h1:xSYQAFmZvHbQhK8Ay9FvpecMcqVhTGZbLSRiDyxEVBs= +google.golang.org/genai v0.3.0/go.mod h1:yPyKKBezIg2rqZziLhHQ5CD62HWr7sLDLc2PDzdrNVs= google.golang.org/genproto v0.0.0-20250115164207-1a7da9e5054f h1:387Y+JbxF52bmesc8kq1NyYIp33dnxCw6eiA7JMsTmw= google.golang.org/genproto v0.0.0-20250115164207-1a7da9e5054f/go.mod h1:0joYwWwLQh18AOj8zMYeZLjzuqcYTU3/nC5JdCvC3JI= google.golang.org/genproto/googleapis/api v0.0.0-20250115164207-1a7da9e5054f h1:gap6+3Gk41EItBuyi4XX/bp4oqJ3UwuIMl25yGinuAA=