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
-
#get(file:, **params) ⇒ LLM::Response
Get a file.
-
#all(**params) ⇒ LLM::Response
List all files.
-
#create(file:, **params) ⇒ LLM::Response
Create a file.
-
#initialize(provider) ⇒ LLM::Anthropic::Files
constructor
Returns a new Files object.
-
#get_metadata(file:, **params) ⇒ LLM::Response
(also: #retrieve_metadata)
Retrieve file metadata.
-
#delete(file:) ⇒ LLM::Response
Delete a file.
-
#download(file:, **params) ⇒ LLM::Response
Download the contents of a file.
Constructor Details
#initialize(provider) ⇒ LLM::Anthropic::Files
Returns a new Files object
23 24 25 |
# File 'lib/llm/providers/anthropic/files.rb', line 23 def initialize(provider) @provider = provider end |
Instance Method Details
#get(file:, **params) ⇒ LLM::Response
Get a file
76 77 78 79 80 81 82 |
# File 'lib/llm/providers/anthropic/files.rb', line 76 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) LLM::Response.new(res).extend(LLM::Anthropic::Response::File) end |
#all(**params) ⇒ LLM::Response
List all files
39 40 41 42 43 44 |
# File 'lib/llm/providers/anthropic/files.rb', line 39 def all(**params) query = URI.encode_www_form(params) req = Net::HTTP::Get.new("/v1/files?#{query}", headers) res = execute(request: req) LLM::Response.new(res).extend(LLM::Anthropic::Response::Enumerable) end |
#create(file:, **params) ⇒ LLM::Response
Create a file
56 57 58 59 60 61 62 63 |
# File 'lib/llm/providers/anthropic/files.rb', line 56 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) LLM::Response.new(res).extend(LLM::Anthropic::Response::File) end |
#get_metadata(file:, **params) ⇒ LLM::Response Also known as: retrieve_metadata
Retrieve file metadata
95 96 97 98 99 100 101 |
# File 'lib/llm/providers/anthropic/files.rb', line 95 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) LLM::Response.new(res).extend(LLM::Anthropic::Response::File) end |
#delete(file:) ⇒ LLM::Response
Delete a file
114 115 116 117 118 119 |
# File 'lib/llm/providers/anthropic/files.rb', line 114 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
136 137 138 139 140 141 142 143 |
# File 'lib/llm/providers/anthropic/files.rb', line 136 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 |