import re

# try:
from .questions_grammar import questions_grammar
from llama_cpp import Llama
from .constants import LOGICAL_MODEL
from .strip_steps import strip_steps

def generate_questions(para_tuple, plan, logic_llm):
    """
    Produce a list of questions based off of an input text. The min between (4, as many good questions as the text permits)

    Format: Question: [question]\n\n
    """
    # Determine which paragraphs are worthy of making questions from
    made_questions = False
    retries = 0
    questions = []
    while not made_questions and (retries <= 5):
        question_prompt = f"""You are an expert educational AI that, given a paragraph or two from a text, will create suitable educational questions based on the paragraphs, and *only* based on the paragraphs. You are focusing on understanding, application, analysis, and synthesis of ideas (cognitive levels). The questions you create will lean towards longer, more difficult questions that require some thought to solve — but can still be solved given the paragraphs provided. Essentially: the questions will test comprehension of real information that would be worthy to teach. After the question, you will also write its answer.

Do not explicitly mention the paragraphs in the questions themselves — just ask about the concepts related to the questions. BE CAREFUL NOT TO ASK QUESTIONS ABOUT THINGS THAT DO NOT APPEAR IN THE TEXT.

You will not mention the text explicitly in any questions you think of, since the questions you generate are intended to test people's knowledge of the information — when given the questions, they WILL NOT HAVE THE TEXT ON HAND, and so if you mention the author they won't have a clue what you're talking about.

### Instruction:
Text details: Introduction to Mathematics, by Jane Smith

Text to make questions from:
\"\"\"
In mathematics, the concept of a 'function' is fundamental, defining a relationship where each input is associated with exactly one output. An important class of functions is 'linear functions', represented by the equation y = mx + b, where 'm' is the slope and 'b' is the y-intercept. The slope 'm' measures the steepness and direction of the linear function, while the y-intercept 'b' indicates the point where the line crosses the y-axis. Understanding these components is crucial in graphing linear functions and solving real-world problems. Another vital concept is the 'quadratic function', typically expressed as y = ax² + bx + c. The 'a' coefficient determines the opening direction and width of the parabola, 'b' influences the axis of symmetry, and 'c' represents the y-intercept. These functions form the basis of algebra and are extensively used in various fields including physics, economics, and engineering.
\"\"\"

### Response:
## Reasoning and thought process:
Identify Key Topics: The key topics in this paragraph are linear and quadratic functions in mathematics, their definitions, components, and applications.
Brainstorm and Develop Questions Testing Recall: Formulate questions that test the recall of definitions and components of these functions. Example: "What does the 'm' in the linear function equation represent?"
Devise Questions Exploring Relationships Between Components: Generate questions that explore the relationship between different parts of the equations. Example: "How does the coefficient 'a' in a quadratic function affect its graph?"
Create Questions Investigating Vital Concepts: Make questions about key concepts from the text, without mentioning the text (this is not a literary or philosophical text). Example: "What are the formulae for linear and quadratic functions, respectively?"
Make a Question that Naturally Complements the Text's Focus: The text by Elise Delacroix focuses on linear and quadratic functions in mathematics (as well as functions in general), and another question on this subject would be suitable. The text focuses on explaining properties of linear and quadratic functions, but it also briefly defines what a function is. Since this knowledge is foundational to the other concepts, it makes sense to create a question off of it; but since the definition in the text is brief, the question will only require a brief answer. Example: "Define the concept of a function in mathematics, in one sentence."

## Questions:
1.) 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.

2.) What role does the y-intercept 'b' play in graphing a linear function?
Answer: The y-intercept 'b' in the linear function equation y = mx + b represents the point where the line crosses the y-axis.

3.) In the equation of a quadratic function y = ax² + bx + c, how does the coefficient 'a' influence the graph of the function?
Answer: The coefficient 'a' in a quadratic function determines the opening direction and the width of the parabola.

4.) Define the concept of a function in mathematics, in one sentence.
Answer: A function is a relationship where each input is associated with exactly one output.

### Instruction:
Text details: Thus Spake Zarathustra, by Friedrich Nietzsche

Text to make questions from:
\"\"\"
When Zarathustra was thirty years old, he left his home and the lake of his home, and went into the mountains. There he enjoyed his spirit and
solitude, and for ten years did not weary of it. But at last his heart changed,—and rising one morning with the rosy dawn, he went before the
sun, and spake thus unto it:

Thou great star! What would be thy happiness if thou hadst not those for
whom thou shinest!

For ten years hast thou climbed hither unto my cave: thou wouldst have
wearied of thy light and of the journey, had it not been for me, mine
eagle, and my serpent.

But we awaited thee every morning, took from thee thine overflow and
blessed thee for it.

Lo! I am weary of my wisdom, like the bee that hath gathered too much
honey; I need hands outstretched to take it.

I would fain bestow and distribute, until the wise have once more become
joyous in their folly, and the poor happy in their riches.

Therefore must I descend into the deep: as thou doest in the
evening, when thou goest behind the sea, and givest light also to the
nether-world, thou exuberant star!

Like thee must I GO DOWN, as men say, to whom I shall descend.
\"\"\"

### Response:
## Reasoning and Thought Process:
Identify Key Themes: In this excerpt, we find themes of transformation, self-discovery, the value of wisdom, and the need for recognition of effort and wisdom.
Brainstorm and Develop Questions Testing Recall: Develop questions asking for specific information mentioned in this text. Since this is a literary and philosophical text, I can mention the text — but by name only. For instance, "Finish the quote from Thus Spake Zarathustra: I am weary of my wisdom, like..."
Devise Questions Testing Comprehension of, and Belief in, the Text's Opinions: Questions can be formed to check understanding and acceptance of the text's philosophy. For instance, "What do people undergoing difficult journeys or possessing wisdom need, in order to make their efforts more bearable?" Which might be answered with, "They need the acknowledgement and admiration of others." and then supported by lines such as "Thou great star! What would be thy happiness if thou hadst not those for whom thou shinest?" I will be careful that all lines I ask about have enough context to be answered by themselves.
Create Questions Investigating Interpretations: Given the text's rich language, I will devise questions interpreting its meaning; since this is a literary text I can mention the text, but only by name. Example: "Why did Zarathustra, in 'Thus Spake Zarathustra', leave the mountains and become a man again?"
Make a Question that Naturally Complements the Text's Focus: The text by Friedrich Nietzsche focuses on Zarathustra's monologue, and another question on this subject would be suitable. Zarathustra compares himself and his actions to the sun throughout his monologue, such as when he relates his intention to "descend into the deep" to the sun's own going to the nether-world. A good question might ask after this crucial comparison, and could be "List one way in which Zarathustra compares himself to the sun, in 'Thus Spake Zarathustra'."

## Questions:
1.) Finish the quote from Thus Spake Zarathustra: I am weary of my wisdom, like...
Answer: "... the bee that hath gathered too much honey; I need hands outstretched to take it."

2.) 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 from 'Thus Spake Zarathustra' by Friedrich Nietzsche: "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.

3.) Why did Zarathustra, in 'Thus Spake Zarathustra', leave the mountains and become a man again?
Answer: After enjoying his spirit and solitude for ten years, he had a change of heart, and realized that wisdom unshared, without acknowledgement, brings little satisfaction. He became a man and descended the mountains in order to "fain bestow and distribute, until the wise have once more become joyous in their folly, and the poor happy in their riches."

4.) List one way in which Zarathustra compares himself to the sun, in 'Thus Spake Zarathustra'.
Answer: Zarathustra explains to the sun, in Thus Spake Zarathustra, that he must descend into the deep — and he compares this action to the sun's own going "behind the sea" where it gives light to the "nether-world".

### Instruction:
Text details: The Republic, by Plato

Text to plan questions from:
\"\"\"
I went down yesterday to the Piraeus with Glaucon the son of Ariston,
that I might offer up my prayers to the goddess (Bendis, the Thracian
Artemis.); and also because I wanted to see in what manner they would
celebrate the festival, which was a new thing. I was delighted with the
procession of the inhabitants; but that of the Thracians was equally,
if not more, beautiful. When we had finished our prayers and viewed the
spectacle, we turned in the direction of the city; and at that instant
Polemarchus the son of Cephalus chanced to catch sight of us from a
distance as we were starting on our way home, and told his servant to
run and bid us wait for him. The servant took hold of me by the cloak
behind, and said: Polemarchus desires you to wait.

I turned round, and asked him where his master was.

There he is, said the youth, coming after you, if you will only wait.

Certainly we will, said Glaucon; and in a few minutes Polemarchus
appeared, and with him Adeimantus, Glaucon’s brother, Niceratus the son
of Nicias, and several others who had been at the procession.

Polemarchus said to me: I perceive, Socrates, that you and your
companion are already on your way to the city.

You are not far wrong, I said.

But do you see, he rejoined, how many we are?

Of course.

And are you stronger than all these? for if not, you will have to
remain where you are.

May there not be the alternative, I said, that we may persuade you to
let us go?

But can you persuade us, if we refuse to listen to you? he said.

Certainly not, replied Glaucon.

Then we are not going to listen; of that you may be assured.
\"\"\"

### Response:
## Reasoning and Thought Process:
Identify Key Themes: This excerpt from Plato's "The Republic" presents themes of social interaction, philosophical dialogue, and the dynamics of persuasion and power.
Brainstorm and Develop Questions Testing Recall: Instead of focusing on dry, context-dependent questions like the order of events or family relations, I will concentrate on more meaningful historical details present in the text. For instance, "Which goddess is Bendis equivalent to?"
Devise Questions Exploring Comprehension of, and Belief in, the Text's Opinions: I will create questions that delve into the mind and thoughts of the writer. For example, drawing on the contentious interaction between Polemarchus and Socrates, I could ask, "If someone indicates that they will force you to take a course of action, using physical force, what is an alternative you can take?" with the answer being "Persuade them to let me go." This question encourages the reader to put themselves in the mind of the author.
Create Questions Investigating Interpretations: Focusing on the dialogue's subtleties, I will craft an interpretative question. Since this is a literary text, I can reference the text, but only by name. So the question might be, "In Plato's "The Republic," in the dialogue where Polemarchus comments on the size of his group and questions Socrates' strength compared to it, ultimately stating that Socrates will have to remain where he is, what is Polemarchus implying?" This question prompts critical thinking about power dynamics and conversational implications depicted.
Make a Question that Naturally Complements the Text's Focus: The text by Plato focuses on the visit to Piraeus and the interaction with Glaucon, and another question on this subject would be suitable. The text focuses on the interaction between Polemarchus and Socrates, but enough questions have already been asked about that subject to cover it completely. That leaves the visit to Piraeus as a subject for questions. The cause behind Socrates' visit to Piraeus is directly mentioned in the text and could make for a good recall question, so one question may be "Why did Socrates visit Piraeus with Glaucon in Plato's 'The Republic'?"

## Questions:
1.) Which goddess is Bendis equivalent to?
Answer: The Thracian Goddess Bendis is equivalent to Artemis.

2.) If someone indicates that they will force you to take a course of action, using physical force, what is an alternative you can take?
Answer: One viable alternative I can take is to persuade them to let me go. This, of course, requires that the other party does not refuse to listen to me.

3.) In Plato's "The Republic," in the dialogue where Polemarchus comments on the size of his group and questions Socrates' strength compared to it, ultimately stating that Socrates will have to remain where he is, what is Polemarchus implying?
Answer: Polemarchus is implying that since his group is stronger than Socrates, he can force Socrates to remain where he is.

4.) Why did Socrates visit Piraeus with Glaucon in Plato's "The Republic"?
Answer: Socrates visited Piraeus to offer up his prayers to the goddess Bendis, as well as to see in what manner the Thracians would celebrate the festival.

### Instruction:
Text details: Great Construction Projects Throughout History, by Marco Gonzalez

Text to make questions from:
\"\"\"
During the construction of the Panama Canal, a massive engineering feat completed in 1914, several challenges and achievements were noted. The canal, spanning approximately 50 miles, was designed to shorten the maritime route between the Atlantic and Pacific Oceans. Notably, the construction saw the use of innovative excavation techniques, with over 200 million cubic yards of earth removed. The project also faced significant health challenges, including combating malaria and yellow fever, which were overcome through extensive public health measures. The completion of the canal significantly impacted global trade, reducing the sea voyage from San Francisco to New York by around 8,000 miles.
\"\"\"

### Response:
## Reasoning and Thought Process:
Identify Key Topics: The paragraph details specific aspects of the Panama Canal's construction, focusing on its challenges, innovations, and impacts. Topics include construction challenges, health issues, excavation techniques, and the canal's impact on global trade.
Brainstorm and Develop Questions Testing Recall: Questions can be formed to recall factual data from the text. Example: "How much earth was excavated during the construction of the Panama Canal?"
Devise Questions Exploring Cause and Effect Relationships: This involves creating questions that explore how certain challenges led to specific solutions and impacts. Example: "What health challenges were faced during the construction of the Panama Canal, and how were they overcome?"
Create Questions Investigating Quantitative Values: Given the text's focus on concrete numbers, I will devise questions that require analyzing these figures. Example: "By how many miles did the Panama Canal reduce the sea voyage from San Francisco to New York?"
Make a Question that Naturally Complements the Text's Focus: The text by Marco Gonzalez focuses on the Panama Canal (and recall-oriented facts about it), and another question on this subject would be suitable. One small area that has not yet been tested, and which is an important detail mentioned in the text, is be: "In what year was the Panama Canal completed?"

## Questions:
1.) How much earth was excavated during the construction of the Panama Canal?
Answer: Over 200 million cubic yards of earth were excavated during the construction of the Panama Canal.

2.) What health challenges were faced during the construction of the Panama Canal, and how were they overcome?
Answer: The construction faced significant health challenges, notably malaria and yellow fever.

3.) By how many miles did the Panama Canal reduce the sea voyage from San Francisco to New York?
Answer: The completion of the Panama Canal reduced the sea voyage from San Francisco to New York by around 8,000 miles.

4.) In what year was the Panama Canal completed?
Answer: The Panama Canal's construction was completed in 1914.

### Instruction:
Text details: The Scale of the Solar System, by Rob Robertson

Text to plan questions from:
\"\"\"
If, then, we represent our earth as a little ball of one inch diameter, the sun would be a big globe nine feet across and 323 yards away, that is about a fifth of a mile, four or five minutes’ walking. The moon would be a small pea two feet and a half from the world.  Between earth and sun there would be the two inner planets, Mercury and Venus, at distances of one hundred and twenty-five and two hundred and fifty  yards from the sun. All round and about these bodies there would be  emptiness until you came to Mars, a hundred and seventy-five feet beyond the earth; Jupiter nearly a mile away, a foot in diameter; Saturn, a little smaller, two miles off; Uranus four miles off and Neptune six miles off. Then nothingness and nothingness except for small particles and drifting scraps of attenuated vapour for thousands of miles.
\"\"\"

### Response:
## Reasoning and thought process:
Identify Key Topics: The key topic in this paragraph is the scale of the solar system, which is presented using analogy to make the distances easier to conceive of.
Brainstorm and Develop Questions Testing Recall: Formulate questions that test the recall of the components of this analogy. Since the text is not provided while the question is being asked, I will have to include the analogy in the question. Example: "If the Earth had a diameter of only one inch, how far would the moon be from the Earth, if it (and its distance from the Earth) were also resized proportionally?"
Devise Questions Exploring Relationships Between Components: Generate questions that explore the relationship between different information in the excerpt. Notably, in this text the distances between planets are much greater than the planets' sizes. Example question: "How do the distances between
# ... truncated ...


import re
from .make_regenerate_answer_plan_grammar import make_regenerate_answer_plan_grammar
from llama_cpp import Llama
from .constants import LOGICAL_MODEL
from .strip_steps import strip_steps

# Question regeneration (triggered after a relevance-check fails on the question).
def make_regenerate_answer_plan(qatuple, dissenting_reasoning, logic_llm):
    retries = 0
    while retries < 5:
        decision_prompt = f"""You are an expert educational AI. Someone has provided an answer to a question based on a few paragraphs of text, but the answer may be incorrect due to contradicting the text or including erroneous information. Your task is to plan out and think through a correct answer to the question, ensuring it aligns with the information provided in the text.

