Class: LLM::Anthropic::Files

Inherits:
Object
  • Object
show all
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.

Examples:

#!/usr/bin/env ruby
require "llm"

llm = LLM.anthropic(key: ENV["KEY"])
bot = LLM::Bot.new(llm)
file = llm.files.create file: "/books/goodread.pdf"
bot.chat ["Tell me about this PDF", file]
bot.messages.select(&:assistant?).each { print "[#{_1.role}]", _1.content, "\n" }

Instance Method Summary collapse

Constructor Details

#initialize(provider) ⇒ LLM::Anthropic::Files

Returns a new Files object

Parameters:



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

Examples:

llm = LLM.anthropic(key: ENV["KEY"])
res = llm.files.get(file: "file-1234567890")
print "id: ", res.id, "\n"

Parameters:

  • file (#id, #to_s)

    The file ID

  • params (Hash)

    Other parameters - if any (see Anthropic docs)

Returns:

See Also:



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

Examples:

llm = LLM.anthropic(key: ENV["KEY"])
res = llm.files.all
res.each do |file|
  print "id: ", file.id, "\n"
end

Parameters:

  • params (Hash)

    Other parameters (see Anthropic docs)

Returns:

See Also:



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

Examples:

llm = LLM.anthropic(key: ENV["KEY"])
res = llm.files.create file: "/documents/haiku.txt"

Parameters:

  • file (File, LLM::File, String)

    The file

  • params (Hash)

    Other parameters (see Anthropic docs)

Returns:

See Also:



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

Examples:

llm = LLM.anthropic(key: ENV["KEY"])
res = llm.files.(file: "file-1234567890")
print "id: ", res.id, "\n"

Parameters:

  • file (#id, #to_s)

    The file ID

  • params (Hash)

    Other parameters - if any (see Anthropic docs)

Returns:

See Also:



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

Examples:

llm = LLM.anthropic(key: ENV["KEY"])
res = llm.files.delete(file: "file-1234567890")
print res.deleted, "\n"

Parameters:

  • file (#id, #to_s)

    The file ID

Returns:

See Also:



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

Examples:

llm = LLM.anthropic(key: ENV["KEY"])
res = llm.files.download(file: "file-1234567890")
File.binwrite "program.c", res.file.read
print res.file.read, "\n"

Parameters:

  • file (#id, #to_s)

    The file ID

  • params (Hash)

    Other parameters (see Anthropic docs)

Returns:

See Also:



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