From 989674286887f2b8722e27826adee2eb40ea4db8 Mon Sep 17 00:00:00 2001 From: aodulov Date: Fri, 10 Oct 2025 15:50:27 +0300 Subject: [PATCH] Works! MVP --- backend/src/services/LLMService.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) 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; } }