Text: \"\"\"{qatuple[2]}\"\"\"

Question (based on text): \"\"\"{qatuple[0]}\"\"\"

Allegedly incorrect answer to the question (for fact-checking): \"\"\"{qatuple[1]}\"\"\"

Reasoning as to why the answer is incorrect: \"\"\"{strip_steps(dissenting_reasoning)}\"\"\"

For instance, if the text discusses the process of photosynthesis in plants, a question about the role of sunlight in photosynthesis, an answer stating that sunlight is not necessary for photosynthesis, and reasoning pointing out this contradiction, you might write:
Step 1. Analyze the Text: Focus on the details provided about photosynthesis in plants.
Step 2. Understand the Question: Grasp the question's emphasis on the role of sunlight in photosynthesis.
Step 3. Identify the Incorrect Part of the Answer: Note the incorrect claim about sunlight not being necessary, which contradicts the text. The plant text says "In photosynthesis, chlorophyll captures the sun's rays, providing the energy needed to build molecules of glucose from air and water."
Step 4. Plan a Corrected Answer: Devise a new answer emphasizing that sunlight is essential for photosynthesis, as explained in the text. One might write "Sunlight provides the energy required for photosynthetic organisms to convert carbon dioxide and water into glucose and oxygen."

Based on this example, plan out a revised version of the answer \"\"\"{qatuple[1]}\"\"\" with respect to the text and reasoning provided.

