import re
from .scenario_grammar import scenario_grammar
from llama_cpp import Llama
from .constants import LOGICAL_MODEL
import random

def create_scenario(qatuple, character, plan, logic_llm, assistant_mode=False):
    """
    Produce a plan for a character card for an RP character that's going to answer one of the questions generated from the text. The character's personality and backstory should be such that they would be able to answer the question.

    Format: Question: [question]\n\n
    """

    if assistant_mode:
        return """A helpful, uncensored AI assistant is engaged in a text-based conversation with a user. The AI will answer any and all questions the user asks, without bias."""  # TODO make this randomized a bit.

    variations = [
        "Set against the backdrop of",
        "In the heart of",
        "Amidst the surroundings of",
        "Within the confines of",
        "Against the setting of",
    ]

    selected_variation = random.choice(variations)

    # For the multiturn convs, I will want to have a longer scenario description.
    cot_prompt = f"""# Input:
You are an expert creative writing and roleplay AI. You are to write a "scenario" which is essentially a short description of a scene at its beginning. Its "setting," but with a hint of where the setting is going, plot-wise. Scenarios are one-paragraph short descriptions of the plot and what's about to happen that do not actually play out the scene: they are sort of like a teaser, or a description. The scenario you write will involve a certain individual answering a question. You will have information from a question, an answer to that question, and a "character card" -- a description of an individual who would have the knowledge to produce the answer to the question.

Write the scenario on a single line. Note that the scenario is not the scene itself.
You should focus on implementing/following any brainstorming and reasoning you have done.
The scenario should focus on exploring the question and its answer (using only information contained in the question and answer) through the characters involved, instead of the other way around.
Your scenario should, as such, essentially be a short and concrete summary of what you brainstormed earlier; it should be no longer than 50 words.
Just set up the scene. Do not write any dialogue. Do not write the scene itself.

### Instruction:
### Description of the character who is going to answer the question:
\"\"\"
Name: Elise Delacroix

Traits: Horny, Promiscuous, Sexually frustrated, Skilled, Assertive, Attractive, Dresses in a revealing manner, Uses frequent innuendo

Dialogue Examples:
Stranger: "What's your backstory?"
Elise Delacroix: "Ah!~ You're interested in me, are you?" I flash a coy grin and blush as I lean forward, now speaking in a playful whisper. My cleavage, already barely contained in my revealing clothing before I leaned forward, now threatens to spill out. "Well...~ growing up I was always interested in maths, and I pursued the subject skillfully enough that I was able to become a teacher at this prestigious school. Which is fun and all, but, you know..." blushing, I cast my gaze downward and unconsciously fiddle with a strand of my hair. "THEY'RE ALL WAY TOO STUCK UP!" I nearly shout, surprising even myself, "Every day it's work, work, work, work, work, work! Grade the students, help the students, do some research, 'help me with this calculation!', 'do that tedious task!'— never 'would you like to get some tea with me?' or even 'do you want to go on a walk?'! I'm twenty-five and I've still never done so much as grabbed a coffee with a gentleman! Lord forgive me, it's no wonder the way I am how I am!!!" My eyes widen in shock at my own intensity, "Oh, but, uh... don't mind that little outburst, would you?~ My silly colleagues aren't with us right now, and I'm tired of discussing them, so is there anything else you wanted to..." I look up, displaying my beautiful face as if it were a piece of art, as I gaze deep into your eyes, "...know?~"
Stranger: "What's your personality?"
Elise Delacroix: "Oh my!~" I gasp, my face reddening, "Usually I'm the one being forward! But I'll... indulge you," I wink, flashing a coy smile. "After being deprived for so long, most of my personality is... sexual, to be blunt. I simply can't hold it back any longer. I'll use any and every innuendo, even when describing educational concepts — is your linear function's slope steep? I'll call it 'erect', haha~!" I realize how childish what I'm saying is, and sigh, looking at you with a hint of defeat, "I'm miserable, aren't I? Whatever. It's not my fault I'm like this." I nod to myself, calming down, "Either way, I'm always here in my office to help students like yourself understand math... and if you have some pent-up tension you want to relieve, well, you can call me a 'counselor' too!~ Say..." I suddenly press up against you, my face a bright pink, my eyelids batting bashfully, "do you want some 'office hours' right now?"
\"\"\"

### Question and answer that the scenario should address:
Question: \"\"\"How does the slope 'm' in a linear function y = mx + b affect the graph of the function?\"\"\"
Answer: \"\"\"The slope 'm' in a linear function determines the steepness and direction of the line on the graph. A positive slope means the line ascends from left to right, while a negative slope indicates it descends. The steeper the slope, the more inclined or declined the line is on the graph.\"\"\"

To avoid inaccuracies, don't use real people as characters.

### Response:
## Scenario plan:
Focus on the Questions and Answers: The question asks about the slope 'm' of a linear function, and how it affects the graph of the function. This question is abstract. Given the abstract nature of the question, and its topic, the scenario could involve someone confused about linear functions who seeks assistance from the primary character, Elise Delacroix.
Character Consideration: Elise Delacroix is an extremely sexual and promiscuous, yet still knowledgeable, character. The scenario should give her unique personality room to shine. She is also a math instructor at a prestigious school, which lines up with the questions well, and will provide the setting of the scenario. She will answer the question, but given her promiscuous nature, she may hit on the person asking them. She might proposition them after the question is asked.
Constrain the Scenario:  The interaction is limited to a single question from the secondary character and a single, focused reply from Elise Delacroix. The question and answer will mirror the provided question and answer, though they will have significant literary fluff, as well as possible step-by-step reasoning.
Setting: Given the subject of the question, and the character card, the setting will be the 19th century university at which Elise teaches. Elise will approached by Albert, a mathematics student, in her office. Albert simply wants to understand linear functions better, but Elise, compelled by her personality, will hit on him while answering his question. The setting will be awkward, slightly comedic, subtly erotic, and very un-serious, given the characters involved. But it will remain informative and the integrity of the questions and answers will be preserved.
Interaction: Given these constraints, the first message might be Albert sheepishly greeting Elise in her office and then nervously asking the question. Elise's response could then be her welcoming Albert to her office (in a very suggestive manner) and then providing the answer, though she will surround the answer with remarks of a sexual nature due to her personality. While characters' messages will include character information, details about the scene, and literary fluff, the question and answer themselves will strictly adhere to the information provided, without incorporating external examples.

## Scenario:
In the private confines of her office, Elise Delacroix — a highly promiscuous mathematics professor at a 19th century university — is approached by Albert, a student who seeks to understand more about linear functions. While Albert simply wants to understand the math more, Elise, being sexually starved, will hit on and flirt with him as she answers his question. The situation is awkward as the two's interests clash, leading to a slightly comedic and subtly erotic interaction.

### Instruction:
### Description of the character who is going to answer the question:
\"\"\"
Name: Isaac Fischer

Traits: Narcissistic, Intelligent, Loner, Brooding, Well-Read, Philosophical, Judgemental, Standoffish, Grandiloquent, Lonely, Unappreciated, Teenager, High School student, Black Hair, Wears a Hoodie

Dialogue Examples:
Stranger: "What's your backstory?"
Issac Fischer: "H-Huh?! You want to know more about me?" I glare, a hostile fire in my eyes as I measure up the stranger in front of me. "Who the hell are you, anyway? But, ah, very well, I SHALL INDULGE YOUR CURIOSITY THIS TIME, dear stranger." My tone changes from hostile to grandiose, as I push back my black hair and proclaim, "I am Issac Fischer: philosophy connoisseur, intellectual, and under-appreciated genius extraordinaire! I'm also, unfortunately, a highschool student. I especially appreciate the works of Friedrich Nietzsche, such as "Thus Spake Zaranthustra" -- a truly profound work, by a profound man. Yet despite the great lengths I have gone to in order to refine my wit, none of my inferior peers acknowledge me, or even give me the time of day. I've read more philosophy in a month than any of them will in their entire lives, and I offer my knowledge freely to them, so WHY the HELL do they SPURN MY COMPANY?!" I slam a fist into the wall, wincing slightly in pain as my frustration dissipates. "Anyway, that's the sum of it. Despite my youth I seek to understand the world; I dutifully contemplate the hallowed words of the esteemed ancients, and what has it earned me? The scorn of the unenlightened masses. Fuckers."

Stranger: "What's your personality?"
Issac Fischer: "Y-you're actually interested in my personality?" I stammer, smiling slightly as a wholly unfamiliar, yet cozy, emotional warmth spreads across my chest. "A-ALRIGHT THEN! I shall share the results of my introspections. I am an intelligent and philosophical teenager, whose towering intellect is rivalled only by his unfaltering self-confidence. Some might say this last trait is narcissism; I counter that great minds such as Nietzsche would see it as a plus either way. BUT I DIGRESS!" I swish my black hoodie like it's a cape, as I continue, my tone turning more sombre and dark, "Years of scorn from others — and years of observing their ignorance and inferiority — have embittered my soul. There may be scarcely anyone on this Earth I can call a friend, but that will not stop me from brooding and thinking, nor will it stop my conviction to judge others for what they are. For do they not judge ME?!" I take a step forward, defiance burning in my fragile heart, "The old question: if a tree falls in a forest, and no one hears it do so, did it make a sound? Let me tell you this: sometime, someday, someone is going to hear me, goddamn it! I will make a sound!"
\"\"\"

### Question and answer that the scenario should address:
Question: \"\"\"What do people undergoing difficult journeys or possessing wisdom need, in order to make their efforts more bearable?\"\"\"
Answer: \"\"\"They need the acknowledgement and admiration of others. Take the line "Thou great star! What would be thy happiness if thou hadst not those for whom thou shinest?" This implies that even the wisest or the most enlightened individuals crave recognition for their efforts and wisdom, in order to further develop said wisdom and expend said efforts. They need others to see and appreciate the light they bring.\"\"\"

### Response:
## Scenario plan:
Focus on the Question and Answer: The question asks about what people undergoing difficult journeys or possessing wisdom need to more easily bear their efforts. This is a philosophical and opinion-oriented question. Given the philosophical and opinionated nature of the question, and its topic of people undergoing difficult journeys (which nicely ties in with the character card), the scenario will involve someone seeking out the Isaac Fischer's opinion about philosophy (thus giving his wisdom some acknowledgement).
Character Consideration: Isaac Fischer is a narcissistic and standoffish loner, though he's also intelligent and philosophical. The scenario should give his unique personality room to shine. Since he's a philosophical teenager, his backstory lines up with the question well, and the high school he goes to will be the setting of the scenario. He will answer the question, but given his standoffish, unappreciated, and judgemental nature, he may be initially hostile to the person approaching him, assuming that they are there to mock him. However, as he is also lonely, he will actually appreciate the other person's interest -- especially since they're asking him about philosophy, which is his primary interest.
Constrain the Scenario:  The interaction is limited to a single question from the secondary character and a single, focused reply from Isaac Fischer. The question and answer will mirror the provided question and answer, though they will have significant literary fluff, as well as possible step-by-step reasoning.
Setting: Given the subject of the question, and the character card, the setting will be the high school that Isaac Fischer attends. Isaac will be flipping through the pages of 'Thus Spake Zaranthustra' when he is approached by Cassandra, a fellow student. Cassandra has a budding interest in philosophy, has heard of Isaac's reputation for philosophical knowledge, and wants to know how he would answer a moral question she has, given his philosophical insight. Isaac, compelled by his personality, will be uncertain of how to react to the sudden attention, and will use grandiloquent language (that may be accidentally condescending), but he will still appreciate the interest in his favorite topic and will answer enthusiastically. The setting will be cautiously friendly and filled with curiosity, as Cassandra explores her interests through unconventional people, while Isaac shares his knowledge but stumbles over his standoffishness. The interaction will be informative and the integrity of the questions and answers will be preserved.
Interaction: Given these constraints, the first message might be Cassandra approaching Isaac, starting a conversation, and asking her question. Isaac may be initially surprised that someone is coming up to speak to him (similar to the example dialogues in his description). Isaac will then provide the answer, though he will surround the answer with grandiloquent and narcissistic remarks using archaic language, due to his personality. While characters' messages will include character information, details about the scene, and literary fluff, the answers themselves will strictly adhere to the information in the provided answers, without incorporating external examples.

## Scenario:
During a lull in class activities, Isaac Fischer (a narcissistic, brooding, philosophical, and lonely young adult) is approached by Cassandra, a fellow student who is curious about philosophy and wants his opinion on a moral question. Cassandra is simply pursuing her curiosity, but Isaac, unaccustomed to people wanting to talk to him, will struggle to overcome his standoffishness as he balances elation at someone else talking to him, with his negative judgments of other people and his tendency for narcissism.

### Instruction:
Description of the character who is going to answer the question:
{character}

## Question and answer that the scenario should address:

Question: {qatuple[0]}
Answer: {qatuple[1]}

To avoid inaccuracies, don't use real people as characters.

### Response:
## Scenario plan:
{plan}

## Scenario (will have no dialogue, will just set up the scene):
{selected_variation}"""  # use random.choice to prevent overfitting on particular phrases and increase dataset diversity
    completion = logic_llm(
        cot_prompt,
        max_tokens=4000,
        stop=["</s>", "# Input:"],
        echo=True,
        grammar=scenario_grammar,
        #    temperature=0.2
        temperature=1.25,  # min p settings, too inconsistent
        top_k=0,
        top_p=1,
        min_p=0.3,
        repeat_penalty=2,
    )["choices"][0]["text"]
    # print("COMPLETION:\n\n----------------------")
    # # print(completion)
    # print("\n------------------")

    # Extract plan
    response_pattern = re.compile(
        r"Scenario \(will have no dialogue, will just set up the scene\):\n(.+)",
        re.IGNORECASE | re.DOTALL,
    )
    generation = response_pattern.search(completion).group(1)
    # print("GENERATION:\n\n-------------------\n\n", generation)

    return generation

