Class: LLM::Anthropic::Files
- Inherits:
-
Object
- Object
- LLM::Anthropic::Files
- Defined in:
- lib/llm/providers/anthropic/files.rb
Overview
The LLM::Anthropic::Files class provides a files object for interacting with Anthropic's Files API.
Instance Method Summary collapse
-
#all(**params) ⇒ LLM::Response
List all files.
-
#create(file:, **params) ⇒ LLM::Response
Create a file.
-
#delete(file:) ⇒ LLM::Response
Delete a file.
-
#download(file:, **params) ⇒ LLM::Response
Download the contents of a file.
-
#get(file:, **params) ⇒ LLM::Response
Get a file.
-
#get_metadata(file:, **params) ⇒ LLM::Response
(also: #retrieve_metadata)
Retrieve file metadata.
-
#initialize(provider) ⇒ LLM::Anthropic::Files
constructor
Returns a new Files object.
Constructor Details
#initialize(provider) ⇒ LLM::Anthropic::Files
Returns a new Files object
22 23 24 |
# File 'lib/llm/providers/anthropic/files.rb', line 22 def initialize(provider) @provider = provider end |
Instance Method Details
#all(**params) ⇒ LLM::Response
List all files
38 39 40 41 42 43 |
# File 'lib/llm/providers/anthropic/files.rb', line 38 def all(**params) query = URI.encode_www_form(params) req = Net::HTTP::Get.new("/v1/files?#{query}", headers) res = execute(request: req) ResponseAdapter.adapt(res, type: :enumerable) end |
#create(file:, **params) ⇒ LLM::Response
Create a file
55 56 57 58 59 60 61 62 |
# File 'lib/llm/providers/anthropic/files.rb', line 55 def create(file:, **params) multi = LLM::Multipart.new(params.merge!(file: LLM.File(file))) req = Net::HTTP::Post.new("/v1/files", headers) req["content-type"] = multi.content_type set_body_stream(req, multi.body) res = execute(request: req) ResponseAdapter.adapt(res, type: :file) end |
#delete(file:) ⇒ LLM::Response
Delete a file
113 114 115 116 117 118 |
# File 'lib/llm/providers/anthropic/files.rb', line 113 def delete(file:) file_id = file.respond_to?(:id) ? file.id : file req = Net::HTTP::Delete.new("/v1/files/#{file_id}", headers) res = execute(request: req) LLM::Response.new(res) end |
#download(file:, **params) ⇒ LLM::Response
Note:
You can only download files that were created by the code execution tool. Files that you uploaded cannot be downloaded.
Download the contents of a file
135 136 137 138 139 140 141 142 |
# File 'lib/llm/providers/anthropic/files.rb', line 135 def download(file:, **params) query = URI.encode_www_form(params) file_id = file.respond_to?(:id) ? file.id : file req = Net::HTTP::Get.new("/v1/files/#{file_id}/content?#{query}", headers) io = StringIO.new("".b) res = execute(request: req) { |res| res.read_body { |chunk| io << chunk } } LLM::Response.new(res).tap { _1.define_singleton_method(:file) { io } } end |
#get(file:, **params) ⇒ LLM::Response
Get a file
75 76 77 78 79 80 81 |
# File 'lib/llm/providers/anthropic/files.rb', line 75 def get(file:, **params) file_id = file.respond_to?(:id) ? file.id : file query = URI.encode_www_form(params) req = Net::HTTP::Get.new("/v1/files/#{file_id}?#{query}", headers) res = execute(request: req) ResponseAdapter.adapt(res, type: :file) end |
#get_metadata(file:, **params) ⇒ LLM::Response Also known as: retrieve_metadata
Retrieve file metadata
94 95 96 97 98 99 100 |
# File 'lib/llm/providers/anthropic/files.rb', line 94 def (file:, **params) query = URI.encode_www_form(params) file_id = file.respond_to?(:id) ? file.id : file req = Net::HTTP::Get.new("/v1/files/#{file_id}?#{query}", headers) res = execute(request: req) ResponseAdapter.adapt(res, type: :file) end |