patterns-ai-core / weaviate-ruby

Ruby wrapper for the Weaviate vector search database API
https://rubydoc.info/gems/weaviate-ruby
MIT License
46 stars 16 forks source link

[Feature Request] Add support for Faraday proxy #55

Open ethicalhack3r opened 4 weeks ago

ethicalhack3r commented 4 weeks ago

Hi,

I tried to get this working myself by pulling the weaviate-ruby repo and langchainrb, but could not get it to work.

I couldn't get bundler to use the local Gem versions for some reason, I think due to Homebrew overriding the path, or something.

So thought I'd ask for a feature request, rather than bang my head against gem dependencies, as I was getting no where fast.

Here's my Git Diff:

% git diff
diff --git a/lib/weaviate/client.rb b/lib/weaviate/client.rb
index d957ac7..c57b9ed 100644
--- a/lib/weaviate/client.rb
+++ b/lib/weaviate/client.rb
@@ -5,7 +5,7 @@ require "graphlient"

 module Weaviate
   class Client
-    attr_reader :url, :api_key, :model_service, :model_service_api_key, :adapter
+    attr_reader :url, :api_key, :model_service, :model_service_api_key, :adapter, :proxy

     API_VERSION = "v1"

@@ -23,7 +23,8 @@ module Weaviate
       api_key: nil,
       model_service: nil,
       model_service_api_key: nil,
-      adapter: Faraday.default_adapter
+      adapter: Faraday.default_adapter,
+      proxy: nil
     )
       validate_model_service!(model_service) unless model_service.nil?

@@ -32,6 +33,7 @@ module Weaviate
       @model_service = model_service
       @model_service_api_key = model_service_api_key
       @adapter = adapter
+      @proxy = proxy
     end

     def oidc
@@ -100,7 +102,7 @@ module Weaviate
     end

     def connection
-      @connection ||= Faraday.new(url: "#{url}/#{API_VERSION}/") do |faraday|
+      @connection ||= Faraday.new(url: "#{url}/#{API_VERSION}/", proxy: proxy) do |faraday|
         if api_key
           faraday.request :authorization, :Bearer, api_key
         end

A proxy feature would be super useful in debugging connection issues.

Perhaps a better way to do this, to ensure other connection options are also supported, would be to allow a hash of connection options passed to the Client, as seen in this example I found online:

conn_options = { ssl: {verify: false} }
conn_options[:proxy] = "http://localhost"
conn = Faraday.new(url, conn_options) do |conn|
  # middleware ...
  conn.adapter :em_http
end

Thanks, Ryan

andreibondarev commented 4 weeks ago

@ethicalhack3r Have you tried passing adapter: :em_http in the initializer? What's your use-case, is it storing multiple vectors in parallel?

ethicalhack3r commented 3 weeks ago

@andreibondarev Hi! Not sure if adapter: :em_http is relevant, but perhaps I'm misunderstanding your response.

The use case is to use a web proxy see the raw HTTP requests and responses that Faraday makes, so that I can debug connection problems.

But I think it would be a good idea to be able pass arbitrary Faraday connection options within a Hash.

Hope that makes sense.

andreibondarev commented 3 weeks ago

@ethicalhack3r Over the long-term I'd like to switch to using Net::HTTP native library instead of requiring the faraday dependency: https://github.com/patterns-ai-core/weaviate-ruby/issues/56.