googleapis/google-api-ruby-client

Causing many GRPC requests per calling discovery engine search api once

Closed this issue · 1 comments

Environment details

  • OS: Sonoma 14.4.1
  • Ruby version: 3.2.2
  • Gem name and version: google-cloud-discovery_engine-v1 (0.7.0)

Code example

# document: https://cloud.google.com/ruby/docs/reference/google-cloud-discovery_engine-v1/0.1.0
require 'google/cloud/discovery_engine/v1'

class VertexAIRepository
  class << self
    def search_event_ids(search_query:)
      client = initialize_search_client
      request = build_search_request(search_query:)
      fetch_event_ids_from_vertex_ai(client:, request:)
    end

    private

    def initialize_search_client
      service_account_creds = ENV.fetch('GOOGLE_CLOUD_CREDENTIALS')
      begin
        secret_data = JSON.parse(service_account_creds)
      rescue JSON::ParserError => e
        raise "Error parsing JSON secret string: #{e.message}"
      end

      Google::Cloud::DiscoveryEngine::V1::DocumentService::Client.configure do |config|
        config.credentials = Google::Auth::Credentials.new(secret_data)
      end
      Google::Cloud::DiscoveryEngine::V1::SearchService::Client.new
    end

    def build_search_request(search_query:)
      gcp_project_id = ENV.fetch('GOOGLE_CLOUD_PROJECT_ID')
      vertex_ai_engine_id = ENV.fetch('VERTEX_AI_ENGINE_ID')

      serving_config = "projects/#{gcp_project_id}/locations/global/collections/default_collection/engines/#{vertex_ai_engine_id}/servingConfigs/default_config"
      content_search_spec = Google::Cloud::DiscoveryEngine::V1::SearchRequest::ContentSearchSpec.new(
        snippet_spec: Google::Cloud::DiscoveryEngine::V1::SearchRequest::ContentSearchSpec::SnippetSpec.new
        # NOTE: summary spec is not currently supproted for Ruby sdk.
      )
      # Create a request. To set request fields, pass in keyword arguments.
      Google::Cloud::DiscoveryEngine::V1::SearchRequest.new(
        serving_config:, query: search_query, page_size: 10, content_search_spec:
      )
    end

    def fetch_event_ids_from_vertex_ai(client:, request:)
      event_ids = []
      begin
        result = client.search request
        result.each do |item|
          event_ids.push(item.to_h[:id])
        end
        event_ids
      rescue Google::Cloud::Error => e
        Rails.logger.error e.message
        Sentry.capture_message(e.message)
      end
      event_ids
    end
  end
end

Then, I checked cloud logging and there were many GRPC request per single call of the above code.

My guess is that if there are 50 search results for page_size=10, the number of GRPCs would be 50/10 = 5 times. If this guess is correct, is this the expected behavior and what should I do to reduce the number of requests?