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
22 23 24 |
# File 'lib/llm/providers/anthropic/files.rb', line 22 def initialize(provider) @provider = provider end |
Instance Method Details
#get(file:, **params) ⇒ LLM::Response
Get a file
77 78 79 80 81 82 83 84 |
# File 'lib/llm/providers/anthropic/files.rb', line 77 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, span = execute(request: req, operation: "request") res = ResponseAdapter.adapt(res, type: :file) finish_trace(operation: "request", res:, span:) end |
#all(**params) ⇒ LLM::Response
List all files
38 39 40 41 42 43 44 |
# 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, span = execute(request: req, operation: "request") res = ResponseAdapter.adapt(res, type: :enumerable) finish_trace(operation: "request", res:, span:) end |
#create(file:, **params) ⇒ LLM::Response
Create a file
56 57 58 59 60 61 62 63 64 |
# 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, span = execute(request: req, operation: "request") res = ResponseAdapter.adapt(res, type: :file) finish_trace(operation: "request", res:, span:) end |
#get_metadata(file:, **params) ⇒ LLM::Response Also known as: retrieve_metadata
Retrieve file metadata
97 98 99 100 101 102 103 104 |
# File 'lib/llm/providers/anthropic/files.rb', line 97 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, span = execute(request: req, operation: "request") res = ResponseAdapter.adapt(res, type: :file) finish_trace(operation: "request", res:, span:) end |
#delete(file:) ⇒ LLM::Response
Delete a file
117 118 119 120 121 122 123 |
# File 'lib/llm/providers/anthropic/files.rb', line 117 def delete(file:) file_id = file.respond_to?(:id) ? file.id : file req = Net::HTTP::Delete.new("/v1/files/#{file_id}", headers) res, span = execute(request: req, operation: "request") res = LLM::Response.new(res) finish_trace(operation: "request", res:, span:) 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
140 141 142 143 144 145 146 147 148 |
# File 'lib/llm/providers/anthropic/files.rb', line 140 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, span = execute(request: req, operation: "request") { |res| res.read_body { |chunk| io << chunk } } res = LLM::Response.new(res).tap { _1.define_singleton_method(:file) { io } } finish_trace(operation: "request", res:, span:) end |