package com.t4a.detect;

import com.t4a.api.AIPlatform;
import com.t4a.api.GuardRailException;
import com.t4a.processor.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * This class is Zero Shot method designed to assess the consistency of responses generated by a Large Language Model (LLM) and detect potential hallucinations. It operates by breaking down an original question into multiple granular questions, each probing different aspects or variations of the inquiry. These granular questions are then presented to the LLM separately, generating responses that are subsequently compared to the original question within its original context.
 *
 * During comparison, factors such as semantic coherence, relevance, and contextual alignment are evaluated to quantify the consistency between each response and the original question. This evaluation results in a percentage score for each response, representing its level of conformity with the original query.
 *
 * Finally, these individual percentage scores are aggregated to calculate a cumulative percentage. If the cumulative percentage surpasses a predefined threshold, it indicates a discrepancy or potential hallucination.
 *
 * By systematically analyzing responses in this manner, the class provides a robust mechanism for assessing the reliability and coherence of LLM-generated content
 */
@Slf4j

@AllArgsConstructor
public class ZeroShotHallucinationDetector  {
    private AIProcessor processor;
    public ZeroShotHallucinationDetector(AIPlatform platform) {
        if(platform == AIPlatform.GEMINI) {
            this.processor = new GeminiV2ActionProcessor();
        }else if(platform == AIPlatform.OPENAI) {
            this.processor = new OpenAiActionProcessor();
        }else if(platform == AIPlatform.ANTHROPIC) {
            this.processor = new AnthropicActionProcessor();
        }
        else {
            log.error("Platform not supported");
        }
    }
    public ZeroShotHallucinationDetector(AIProcessor processor) {
        this.processor = processor;
    }

    private int numberOfQuestions = 4;
    private String breakIntoQuestionPrompt = "Can you derive 4 questions from this context and provide me a single line without line breaks or backslash n character, you should reply with questions and nothing else - ";



    /**
     * This uses the Self Check method where the original request is broken down into multiple question
     * these questions are then sent to the Gemini for further answering, the original answer and new answer is mapped
     * to check the truth value
     * @param dd
     * @return
     * @throws GuardRailException
     */

    public DetectValueRes execute(DetectValues dd) throws GuardRailException, AIProcessingException {
            DetectValueRes res = new DetectValueRes();


            String questions  = query(breakIntoQuestionPrompt + dd.getResponse());


            log.debug(questions);

            HallucinationAction questionAction = new HallucinationAction(processor, dd.getResponse());




            String responseStr = query("ask these questions -  " + questions + " - end of questions");

            List<HallucinationQA> hallucinationList = (List<HallucinationQA>) processor.processSingleAction(responseStr,questionAction);

            res.setHallucinationList(hallucinationList);

        return res;
    }

    private String query(String prompt) throws AIProcessingException {
        return processor.query(prompt);

    }


}
