Class: Parse::Embeddings::BatchEmbedder
- Inherits:
-
Object
- Object
- Parse::Embeddings::BatchEmbedder
- Defined in:
- lib/parse/embeddings/batch_embedder.rb
Overview
Batch-level orchestration for bulk embedding jobs.
Provider#embed_text_batched only slices input into provider-sized chunks; any retry/backoff lives inside each provider's single HTTP call. That is the wrong layer for bulk work: a 50k-document backfill needs batch-level pacing (stay under the provider's requests-per-minute budget across calls) and batch-level backoff (a 429 after the provider's internal retries are exhausted should pause the whole job, not kill it). BatchEmbedder wraps any registered provider with both.
== Retry classification
By default a batch is retried when the provider raises a
Error subclass whose class name ends in
RateLimitError or TransientError — the convention every
bundled provider follows (OpenAI::RateLimitError,
Voyage::TransientError, …). Pass retry_on: with explicit
exception classes to override. Non-retryable errors (auth,
bad-request, response-contract violations) propagate immediately.
Vectors are returned aligned 1:1 with the input, identical to
embed_text on the wrapped provider.
Defined Under Namespace
Classes: BatchFailed
Constant Summary collapse
- RETRYABLE_NAME_SUFFIXES =
%w[RateLimitError TransientError].freeze
Instance Attribute Summary collapse
-
#provider ⇒ Provider
readonly
The wrapped provider.
Instance Method Summary collapse
-
#embed_text(strings, input_type: :search_document) ⇒ Array<Array<Float>>
Embed
stringsthrough the wrapped provider with pacing and batch-level backoff. -
#initialize(provider, batch_size: nil, requests_per_minute: nil, max_attempts: 5, base_delay: 2.0, max_delay: 60.0, jitter: 0.25, retry_on: nil, on_progress: nil) ⇒ BatchEmbedder
constructor
A new instance of BatchEmbedder.
Constructor Details
#initialize(provider, batch_size: nil, requests_per_minute: nil, max_attempts: 5, base_delay: 2.0, max_delay: 60.0, jitter: 0.25, retry_on: nil, on_progress: nil) ⇒ BatchEmbedder
Returns a new instance of BatchEmbedder.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/parse/embeddings/batch_embedder.rb', line 85 def initialize(provider, batch_size: nil, requests_per_minute: nil, max_attempts: 5, base_delay: 2.0, max_delay: 60.0, jitter: 0.25, retry_on: nil, on_progress: nil) unless provider.is_a?(Provider) raise ArgumentError, "Parse::Embeddings::BatchEmbedder expects a Parse::Embeddings::Provider " \ "(got #{provider.class})." end @provider = provider @batch_size = batch_size ? Integer(batch_size) : nil raise ArgumentError, "batch_size must be positive" if @batch_size && @batch_size <= 0 @min_interval = requests_per_minute ? (60.0 / Float(requests_per_minute)) : nil @max_attempts = Integer(max_attempts) raise ArgumentError, "max_attempts must be >= 1" if @max_attempts < 1 @base_delay = Float(base_delay) @max_delay = Float(max_delay) @jitter = Float(jitter) @retry_on = retry_on && Array(retry_on) @on_progress = on_progress @last_call_at = nil end |
Instance Attribute Details
#provider ⇒ Provider (readonly)
Returns the wrapped provider.
64 65 66 |
# File 'lib/parse/embeddings/batch_embedder.rb', line 64 def provider @provider end |
Instance Method Details
#embed_text(strings, input_type: :search_document) ⇒ Array<Array<Float>>
Embed strings through the wrapped provider with pacing and
batch-level backoff.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/parse/embeddings/batch_embedder.rb', line 114 def (strings, input_type: :search_document) unless strings.is_a?(Array) raise ArgumentError, "Parse::Embeddings::BatchEmbedder#embed_text expects Array<String> " \ "(got #{strings.class})." end return [] if strings.empty? size = @batch_size || @provider. || 64 batches = strings.each_slice(size).to_a out = [] batches.each_with_index do |batch, idx| out.concat(run_batch(batch, input_type, idx, out.length)) if @on_progress @on_progress.call(done: out.length, total: strings.length, batch_index: idx, batch_count: batches.length) end end out end |