Sfoglia il codice sorgente

changes for try catch

VISHAL BHANUSHALI 3 mesi fa
parent
commit
ec5cabd84e
1 ha cambiato i file con 27 aggiunte e 17 eliminazioni
  1. 27 17
      core/services/gemini_service.py

+ 27 - 17
core/services/gemini_service.py

@@ -478,23 +478,33 @@ class GeminiAttributeService:
     )
     def _call_gemini_api(self, prompt, max_tokens=8192):
         """Helper method to call Gemini API with retry logic"""
-        return self.model.generate_content(
-            prompt,
-            generation_config=genai.types.GenerationConfig(
-                temperature=0.2,
-                top_p=0.9,
-                top_k=40,
-                max_output_tokens=max_tokens,
-                response_mime_type="application/json"
-            ),
-            safety_settings={
-                genai.types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
-                genai.types.HarmCategory.HARM_CATEGORY_HARASSMENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
-                genai.types.HarmCategory.HARM_CATEGORY_HATE_SPEECH: genai.types.HarmBlockThreshold.BLOCK_NONE,
-                genai.types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: genai.types.HarmBlockThreshold.BLOCK_NONE
-            }
-        )
-    
+        try:
+            return self.model.generate_content(
+                prompt,
+                generation_config=genai.types.GenerationConfig(
+                    temperature=0.2,
+                    top_p=0.9,
+                    top_k=40,
+                    max_output_tokens=max_tokens,
+                    response_mime_type="application/json"
+                ),
+                safety_settings={
+                    genai.types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
+                    genai.types.HarmCategory.HARM_CATEGORY_HARASSMENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
+                    genai.types.HarmCategory.HARM_CATEGORY_HATE_SPEECH: genai.types.HarmBlockThreshold.BLOCK_NONE,
+                    genai.types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: genai.types.HarmBlockThreshold.BLOCK_NONE
+                }
+            )
+        except genai.types.GenerationError as e:
+            # Handle specific generation errors
+            print("Generation error:", str(e))
+            return {"error": "Content generation failed", "details": str(e)}
+
+        except Exception as e:
+            # Catch-all for any other unexpected errors
+            print("Unexpected error:", str(e))
+            return {"error": "Unexpected error occurred", "details": str(e)}
+        
     def generate_comprehensive_suggestions(
         self,
         product: Dict,