Class: OpenAI::Resources::VectorStores::Files

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

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ Files

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 Files.

Parameters:



429
430
431
# File 'lib/openai/resources/vector_stores/files.rb', line 429

def initialize(client:)
  @client = client
end

Instance Method Details

#content(file_id, vector_store_id:, request_options: {}) ⇒ OpenAI::Internal::Page<OpenAI::Models::VectorStores::FileContentResponse>

Retrieve the parsed contents of a vector store file.

Parameters:

  • file_id (String)

    The ID of the file within the vector store.

  • vector_store_id (String)

    The ID of the vector store.

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

Returns:

See Also:



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/openai/resources/vector_stores/files.rb', line 410

def content(file_id, params)
  parsed, options = OpenAI::VectorStores::FileContentParams.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/files/%2$s/content", vector_store_id, file_id],
    page: OpenAI::Internal::Page,
    model: OpenAI::Models::VectorStores::FileContentResponse,
    security: {bearer_auth: true},
    options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
  )
end

#create(vector_store_id, file_id:, attributes: nil, chunking_strategy: nil, request_options: {}) ⇒ OpenAI::Models::VectorStores::VectorStoreFile

Create a vector store file by attaching a File to a vector store.

Parameters:

  • vector_store_id (String)

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

  • file_id (String)

    A File ID that the vector store should use. Useful for tools like file_search that can access files. For multi-file ingestion, we recommend file_batches to minimize per-vector-store write requests.

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

    Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format, and querying for objects via API or the dashboard. Keys are strings with a maximum length of 64 characters. Values are strings with a maximum length of 512 characters, booleans, or numbers.

  • chunking_strategy (OpenAI::Models::AutoFileChunkingStrategyParam, OpenAI::Models::StaticFileChunkingStrategyObjectParam)

    The chunking strategy used to chunk the file(s). If not set, will use the auto strategy. Only applicable if file_ids is non-empty.

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

Returns:

See Also:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/openai/resources/vector_stores/files.rb', line 41

def create(vector_store_id, params)
  parsed, options = OpenAI::VectorStores::FileCreateParams.dump_request(params)
  @client.request(
    method: :post,
    path: ["vector_stores/%1$s/files", vector_store_id],
    body: parsed,
    model: OpenAI::VectorStores::VectorStoreFile,
    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, file_id:, attributes: nil, chunking_strategy: nil, poll_interval: nil, timeout: 1800.0, request_options: {}) ⇒ OpenAI::Models::VectorStores::VectorStoreFile

Attach a file to a vector store and wait for processing to finish.

The returned file may have a failed or cancelled status; callers should inspect the status and last_error. 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.

  • file_id (String)

    A File ID to attach to the vector store.

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

    Attributes to attach to the vector store file.

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

    The chunking strategy used to chunk the file.

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

    How often to retrieve the file. 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:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/openai/resources/vector_stores/files.rb', line 87

def create_and_poll(
  vector_store_id,
  file_id:,
  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)

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

#delete(file_id, vector_store_id:, request_options: {}) ⇒ OpenAI::Models::VectorStores::VectorStoreFileDeleted

Delete a vector store file. This will remove the file from the vector store but the file itself will not be deleted. To delete the file, use the delete file endpoint.

Parameters:

  • file_id (String)

    The ID of the file to delete.

  • vector_store_id (String)

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

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

Returns:

See Also:



246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/openai/resources/vector_stores/files.rb', line 246

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

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

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

Returns a list of vector store files.

Parameters:

  • vector_store_id (String)

    The ID of the vector store that the files belong to.

  • after (String)

    A cursor for use in pagination. after is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.

  • before (String)

    A cursor for use in pagination. before is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, starting with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.

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

    Filter by file status. One of in_progress, completed, failed, cancelled.

  • limit (Integer)

    A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.

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

    Sort order by the created_at timestamp of the objects. asc for ascending order and desc for descending order.

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

Returns:

See Also:



214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/openai/resources/vector_stores/files.rb', line 214

