Class: OpenAI::Resources::VectorStores::FileBatches

Inherits:
Object
  • Object
show all
Defined in:
lib/openai/resources/vector_stores/file_batches.rb,
sig/openai/resources/vector_stores/file_batches.rbs

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ FileBatches

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of FileBatches.

Parameters:



329
330
331
# File 'lib/openai/resources/vector_stores/file_batches.rb', line 329

def initialize(client:)
  @client = client
end

Instance Method Details

#cancel(batch_id, vector_store_id:, request_options: {}) ⇒ OpenAI::Models::VectorStores::VectorStoreFileBatch

Cancel a vector store file batch. This attempts to cancel the processing of files in this batch as soon as possible.

Parameters:

  • batch_id (String)

    The ID of the file batch to cancel.

  • vector_store_id (String)

    The ID of the vector store that the file batch belongs to.

  • request_options (OpenAI::RequestOptions, Hash{Symbol=>Object}, nil)

Returns:

See Also:



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/openai/resources/vector_stores/file_batches.rb', line 148

def cancel(batch_id, params)
  parsed, options = OpenAI::VectorStores::FileBatchCancelParams.dump_request(params)
  vector_store_id = parsed.delete(:vector_store_id) do
    raise ArgumentError.new("missing required path argument #{_1}")
  end

  @client.request(
    method: :post,
    path: ["vector_stores/%1$s/file_batches/%2$s/cancel", vector_store_id, batch_id],
    model: OpenAI::VectorStores::VectorStoreFileBatch,
    security: {bearer_auth: true},
    options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
  )
end

#create(vector_store_id, attributes: nil, chunking_strategy: nil, file_ids: nil, files: nil, request_options: {}) ⇒ OpenAI::Models::VectorStores::VectorStoreFileBatch

Some parameter documentations has been truncated, see Models::VectorStores::FileBatchCreateParams for more details.

Create a vector store file batch.

Parameters:

Returns:

See Also:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/openai/resources/vector_stores/file_batches.rb', line 31

def create(vector_store_id, params = {})
  parsed, options = OpenAI::VectorStores::FileBatchCreateParams.dump_request(params)
  @client.request(
    method: :post,
    path: ["vector_stores/%1$s/file_batches", vector_store_id],
    body: parsed,
    model: OpenAI::VectorStores::VectorStoreFileBatch,
    security: {bearer_auth: true},
    options: {
      **options,
      extra_headers: OpenAI::Internal::Util.normalized_headers(
        {"OpenAI-Beta" => "assistants=v2"},
        options[:extra_headers].to_h
      )
    }
  )
end

#create_and_poll(vector_store_id, attributes: nil, chunking_strategy: nil, file_ids: nil, files: nil, poll_interval: nil, timeout: 1800.0, request_options: {}) ⇒ OpenAI::Models::VectorStores::VectorStoreFileBatch

Create a vector store file batch and wait for processing to finish.

The returned batch may have a failed or cancelled status; callers should inspect its status and file counts. Polling intervals and the overall timeout are in seconds. Finite timeouts include authentication and request replay time and disable transport retries so the deadline remains strict. Set timeout to nil to wait indefinitely and retain configured transport retries.

Parameters:

  • vector_store_id (String)

    The ID of the vector store for which to create a File Batch.

  • attributes (Hash{Symbol=>String, Float, Boolean}, nil) (defaults to: nil)

    Attributes to apply to each file in file_ids.

  • chunking_strategy (OpenAI::Models::AutoFileChunkingStrategyParam, OpenAI::Models::StaticFileChunkingStrategyObjectParam) (defaults to: nil)

    The chunking strategy used to chunk the files.

  • file_ids (Array<String>) (defaults to: nil)

    File IDs to add to the vector store.

  • files (Array<OpenAI::Models::VectorStores::FileBatchCreateParams::File>) (defaults to: nil)

    File IDs with per-file attributes or chunking strategies.

  • poll_interval (Integer, Float, nil) (defaults to: nil)

    How often to retrieve the batch. When omitted, the SDK honors the server's polling hint and otherwise waits 5 seconds.

  • timeout (Integer, Float, nil) (defaults to: OpenAI::Internal::Poller::DEFAULT_TIMEOUT)

    Maximum total time to poll. Defaults to 30 minutes. Set to nil to wait indefinitely.

  • request_options (OpenAI::RequestOptions, Hash{Symbol=>Object}, nil) (defaults to: {})

Returns:

Raises:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/openai/resources/vector_stores/file_batches.rb', line 79

