diff --git a/lib/completions/endpoints/base.rb b/lib/completions/endpoints/base.rb index e86a9a8e..d9962a5b 100644 --- a/lib/completions/endpoints/base.rb +++ b/lib/completions/endpoints/base.rb @@ -69,7 +69,7 @@ module DiscourseAi prompt = dialect.translate - Net::HTTP.start( + FinalDestination::HTTP.start( model_uri.host, model_uri.port, use_ssl: true, diff --git a/lib/inference/cloudflare_workers_ai.rb b/lib/inference/cloudflare_workers_ai.rb index 42b5039a..134dc9f6 100644 --- a/lib/inference/cloudflare_workers_ai.rb +++ b/lib/inference/cloudflare_workers_ai.rb @@ -14,7 +14,8 @@ module ::DiscourseAi endpoint = "#{base_url}#{model}" - response = Faraday.post(endpoint, content.to_json, headers) + conn = Faraday.new { |f| f.adapter FinalDestination::FaradayAdapter } + response = conn.post(endpoint, content.to_json, headers) raise Net::HTTPBadResponse if ![200].include?(response.status) diff --git a/lib/inference/discourse_classifier.rb b/lib/inference/discourse_classifier.rb index 041b746c..3784a190 100644 --- a/lib/inference/discourse_classifier.rb +++ b/lib/inference/discourse_classifier.rb @@ -8,7 +8,8 @@ module ::DiscourseAi headers["X-API-KEY"] = api_key if api_key.present? - response = Faraday.post(endpoint, { model: model, content: content }.to_json, headers) + conn = Faraday.new { |f| f.adapter FinalDestination::FaradayAdapter } + response = conn.post(endpoint, { model: model, content: content }.to_json, headers) raise Net::HTTPBadResponse if ![200, 415].include?(response.status) diff --git a/lib/inference/discourse_reranker.rb b/lib/inference/discourse_reranker.rb index 76513886..35b09799 100644 --- a/lib/inference/discourse_reranker.rb +++ b/lib/inference/discourse_reranker.rb @@ -8,8 +8,9 @@ module ::DiscourseAi headers["X-API-KEY"] = api_key if api_key.present? + conn = Faraday.new { |f| f.adapter FinalDestination::FaradayAdapter } response = - Faraday.post( + conn.post( endpoint, { model: model, content: content, candidates: candidates }.to_json, headers, diff --git a/lib/inference/gemini_embeddings.rb b/lib/inference/gemini_embeddings.rb index 933b0fe3..0bd78645 100644 --- a/lib/inference/gemini_embeddings.rb +++ b/lib/inference/gemini_embeddings.rb @@ -11,7 +11,8 @@ module ::DiscourseAi body = { content: { parts: [{ text: content }] } } - response = Faraday.post(url, body.to_json, headers) + conn = Faraday.new { |f| f.adapter FinalDestination::FaradayAdapter } + response = conn.post(url, body.to_json, headers) raise Net::HTTPBadResponse if ![200].include?(response.status) diff --git a/lib/inference/hugging_face_text_embeddings.rb b/lib/inference/hugging_face_text_embeddings.rb index 09118bcb..6ec7af88 100644 --- a/lib/inference/hugging_face_text_embeddings.rb +++ b/lib/inference/hugging_face_text_embeddings.rb @@ -18,7 +18,8 @@ module ::DiscourseAi headers["X-API-KEY"] = SiteSetting.ai_hugging_face_tei_api_key end - response = Faraday.post(api_endpoint, body, headers) + conn = Faraday.new { |f| f.adapter FinalDestination::FaradayAdapter } + response = conn.post(api_endpoint, body, headers) raise Net::HTTPBadResponse if ![200].include?(response.status) diff --git a/lib/inference/open_ai_embeddings.rb b/lib/inference/open_ai_embeddings.rb index 6826a896..9ffcaa49 100644 --- a/lib/inference/open_ai_embeddings.rb +++ b/lib/inference/open_ai_embeddings.rb @@ -15,7 +15,8 @@ module ::DiscourseAi payload = { model: model, input: content } payload[:dimensions] = dimensions if dimensions.present? - response = Faraday.post(SiteSetting.ai_openai_embeddings_url, payload.to_json, headers) + conn = Faraday.new { |f| f.adapter FinalDestination::FaradayAdapter } + response = conn.post(SiteSetting.ai_openai_embeddings_url, payload.to_json, headers) case response.status when 200 diff --git a/lib/inference/open_ai_image_generator.rb b/lib/inference/open_ai_image_generator.rb index f8cc203b..d91168f4 100644 --- a/lib/inference/open_ai_image_generator.rb +++ b/lib/inference/open_ai_image_generator.rb @@ -28,7 +28,7 @@ module ::DiscourseAi response_format: "b64_json", } - Net::HTTP.start( + FinalDestination::HTTP.start( uri.host, uri.port, use_ssl: uri.scheme == "https", diff --git a/lib/inference/stability_generator.rb b/lib/inference/stability_generator.rb index cdd1a7b1..47bc6814 100644 --- a/lib/inference/stability_generator.rb +++ b/lib/inference/stability_generator.rb @@ -57,7 +57,8 @@ module ::DiscourseAi endpoint = "v1/generation/#{engine}/text-to-image" - response = Faraday.post("#{api_url}/#{endpoint}", payload.to_json, headers) + conn = Faraday.new { |f| f.adapter FinalDestination::FaradayAdapter } + response = conn.post("#{api_url}/#{endpoint}", payload.to_json, headers) if response.status != 200 Rails.logger.error( diff --git a/spec/lib/completions/endpoints/endpoint_compliance.rb b/spec/lib/completions/endpoints/endpoint_compliance.rb index 0fc3671c..d26ec91d 100644 --- a/spec/lib/completions/endpoints/endpoint_compliance.rb +++ b/spec/lib/completions/endpoints/endpoint_compliance.rb @@ -99,13 +99,13 @@ class EndpointMock def with_chunk_array_support mock = mocked_http - @original_net_http = ::Net.send(:remove_const, :HTTP) - ::Net.send(:const_set, :HTTP, mock) + @original_net_http = ::FinalDestination.send(:remove_const, :HTTP) + ::FinalDestination.send(:const_set, :HTTP, mock) yield ensure - ::Net.send(:remove_const, :HTTP) - ::Net.send(:const_set, :HTTP, @original_net_http) + ::FinalDestination.send(:remove_const, :HTTP) + ::FinalDestination.send(:const_set, :HTTP, @original_net_http) end protected @@ -113,7 +113,7 @@ class EndpointMock # Copied from https://github.com/bblimke/webmock/issues/629 # Workaround for stubbing a streamed response def mocked_http - Class.new(::Net::HTTP) do + Class.new(FinalDestination::HTTP) do def request(*) super do |response| response.instance_eval do