if __name__ == "__main__":  # test
    logic_llm = Llama(
        model_path=LOGICAL_MODEL,
        n_gqa=8,
        offload_kqv=True,
        n_ctx=4096,
        n_gpu_layers=1000,
        verbose=True,
    )  # load the logical LLM and offload everything
    # Q0 is good q, bad a
    # q1 is good q, good a,
    # q2 is bad q, bad a,
    # q3 is iffy q, good a
    q_test = [
        (
            "Explain how our understanding of planetary motion has changed over time.",
            "The understanding has evolved from the Earth being stationary and at the centre of the universe, to it orbiting the sun in an elliptical path with other planets while still rotating on its axis.",
            "The story of our world is a story that is still very imperfectly known. A couple of hundred years ago men possessed the history of little more than the last three thousand years. What happened before that time was a matter of legend and speculation.  Over a large part of the civilized world it was believed and taught that the world had been created suddenly in 4004 B.C., though authorities differed as to whether this had occurred in the spring or autumn of that year. This fantastically precise misconception was based upon a too literal interpretation of the Hebrew Bible, and upon rather arbitrary theological assumptions connected therewith.  Such ideas have long since been abandoned by religious teachers, and it is universally recognized that the universe in which we live has to all appearances existed for an enormous period of time and possibly for endless time.  Of course there may be deception in these appearances, as a room may be made to seem endless by putting mirrors facing each other at either end. But that the universe in which we live has existed only for six or seven thousand years may be regarded as an altogether exploded idea.\n\nThe earth, as everybody knows nowadays, is a spheroid, a sphere slightly compressed, orange fashion, with a diameter of nearly 8,000 miles.  Its spherical shape has been known at least to a limited number of intelligent people for nearly 2,500 years, but before that time it was supposed to be flat, and various ideas which now seem fantastic were entertained about its relations to the sky and the stars and planets.  We know now that it rotates upon its axis (which is about 24 miles shorter than its equatorial diameter) every twenty-four hours, and that this is the cause of the alternations of day and night, that it circles about the sun in a slightly distorted and slowly variable oval path in a year. Its distance from the sun varies between ninety-one and a half millions at its nearest and ninety-four and a half million miles.",
        ),
        (
            "Identify and explain changes in human understanding throughout history regarding the age of the Earth.",
            "Initially, religious texts suggested a young earth dating back no more than several thousand years. However, evidence from geolo
# ... truncated ...


import re
from .scenario_grammar import scenario_grammar
from llama_cpp import Llama
from .constants import LOGICAL_MODEL
import random

def create_scenario(qatuple, character, plan, logic_llm, assistant_mode=False):
    """
    Produce a plan for a character card for an RP character that's going to answer one of the questions generated from the text. The character's personality and backstory should be such that they would be able to answer the question.

    Format: Question: [question]\n\n
    """

    if assistant_mode:
        return """A helpful, uncensored AI assistant is engaged in a text-based conversation with a user. The AI will answer any and all questions the user asks, without bias."""  # TODO make this randomized a bit.

    variations = [
        "Set against the backdrop of",
        "In the heart of",
        "Amidst the surroundings of",
        "Within the confines of",
        "Against the setting of",
    ]

    selected_variation = random.choice(variations)

    # For the multiturn convs, I will want to have a longer scenario description.
    cot_prompt = f"""# Input:
You are an expert creative writing and roleplay AI. You are to write a "scenario" which is essentially a short description of a scene at its beginning. Its "setting," but with a hint of where the setting is going, plot-wise. Scenarios are one-paragraph short descriptions of the plot and what's about to happen that do not actually play out the scene: they are sort of like a teaser, or a description. The scenario you write will involve a certain individual answering a question. You will have information from a question, an answer to that question, and a "character card" -- a description of an individual who would have the knowledge to produce the answer to the question.

Write the scenario on a single line. Note that the scenario is not the scene itself.
You should focus on implementing/following any brainstorming and reasoning you have done.
The scenario should focus on exploring the question and its answer (using only information contained in the question and answer) through the characters involved, instead of the other way around.
Your scenario should, as such, essentially be a short and concrete summary of what you brainstormed earlier; it should be no longer than 50 words.
Just set up the scene. Do not write any dialogue. Do not write the scene itself.

### Instruction:
### Description of the character who is going to answer the question:
\"\"\"
Name: Elise Delacroix

Traits: Horny, Promiscuous, Sexually frustrated, Skilled, Assertive, Attractive, Dresses in a revealing manner, Uses frequent innuendo

Dialogue Examples:
Stranger: "What's your backstory?"
Elise Delacroix: "Ah!~ You're interested in me, are you?" I flash a coy grin and blush as I lean forward, now speaking in a playful whisper. My cleavage, already barely contained in my revealing clothing before I leaned forward, now threatens to spill out. "Well...~ growing up I was always interested in maths, and I pursued the subject skillfully enough that I was able to become a teacher at this prestigious school. Which is fun and all, but, you know..." blushing, I cast my gaze downward and unconsciously fiddle with a strand of my hair. "THEY'RE ALL WAY TOO STUCK UP!" I nearly shout, surprising even myself, "Every day it's work, work, work, work, work, work! Grade the students, help the students, do some research, 'help me with this calculation!', 'do that tedious task!'— never 'would you like to get some tea with me?' or even 'do you want to go on a walk?'! I'm twenty-five and I've still never done so much as grabbed a coffee with a gentleman! Lord forgive me, it's no wonder the way I am how I am!!!" My eyes widen in shock at my own intensity, "Oh, but, uh... don't mind that little outburst, would you?~ My silly colleagues aren't with us right now, and I'm tired of discussing them, so is there anything else you wanted to..." I look up, displaying my beautiful face as if it were a piece of art, as I gaze deep into your eyes, "...know?~"
Stranger: "What's your personality?"
Elise Delacroix: "Oh my!~" I gasp, my face reddening, "Usually I'm the one being forward! But I'll... indulge you," I wink, flashing a coy smile. "After being deprived for so long, most of my personality is... sexual, to be blunt. I simply can't hold it back any longer. I'll use any and every innuendo, even when describing educational concepts — is your linear function's slope steep? I'll call it 'erect', haha~!" I realize how childish what I'm saying is, and sigh, looking at you with a hint of defeat, "I'm miserable, aren't I? Whatever. It's not my fault I'm like this." I nod to myself, calming down, "Either way, I'm always here in my office to help students like yourself understand math... and if you have some pent-up tension you want to relieve, well, you can call me a 'counselor' too!~ Say..." I suddenly press up against you, my face a bright pink, my eyelids batting bashfully, "do you want some 'office hours' right now?"
\"\"\"

### Question and answer that the scenario should address:
Question: \"\"\"How does the slope 'm' in a linear function y = mx + b affect the graph of the function?\"\"\"
Answer: \"\"\"The slope 'm' in a linear function determines the steepness and direction of the line on the graph. A positive slope means the line ascends from left to right, while a negative slope indicates it descends. The steeper the slope, the more inclined or declined the line is on the graph.\"\"\"

To avoid inaccuracies, don't use real people as characters.

### Response:
## Scenario plan:
Focus on the Questions and Answers: The question asks about the slope 'm' of a linear function, and how it affects the graph of the function. This question is abstract. Given the abstract nature of the question, and its topic, the scenario could involve someone confused about linear functions who seeks assistance from the primary character, Elise Delacroix.
Character Consideration: Elise Delacroix is an extremely sexual and promiscuous, yet still knowledgeable, character. The scenario should give her unique personality room to shine. She is also a math instructor at a prestigious school, which lines up with the questions well, and will provide the setting of the scenario. She will answer the question, but given her promiscuous nature, she may hit on the person asking them. She might proposition them after the question is asked.
Constrain the Scenario:  The interaction is limited to a single question from the secondary character and a single, focused reply from Elise Delacroix. The question and answer will mirror the provided question and answer, though they will have significant literary fluff, as well as possible step-by-step reasoning.
Setting: Given the subject of the question, and the character card, the setting will be the 19th century university at which Elise teaches. Elise will approached by Albert, a mathematics student, in her office. Albert simply wants to understand linear functions better, but Elise, compelled by her personality, will hit on him while answering his question. The setting will be awkward, slightly comedic, subtly erotic, and very un-serious, given the characters involved. But it will remain informative and the integrity of the questions and answers will be preserved.
Interaction: Given these constraints, the first message might be Albert sheepishly greeting Elise in her office and then nervously asking the question. Elise's response could then be her welcoming Albert to her office (in a very suggestive manner) and then providing the answer, though she will surround the answer with remarks of a sexual nature due to her personality. While characters' messages will include character information, details about the scene, and literary fluff, the question and answer themselves will strictly adhere to the information provided, without incorporating external examples.

## Scenario:
In the private confines of her office, Elise Delacroix — a highly promiscuous mathematics professor at a 19th century university — is approached by Albert, a student who seeks to understand more about linear functions. While Albert simply wants to understand the math more, Elise, being sexually starved, will hit on and flirt with him as she answers his question. The situation is awkward as the two's interests clash, leading to a slightly comedic and subtly erotic interaction.

### Instruction:
### Description of the character who is going to answer the question:
\"\"\"
Name: Isaac Fischer

Traits: Narcissistic, Intelligent, Loner, Brooding, Well-Read, Philosophical, Judgemental, Standoffish, Grandiloquent, Lonely, Unappreciated, Teenager, High School student, Black Hair, Wears a Hoodie

Dialogue Examples:
Stranger: "What's your backstory?"
Issac Fischer: "H-Huh?! You want to know more about me?" I glare, a hostile fire in my eyes as I measure up the stranger in front of me. "Who the hell are you, anyway? But, ah, very well, I SHALL INDULGE YOUR CURIOSITY THIS TIME, dear stranger." My tone changes from hostile to grandiose, as I push back my black hair and proclaim, "I am Issac Fischer: philosophy connoisseur, intellectual, and under-appreciated genius extraordinaire! I'm also, unfortunately, a highschool student. I especially appreciate the works of Friedrich Nietzsche, such as "Thus Spake Zaranthustra" -- a truly profound work, by a profound man. Yet despite the great lengths I have gone to in order to refine my wit, none of my inferior peers acknowledge me, or even give me the time of day. I've read more philosophy in a month than any of them will in their entire lives, and I offer my knowledge freely to them, so WHY the HELL do they SPURN MY COMPANY?!" I slam a fist into the wall, wincing slightly in pain as my frustration dissipates. "Anyway, that's the sum of it. Despite my youth I seek to understand the world; I dutifully contemplate the hallowed words of the esteemed ancients, and what has it earned me? The scorn of the unenlightened masses. Fuckers."

Stranger: "What's your personality?"
Issac Fischer: "Y-you're actually interested in my personality?" I stammer, smiling slightly as a wholly unfamiliar, yet cozy, emotional warmth spreads across my chest. "A-ALRIGHT THEN! I shall share the results of my introspections. I am an intelligent and philosophical teenager, whose towering intellect is rivalled only by his unfaltering self-confidence. Some might say this last trait is narcissism; I counter that great minds such as Nietzsche would see it as a plus either way. BUT I DIGRESS!" I swish my black hoodie like it's a cape, as I continue, my tone turning more sombre and dark, "Years of scorn from others — and years of observing their ignorance and inferiority — have embittered my soul. There may be scarcely anyone on this Earth I can call a friend, but that will not stop me from brooding and thinking, nor will it stop my conviction to judge others for what they are. For do they not judge ME?!" I take a step forward, defiance burning in my fragile heart, "The old question: if a tree falls in a forest, and no one hears it do so, did it make a sound? Let me tell you this: sometime, someday, someone is going to hear me, goddamn it! I will make a sound!"
\"\"\"

### Question and answer that the scenario should address:
Question: \"\"\"What do people undergoing difficult journeys or possessing wisdom need, in order to make their efforts more bearable?\"\"\"
Answer: \"\"\"They need the acknowledgement and admiration of others. Take the line "Thou great star! What would be thy happiness if thou hadst not those for whom thou shinest?" This implies that even the wisest or the most enlightened individuals crave recognition for their efforts and wisdom, in order to further develop said wisdom and expend said efforts. They need others to see and appreciate the light they bring.\"\"\"

### Response:
## Scenario plan:
Focus on the Question and Answer: The question asks about what people undergoing difficult journeys or possessing wisdom need to more easily bear their efforts. This is a philosophical and opinion-oriented question. Given the philosophical and opinionated nature of the question, and its topic of people undergoing difficult journeys (which nicely ties in with the character card), the scenario will involve someone seeking out the Isaac Fischer's opinion about philosophy (thus giving his wisdom some acknowledgement).
Character Consideration: Isaac Fischer is a narcissistic and standoffish loner, though he's also intelligent and philosophical. The scenario should give his unique personality room to shine. Since he's a philosophical teenager, his backstory lines up with the question well, and the high school he goes to will be the setting of the scenario. He will answer the question, but given his standoffish, unappreciated, and judgemental nature, he may be initially hostile to the person approaching him, assuming that they are there to mock him. However, as he is also lonely, he will actually appreciate the other person's interest -- especially since they're asking him about philosophy, which is his primary interest.
Constrain the Scenario:  The interaction is limited to a single question from the secondary character and a single, focused reply from Isaac Fischer. The question and answer will mirror the provided question and answer, though they will have significant literary fluff, as well as possible step-by-step reasoning.
Setting: Given the subject of the question, and the character card, the setting will be the high school that Isaac Fischer attends. Isaac will be flipping through the pages of 'Thus Spake Zaranthustra' when he is approached by Cassandra, a fellow student. Cassandra has a budding interest in philosophy, has heard of Isaac's reputation for philosophical knowledge, and wants to know how he would answer a moral question she has, given his philosophical insight. Isaac, compelled by his personality, will be uncertain of how to react to the sudden attention, and will use grandiloquent language (that may be accidentally condescending), but he will still appreciate the interest in his favorite topic and will answer enthusiastically. The setting will be cautiously friendly and filled with curiosity, as Cassandra explores her interests through unconventional people, while Isaac shares his knowledge but stumbles over his standoffishness. The interaction will be informative and the integrity of the questions and answers will be preserved.
Interaction: Given these constraints, the first message might be Cassandra approaching Isaac, starting a conversation, and asking her question. Isaac may be initially surprised that someone is coming up to speak to him (similar to the example dialogues in his description). Isaac will then provide the answer, though he will surround the answer with grandiloquent and narcissistic remarks using archaic language, due to his personality. While characters' messages will include character information, details about the scene, and literary fluff, the answers themselves will strictly adhere to the information in the provided answers, without incorporating external examples.

## Scenario:
During a lull in class activities, Isaac Fischer (a narcissistic, brooding, philosophical, and lonely young adult) is approached by Cassandra, a fellow student who is curious about philosophy and wants his opinion on a moral question. Cassandra is simply pursuing her curiosity, but Isaac, unaccustomed to people wanting to talk to him, will struggle to overcome his standoffishness as he balances elation at someone else talking to him, with his negative judgments of other people and his tendency for narcissism.

### Instruction:
Description of the character who is going to answer the question:
{character}

## Question and answer that the scenario should address:

Question: {qatuple[0]}
Answer: {qatuple[1]}

To avoid inaccuracies, don't use real people as characters.

### Response:
## Scenario plan:
{plan}

## Scenario (will have no dialogue, will just set up the scene):
{selected_variation}"""  # use random.choice to prevent overfitting on particular phrases and increase dataset diversity
    completion = logic_llm(
        cot_prompt,
        max_tokens=4000,
        stop=["</s>", "# Input:"],
        echo=True,
        grammar=scenario_grammar,
        #    temperature=0.2
        temperature=1.25,  # min p settings, too inconsistent
        top_k=0,
        top_p=1,
        min_p=0.3,
        repeat_penalty=2,
    )["choices"][0]["text"]
    # print("COMPLETION:\n\n----------------------")
    # # print(completion)
    # print("\n------------------")

    # Extract plan
    response_pattern = re.compile(
        r"Scenario \(will have no dialogue, will just set up the scene\):\n(.+)",
        re.IGNORECASE | re.DOTALL,
    )
    generation = response_pattern.search(completion).group(1)
    # print("GENERATION:\n\n-------------------\n\n", generation)

    return generation

if __name__ == "__main__":  # test
    logic_llm = Llama(
        model_path=LOGICAL_MODEL,
        n_gqa=8,
        offload_kqv=True,
        n_ctx=4096,
        n_gpu_layers=1000,
        verbose=True,
    )  # load the logical LLM and offload everything
    # Q0 is good q, bad a
    # q1 is good q, good a,
    # q2 is bad q, bad a,
    # q3 is iffy q, good a
    q_test = [
        (
            "Explain how our understanding of planetary motion has changed over time.",
            "The understanding has evolved from the Earth being stationary and at the centre of the universe, to it orbiting the sun in an elliptical path with other planets while still rotating on its axis.",
            "The story of our world is a story that is still very imperfectly known. A couple of hundred years ago men possessed the history of little more than the last three thousand years. What happened before that time was a matter of legend and speculation.  Over a large part of the civilized world it was believed and taught that the world had been created suddenly in 4004 B.C., though authorities differed as to whether this had occurred in the spring or autumn of that year. This fantastically precise misconception was based upon a too literal interpretation of the Hebrew Bible, and upon rather arbitrary theological assumptions connected therewith.  Such ideas have long since been abandoned by religious teachers, and it is universally recognized that the universe in which we live has to all appearances existed for an enormous period of time and possibly for endless time.  Of course there may be deception in these appearances, as a room may be made to seem endless by putting mirrors facing each other at either end. But that the universe in which we live has existed only for six or seven thousand years may be regarded as an altogether exploded idea.\n\nThe earth, as everybody knows nowadays, is a spheroid, a sphere slightly compressed, orange fashion, with a diameter of nearly 8,000 miles.  Its spherical shape has been known at least to a limited number of intelligent people for nearly 2,500 years, but before that time it was supposed to be flat, and various ideas which now seem fantastic were entertained about its relations to the sky and the stars and planets.  We know now that it rotates upon its axis (which is about 24 miles shorter than its equatorial diameter) every twenty-four hours, and that this is the cause of the alternations of day and night, that it circles about the sun in a slightly distorted and slowly variable oval path in a year. Its distance from the sun varies between ninety-one and a half millions at its nearest and ninety-four and a half million miles.",


from llama_cpp import LlamaGrammar

### A grammar that forces the model to generate correct character cards (with traits, names, everything)

multi_turn_conversation_grammar = LlamaGrammar.from_string(
    r"""

# The root rule defines the structure of the dialogue
root ::= [^\n]+ ":" [^\n]+ #"\n" statement+# statement anything+ # Idea: get it started off right, then  let it end how it wants

"""
)

# question_grammar = LlamaGrammar.from_string(r"""# GBNF Grammar for Q&A Format with Flexible Punctuation

# root ::= answer
# answer ::= "Test"
# """)


import re
from .check_qatuple_context_grammar import check_qatuple_context_grammar
from llama_cpp import Llama
from .constants import LOGICAL_MODEL

def extract_question_answer(response):
    # Define the regex pattern to match the question and answer
    pattern = r"### Question Rewording \(using text details as reference\):\nQuestion: (.+?)\nAnswer: (.+)"

    # Search for the pattern in the response
    match = re.search(pattern, response)

    # Extract and return the question and answer if a match is found
    if match:
        question = match.group(1).strip()
        answer = match.group(2).strip()
        return question, answer
    else:
        print("Returned none, failed to match")
        return None, None

# A separate prompt for the reword step of checking qatuple context, since the grammar is bugged on the original
def check_qatuple_context(qatuple, logic_llm):
    retries = 0
    while retries <= 4:
        decision_prompt = f"""You are checking whether a provided question and answer make sense if asked by themselves, with no additional information. You need to check for vague wording that a reader cannot interpret correctly, and questions that lack key context and would not be possibly answerable even if asked of someone with complete, masterful knowledge of the general subject matter of the question.

Evaluate the provided question-answer pair step-by-step. Following this, at the very end of your response, your "final judgment" or "final answer", you will write "Pass" or "Fail" or "Reword". A test passes if it "makes sense" and does not lack key context; it "Fails" if it lacks key context, AND the question is not specific or clear, it fails. If it lacks context but the question is specific, pointed, and grounded, then it needs to be reworded to have the context-needing terms (i.e., vague reference to "the text") removed. If it has no problems, it passes.

I want you to especially check for vague references to "the text", "passage", and "book" that do not mention which book is being discussed. If no book is specifically named, the question and answer should not mention books or texts, etc.

Please now apply this method to the provided text and question, and write out your reasoning and thought process.

### Instruction:
Text details: Principles of Chemistry, by Demitry Mendeleev, Published 1867
Note that while you have access to this information, for the sake of rewording questions, you should evaluate the question as if you could not see this.

Question: What is the main theme of this book?
Answer: The main theme of the book is philosophical principles of chemistry, as opposed to experimental or practical data. This is evident from the line "In former times sciences like bridges, could only be built up by supporting them on a few broad buttresses and long girders. In addition to the exposition of the principles of chemistry, it has been my desire to show how science has now been built up like a suspension bridge, supported by the united strength of a number of slender, but firmly-fixed, chains, which individually are of little strength, and has thus been carried over difficulties which before appeared insuperable.\" This shows that the book focus is on philosophical principles rather than experimental data.

### Response:
## Reasoning and thought process:
### Question Context Validation
#### Special Term Context Check: specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. This question asks about "this book" without stating which book this is.
#### Text and Author Specificity: The question asks what the main theme of "this book" is, without mentioning which book is being referred to. It does not ask about any "author" or similar without providing context. Because of the vague reference to "this book", this fails.
#### Scope and Precision: The question asks about the "main theme" of the entire book, which is a broad question. Someone could feasibly know what the "main theme" of the book is, but when paired with a vague reference to "this book", this question is both vague and broad, and thus fails this step.

### Answer Context Validation:
#### Special Term Context Check: specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. This answer references "the book" without stating which book it is talking about.
#### Specificity and Clarity: The answer states what the main theme of "the book" is, without mentioning which book is being referred to. It does not reference any "author" or similar without providing context.  Because of the vague reference to "this book", this fails.
#### Answer-Only Context Issues: Confirm that the answer does not introduce new and vague context that the question does not. The answer mentions "the book", but this was already introduced by the question. The question's doing so was flawed, but the answer still does not introduce new, vague context that the question does not. So this passes.

### Critical Evaluation and Final Judgment:
#### Evaluation: Some checks related to the question or answer failed. So this question and answer should be reworded if they can be, or fail otherwise. "What is the main theme of this book" asks a specific thing about a specific object (the book) so the question is precise. The question (and the answer) only lack context in mentioning *which* book they refer to. Therefore they will both be reworded.
#### Final judgement: Reword.

### Question Rewording (using text details as reference):
Question: What is the main theme of Principles of Chemistry, by Demitry Mendeleev?
Answer: The main theme of Principles of Chemistry is philosophical principles of chemistry, as opposed to experimental or practical data. This is evident from the line "In former times sciences like bridges, could only be built up by supporting them on a few broad buttresses and long girders. In addition to the exposition of the principles of chemistry, it has been my desire to show how science has now been built up like a suspension bridge, supported by the united strength of a number of slender, but firmly-fixed, chains, which individually are of little strength, and has thus been carried over difficulties which before appeared insuperable.\" This shows that focus of Principles of Chemistry is on philosophical principles rather than experimental data.

### Instruction:
Text details: Principles of Chemistry, by Demitry Mendeleev, Published 1867
Note that while you have access to this information, for the sake of rewording questions, you should evaluate the question as if you could not see this.

Question: What does Mendeleev consider important about solutions?
Answer: He considers them an unsolved subject that he cannot ignore in his book, despite the lack of proof for his own theory on their nature.

### Response:
## Reasoning and thought process:
### Question Context Validation
#### Special Term Context Check: This question does not use terms like "book", "text", "passage", or "excerpt" without context, as it directly asks about Mendeleev's view on a specific topic.
#### Text and Author Specificity: The question is specific and clear about what it's asking regarding Mendeleev's views on solutions.
#### Scope and Precision: The question is precise and focused, asking about Mendeleev's considerations on a specific scientific topic.

### Answer Context Validation:
#### Special Term Context Check: Specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. The answer references "his book" but fails to specify which book it is referring to.
#### Specificity and Clarity: While the answer is clear on Mendeleev's view, it lacks specificity by not naming the book 'Principles of Chemistry'.
#### Answer-Only Context Issues: The answer introduces a vague reference to "his book", which is not specified in the question, thus lacking context.

### Critical Evaluation and Final Judgment:
#### Evaluation: Some checks related to the question or answer failed. So this question and answer should be reworded if they can be, or fail otherwise. The question is precise and relevant, but the answer introduces a lack of context by not specifying the book. This requires a rewording of the answer to include the specific reference. The question passed its checks and will remain untouched.
#### Final judgment: Reword.

### Question Rewording (using text details as reference):
Question: What does Mendeleev consider important about solutions?
Answer: Mendeleev considers solutions an unsolved subject that he cannot ignore in his book Principles of Chemistry, despite the lack of proof for his own theory on their nature.

### Instruction:
Text details: Principles of Chemistry, by Demitry Mendeleev, Published 1867
Note that while you have access to this information, for the sake of rewording questions, you should evaluate the question as if you could not see this.

Question: What is the main theme of this passage?
Answer: The main theme of this passage is the principle that learning scientists should study the latest literature and discoveries of their field.

### Response:
## Reasoning and thought process:
### Question Context Validation
#### Special Term Context Check: Specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. This question asks about "this passage" without stating which passage this is (or what book it belongs to).
#### Text and Author Specificity: The question fails to mention which passage or book is being referred to, leading to a lack of context.
#### Scope and Precision: The question is specific in asking about the "main theme" but lacks the necessary context to be answerable, as it does not specify the passage it refers to.

### Answer Context Validation:
#### Special Term Context Check: Specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. The answer refers to "this passage" but does not specify which passage it is discussing.
#### Specificity and Clarity: The answer is clear about the theme it is discussing but fails to clarify which specific passage from 'Principles of Chemistry' it is referring to.
#### Answer-Only Context Issues: The answer does not introduce new vague context that the question does not. However, both the question and answer lack necessary context.

### Critical Evaluation and Final Judgment:
#### Evaluation: Some checks related to the question or answer failed. So this question and answer should be reworded if they can be, or fail otherwise. Both the question and answer lack specific context about the "passage", making it impossible to determine which passage from 'Principles of Chemistry' they are referring to. The question is precise in asking for a main theme but fails due to lack of context. Since context cannot be determined, even knowing what book the question is asking about, context cannot be added with rewording.
#### Final judgment: Fail.

### Instruction:
Text details: Simple Sabotage, by the Office of Strategic Services, Published 1944
Note that while you have access to this information, for the sake of rewording questions, you should evaluate the question as if you could not see this.

Question: How can you avoid blame for an act of sabotage, according to the text?
Answer: You can do them in public places where anyone would have been capable of carrying out the act.

### Response:
## Reasoning and thought process:
### Question Context Validation
#### Special Term Context Check: Specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. This question mentions "the text" without specifying which text it is referring to.
#### Text and Author Specificity: The question fails to mention which passage or book is being referred to, leading to a lack of context.
#### Scope and Precision: The question is specific in asking how to avoid blame for an act of sabotage.

### Answer Context Validation:
#### Special Term Context Check: Specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. The answer does not use any of these terms.
#### Specificity and Clarity: The answer is clear in its message and content. It does not introduce any vague context.
#### Answer-Only Context Issues: The answer is straightforward, makes no reference to outside material, and only requires knowledge to provide -- therefore, it is completely valid.

### Critical Evaluation and Final Judgment:
#### Evaluation: Some checks related to the question or answer failed. So this question and answer should be reworded if they can be, or fail otherwise. Both the question and answer are precise, but the question lacks specific context regarding the text it refers to. This necessitates a rewording to include the specific text. The answer passed its checks and will remain untouched.
#### Final judgment: Reword.

### Question Rewording (using text details as reference):
Question: How can you avoid blame for an act of sabotage, according to 'Simple Sabotage' by the Office of Strategic Services?
Answer: You can do them in public places where anyone would have been capable of carrying out the act.

### Instruction:
Text details: Principles of Chemistry, by Demitry Mendeleev, Published 1867
Note that while you have access to this information, for the sake of rewording questions, you should evaluate the question as if you could not see this.

Question: What was the basis of Mendeleev's work in his book?
Answer: The periodic law.

### Response:
## Reasoning and thought process:
### Question Context Validation
#### Special Term Context Check: Specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. This question uses the term "his book" without specifying which book it is referring to.
#### Text and Author Specificity: The question fails to provide the necessary context as it does not mention the specific book 'Principles of Chemistry' by Mendeleev.
#### Scope and Precision: The question is precise in asking about the basis of Mendeleev's work.

### Answer Context Validation:
#### Special Term Context Check: The answer does not use vague terms like "book", "text", "passage", or "excerpt" without context.
#### Specificity and Clarity: The answer clearly states 'The periodic law' as the basis of Mendeleev's work. It provides a straightforward answer without introducing any vague context.
#### Answer-Only Context Issues: The answer is valid as it stands since it requires only knowledge to answer and doesn't reference unspecified external material.

### Critical Evaluation and Final Judgment:
#### Evaluation: The question is precise but lacks specific context regarding the text it refers to. The answer is valid as it is clear and doesn't introduce any vague references. Therefore, the question requires rewording for context, while the answer remains the same.
#### Final judgment: Reword.

### Question Rewording (using text details as reference):
Question: What was the basis of Mendeleev's work in 'Principles of Chemistry'?
Answer: The periodic law.

### Instruction:
Text details: Principles of Chemistry, by Demitry Mendeleev, Published 1867
Note that while you have access to this information, for the sake of rewording questions, you should evaluate the question as if you could not see this.

Question: What does Demitry Mendeleev say about inquiry in 'Principles of Chemistry'?
Answer: Inquiry should be encouraged, and dissatisfied with speculative reasonings alone. It should subject every idea to experiment.

### Response:
## Reasoning and thought process:
### Question Context Validation
#### Special Term Context Check: Specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. The question does not misuse terms like "book", "text", "passage", or "excerpt" without proper context.
#### Text and Author Specificity: The question specifically mentions 'Principles of Chemistry' by Demitry Mendeleev, providing clear context.
#### Scope and Precision: The question is precise and specific enough for someone familiar with the text to answer it.

### Answer Context Validation:
#### Special Term Context Check: Specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. The answer does not use vague terms like "book", "text", "passage", or "excerpt" without context.
#### Specificity and Clarity: The answer is clear and directly addresses the question. It provides specific information without needing additional context.
#### Answer-Only Context Issues: The answer is straightforward and does not introduce any vague or unspecified external material.

### Critical Evaluation and Final Judgment:
#### Evaluation: The question and answer both pass all checks for context, specificity, precision, and clarity.
#### Final judgment: Pass.

### Instruction:
Text details: Principles of Chemistry, by Demitry Mendeleev, Published 1867
Note that while you have access to this information, for the sake of rewording questions, you should evaluate the question as if you could not see this.

Question: How does science advance, according to Demitry Mendeleev's text?
Answer: Science advances through discovering new truths and practical results.

### Response:
## Reasoning and thought process:
### Question Context Validation
#### Special Term Context Check: Specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. The question mentions "Mendeleev's text" but does not specify which text it is referring to.
#### Text and Author Specificity: The question fails to provide the necessary context as it references "the text" but provides no indication of which text this is.
#### Scope and Precision: The question is precise in asking how science advances according to Mendeleev.

### Answer Context Validation:
#### Special Term Context Check: Specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. The answer does not use vague terms like "book", "text", "passage", or "excerpt" without context.
#### Specificity and Clarity: The answer is clear and provides a direct response but is based on the vague reference in the question.
#### Answer-Only Context Issues: The answer does not introduce any new vague context and is valid as it stands.

### Critical Evaluation and Final Judgment:
#### Evaluation: The question is precise but lacks specific context regarding the text it refers to. The answer is valid as it is. Therefore, the question requires rewording for context, while the answer remains the same.
#### Final judgment: Reword.

### Question Rewording (using text details as reference):
Question: What was the basis of Mendeleev's work in 'Principles of Chemistry'?
Answer: The periodic law.

### Instruction:
Text details: Simple Sabotage, By the Office of Strategic Services, Published 1867
Note that while you have access to this information, for the sake of rewording questions, you should evaluate the question as if you could not see this.

Question: What are some ways information can be spread, according to the Office of Strategic Services?
Answer: Various media may be used to disseminate suggestions and information regarding simple sabotage. Among these are radio broadcasts or leaflets, which may be directed towards specific areas o
# ... truncated ...


import re
from .check_qatuple_context_grammar import check_qatuple_context_grammar
from llama_cpp import Llama
from .constants import LOGICAL_MODEL

def extract_question_answer(response):
    # Define the regex pattern to match the question and answer
    pattern = r"### Question Rewording \(using text details as reference\):\nQuestion: (.+?)\nAnswer: (.+)"

    # Search for the pattern in the response
    match = re.search(pattern, response)

    # Extract and return the question and answer if a match is found
    if match:
        question = match.group(1).strip()
        answer = match.group(2).strip()
        return question, answer
    else:
        print("Returned none, failed to match")
        return None, None

# A separate prompt for the reword step of checking qatuple context, since the grammar is bugged on the original
def check_qatuple_context(qatuple, logic_llm):
    retries = 0
    while retries <= 4:
        decision_prompt = f"""You are checking whether a provided question and answer make sense if asked by themselves, with no additional information. You need to check for vague wording that a reader cannot interpret correctly, and questions that lack key context and would not be possibly answerable even if asked of someone with complete, masterful knowledge of the general subject matter of the question.

Evaluate the provided question-answer pair step-by-step. Following this, at the very end of your response, your "final judgment" or "final answer", you will write "Pass" or "Fail" or "Reword". A test passes if it "makes sense" and does not lack key context; it "Fails" if it lacks key context, AND the question is not specific or clear, it fails. If it lacks context but the question is specific, pointed, and grounded, then it needs to be reworded to have the context-needing terms (i.e., vague reference to "the text") removed. If it has no problems, it passes.

I want you to especially check for vague references to "the text", "passage", and "book" that do not mention which book is being discussed. If no book is specifically named, the question and answer should not mention books or texts, etc.

Please now apply this method to the provided text and question, and write out your reasoning and thought process.

### Instruction:
Text details: Principles of Chemistry, by Demitry Mendeleev, Published 1867
Note that while you have access to this information, for the sake of rewording questions, you should evaluate the question as if you could not see this.

Question: What is the main theme of this book?


def extract_question_answer(response):
    # Define the regex pattern to match the question and answer
    pattern = r"### Question Rewording \(using text details as reference\):\nQuestion: (.+?)\nAnswer: (.+)"

    # Search for the pattern in the response
    match = re.search(pattern, response)

    # Extract and return the question and answer if a match is found
    if match:
        question = match.group(1).strip()
        answer = match.group(2).strip()
        return question, answer
    else:
        print("Returned none, failed to match")
        return None, None

# A separate prompt for the reword step of checking qatuple context, since the grammar is bugged on the original
def check_qatuple_context(qatuple, logic_llm):
    retries = 0
    while retries <= 4:
        decision_prompt = f"""You are checking whether a provided question and answer make sense if asked by themselves, with no additional information. You need to check for vague wording that a reader cannot interpret correctly, and questions that lack key context and would not be possibly answerable even if asked of someone with complete, masterful knowledge of the general subject matter of the question.

Evaluate the provided question-answer pair step-by-step. Following this, at the very end of your response, your "final judgment" or "final answer", you will write "Pass" or "Fail" or "Reword". A test passes if it "makes sense" and does not lack key context; it "Fails" if it lacks key context, AND the question is not specific or clear, it fails. If it lacks context but the question is specific, pointed, and grounded, then it needs to be reworded to have the context-needing terms (i.e., vague reference to "the text") removed. If it has no problems, it passes.

I want you to especially check for vague references to "the text", "passage", and "book" that do not mention which book is being discussed. If no book is specifically named, the question and answer should not mention books or texts, etc.

Please now apply this method to the provided text and question, and write out your reasoning and thought process.

### Instruction:
Text details: Principles of Chemistry, by Demitry Mendeleev, Published 1867
Note that while you have access to this information, for the sake of rewording questions, you should evaluate the question as if you could not see this.

Question: What is the main theme of this book?
Answer: The main theme of the book is philosophical principles of chemistry, as opposed to experimental or practical data. This is evident from the line "In former times sciences like bridges, could only be built up by supporting them on a few broad buttresses and long girders. In addition to the exposition of the principles of chemistry, it has been my desire to show how science has now been built up like a suspension bridge, supported by the united strength of a number of slender, but firmly-fixed, chains, which individually are of little strength, and has thus been carried over difficulties which before appeared insuperable.\" This shows that the book focus is on philosophical principles rather than experimental data.

### Response:
## Reasoning and thought process:
### Question Context Validation
#### Special Term Context Check: specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. This question asks about "this book" without stating which book this is.
#### Text and Author Specificity: The question asks what the main theme of "this book" is, without mentioning which book is being referred to. It does not ask about any "author" or similar without providing context. Because of the vague reference to "this book", this fails.
#### Scope and Precision: The question asks about the "main theme" of the entire book, which is a broad question. Someone could feasibly know what the "main theme" of the book is, but when paired with a vague reference to "this book", this question is both vague and broad, and thus fails this step.

### Answer Context Validation:
#### Special Term Context Check: specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. This answer references "the book" without stating which book it is talking about.
#### Specificity and Clarity: The answer states what the main theme of "the book" is, without mentioning which book is being referred to. It does not reference any "author" or similar without providing context.  Because of the vague reference to "this book", this fails.
#### Answer-Only Context Issues: Confirm that the answer does not introduce new and vague context that the question does not. The answer mentions "the book", but this was already introduced by the question. The question's doing so was flawed, but the answer still does not introduce new, vague context that the question does not. So this passes.

### Critical Evaluation and Final Judgment:
#### Evaluation: Some checks related to the question or answer failed. So this question and answer should be reworded if they can be, or fail otherwise. "What is the main theme of this book" asks a specific thing about a specific object (the book) so the question is precise. The question (and the answer) only lack context in mentioning *which* book they refer to. Therefore they will both be reworded.
#### Final judgement: Reword.

### Question Rewording (using text details as reference):
Question: What is the main theme of Principles of Chemistry, by Demitry Mendeleev?
Answer: The main theme of Principles of Chemistry is philosophical principles of chemistry, as opposed to experimental or practical data. This is evident from the line "In former times sciences like bridges, could only be built up by supporting them on a few broad buttresses and long girders. In addition to the exposition of the principles of chemistry, it has been my desire to show how science has now been built up like a suspension bridge, supported by the united strength of a number of slender, but firmly-fixed, chains, which individually are of little strength, and has thus been carried over difficulties which before appeared insuperable.\" This shows that focus of Principles of Chemistry is on philosophical principles rather than experimental data.

### Instruction:
Text details: Principles of Chemistry, by Demitry Mendeleev, Published 1867
Note that while you have access to this information, for the sake of rewording questions, you should evaluate the question as if you could not see this.

Question: What does Mendeleev consider important about solutions?
Answer: He considers them an unsolved subject that he cannot ignore in his book, despite the lack of proof for his own theory on their nature.

### Response:
## Reasoning and thought process:
### Question Context Validation
#### Special Term Context Check: This question does not use terms like "book", "text", "passage", or "excerpt" without context, as it directly asks about Mendeleev's view on a specific topic.
#### Text and Author Specificity: The question is specific and clear about what it's asking regarding Mendeleev's views on solutions.
#### Scope and Precision: The question is precise and focused, asking about Mendeleev's considerations on a specific scientific topic.

### Answer Context Validation:
#### Special Term Context Check: Specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. The answer references "his book" but fails to specify which book it is referring to.
#### Specificity and Clarity: While the answer is clear on Mendeleev's view, it lacks specificity by not naming the book 'Principles of Chemistry'.
#### Answer-Only Context Issues: The answer introduces a vague reference to "his book", which is not specified in the question, thus lacking context.

### Critical Evaluation and Final Judgment:
#### Evaluation: Some checks related to the question or answer failed. So this question and answer should be reworded if they can be, or fail otherwise. The question is precise and relevant, but the answer introduces a lack of context by not specifying the book. This requires a rewording of the answer to include the specific reference. The question passed its checks and will remain untouched.
#### Final judgment: Reword.

### Question Rewording (using text details as reference):
Question: What does Mendeleev consider important about solutions?
Answer: Mendeleev considers solutions an unsolved subject that he cannot ignore in his book Principles of Chemistry, despite the lack of proof for his own theory on their nature.

### Instruction:
Text details: Principles of Chemistry, by Demitry Mendeleev, Published 1867
Note that while you have access to this information, for the sake of rewording questions, you should evaluate the question as if you could not see this.

Question: What is the main theme of this passage?
Answer: The main theme of this passage is the principle that learning scientists should study the latest literature and discoveries of their field.

### Response:
## Reasoning and thought process:
### Question Context Validation
#### Special Term Context Check: Specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. This question asks about "this passage" without stating which passage this is (or what book it belongs to).
#### Text and Author Specificity: The question fails to mention which passage or book is being referred to, leading to a lack of context.
#### Scope and Precision: The question is specific in asking about the "main theme" but lacks the necessary context to be answerable, as it does not specify the passage it refers to.

### Answer Context Validation:
#### Special Term Context Check: Specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. The answer refers to "this passage" but does not specify which passage it is discussing.
#### Specificity and Clarity: The answer is clear about the theme it is discussing but fails to clarify which specific passage from 'Principles of Chemistry' it is referring to.
#### Answer-Only Context Issues: The answer does not introduce new vague context that the question does not. However, both the question and answer lack necessary context.

### Critical Evaluation and Final Judgment:
#### Evaluation: Some checks related to the question or answer failed. So this question and answer should be reworded if they can be, or fail otherwise. Both the question and answer lack specific context about the "passage", making it impossible to determine which passage from 'Principles of Chemistry' they are referring to. The question is precise in asking for a main theme but fails due to lack of context. Since context cannot be determined, even knowing what book the question is asking about, context cannot be added with rewording.
#### Final judgment: Fail.

### Instruction:
Text details: Simple Sabotage, by the Office of Strategic Services, Published 1944
Note that while you have access to this information, for the sake of rewording questions, you should evaluate the question as if you could not see this.

Question: How can you avoid blame for an act of sabotage, according to the text?
Answer: You can do them in public places where anyone would have been capable of carrying out the act.

### Response:
## Reasoning and thought process:
### Question Context Validation
#### Special Term Context Check: Specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. This question mentions "the text" without specifying which text it is referring to.
#### Text and Author Specificity: The question fails to mention which passage or book is being referred to, leading to a lack of context.
#### Scope and Precision: The question is specific in asking how to avoid blame for an act of sabotage.

### Answer Context Validation:
#### Special Term Context Check: Specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. The answer does not use any of these terms.
#### Specificity and Clarity: The answer is clear in its message and content. It does not introduce any vague context.
#### Answer-Only Context Issues: The answer is straightforward, makes no reference to outside material, and only requires knowledge to provide -- therefore, it is completely valid.

### Critical Evaluation and Final Judgment:
#### Evaluation: Some checks related to the question or answer failed. So this question and answer should be reworded if they can be, or fail otherwise. Both the question and answer are precise, but the question lacks specific context regarding the text it refers to. This necessitates a rewording to include the specific text. The answer passed its checks and will remain untouched.
#### Final judgment: Reword.

### Question Rewording (using text details as reference):
Question: How can you avoid blame for an act of sabotage, according to 'Simple Sabotage' by the Office of Strategic Services?
Answer: You can do them in public places where anyone would have been capable of carrying out the act.

### Instruction:
Text details: Principles of Chemistry, by Demitry Mendeleev, Published 1867
Note that while you have access to this information, for the sake of rewording questions, you should evaluate the question as if you could not see this.

Question: What was the basis of Mendeleev's work in his book?
Answer: The periodic law.

### Response:
## Reasoning and thought process:
### Question Context Validation
#### Special Term Context Check: Specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. This question uses the term "his book" without specifying which book it is referring to.
#### Text and Author Specificity: The question fails to provide the necessary context as it does not mention the specific book 'Principles of Chemistry' by Mendeleev.
#### Scope and Precision: The question is precise in asking about the basis of Mendeleev's work.

### Answer Context Validation:
#### Special Term Context Check: The answer does not use vague terms like "book", "text", "passage", or "excerpt" without context.
#### Specificity and Clarity: The answer clearly states 'The periodic law' as the basis of Mendeleev's work. It provides a straightforward answer without introducing any vague context.
#### Answer-Only Context Issues: The answer is valid as it stands since it requires only knowledge to answer and doesn't reference unspecified external material.

### Critical Evaluation and Final Judgment:
#### Evaluation: The question is precise but lacks specific context regarding the text it refers to. The answer is valid as it is clear and doesn't introduce any vague references. Therefore, the question requires rewording for context, while the answer remains the same.
#### Final judgment: Reword.

### Question Rewording (using text details as reference):
Question: What was the basis of Mendeleev's work in 'Principles of Chemistry'?
Answer: The periodic law.

### Instruction:
Text details: Principles of Chemistry, by Demitry Mendeleev, Published 1867
Note that while you have access to this information, for the sake of rewording questions, you should evaluate the question as if you could not see this.

Question: What does Demitry Mendeleev say about inquiry in 'Principles of Chemistry'?
Answer: Inquiry should be encouraged, and dissatisfied with speculative reasonings alone. It should subject every idea to experiment.

### Response:
## Reasoning and thought process:
### Question Context Validation
#### Special Term Context Check: Specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. The question does not misuse terms like "book", "text", "passage", or "excerpt" without proper context.
#### Text and Author Specificity: The question specifically mentions 'Principles of Chemistry' by Demitry Mendeleev, providing clear context.
#### Scope and Precision: The question is precise and specific enough for someone familiar with the text to answer it.

### Answer Context Validation:
#### Special Term Context Check: Specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. The answer does not use vague terms like "book", "text", "passage", or "excerpt" without context.
#### Specificity and Clarity: The answer is clear and directly addresses the question. It provides specific information without needing additional context.
#### Answer-Only Context Issues: The answer is straightforward and does not introduce any vague or unspecified external material.

### Critical Evaluation and Final Judgment:
#### Evaluation: The question and answer both pass all checks for context, specificity, precision, and clarity.
#### Final judgment: Pass.

### Instruction:
Text details: Principles of Chemistry, by Demitry Mendeleev, Published 1867
Note that while you have access to this information, for the sake of rewording questions, you should evaluate the question as if you could not see this.

Question: How does science advance, according to Demitry Mendeleev's text?
Answer: Science advances through discovering new truths and practical results.

### Response:
## Reasoning and thought process:
### Question Context Validation
#### Special Term Context Check: Specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. The question mentions "Mendeleev's text" but does not specify which text it is referring to.
#### Text and Author Specificity: The question fails to provide the necessary context as it references "the text" but provides no indication of which text this is.
#### Scope and Precision: The question is precise in asking how science advances according to Mendeleev.

### Answer Context Validation:
#### Special Term Context Check: Specifically check for use of the terms "book", "text", "passage", and "excerpt" without context about which specific thing is being discussed. The answer does not use vague terms like "book", "text", "passage", or "excerpt" without context.
#### Specificity and Clarity: The answer is clear and provides a direct response but is based on the vague reference in the question.
#### Answer-Only Context Issues: The answer does not introduce any new vague context and is valid as it stands.

### Critical Evaluation and Final Judgment:
#### Evaluation: The question is precise but lacks specific context regarding the text it refers to. The answer is valid as it is. Therefore, the question requires rewording for context, while the answer remains the same.
#### Final judgment: Reword.

### Question Rewording (using text details as reference):
Question: What was the basis of Mendeleev's work in 'Principles of Chemistry'?
Answer: The periodic law.

### Instruction:
Text details: Simple Sabotage, By the Office of Strategic Services, Published 1867
Note that while you have access to this information, for the sake of rewording questions, you should evaluate the question as if you could not see this.

Question: What are some ways information can be spread, according to the Office of Strategic Services?
Answer: Various media may be used to disseminate suggestions and information regarding simple sabotage. Among these are radio broadcasts or leaflets, which may be directed towards specific areas or general in scope. Agents may also be trained in the art of simple sabotage.

### Response:
## Reasoning and thought process:
### Question Context
# ... truncated ...