def create_and_poll(
  vector_store_id,
  attributes: nil,
  chunking_strategy: nil,
  file_ids: nil,
  files: nil,
  poll_interval: nil,
  timeout: OpenAI::Internal::Poller::DEFAULT_TIMEOUT,
  request_options: {}
)
  OpenAI::Internal::Poller.validate!(poll_interval: poll_interval, timeout: timeout)

  params = {request_options: request_options}
  params[:attributes] = attributes unless attributes.nil?
  params[:chunking_strategy] = chunking_strategy unless chunking_strategy.nil?
  params[:file_ids] = file_ids unless file_ids.nil?
  params[:files] = files unless files.nil?
  batch = create(vector_store_id, params)
  poll(
    batch.id,
    vector_store_id: vector_store_id,
    poll_interval: poll_interval,
    timeout: timeout,
    request_options: request_options
  )
end

#list_files(batch_id, vector_store_id:, after: nil, before: nil, filter: nil, limit: nil, order: nil, request_options: {}) ⇒ OpenAI::Internal::CursorPage<OpenAI::Models::VectorStores::VectorStoreFile>

Some parameter documentations has been truncated, see Models::VectorStores::FileBatchListFilesParams for more details.

Returns a list of vector store files in a batch.

Parameters:

  • batch_id (String)

    Path param: The ID of the file batch that the files belong to.

  • vector_store_id (String)

    Path param: The ID of the vector store that the files belong to.

  • after (String)

    Query param: A cursor for use in pagination. after is an object ID that define

  • before (String)

    Query param: A cursor for use in pagination. before is an object ID that defin

  • filter (Symbol, OpenAI::Models::VectorStores::FileBatchListFilesParams::Filter)

    Query param: Filter by file status. One of in_progress, completed, failed,

  • limit (Integer)

    Query param: A limit on the number of objects to be returned. Limit can range be

  • order (Symbol, OpenAI::Models::VectorStores::FileBatchListFilesParams::Order)

    Query param: Sort order by the created_at timestamp of the objects. asc for

  • request_options (OpenAI::RequestOptions, Hash{Symbol=>Object}, nil)

Returns:

See Also:



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/openai/resources/vector_stores/file_batches.rb', line 189

def list_files(batch_id, params)
  parsed, options = OpenAI::VectorStores::FileBatchListFilesParams.dump_request(params)
  vector_store_id = parsed.delete(:vector_store_id) do
    raise ArgumentError.new("missing required path argument #{_1}")
  end

  query = OpenAI::Internal::Util.encode_query_params(parsed)
  @client.request(
    method: :get,
    path: ["vector_stores/%1$s/file_batches/%2$s/files", vector_store_id, batch_id],
    query: query,
    page: OpenAI::Internal::CursorPage,
    model: OpenAI::VectorStores::VectorStoreFile,
    security: {bearer_auth: true},
    options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
  )
end

#poll(batch_id, vector_store_id:, poll_interval: nil, timeout: 1800.0, request_options: {}) ⇒ OpenAI::Models::VectorStores::VectorStoreFileBatch

Wait for a vector store file batch to finish processing.

The returned batch may have a failed or cancelled status; callers should inspect its status and file counts. Polling intervals and the overall timeout are in seconds. Set timeout to nil to wait indefinitely.

Parameters:

  • batch_id (String)

    The ID of the file batch being retrieved.

  • vector_store_id (String)

    The ID of the vector store that the file batch belongs to.

  • poll_interval (Integer, Float, nil) (defaults to: nil)

    How often to retrieve the batch. When omitted, the SDK honors the server's polling hint and otherwise waits 5 seconds.

  • timeout (Integer, Float, nil) (defaults to: OpenAI::Internal::Poller::DEFAULT_TIMEOUT)

    Maximum total time to poll. Defaults to 30 minutes. Set to nil to wait indefinitely.

  • request_options (OpenAI::RequestOptions, Hash{Symbol=>Object}, nil) (defaults to: {})

Returns:

Raises:



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/openai/resources/vector_stores/file_batches.rb', line 229

def poll(
  batch_id,
  vector_store_id:,
  poll_interval: nil,
  timeout: OpenAI::Internal::Poller::DEFAULT_TIMEOUT,
  request_options: {}
)
  OpenAI::Helpers::ResourcePolling.poll_vector_store_file_batch(
    self,
    batch_id,
    vector_store_id: vector_store_id,
    poll_interval: poll_interval,
    timeout: timeout,
    request_options: request_options
  )
end

#retrieve(batch_id, vector_store_id:, request_options: {}) ⇒ OpenAI::Models::VectorStores::VectorStoreFileBatch