### Response:
## New answer plan:
"""
        completion = logic_llm(
            decision_prompt,
            max_tokens=4000,
            stop=["</s>", "# Input:"],
            echo=True,
            grammar=make_regenerate_answer_plan_grammar,
            temperature=0.2,
        )["choices"][0]["text"]

        # print("DEBUG\n\n")
        # print(completion)
        completion_pattern = re.compile(r"New answer plan:\n(.+)", re.DOTALL)
        correction = completion_pattern.search(completion).group(1)
        return correction.strip()

if __name__ == "__main__":  # test
    logic_llm = Llama(
        model_path=LOGICAL_MODEL,
        n_gqa=8,
        offload_kqv=True,
        n_ctx=4096,
        n_gpu_layers=1000,
    )  # load the logical LLM and offload everything
    inaccurate_qa_tuple = (
        "For how long has the concept of a spherical Earth been known to at least a limited number of intelligent people?",
        "The concept of a spherical Earth has been known for only about 1,000 years.",
        "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.",
    )

    dissenting_reasoning = """Step 1. Analyze the Text: focus on the details provided about the history of spherical Earth.
Step 2. Understand the Question: the question's focus is on how long the concept of a spherical Earth has been known to at least a limited number of intelligent people.
Step 3. Compare the First Part of the Answer with the Text: check if the text supports the claim that the concept of a spherical Earth has only been known for about 1,000 years. It does, so this part is accurate.
Step 4. Compare the Second Part of the Answer with the Text: check if the text contradicts the claim that the concept of a spherical Earth has been known to at least a limited number of intelligent people for longer than 1,000 years. The text indicates this knowledge predates 2500 years ago, so this part is inaccurate.
Step 5. Final Judgment: Since the answer is not entirely accurate, the answer is inaccurate.  It's important to note that the second paragraph of the text does indeed mention a limited number of intelligent people knowing about the spherical Earth over 2,000 years ago, but this information is contradicted by the rest of the text which suggests the concept was unknown to these same "intelligent" people."""
    # Old thing below. Maybe use if I want to test with bad reasoning
    #     """Step 1. Analyze the Text: Focus on the main points stated in the text about the Earth's diameter, distance from the sun, and its spherical shape.
    # Step 2. Understand the Question: The question is asking about specific scientific principles used to explain why we know the Earth is approximately 8000 miles in diameter and how its distance from the sun varies.
    # Step 3. Compare the First Part of the Answer with the Text: Check if the text states any scientific principles about measuring Earth's diameter. It doesn't, so this part seems irrelevant.
    # Step 4. Compare the Second Part of the Answer with the Text: Check if the text states how we know that the Earth is approximately 8000 miles in diameter. It does mention "measurements of its circumference made using GPS data" as the method for determining this, which aligns with what is mentioned in the answer, so this part of the answer reflects aspects of the text and is relevant.
    # Step 5. Compare the Third Part of the Answer with the text: Check if the text mentions any scientific principles explaining why Earth's distance from the sun varies. It doesn't mention an elliptical orbit or a varying point of closest approach and farthest departure, so this part seems irrelevant.
    # Step 6. Final Judgment: Since only part of the answer reflects aspects of the text (specifically, how we know Earth's diameter), it is "Irrelevant"."""

    print("Begin HGWELLS test")
    result = make_regenerate_answer_plan(
        inaccurate_qa_tuple, dissenting_reasoning, logic_llm
    )


import re
from .make_regenerate_answer_plan_grammar import make_regenerate_answer_plan_grammar
from llama_cpp import Llama
from .constants import LOGICAL_MODEL
from .strip_steps import strip_steps

# Question regeneration (triggered after a relevance-check fails on the question).
def make_regenerate_answer_plan(qatuple, dissenting_reasoning, logic_llm):
    retries = 0
    while retries < 5:
        decision_prompt = f"""You are an expert educational AI. Someone has provided an answer to a question based on a few paragraphs of text, but the answer may be incorrect due to contradicting the text or including erroneous information. Your task is to plan out and think through a correct answer to the question, ensuring it aligns with the information provided in the text.

Text: \"\"\"{qatuple[2]}\"\"\"

Question (based on text): \"\"\"{qatuple[0]}\"\"\"

Allegedly incorrect answer to the question (for fact-checking): \"\"\"{qatuple[1]}\"\"\"

Reasoning as to why the answer is incorrect: \"\"\"{strip_steps(dissenting_reasoning)}\"\"\"

For instance, if the text discusses the process of photosynthesis in plants, a question about the role of sunlight in photosynthesis, an answer stating that sunlight is not necessary for photosynthesis, and reasoning pointing out this contradiction, you might write:
Step 1. Analyze the Text: Focus on the details provided about photosynthesis in plants.
Step 2. Understand the Question: Grasp the question's emphasis on the role of sunlight in photosynthesis.
Step 3. Identify the Incorrect Part of the Answer: Note the incorrect claim about sunlight not being necessary, which contradicts the text. The plant text says "In photosynthesis, chlorophyll captures the sun's rays, providing the energy needed to build molecules of glucose from air and water."
Step 4. Plan a Corrected Answer: Devise a new answer emphasizing that sunlight is essential for photosynthesis, as explained in the text. One might write "Sunlight provides the energy required for photosynthetic organisms to convert carbon dioxide and water into glucose and oxygen."

Based on this example, plan out a revised version of the answer \"\"\"{qatuple[1]}\"\"\" with respect to the text and reasoning provided.

### Response:
## New answer plan:
"""
        completion = logic_llm(
            decision_prompt,
            max_tokens=4000,
            stop=["</s>", "# Input:"],
            echo=True,
            grammar=make_regenerate_answer_plan_grammar,
            temperature=0.2,
        )["choices"][0]["text"]

        # print("DEBUG\n\n")
        # print(completion)
        completion_pattern = re.compile(r"New answer plan:\n(.+)", re.DOTALL)
        correction = completion_pattern.search(completion).group(1)
        return correction.strip()

