Class: Rubyrag::Rags::CloudflareAutoRag

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyrag/cloudflare_auto_rag.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

DEFAULT_OPTIONS =
{
  region: "auto"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ CloudflareAutoRag



20
21
22
23
24
25
26
27
28
29
# File 'lib/rubyrag/cloudflare_auto_rag.rb', line 20

def initialize(options)
  @options = options.merge(DEFAULT_OPTIONS)

  @client = Aws::S3::Client.new(
    access_key_id: options[:access_key_id],
    secret_access_key: options[:secret_access_key],
    endpoint: options[:r2_endpoint],
    region: "auto"
  )
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



12
13
14
# File 'lib/rubyrag/cloudflare_auto_rag.rb', line 12

def client
  @client
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/rubyrag/cloudflare_auto_rag.rb', line 12

def options
  @options
end

Instance Method Details

#add(file_path:, key: nil) ⇒ Object

Raises:



31
32
33
34
35
36
37
38
39
40
# File 'lib/rubyrag/cloudflare_auto_rag.rb', line 31

def add(file_path:, key: nil)
  raise Error, "Passed file: #{file_path}" unless File.exist?(file_path)

  client.put_object(
    bucket: options[:bucket],
    key: key || Digest::MD5.file(file_path).hexdigest,
    content_length: File.size(file_path),
    body: File.open(file_path)
  )
end

#query(query:) ⇒ Object

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rubyrag/cloudflare_auto_rag.rb', line 42

def query(query:)
  raise Error, "Invalid or empty query provided" unless query.length.positive?

  uri = URI("#{options[:autorag_endpoint]}/ai-search")

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  request = Net::HTTP::Post.new(uri.path, {
                                  "Content-Type" => "application/json",
                                  "Authorization" => "Bearer #{options[:autorag_access_token]}"
                                })

  request.body = { query: query }.to_json

  response = http.request(request)

  JSON.parse(response.body)
end