def list(vector_store_id, params = {})
  parsed, options = OpenAI::VectorStores::FileListParams.dump_request(params)
  query = OpenAI::Internal::Util.encode_query_params(parsed)
  @client.request(
    method: :get,
    path: ["vector_stores/%1$s/files", vector_store_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(file_id, vector_store_id:, poll_interval: nil, timeout: 1800.0, request_options: {}) ⇒ OpenAI::Models::VectorStores::VectorStoreFile

Wait for a vector store file to finish processing.

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

Parameters:

  • file_id (String)

    The ID of the file being retrieved.

  • vector_store_id (String)

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

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

    How often to retrieve the file. 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:



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/openai/resources/vector_stores/files.rb', line 283

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

#retrieve(file_id, vector_store_id:, request_options: {}) ⇒ OpenAI::Models::VectorStores::VectorStoreFile

Retrieves a vector store file.

Parameters:

  • file_id (String)

    The ID of the file being retrieved.

  • vector_store_id (String)

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

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

Returns:

See Also:



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/openai/resources/vector_stores/files.rb', line 126

def retrieve(file_id, params)
  parsed, options = OpenAI::VectorStores::FileRetrieveParams.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/files/%2$s", vector_store_id, file_id],
    model: OpenAI::VectorStores::VectorStoreFile,
    security: {bearer_auth: true},
    options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
  )
end

#update(file_id, vector_store_id:, attributes:, request_options: {}) ⇒ OpenAI::Models::VectorStores::VectorStoreFile

Update attributes on a vector store file.

Parameters:

  • file_id (String)

    Path param: The ID of the file to update attributes.

  • vector_store_id (String)

    Path param: The ID of the vector store the file belongs to.

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

    Body param: Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format, and querying for objects via API or the dashboard. Keys are strings with a maximum length of 64 characters. Values are strings with a maximum length of 512 characters, booleans, or numbers.

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

Returns:

See Also:



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/openai/resources/vector_stores/files.rb', line 163

def update(file_id, params)
  parsed, options = OpenAI::VectorStores::FileUpdateParams.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/files/%2$s", vector_store_id, file_id],
    body: parsed,
    model: OpenAI::VectorStores::VectorStoreFile,
    security: {bearer_auth: true},
    options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
  )
end

#upload(vector_store_id, file:, attributes: nil, chunking_strategy: nil, request_options: {}) ⇒ OpenAI::Models::VectorStores::VectorStoreFile

Upload a file and attach it to a vector store.

Processing continues asynchronously; use #upload_and_poll to wait until the file is ready.

Parameters:

Returns:



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/openai/resources/vector_stores/files.rb', line 319

def upload(
  vector_store_id,
  file:,
  attributes: nil,
  chunking_strategy: nil,
  request_options: {}
)
  request_options_scope = OpenAI::Internal::RequestOptionsScope.new(request_options)
  uploaded = @client.files.create(
    file: file,
    purpose: :assistants,
    request_options: request_options_scope.child("file-upload")
  )
  params = {
    file_id: uploaded.id,
    request_options: request_options_scope.child("vector-store-file")
  }
  params[:attributes] = attributes unless attributes.nil?
  params[:chunking_strategy] = chunking_strategy unless chunking_strategy.nil?
  create(vector_store_id, params)
end

#upload_and_poll(vector_store_id, file:, attributes: nil, chunking_strategy: nil, poll_interval: nil, timeout: 1800.0, request_options: {}) ⇒ OpenAI::Models::VectorStores::VectorStoreFile

Upload a file, attach it to a vector store, and wait for processing to finish.

The returned file may have a failed or cancelled status; callers should inspect the status and last_error.

Parameters:

  • vector_store_id (String)

    The ID of the vector store to attach the file to.

  • file (Pathname, StringIO, IO, String, OpenAI::FilePart)

    The file to upload.

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

    Attributes to attach to the vector store file.

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

    The chunking strategy used to chunk the file.

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

    How often to retrieve the file. 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 the upload, attach, and polling requests. Idempotency keys are scoped to each write.

Returns:

Raises:



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/openai/resources/vector_stores/files.rb', line 368

def upload_and_poll(
  vector_store_id,
  file:,
  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)

  attached = upload(
    vector_store_id,
    file: file,
    attributes: attributes,
    chunking_strategy: chunking_strategy,
    request_options: request_options
  )
  poll(
    attached.id,
    vector_store_id: vector_store_id,
    poll_interval: poll_interval,
    timeout: timeout,
    request_options: request_options
  )
end