if __name__ == "__main__":  # test
    logic_llm = Llama(
        model_path=LOGICAL_MODEL,
        n_gqa=8,
        offload_kqv=True,
        n_ctx=4096,
        n_gpu_layers=1000,
    )  # load the logical LLM and offload everything
    inaccurate_qa_tuple = (
        "For how long has the concept of a spherical Earth been known to at least a limited number of intelligent people?",
        "The concept of a spherical Earth has been known for only about 1,000 years.",
        "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.",
    )

    dissenting_reasoning = """Step 1. Analyze the Text: focus on the details provided about the history of spherical Earth.
Step 2. Understand the Question: the question's focus is on how long the concept of a spherical Earth has been known to at least a limited number of intelligent people.
Step 3. Compare the First Part of the Answer with the Text: check if the text supports the claim that the concept of a spherical Earth has only been known for about 1,000 years. It does, so this part is accurate.
Step 4. Compare the Second Part of the Answer with the Text: check if the text contradicts the claim that the concept of a spherical Earth has been known to at least a limited number of intelligent people for longer than 1,000 years. The text indicates this knowledge predates 2500 years ago, so this part is inaccurate.


from llama_cpp import LlamaGrammar

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

regenerate_answer_grammar = LlamaGrammar.from_string(
    r"""

root ::= reasoning

reasoning ::= [^\n]+ "."
"""
)


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

# But frankly, I can just use that text fixer thing rather than using an LLM. MUCH faster that. ftfy I believe it was called.

# This does actually work decently though!
def proofread_output(text, logic_llm):
    """
    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
    """

    prompt = f"""You are an expert "mechanical editing" AI that is going to fix all typographical, grammatical, and spelling errors in a provided text, without making any other corrections. You will leave stylistic choices and everything else completely unchanged.

Text to edit: \"\"\"{text}\"\"\"

First, plan out your edit, step-by-step, identifying mistakes in the source text. Then, make the edit, changing only mechanical errors.

Example:
Step 1. Analyze the text. The text describes XYZ and is written in a style that etc...
Step 2. There is an error... etc...
...etc...
Step N. End of reasoning.

Begin Edit: [the full text, with all mechanical errors fixed]

Add as many steps as you need, then output "End of reasoning" as a final step. Then do the edit.

### Response:
## Edit plan:
"""
    completion = logic_llm(
        prompt,
        max_tokens=4000,
        stop=["</s>", "# Input:"],
        echo=True,
        grammar=proofread_output_grammar,
        temperature=0.2,
    )["choices"][0]["text"]
    # print("COMPLETION:\n\n----------------------")
    # print(completion)
    # print("\n------------------")

    # Extract plan
    response_pattern = re.compile(r"Begin Edit: (.+)", 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,
    )  # 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
    text = """ Given the question, its answer, and the provided primary character card, one compelling possibility for a scenario that makes sense is &Our setting will be a cozy bookstore, where Drummond is currently tending to the shelves. The secondary character who asks him a question will be an enthusiastic patron of this store, and they have come in today because they were drawn by the advertisement for a discussion on "Earth's Aging". Our secondary character is eager to understand how our perception of the Earth's age has changed throughout history.  The conversation would likely take place at the counter where Drummond is sorting books, with him pausing occasionally to help patrons browse or answer other questions as he sees fit."""

    print("Begin Drummond Test")
    edit = proofread_output(text, logic_llm)

# !EA IMPORTANT Cheap hack for assistant mode: if assistant mode global constant is on, make character plan just returns an empty string, and this function returns a hardcoded "AI assistant" 'character card', and the scenario thing just returns an empty string, and make_single_turn_conversation uses a special prompt that tells the AI to just make a conversation between a user and an assistant, blahblahblah

# Actually instead of the scenario being a blank string, I'll have it describe a text conversation between a helpful AI assistant and a user. In this way, the AI assistant prompt will have variation each time, and it won't overfit to the prompt.


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

# But frankly, I can just use that text fixer thing rather than using an LLM. MUCH faster that. ftfy I believe it was called.

# This does actually work decently though!
def proofread_output(text, logic_llm):
    """
    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
    """

    prompt = f"""You are an expert "mechanical editing" AI that is going to fix all typographical, grammatical, and spelling errors in a provided text, without making any other corrections. You will leave stylistic choices and everything else completely unchanged.

Text to edit: \"\"\"{text}\"\"\"

First, plan out your edit, step-by-step, identifying mistakes in the source text. Then, make the edit, changing only mechanical errors.

Example:
Step 1. Analyze the text. The text describes XYZ and is written in a style that etc...
Step 2. There is an error... etc...
...etc...
Step N. End of reasoning.

Begin Edit: [the full text, with all mechanical errors fixed]

Add as many steps as you need, then output "End of reasoning" as a final step. Then do the edit.

### Response:
## Edit plan:
"""
    completion = logic_llm(
        prompt,
        max_tokens=4000,
        stop=["</s>", "# Input:"],
        echo=True,
        grammar=proofread_output_grammar,
        temperature=0.2,
    )["choices"][0]["text"]
    # print("COMPLETION:\n\n----------------------")
    # print(completion)
    # print("\n------------------")

    # Extract plan
    response_pattern = re.compile(r"Begin Edit: (.+)", 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,
    )  # 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
    text = """ Given the question, its answer, and the provided primary character card, one compelling possibility for a scenario that makes sense is &Our setting will be a cozy bookstore, where Drummond is currently tending to the shelves. The secondary character who asks him a question will be an enthusiastic patron of this store, and they have come in today because they were drawn by the advertisement for a discussion on "Earth's Aging". Our secondary character is eager to understand how our perception of the Earth's age has changed throughout history.  The conversation would likely take place at the counter where Drummond is sorting books, with him pausing occasionally to help patrons browse or answer other questions as he sees fit."""

    print("Begin Drummond Test")
    edit = proofread_output(text, logic_llm)


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

