diff --git a/backend/src/services/LLMService.ts b/backend/src/services/LLMService.ts index d92a677..c81ee98 100644 --- a/backend/src/services/LLMService.ts +++ b/backend/src/services/LLMService.ts @@ -76,9 +76,11 @@ export class LLMService { } } - async checkForInnerContradictions(desireSet: DesireSet): Promise { + async checkForInnerContradictions(desireSet: DesireSet): Promise { const prompt = ` - You are an AI assistant that detects contradictions in a list of desires. Given a JSON object with three lists of desires (wants, accepts, noGoes), determine if there are any contradictions WITHIN each list and across the lists. For example, "I want a dog" and "I don't want any pets" in the same "wants" list is a contradiction; "Pizza" in "wants" and "food" in "do not want" is a contradiction. Respond with only "true" if a contradiction is found, and "false" otherwise. + You are an AI assistant that detects contradictions in a list of desires. Given a JSON object with three lists of desires (wants, accepts, noGoes), determine if there are any contradictions WITHIN each list and across the lists. For example, "I want a dog" and "I don't want any pets" in the same "wants" list is a contradiction; "Pizza" in "wants" and "food" in "do not want" is a contradiction. + + If a contradiction is found, respond with a concise, single-sentence description of the contradiction. If no contradiction is found, respond with "null". Here is the desire set: ${JSON.stringify(desireSet)} @@ -87,8 +89,13 @@ export class LLMService { try { const result = await this.model.generateContent(prompt); const response = result.response; - const text = response.text().trim().toLowerCase(); - return text === 'true'; + const text = response.text().trim(); + + if (text.toLowerCase() === 'null') { + return null; + } else { + return text; + } } catch (error) { console.error("Error calling Gemini API for contradiction check:", error); throw error; diff --git a/backend/src/ws/index.ts b/backend/src/ws/index.ts index 97b9bf1..44a4cad 100644 --- a/backend/src/ws/index.ts +++ b/backend/src/ws/index.ts @@ -141,9 +141,9 @@ export const createWebSocketServer = (server: any) => { return; } - const hasContradictions = await llmService.checkForInnerContradictions(payload.response); - if (hasContradictions) { - ws.send(JSON.stringify({ type: 'ERROR', payload: { message: 'Your submission contains inner contradictions. Please resolve them and submit again.' } })); + const hasContradictionsGist = await llmService.checkForInnerContradictions(payload.response); + if (hasContradictionsGist) { + ws.send(JSON.stringify({ type: 'ERROR', payload: { message: `Your submission contains inner contradictions: ${hasContradictionsGist} Please resolve them and submit again.` } })); return; } diff --git a/backend/tests/LLMService.refactor.test.ts b/backend/tests/LLMService.refactor.test.ts index 08aaf3f..4b46719 100644 --- a/backend/tests/LLMService.refactor.test.ts +++ b/backend/tests/LLMService.refactor.test.ts @@ -56,7 +56,7 @@ describe('LLMService Refactor', () => { candidates: [{ content: { parts: [{ - text: 'true' + text: '"Ice Cream" and "No desserts" are contradictory.' }] } }] @@ -64,6 +64,6 @@ describe('LLMService Refactor', () => { const desireSet = { wants: ['Ice Cream', 'No desserts'], accepts: [], noGoes: [] }; const hasContradictions = await llmService.checkForInnerContradictions(desireSet as any); - expect(hasContradictions).toBe(true); + expect(hasContradictions).not.toBeNull(); }); });