From 7dc0a2a20af5f50acdacb5e797c5331b4000adc3 Mon Sep 17 00:00:00 2001 From: Rafael dos Santos Silva Date: Thu, 1 Aug 2024 17:25:24 -0300 Subject: [PATCH] DEV: Log error responses on Gemini / Cloudflare embeddings requests (#736) --- lib/inference/cloudflare_workers_ai.rb | 12 +++++++++++- lib/inference/gemini_embeddings.rb | 14 +++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/inference/cloudflare_workers_ai.rb b/lib/inference/cloudflare_workers_ai.rb index 134dc9f6..099ae5be 100644 --- a/lib/inference/cloudflare_workers_ai.rb +++ b/lib/inference/cloudflare_workers_ai.rb @@ -19,7 +19,17 @@ module ::DiscourseAi raise Net::HTTPBadResponse if ![200].include?(response.status) - JSON.parse(response.body, symbolize_names: true) + case response.status + when 200 + JSON.parse(response.body, symbolize_names: true) + when 429 + # TODO add a AdminDashboard Problem? + else + Rails.logger.warn( + "Cloudflare Workers AI Embeddings failed with status: #{response.status} body: #{response.body}", + ) + raise Net::HTTPBadResponse + end end end end diff --git a/lib/inference/gemini_embeddings.rb b/lib/inference/gemini_embeddings.rb index 0bd78645..cedda24c 100644 --- a/lib/inference/gemini_embeddings.rb +++ b/lib/inference/gemini_embeddings.rb @@ -14,9 +14,17 @@ module ::DiscourseAi conn = Faraday.new { |f| f.adapter FinalDestination::FaradayAdapter } response = conn.post(url, body.to_json, headers) - raise Net::HTTPBadResponse if ![200].include?(response.status) - - JSON.parse(response.body, symbolize_names: true) + case response.status + when 200 + JSON.parse(response.body, symbolize_names: true) + when 429 + # TODO add a AdminDashboard Problem? + else + Rails.logger.warn( + "Google Gemini Embeddings failed with status: #{response.status} body: #{response.body}", + ) + raise Net::HTTPBadResponse + end end end end