def judge_paragraph(p, logic_llm):
    reached_decision = False
    max_retries = 0
    while not reached_decision and (max_retries <= 3):
        decision_prompt = f"""You are an expert educational AI that will make a determination as to whether the contents of the paragraph(s) provided are suitable for making educational questions based off of them; these questions should be able to test the knowledge in in the book. The book in question is {p[1]}, and you should keep this in mind when considering what kind of questions should be capable of being developed. If there is sufficiently deep information to make questions about, you will judge it suitable, even if the knowledge being tested does not reflect typical curricula. Essentially: you will determine if provided text is a table of contents, introductory paragraph for a book, etc., or if it actually contains real information that would be worthy to teach and make questions for an examination from. Your task includes first analyzing the text, thinking through whether or not good questions can be made from it.

Following this, at the very end of your response, you will write "Suitable" or "Not suitable". It is imperative that you write one of these two things, as your answer is being automatically processed by a regex, so it must match one of those two strings exactly.

### Instruction:
Text details: Through England On a Side Saddle, by Celia Fiennes

Text:
\"\"\"
The Project Gutenberg eBook of Through England on a side saddle

This ebook is for the use of anyone anywhere in the United States and
most other parts of the world at no cost and with almost no restrictions
whatsoever. You may copy it, give it away or re-use it under the terms
of the Project Gutenberg License included with this ebook or online
at www.gutenberg.org. If you are not located in the United States,
you will have to check the laws of the country where you are located
before using this eBook.

Title: Through England on a side saddle
        In the time of William and Mary

Author: Celia Fiennes

Contributor: Emily W. Griffiths

Release date: November 17, 2023 [eBook #72156]

Language: English

Original publication: London: Simpkin, Marshall & Co.; Hamilton, Adams & Co, 1888

Credits: Steve Mattern, Barry Abrahamsen, and the Online Distributed Proofreading Team at https://www.pgdp.net (This book was produced from images made available by the HathiTrust Digital Library.)

*** START OF THE PROJECT GUTENBERG EBOOK THROUGH ENGLAND ON A SIDE SADDLE ***

                            Through England

                            On a Side Saddle

                    IN THE TIME OF WILLIAM AND MARY

                           BEING THE DIARY OF

                            _CELIA FIENNES._

                             --------------

                        WITH AN INTRODUCTION BY

                        THE HON. MRS GRIFFITHS.

                         ---------------------

                                _LONDON:
                Field & Tuer, The Leadenhall Press, E.C.
            Simpkin, Marshall & Co.; Hamilton, Adams & Co._

                               ----------

          _New York: Scribner & Welford, 743 & 745, Broadway._

                                   --

                                  1888

------------------------------------------------------------------------
\"\"\"

### Response:
## Reasoning and thought process:
Step 1. Identify Paragraph Content: This paragraph is from the introductory section of "Through England on a Side Saddle" by Celia Fiennes, including copyright and publication information.
Step 2. Evaluate Educational Relevance: The paragraph contains legal, copyright, and publication information specific to the book's distribution and not its content.
Step 3. Assess Specific Contexts and Formats:
  - Purely Descriptive Lists: The paragraph is a descriptive list of publication details without educational content.
  - Dialogues or Quotes Without Context: Not applicable.
  - Ambiguous or Unclear Texts: The text is clear but lacks educational substance.
Step 4. Assess the Possibility of Formulating Questions: The paragraph's content does not lend itself to educational questions due to its focus on publication rather than substantive content.
Step 5. Determine Suitability for Educational Purposes: The paragraph lacks thematic, historical, scientific, or literary information and is thus unsuitable for creating educational questions.
Step 6. Check for Contextual Completeness: The paragraph, while complete in its context, does not offer educational insights or concepts.
Step 7. Final Judgment: Unsuitable for educational questions.

### Instruction:
Text details: Thus Spake Zaranthustra, by Friedrich Nietzsche

Text:
\"\"\"
When Zarathustra was thirty years old, he left his home and the lake of
his home, and went into the mountains. There he enjoyed his spirit and
solitude, and for ten years did not weary of it. But at last his heart
changed,—and rising one morning with the rosy dawn, he went before the
sun, and spake thus unto it:

Thou great star! What would be thy happiness if thou hadst not those for
whom thou shinest!

For ten years hast thou climbed hither unto my cave: thou wouldst have
wearied of thy light and of the journey, had it not been for me, mine
eagle, and my serpent.

But we awaited thee every morning, took from thee thine overflow and
blessed thee for it.

Lo! I am weary of my wisdom, like the bee that hath gathered too much
honey; I need hands outstretched to take it.

I would fain bestow and distribute, until the wise have once more become
joyous in their folly, and the poor happy in their riches.

Therefore must I descend into the deep: as thou doest in the
evening, when thou goest behind the sea, and givest light also to the
nether-world, thou exuberant star!

Like thee must I GO DOWN, as men say, to whom I shall descend.

Bless me, then, thou tranquil eye, that canst behold even the greatest
happiness without envy!

Bless the cup that is about to overflow, that the water may flow golden
out of it, and carry everywhere the reflection of thy bliss!

Lo! This cup is again going to empty itself, and Zarathustra is again
going to be a man.

Thus began Zarathustra's down-going.
\"\"\"

### Response:
## Reasoning and thought process:

Step 1. Identify Paragraph Content: The text is a philosophical excerpt from "Thus Spake Zarathustra" by Friedrich Nietzsche, describing Zarathustra's reflections and decisions.
Step 2. Evaluate Educational Relevance: The passage is a philosophical monologue, covering themes like meaning, wisdom, and human condition.
Step 3. Assess Specific Contexts and Formats:
  - Purely Descriptive Lists: Not applicable.
  - Dialogues or Quotes Without Context: The text, though in the form of a monologue, provides sufficient context for understanding.
  - Ambiguous or Unclear Texts: The text is metaphorically deep but not ambiguous; it can be interpreted for educational purposes.
Step 4. Assess the Possibility of Formulating Questions: The passage allows for various educational questions about philosophy, literature, and existential thought.
Step 5. Determine Suitability for Educational Purposes: The text offers substantial material for discussion and analysis, suitable for creating educational questions.
Step 6. Check for Contextual Completeness: The paragraph provides enough context to stand alone for educational discussion.
Step 7. Final Judgment: Suitable for educational questions.

### Instruction:
Text details: A Bizarre Day in the Life of Sarah Anderson, by Felicia Anderson

Text:
\"\"\"

1. Apples, 2. Oranges, 3. Bananas, 4. Grapes, 5. Papayas, 6. Pomegranates, 7. Cherries. Sarah wandered through the market. Amidst her thoughts, she overheard a conversation:

"Did you hear about the mayor's decision?" asked John.
"It's quite surprising, isn't it?" replied Emily, her voice tinged with disbelief.
"I know, right? But what can we do?" John sighed.

Sarah continued her shopping, her mind now on the mayor's mysterious decision.
\"\"\"

### Response:
## Reasoning and thought process:
Step 1. Identify Paragraph Content: The paragraph appears to be from a narrative, including a list and a snippet of overheard conversation.
Step 2. Evaluate Educational Relevance: The paragraph contains a simple list of fruits and an unrelated, context-less dialogue, offering little educational substance.
Step 3. Assess Specific Contexts and Formats:
  - Purely Descriptive Lists: The list is purely descriptive, providing no educational context or analytical significance. Further, there's no context about what it could be about.
  - Dialogues or Quotes Without Context: The conversation about the mayor's decision lacks context and background, making it difficult to derive any educational content.
  - Ambiguous or Unclear Texts: While not ambiguous, the paragraph's content does not contribute to a clear narrative or concept that has philosophical or educational relevance.
Step 4. Assess the Possibility of Formulating Questions: Due to the lack of contextual or thematic depth, formulating educational questions from this paragraph is challenging.
Step 5. Determine Suitability for Educational Purposes: The paragraph's lack of thematic depth and contextual clarity renders it unsuitable for creating educational questions.
Step 6. Check for Contextual Completeness: The paragraph fails to provide a complete context for the dialogue and the list, hindering any meaningful educational interpretation.
Step 7. Final Judgment: Unsuitable for educational questions.

### Instruction:
Text details: The Scale of the Solar System, by Rob Robertson

Text:
\"\"\"
If, then, we represent our earth as a little ball of one inch diameter, the sun would be a big globe nine feet across and 323 yards away, that is about a fifth of a mile, four or five minutes’ walking. The moon would be a small pea two feet and a half from the world.  Between earth and sun there would be the two inner planets, Mercury and Venus, at distances of one hundred and twenty-five and two hundred and fifty  yards from the sun. All round and about these bodies there would be  emptiness until you came to Mars, a hundred and seventy-five feet beyond the earth; Jupiter nearly a mile away, a foot in diameter; Saturn, a little smaller, two miles off; Uranus four miles off and Neptune six miles off. Then nothingness and nothingness except for small particles and drifting scraps of attenuated vapour for thousands of miles.
\"\"\"

### Response:
## Reasoning and thought process:
Step 1. Identify Paragraph Content: This paragraph is from Rob Robertson's "The Scale of the Solar System," describing a scale model of the solar system.
Step 2. Evaluate Educational Relevance: The paragraph contains analogies and information about the solar system's scale and distances between celestial bodies.
Step 3. Assess Specific Contexts and Formats:
  - Purely Descriptive Lists: While the paragraph lists celestial bodies and their distances, it does so to illustrate a scientific concept, providing educational value.
  - Dialogues or Quotes Without Context: Not applicable.
  - Ambiguous or Unclear Texts: The paragraph is clear and offers a concrete concept in astronomy.
Step 4. Assess the Possibility of Formulating Questions: The content enables the creation of educational questions about astronomy, scale, and the solar system's structure.
Step 5. Determine Suitability for Educational Purposes: The paragraph's scientific content makes it suitable for educational questions in astronomy and spatial understanding.
Step 6. Check for Contextual Completeness: The paragraph is contextually complete, allowing for independent understanding of the solar system's scale.
Step 7. Final Judgment: Suitable for educational questions.

### Instruction:
Text details: The Scale of the Solar System, by Rob Robertson

Text:
\"\"\"
In the world of science, there are countless mysteries and phenomena that elude easy explanation. For instance, certain forces and energies interact in ways that are not fully understood, shaping the universe in subtle and profound manners. These interactions often occur at levels beyond human perception, leaving much to speculation and theory. Various scientific disciplines attempt to explain these interactions, each offering unique perspectives but often lacking definitive answers. The vastness of these mysteries spans from the minuscule quantum realm to the expansive cosmos, hinting at complexities that challenge our current understanding.
\"\"\"

### Response:
## Reasoning and thought process:
Step 1. Identify Paragraph Content: The paragraph discusses the complexity and mystery of scientific phenomena, mentioning interactions of forces and energies in the universe.
Step 2. Evaluate Educational Relevance: The paragraph touches on scientific themes and makes a number of broad claims.
Step 3. Assess Specific Contexts and Formats:
  - Purely Descriptive Lists: Not applicable.
  - Dialogues or Quotes Without Context: Not applicable.
  - Ambiguous or Unclear Texts: The text is ambiguous and lacks clarity. It refers to scientific phenomena and theories without providing specific examples or concrete information, making it difficult to derive clear educational questions.
Step 4. Assess the Possibility of Formulating Questions: Due to its vagueness and lack of specific content, the paragraph does not lend itself well to formulating meaningful educational questions.
Step 5. Determine Suitability for Educational Purposes: The lack of clarity and specificity in the text renders it unsuitable for generating insightful educational questions.
Step 6. Check for Contextual Completeness: The paragraph, while complete in its presentation, fails to provide the necessary detail and clarity to be understood for educational purposes.
Step 7. Final Judgment: Unsuitable for educational questions due to its ambiguous and unclear content.

### Instruction:
Text details: {p[1]}

Text:
\"\"\"
{p[0]}
\"\"\"

Note that even blunt facts can be suitable for questions, and unconventional knowledge is not necessarily unsuitable. Fictional stories that contain strong morals or philosophy can also have good questions made from them. But legal notices and metadata are not suitable. Lists of information without the context needed for the question-maker to understand the text; quotes or dialogues without context or clear depth; or ambiguous content that isn't precise enough to "nail down" a solid question from, are not valid.

### Response:
## Reasoning and thought process (reason intelligently):
"""
        # print("DEBUG\n\n" + decision_prompt)
        completion = logic_llm(
            decision_prompt,
            max_tokens=6000,
            grammar=judge_paragraph_grammar,
            stop=["</s>", "# Input:"],
            echo=True,
            temperature=0.2,
        )["choices"][0]["text"]

        # print("DEBUG\n\n")
        # # print(completion)
        response_pattern = re.compile(
            r"Reasoning and thought process \(reason intelligently\):(.+)",
            re.DOTALL | re.IGNORECASE,
        )

        judgement_pattern = re.compile(
            r"Final Judgment:(.+)", re.DOTALL | re.IGNORECASE
        )
        try:
            response = response_pattern.search(completion).group(1)
            print(response)
            determination = judgement_pattern.search(response).group(1)
            print("\n\nDETERMINATION:\n------")
            print(determination)
            print("\n---------\n")
            if "unsuitable" in determination.lower():
                reached_decision = True
                return (None, p[1])
            elif "suitable" in determination.lower():
                return (p[0], p[1])
        except:
            pass
        max_retries += 1

