Álvaro Valenzuela Valdes commited on
Commit ·
a50143a
1
Parent(s): 850319d
fix: Improved agent diagnostics for API troubleshooting
Browse files- backend/app/services/llm.py +16 -1
backend/app/services/llm.py
CHANGED
|
@@ -114,8 +114,19 @@ def generate_mock_analysis(tender: Tender, company: CompanyProfile) -> AnalysisR
|
|
| 114 |
)
|
| 115 |
|
| 116 |
def generate_analysis(tender: Tender, company: CompanyProfile, document_text: str | None = None) -> AnalysisResult:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
prompt = _build_analysis_prompt(tender, company, document_text)
|
| 118 |
output = call_gemini(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
parse_result = _parse_gemini_response(output)
|
| 120 |
|
| 121 |
if parse_result:
|
|
@@ -125,7 +136,11 @@ def generate_analysis(tender: Tender, company: CompanyProfile, document_text: st
|
|
| 125 |
return AnalysisResult(**parse_result)
|
| 126 |
except Exception as e:
|
| 127 |
print(f"Error mapping to AnalysisResult: {e}")
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
analysis = generate_mock_analysis(tender, company)
|
| 130 |
-
analysis.audit_log.append(
|
|
|
|
| 131 |
return analysis
|
|
|
|
| 114 |
)
|
| 115 |
|
| 116 |
def generate_analysis(tender: Tender, company: CompanyProfile, document_text: str | None = None) -> AnalysisResult:
|
| 117 |
+
if not settings.gemini_api_key:
|
| 118 |
+
analysis = generate_mock_analysis(tender, company)
|
| 119 |
+
analysis.audit_log = ["⚠️ Error: GEMINI_API_KEY is not configured.", "Fallback: Using deterministic mock analysis."]
|
| 120 |
+
return analysis
|
| 121 |
+
|
| 122 |
prompt = _build_analysis_prompt(tender, company, document_text)
|
| 123 |
output = call_gemini(prompt)
|
| 124 |
+
|
| 125 |
+
if not output:
|
| 126 |
+
analysis = generate_mock_analysis(tender, company)
|
| 127 |
+
analysis.audit_log = ["❌ Error: Gemini API returned an empty response.", "Check quota or region restrictions.", "Fallback: Using mock analysis."]
|
| 128 |
+
return analysis
|
| 129 |
+
|
| 130 |
parse_result = _parse_gemini_response(output)
|
| 131 |
|
| 132 |
if parse_result:
|
|
|
|
| 136 |
return AnalysisResult(**parse_result)
|
| 137 |
except Exception as e:
|
| 138 |
print(f"Error mapping to AnalysisResult: {e}")
|
| 139 |
+
error_msg = f"🔍 Data Mapping Error: {str(e)[:50]}..."
|
| 140 |
+
else:
|
| 141 |
+
error_msg = "🧩 Format Error: Gemini response was not valid JSON."
|
| 142 |
|
| 143 |
analysis = generate_mock_analysis(tender, company)
|
| 144 |
+
analysis.audit_log.append(error_msg)
|
| 145 |
+
analysis.audit_log.append("Fallback: Reverting to system default analysis.")
|
| 146 |
return analysis
|