Class: Rubyrag::Rags::CloudflareAutoRag
- Inherits:
-
Object
- Object
- Rubyrag::Rags::CloudflareAutoRag
- 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
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #add(file_path:, key: nil) ⇒ Object
-
#initialize(options) ⇒ CloudflareAutoRag
constructor
A new instance of CloudflareAutoRag.
- #query(query:) ⇒ Object
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() = .merge(DEFAULT_OPTIONS) @client = Aws::S3::Client.new( access_key_id: [:access_key_id], secret_access_key: [:secret_access_key], endpoint: [:r2_endpoint], region: "auto" ) end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
12 13 14 |
# File 'lib/rubyrag/cloudflare_auto_rag.rb', line 12 def client @client end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
12 13 14 |
# File 'lib/rubyrag/cloudflare_auto_rag.rb', line 12 def end |
Instance Method Details
#add(file_path:, key: nil) ⇒ Object
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: [:bucket], key: key || Digest::MD5.file(file_path).hexdigest, content_length: File.size(file_path), body: File.open(file_path) ) end |
#query(query:) ⇒ Object
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 |