diff --git a/backend/src/services/LLMService.ts b/backend/src/services/LLMService.ts index 7d5e618..dfad7d2 100644 --- a/backend/src/services/LLMService.ts +++ b/backend/src/services/LLMService.ts @@ -41,10 +41,22 @@ export class LLMService { try { const result = await this.model.generateContent(prompt); const response = result.response; - const text = response.text(); + let text = response.text(); + + const newLocal = text.match(/\{.*?\}/s); + // Clean the response to ensure it is valid JSON + const jsonMatch = newLocal; + if (jsonMatch) { + text = jsonMatch[0]; + } else { + // Handle cases where no JSON is found + console.error("LLM did not return a valid JSON object. Response:", text); + throw new Error('Failed to parse LLM response as JSON.'); + } + return JSON.parse(text); } catch (error) { - console.error("Error calling Gemini API:", error); + console.error("Error calling Gemini API or parsing response:", error); throw error; } }