if __name__ == "__main__":
    test = (
        "Slowly by degrees as one million of years followed another, this fiery scene would lose its eruptive incandescence.  The vapours in the sky would rain down and become less dense overhead; great slaggy cakes of solidifying rock would appear upon the surface of the molten sea, and sink under it, to be replaced by other floating masses.  The sun and moon growing now each more distant and each smaller, would rush with diminishing swiftness across the heavens. The moon now, because of its smaller size, would be already cooled far below incandescence, and would be alternately obstructing and reflecting the sunlight in a series of eclipses and full moons.\n\nAnd so with a tremendous slowness through the vastness of time, the earth would grow more and more like the earth on which we live, until at last an age would come when, in the cooling air, steam would begin to condense into clouds, and the first rain would fall hissing upon the first rocks below.  For endless millenia the greater part of the earth’s water would still be vaporized in the atmosphere, but there would now be hot streams running over the crystallizing rocks below and pools and lakes into which these streams would be carrying detritus and depositing sediment.",
        "A Short History of the World, by HG Wells, published 1922",
    )
    logic_llm = Llama(
        model_path=LOGICAL_MODEL,
        n_gqa=8,
        offload_kqv=True,
        n_ctx=7000,
        rope_freq_scale=0.33,
        n_gpu_layers=100,
        verbose=True,
        # n_gqa=8
    )
    right = 0
    d1 = judge_paragraph(test, logic_llm)
    if d1[0]:
        right += 1
    test2 = (
        """

  A

  COLLECTION

  OF

  LATIN MAXIMS & RULES,

  IN LAW AND EQUITY,

  SELECTED FROM THE MOST EMINENT AUTHORS, ON THE
  CIVIL, CANON, FEUDAL, ENGLISH AND SCOTS LAW,

  WITH

  AN ENGLISH TRANSLATION,

  AND

  AN APPENDIX

  OF REFERENCE TO THE AUTHORITIES FROM WHICH
  THE MAXIMS ARE SELECTED.

  BY

  PETER HALKERSTON, L.L.D.
  AUTHOR OF “THE COMPENDIUM OF THE FACULTY COLLECTION
  OF DECISIONS, &C.”

  PRINCIPLES, CAUSES, AND ELEMENTS, BEING UNKNOWN,
  THE SCIENCE WHEREOF THEY ARE, IS ALTOGETHER
  UNKNOWN.
            FORTESCUE b. 8.

  EDINBURGH:

  PRINTED FOR JOHN ANDERSON AND CO. ROYAL EXCHANGE;
  MACREDIE, SKELLY, AND CO. 34, PRINCES STREET; AND
  CHARLES HUNTER, LAW BOOKSELLER, LONDON.

  1823.

  Wm. BAYNE, Printer,
  James’s Court, Edinburgh.

""",
        "Collection of latin maxims & rules in law and equity",
    )

    d2 = judge_paragraph(test2, logic_llm)
    if not d2[0]:
        right += 1

    test3 = (
        """A communi observantia non est recedendum; et minime mutandæ sunt quæ
certam interpretationem habent.

     We must not depart rashly from common observance; and those
     points are by no means to be changed, which admit of a
     clear interpretation.

A digniori fieri debet denominatio et resolutio.

     Title and acquittal ought to proceed from the more worthy
     person.

A facto ad jus non datur consequentia.

     A fact does not necessarily constitute a right.

A non posse ad non esse sequitur argumentum necessarie negativè, licet
non affirmativè.

     From the impossibility of a thing to its nonentity, the
     argument or proof follows of necessity, negatively, though
     not affirmatively.

A principalioribus seu dignioribus est inchoandum.

     We must begin with the more noble, or more important
     matters; or in other words, we must attend to things of the
     greatest consequence.

A rescriptis valet argumentum.

     An argument or proof is valid, from a rescript or letter
     of a prince or emperor, making answers to petitions, or
     other applications; or more laconically, the king’s answer,
     wherein he signifies his pleasure, amounts to a law, and is
     not to be disputed.

A verbis legis non est recedendum.

     We must not depart from the letter of the law.

Ab assueti
# ... truncated ...


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

def judge_paragraph(p, logic_llm):
    reached_decision = False
    max_retries = 0
    while not reached_decision and (max_retries <= 3):
        decision_prompt = f"""You are an expert educational AI that will make a determination as to whether the contents of the paragraph(s) provided are suitable for making educational questions based off of them; these questions should be able to test the knowledge in in the book. The book in question is {p[1]}, and you should keep this in mind when considering what kind of questions should be capable of being developed. If there is sufficiently deep information to make questions about, you will judge it suitable, even if the knowledge being tested does not reflect typical curricula. Essentially: you will determine if provided text is a table of contents, introductory paragraph for a book, etc., or if it actually contains real information that would be worthy to teach and make questions for an examination from. Your task includes first analyzing the text, thinking through whether or not good questions can be made from it.

Following this, at the very end of your response, you will write "Suitable" or "Not suitable". It is imperative that you write one of these two things, as your answer is being automatically processed by a regex, so it must match one of those two strings exactly.

### Instruction:
Text details: Through England On a Side Saddle, by Celia Fiennes

Text:
\"\"\"
The Project Gutenberg eBook of Through England on a side saddle

This ebook is for the use of anyone anywhere in the United States and
most other parts of the world at no cost and with almost no restrictions
whatsoever. You may copy it, give it away or re-use it under the terms
of the Project Gutenberg License included with this ebook or online
at www.gutenberg.org. If you are not located in the United States,
you will have to check the laws of the country where you are located
before using this eBook.

Title: Through England on a side saddle
        In the time of William and Mary

Author: Celia Fiennes

Contributor: Emily W. Griffiths

Release date: November 17, 2023 [eBook #72156]

Language: English

Original publication: London: Simpkin, Marshall & Co.; Hamilton, Adams & Co, 1888

Credits: Steve Mattern, Barry Abrahamsen, and the Online Distributed Proofreading Team at https://www.pgdp.net (This book was produced from images made available by the HathiTrust Digital Library.)

*** START OF THE PROJECT GUTENBERG EBOOK THROUGH ENGLAND ON A SIDE SADDLE ***

                            Through England

                            On a Side Saddle

                    IN THE TIME OF WILLIAM AND MARY

                           BEING THE DIARY OF

                            _CELIA FIENNES._

                             --------------

                        WITH AN INTRODUCTION BY

                        THE HON. MRS GRIFFITHS.

                         ---------------------

                                _LONDON:
                Field & Tuer, The Leadenhall Press, E.C.
            Simpkin, Marshall & Co.; Hamilton, Adams & Co._

                               ----------

          _New York: Scribner & Welford, 743 & 745, Broadway._

                                   --

                                  1888

------------------------------------------------------------------------
\"\"\"

### Response:
## Reasoning and thought process:
Step 1. Identify Paragraph Content: This paragraph is from the introductory section of "Through England on a Side Saddle" by Celia Fiennes, including copyright and publication information.
Step 2. Evaluate Educational Relevance: The paragraph contains legal, copyright, and publication information specific to the book's distribution and not its content.
Step 3. Assess Specific Contexts and Formats:
  - Purely Descriptive Lists: The paragraph is a descriptive list of publication details without educational content.
  - Dialogues or Quotes Without Context: Not applicable.
  - Ambiguous or Unclear Texts: The text is clear but lacks educational substance.
Step 4. Assess the Possibility of Formulating Questions: The paragraph's content does not lend itself to educational questions due to its focus on publication rather than substantive content.
Step 5. Determine Suitability for Educational Purposes: The paragraph lacks thematic, historical, scientific, or literary information and is thus unsuitable for creating educational questions.
Step 6. Check for Contextual Completeness: The paragraph, while complete in its context, does not offer educational insights or concepts.
Step 7. Final Judgment: Unsuitable for educational questions.

### Instruction:
Text details: Thus Spake Zaranthustra, by Friedrich Nietzsche

Text:
\"\"\"
When Zarathustra was thirty years old, he left his home and the lake of
his home, and went into the mountains. There he enjoyed his spirit and
solitude, and for ten years did not weary of it. But at last his heart
changed,—and rising one morning with the rosy dawn, he went before the
sun, and spake thus unto it:

Thou great star! What would be thy happiness if thou hadst not those for
whom thou shinest!

For ten years hast thou climbed hither unto my cave: thou wouldst have
wearied of thy light and of the journey, had it not been for me, mine
eagle, and my serpent.

But we awaited thee every morning, took from thee thine overflow and
blessed thee for it.

Lo! I am weary of my wisdom, like the bee that hath gathered too much
honey; I need hands outstretched to take it.

I would fain bestow and distribute, until the wise have once more become
joyous in their folly, and the poor happy in their riches.

Therefore must I descend into the deep: as thou doest in the
evening, when thou goest behind the sea, and givest light also to the
nether-world, thou exuberant star!

Like thee must I GO DOWN, as men say, to whom I shall descend.

Bless me, then, thou tranquil eye, that canst behold even the greatest
happiness without envy!

Bless the cup that is about to overflow, that the water may flow golden
out of it, and carry everywhere the reflection of thy bliss!

Lo! This cup is again going to empty itself, and Zarathustra is again
going to be a man.

Thus began Zarathustra's down-going.
\"\"\"

### Response:
## Reasoning and thought process:

Step 1. Identify Paragraph Content: The text is a philosophical excerpt from "Thus Spake Zarathustra" by Friedrich Nietzsche, describing Zarathustra's reflections and decisions.
Step 2. Evaluate Educational Relevance: The passage is a philosophical monologue, covering themes like meaning, wisdom, and human condition.
Step 3. Assess Specific Contexts and Formats:
  - Purely Descriptive Lists: Not applicable.
  - Dialogues or Quotes Without Context: The text, though in the form of a monologue, provides sufficient context for understanding.
  - Ambiguous or Unclear Texts: The text is metaphorically deep but not ambiguous; it can be interpreted for educational purposes.
Step 4. Assess the Possibility of Formulating Questions: The passage allows for various educational questions about philosophy, literature, and existential thought.
Step 5. Determine Suitability for Educational Purposes: The text offers substantial material for discussion and analysis, suitable for creating educational questions.
Step 6. Check for Contextual Completeness: The paragraph provides enough context to stand alone for educational discussion.
Step 7. Final Judgment: Suitable for educational questions.

### Instruction:
Text details: A Bizarre Day in the Life of Sarah Anderson, by Felicia Anderson

Text:
\"\"\"

1. Apples, 2. Oranges, 3. Bananas, 4. Grapes, 5. Papayas, 6. Pomegranates, 7. Cherries. Sarah wandered through the market. Amidst her thoughts, she overheard a conversation:

"Did you hear about the mayor's decision?" asked John.
"It's quite surprising, isn't it?" replied Emily, her voice tinged with disbelief.
"I know, right? But what can we do?" John sighed.

Sarah continued her shopping, her mind now on the mayor's mysterious decision.
\"\"\"

### Response:
## Reasoning and thought process:
Step 1. Identify Paragraph Content: The paragraph appears to be from a narrative, including a list and a snippet of overheard conversation.
Step 2. Evaluate Educational Relevance: The paragraph contains a simple list of fruits and an unrelated, context-less dialogue, offering little educational substance.
Step 3. Assess Specific Contexts and Formats:
  - Purely Descriptive Lists: The list is purely descriptive, providing no educational context or analytical significance. Further, there's no context about what it could be about.
  - Dialogues or Quotes Without Context: The conversation about the mayor's decision lacks context and background, making it difficult to derive any educational content.
  - Ambiguous or Unclear Texts: While not ambiguous, the paragraph's content does not contribute to a clear narrative or concept that has philosophical or educational relevance.
Step 4. Assess the Possibility of Formulating Questions: Due to the lack of contextual or thematic depth, formulating educational questions from this paragraph is challenging.
Step 5. Determine Suitability for Educational Purposes: The paragraph's lack of thematic depth and contextual clarity renders it unsuitable for creating educational questions.
Step 6. Check for Contextual Completeness: The paragraph fails to provide a complete context for the dialogue and the list, hindering any meaningful educational interpretation.
Step 7. Final Judgment: Unsuitable for educational questions.

### Instruction:
Text details: The Scale of the Solar System, by Rob Robertson

Text:
\"\"\"
If, then, we represent our earth as a little ball of one inch diameter, the sun would be a big globe nine feet across and 323 yards away, that is about a fifth of a mile, four or five minutes’ walking. The moon would be a small pea two feet and a half from the world.  Between earth and sun there would be the two inner planets, Mercury and Venus, at distances of one hundred and twenty-five and two hundred and fifty  yards from the sun. All round and about these bodies there would be  emptiness until you came to Mars, a hundred and seventy-five feet beyond the earth; Jupiter nearly a mile away, a foot in diameter; Saturn, a little smaller, two miles off; Uranus four miles off and Neptune six miles off. Then nothingness and nothingness except for small particles and drifting scraps of attenuated vapour for thousands of miles.
\"\"\"

### Response:
## Reasoning and thought process:
Step 1. Identify Paragraph Content: This paragraph is from Rob Robertson's "The Scale of the Solar System," describing a scale model of the solar system.
Step 2. Evaluate Educational Relevance: The paragraph contains analogies and information about the solar system's scale and distances between celestial bodies.
Step 3. Assess Specific Contexts and Formats:
  - Purely Descriptive Lists: While the paragraph lists celestial bodies and their distances, it does so to illustrate a scientific concept, providing educational value.
  - Dialogues or Quotes Without Context: Not applicable.
  - Ambiguous or Unclear Texts: The paragraph is clear and offers a concrete concept in astronomy.
Step 4. Assess the Possibility of Formulating Questions: The content enables the creation of educational questions about astronomy, scale, and the solar system's structure.
Step 5. Determine Suitability for Educational Purposes: The paragraph's scientific content makes it suitable for educational questions in astronomy and spatial understanding.
Step 6. Check for Contextual Completeness: The paragraph is contextually complete, allowing for independent understanding of the solar system's scale.
Step 7. Final Judgment: Suitable for educational questions.

### Instruction:
Text details: The Scale of the Solar System, by Rob Robertson

Text:
\"\"\"
In the world of science, there are countless mysteries and phenomena that elude easy explanation. For instance, certain forces and energies interact in ways that are not fully understood, shaping the universe in subtle and profound manners. These interactions often occur at levels beyond human perception, leaving much to speculation and theory. Various scientific disciplines attempt to explain these interactions, each offering unique perspectives but often lacking definitive answers. The vastness of these mysteries spans from the minuscule quantum realm to the expansive cosmos, hinting at complexities that challenge our current understanding.
\"\"\"

### Response:
## Reasoning and thought process:
Step 1. Identify Paragraph Content: The paragraph discusses the complexity and mystery of scientific phenomena, mentioning interactions of forces and energies in the universe.
Step 2. Evaluate Educational Relevance: The paragraph touches on scientific themes and makes a number of broad claims.
Step 3. Assess Specific Contexts and Formats:
  - Purely Descriptive Lists: Not applicable.
  - Dialogues or Quotes Without Context: Not applicable.
  - Ambiguous or Unclear Texts: The text is ambiguous and lacks clarity. It refers to scientific phenomena and theories without providing specific examples or concrete information, making it difficult to derive clear educational questions.
Step 4. Assess the Possibility of Formulating Questions: Due to its vagueness and lack of specific content, the paragraph does not lend itself well to formulating meaningful educational questions.
Step 5. Determine Suitability for Educational Purposes: The lack of clarity and specificity in the text renders it unsuitable for generating insightful educational questions.
Step 6. Check for Contextual Completeness: The paragraph, while complete in its presentation, fails to provide the necessary detail and clarity to be understood for educational purposes.
Step 7. Final Judgment: Unsuitable for educational questions due to its ambiguous and unclear content.

### Instruction:
Text details: {p[1]}

Text:
\"\"\"
{p[0]}
\"\"\"

Note that even blunt facts can be suitable for questions, and unconventional knowledge is not necessarily unsuitable. Fictional stories that contain strong morals or philosophy can also have good questions made from them. But legal notices and metadata are not suitable. Lists of information without the context needed for the question-maker to understand the text; quotes or dialogues without context or clear depth; or ambiguous content that isn't precise enough to "nail down" a solid question from, are not valid.

### Response:
## Reasoning and thought process (reason intelligently):
"""
        # print("DEBUG\n\n" + decision_prompt)
        completion = logic_llm(
            decision_prompt,
            max_tokens=6000,
            grammar=judge_paragraph_grammar,
            stop=["</s>", "# Input:"],
            echo=True,
            temperature=0.2,
        )["choices"][0]["text"]

        # print("DEBUG\n\n")
        # # print(completion)
        response_pattern = re.compile(
            r"Reasoning and thought process \(reason intelligently\):(.+)",
            re.DOTALL | re.IGNORECASE,
        )

        judgement_pattern = re.compile(
            r"Final Judgment:(.+)", re.DOTALL | re.IGNORECASE
        )
        try:
            response = response_pattern.search(completion).group(1)
            print(response)
            determination = judgement_pattern.search(response).group(1)
            print("\n\nDETERMINATION:\n------")
            print(determination)
            print("\n---------\n")
            if "unsuitable" in determination.lower():
                reached_decision = True
                return (None, p[1])
            elif "suitable" in determination.lower():
                return (p[0], p[1])
        except:
            pass
        max_retries += 1

if __name__ == "__main__":
    test = (
        "Slowly by degrees as one million of years followed another, this fiery scene would lose its eruptive incandescence.  The vapours in the sky would rain down and become less dense overhead; great slaggy cakes of solidifying rock would appear upon the surface of the molten sea, and sink under it, to be replaced by other floating masses.  The sun and moon growing now each more distant and each smaller, would rush with diminishing swiftness across the heavens. The moon now, because of its smaller size, would be already cooled far below incandescence, and would be alternately obstructing and reflecting the sunlight in a series of eclipses and full moons.\n\nAnd so with a tremendous slowness through the vastness of time, the earth would grow more and more like the earth on which we live, until at last an age would come when, in the cooling air, steam would begin to condense into clouds, and the first rain would fall hissing upon the first rocks below.  For endless millenia the greater part of the earth’s water would still be vaporized in the atmosphere, but there would now be hot streams running over the crystallizing rocks below and pools and lakes into which these streams would be carrying detritus and depositing sediment.",
        "A Short History of the World, by HG Wells, published 1922",
    )
    logic_llm = Llama(
        model_path=LOGICAL_MODEL,
        n_gqa=8,
        offload_kqv=True,
        n_ctx=7000,
        rope_freq_scale=0.33,
        n_gpu_layers=100,
        verbose=True,
        # n_gqa=8
    )
    right = 0
    d1 = judge_paragraph(test, logic_llm)
    if d1[0]:


from llama_cpp import LlamaGrammar

# A GGBNF grammar that forces the model to output text in a particular format
thought_plan_grammar = LlamaGrammar.from_string(
    r"""

# Root rule defining the overall structure
root ::= step+ "\n"

# Step rule with some text (any characters except newline) followed by a period
step ::= "Step " [0-9]?[0-9] ". " ("Realize" | "Recognize" | "Conclude" | "Recall" | "Remember" | "Formulate" | "Decompose" | "Break down" | "Break" | "Therefore, the answer is" | "The answer is" | "Realise" | "Calculate" | "Understand" | "Note" | "The plan will") [^\n]+ "\n"

# Potential way forward: change these reasoning steps to use
# step ::= "Step " [0-9]?[0-9] ". " ("Realize" | "Recall" | "Remember" | "Formulate" | "Decompose" | "Break down" | "Break" | "Therefore, the answer is" | "The answer is" | "Realise" | "Calculate" | "Understand" | "Note" | "The plan will") [^\n]+ "\n"

"""
)

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

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