Retrieves a vector store file batch.

Parameters:

  • batch_id (String)

    The ID of the file batch being retrieved.

  • vector_store_id (String)

    The ID of the vector store that the file batch belongs to.

  • request_options (OpenAI::RequestOptions, Hash{Symbol=>Object}, nil)

Returns:

See Also:



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/openai/resources/vector_stores/file_batches.rb', line 119

def retrieve(batch_id, params)
  parsed, options = OpenAI::VectorStores::FileBatchRetrieveParams.dump_request(params)
  vector_store_id = parsed.delete(:vector_store_id) do
    raise ArgumentError.new("missing required path argument #{_1}")
  end

  @client.request(
    method: :get,
    path: ["vector_stores/%1$s/file_batches/%2$s", vector_store_id, batch_id],
    model: OpenAI::VectorStores::VectorStoreFileBatch,
    security: {bearer_auth: true},
    options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
  )
end

#upload_and_poll(vector_store_id, files:, file_ids: [], max_concurrency: 5, attributes: nil, chunking_strategy: nil, poll_interval: nil, timeout: 1800.0, request_options: {}) ⇒ OpenAI::Models::VectorStores::VectorStoreFileBatch

Upload files concurrently, create a vector store file batch, and wait for processing to finish.

Existing file IDs can be included alongside new uploads. Upload concurrency is bounded and defaults to 5. Set max_concurrency to 1 for sequential uploads. Inputs are enumerated before requests begin so the 2,000-file API limit can be checked without orphaning uploads. Stream-backed inputs are safely spooled during enumeration, including IO objects yielded from block-scoped enumerators.

Parameters:

  • vector_store_id (String)

    The ID of the vector store for which to create a File Batch.

  • files (Enumerable<Pathname, StringIO, IO, String, OpenAI::FilePart>)

    Files to upload.

  • file_ids (Array<String>) (defaults to: [])

    IDs of files that have already been uploaded.

  • max_concurrency (Integer) (defaults to: 5)

    Maximum number of simultaneous file uploads.

  • attributes (Hash{Symbol=>String, Float, Boolean}, nil) (defaults to: nil)

    Attributes to apply to every file.

  • chunking_strategy (OpenAI::Models::AutoFileChunkingStrategyParam, OpenAI::Models::StaticFileChunkingStrategyObjectParam) (defaults to: nil)

    The chunking strategy used to chunk the files.

  • poll_interval (Integer, Float, nil) (defaults to: nil)

    How often to retrieve the batch. When omitted, the SDK honors the server's polling hint and otherwise waits 5 seconds.

  • timeout (Integer, Float, nil) (defaults to: OpenAI::Internal::Poller::DEFAULT_TIMEOUT)

    Maximum total time to poll. Defaults to 30 minutes. Set to nil to wait indefinitely.

  • request_options (OpenAI::RequestOptions, Hash{Symbol=>Object}, nil) (defaults to: {})

    Applied to every upload and to the batch creation and polling requests. Idempotency keys are scoped to each upload and the batch creation.

Returns:

Raises:



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/openai/resources/vector_stores/file_batches.rb', line 281

def upload_and_poll(
  vector_store_id,
  files:,
  file_ids: [],
  max_concurrency: 5,
  attributes: nil,
  chunking_strategy: nil,
  poll_interval: nil,
  timeout: OpenAI::Internal::Poller::DEFAULT_TIMEOUT,
  request_options: {}
)
  OpenAI::Internal::Poller.validate!(poll_interval: poll_interval, timeout: timeout)
  request_options_scope = OpenAI::Internal::RequestOptionsScope.new(request_options)

  max_files = OpenAI::Internal::VectorStoreFileUploader::MAX_FILES_PER_BATCH
  if file_ids.length > max_files
    raise ArgumentError, "`file_ids` cannot contain more than #{max_files} entries"
  end

  uploaded = OpenAI::Internal::VectorStoreFileUploader
    .new(
      client: @client,
      max_concurrency: max_concurrency,
      request_options: request_options
    )
    .upload(files, max_files: max_files - file_ids.length)

  if uploaded.empty?
    raise(
      ArgumentError,
      "No `files` provided. Use `create_and_poll` when all files are already uploaded."
    )
  end

  create_and_poll(
    vector_store_id,
    file_ids: [*file_ids, *uploaded.map(&:id)],
    attributes: attributes,
    chunking_strategy: chunking_strategy,
    poll_interval: poll_interval,
    timeout: timeout,
    request_options: request_options_scope.child("file-batch")
  )
end