Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Causing many GRPC requests per calling discovery engine search api once #18967

Closed
tsugumi-sys opened this issue May 7, 2024 · 1 comment
Closed
Assignees

Comments

@tsugumi-sys
Copy link

tsugumi-sys commented May 7, 2024

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?

@dazuma
Copy link
Member

dazuma commented Jun 6, 2024

Duplicate of googleapis/google-cloud-ruby#26035

@dazuma dazuma marked this as a duplicate of googleapis/google-cloud-ruby#26035 Jun 6, 2024
@dazuma dazuma closed this as completed Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants