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:



398
399
400
# File 'lib/openai/resources/vector_stores/files.rb', line 398

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:



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/openai/resources/vector_stores/files.rb', line 379

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

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

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

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/files.rb', line 31

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:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/openai/resources/vector_stores/files.rb', line 77

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:



217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/openai/resources/vector_stores/files.rb', line 217

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>

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

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

  • before (String)

    A cursor for use in pagination. before is an object ID that defines your place

  • 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 1

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

    Sort order by the created_at timestamp of the objects. asc for ascending ord

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

Returns:

See Also:



187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/openai/resources/vector_stores/files.rb', line 187

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:



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/openai/resources/vector_stores/files.rb', line 254

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:



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/openai/resources/vector_stores/files.rb', line 114

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

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

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 ca

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

Returns:

See Also:



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

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:



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/openai/resources/vector_stores/files.rb', line 290

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:



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/openai/resources/vector_stores/files.rb', line 339

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