Class: RubyLLM::Protocols::VertexAI::Files

Inherits:
Protocols::Files
  • Object
show all
Defined in:
lib/ruby_llm/protocols/vertexai/files.rb

Overview

Google Cloud Storage-backed files for Vertex AI batch input and output.

Constant Summary collapse

GCS_URI =

GCS object names allow spaces and other characters URI() rejects.

%r{\Ags://([^/]+)/?(.*)\z}m

Instance Method Summary collapse

Instance Method Details

#download(file_id) ⇒ Object

Raises:



48
49
50
51
52
53
54
# File 'lib/ruby_llm/protocols/vertexai/files.rb', line 48

def download(file_id)
  bucket_name, key = parse_gcs_uri(file_id)
  object = bucket(bucket_name).file(key)
  raise Error, "GCS object not found: #{file_id}" unless object

  object.download.string
end

#find(file_id) ⇒ Object

Raises:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ruby_llm/protocols/vertexai/files.rb', line 32

def find(file_id)
  bucket_name, key = parse_gcs_uri(file_id)
  object = bucket(bucket_name).file(key)
  raise Error, "GCS object not found: #{file_id}" unless object

  uploaded_file(
    { 'uri' => file_id },
    id: file_id,
    uri: file_id,
    filename: File.basename(key),
    byte_size: object.size,
    created_at: object.created_at,
    mime_type: object.content_type
  )
end

#list_uris(prefix_uri) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/ruby_llm/protocols/vertexai/files.rb', line 56

def list_uris(prefix_uri)
  bucket_name, prefix = parse_gcs_uri(prefix_uri)
  uris = []
  bucket(bucket_name).files(prefix: prefix).all do |object|
    uris << "gs://#{bucket_name}/#{object.name}"
  end
  uris
end

#upload(file, filename: nil, purpose: nil, expires_in: nil, uri: nil, content_type: nil, provider_options: {}) ⇒ Object

rubocop:disable-next Lint/UnusedMethodArgument



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ruby_llm/protocols/vertexai/files.rb', line 12

def upload(file, filename: nil, purpose: nil, expires_in: nil, uri: nil, content_type: nil,
           provider_options: {})
  attachment = file_attachment(file, filename:)
  target_uri = uri || storage_uri_for(attachment)
  bucket_name, key = parse_gcs_uri(target_uri)

  with_file_body(attachment) do |body|
    bucket(bucket_name).create_file(body, key, content_type: content_type || file_content_type(attachment))
  end

  uploaded_file(
    { 'uri' => target_uri },
    id: target_uri,
    uri: target_uri,
    filename: attachment.filename,
    byte_size: file_size(attachment),
    mime_type: content_type || file_content_type(attachment)
  )
end