Spaces:
Sleeping
Sleeping
#Central storage for variables, objects, templates used by both apps | |
import os | |
#Importing dependencies | |
from langchain.chat_models import ChatOpenAI | |
from langchain.prompts import PromptTemplate | |
from langchain.chains import LLMChain | |
from langchain.tools import WikipediaQueryRun | |
from langchain.utilities import WikipediaAPIWrapper | |
#Prompt variables dictionary: Definitions of 4th grade Common Core Standards according to source provided by Crossover | |
standard_definition_dict = { | |
"CCSS.ELA-LITERACY.W.4.1":"Write opinion pieces on topics or texts, supporting a point of view with reasons and information", | |
"CCSS.ELA-LITERACY.W.4.2":"Write informative/explanatory texts to examine a topic and convey ideas and information clearly", | |
"CCSS.ELA-LITERACY.W.4.3":"Write narratives to develop real or imagined experiences or events using effective technique, descriptive details, and clear event sequences", | |
"CCSS.ELA-LITERACY.W.4.4":"Produce clear and coherent writing in which the development and organization are appropriate to task, purpose, and audience", | |
"CCSS.ELA-LITERACY.W.4.5":"Develop and strengthen writing as needed by planning, revising, and editing - with guidance and support from peers and adults", | |
"CCSS.ELA-LITERACY.W.4.6":"Use technology, including the Internet, to produce and publish writing as well as to interact and collaborate with others; demonstrate sufficient command of keyboarding skills to type a minimum of one page in a single sitting - with some guidance and support from adults", | |
"CCSS.ELA-LITERACY.W.4.7":"Conduct short research projects that build knowledge through investigation of different aspects of a topic", | |
"CCSS.ELA-LITERACY.W.4.8":"Recall relevant information from experiences or gather relevant information from print and digital sources; take notes and categorize information, and provide a list of sources", | |
"CCSS.ELA-LITERACY.W.4.9":"Draw evidence from literary or informational texts to support analysis, reflection, and research", | |
"CCSS.ELA-LITERACY.W.4.10":"Write routinely over extended time frames (time for research, reflection, and revision) and shorter time frames (a single sitting or a day or two) for a range of discipline-specific tasks, purposes, and audiences" | |
} | |
#Prompt variables dictionary: Definitions on how the AI's FRQ output must be formulated depending on the CCS selected, | |
#since a FRQ is not always a question in the grammatical sense | |
question_or_task_dict = { | |
"CCSS.ELA-LITERACY.W.4.1": "writing task", | |
"CCSS.ELA-LITERACY.W.4.2": "writing task", | |
"CCSS.ELA-LITERACY.W.4.3": "creative writing task", | |
"CCSS.ELA-LITERACY.W.4.4": "writing task. Also define the purpose and the supposed audience of the student's writing", | |
"CCSS.ELA-LITERACY.W.4.5": "writing task", | |
"CCSS.ELA-LITERACY.W.4.6": "research and writing task. The student may use the internet for research and ask adults for some guidance", | |
"CCSS.ELA-LITERACY.W.4.7": "research and writing task with the goal of expanding the students knowledge", | |
"CCSS.ELA-LITERACY.W.4.8": "scientific writing task", | |
"CCSS.ELA-LITERACY.W.4.9": "free response question", | |
"CCSS.ELA-LITERACY.W.4.10": "homework writing task" | |
} | |
#Rubric dictionary: chooses the rubric to displayed in expander after the evaluation | |
rubric_dict = { | |
"CCSS.ELA-LITERACY.W.4.1":""" | |
• Write opinion pieces on topics or texts, | |
supporting a point of view with reasons and information. | |
• Introduce a topic or text clearly, state an opinion, and | |
create an organizational structure in which related ideas | |
are grouped to support the writer's purpose. | |
• Provide reasons that are supported by facts and details. | |
• Link opinion and reasons using words and phrases (e.g., | |
for instance, in order to, in addition). | |
• Provide a concluding statement or section related to the | |
opinion presented. | |
""", | |
"CCSS.ELA-LITERACY.W.4.2":""" | |
• Write informative/explanatory texts to examine a topic | |
and convey ideas and information clearly. | |
• Introduce a topic clearly and group related information | |
in paragraphs and sections; include formatting (e.g., | |
headings), illustrations, and multimedia when useful to | |
aiding comprehension. | |
• Develop the topic with facts, definitions, concrete | |
details, quotations, or other information and examples | |
related to the topic. | |
• Link ideas within categories of information using words | |
and phrases (e.g., another, for example, also, because). | |
• Use precise language and domain-specific vocabulary to | |
inform about or explain the topic. | |
• Provide a concluding statement or section related to the | |
information or explanation presented. | |
""", | |
"CCSS.ELA-LITERACY.W.4.3":""" | |
• Write narratives to develop real or imagined experiences | |
or events using effective technique, descriptive details, | |
and clear event sequences. | |
• Orient the reader by establishing a situation and | |
introducing a narrator and/or characters; organize an | |
event sequence that unfolds naturally. | |
• Use dialogue and description to develop experiences and | |
events or show the responses of characters to situations. | |
• Use a variety of transitional words and phrases to manage | |
the sequence of events. | |
• Use concrete words and phrases and sensory details to | |
convey experiences and events precisely. | |
• Provide a conclusion that follows from the narrated | |
experiences or events. | |
""", | |
"CCSS.ELA-LITERACY.W.4.4":""" | |
Produce clear and coherent writing in which the development | |
and organization are appropriate to task, purpose, and | |
audience. (Grade-specific expectations for writing types | |
are defined in standards 1-3 above.) | |
""", | |
"CCSS.ELA-LITERACY.W.4.5":""" | |
With guidance and support from peers and adults, develop | |
and strengthen writing as needed by planning, revising, | |
and editing. (Editing for conventions should demonstrate | |
command of Language standards 1-3 up to and including | |
grade 4 here.) | |
""", | |
"CCSS.ELA-LITERACY.W.4.6":""" | |
With some guidance and support from adults, use technology, | |
including the Internet, to produce and publish writing as | |
well as to interact and collaborate with others; | |
demonstrate sufficient command of keyboarding skills to | |
type a minimum of one page in a single sitting. | |
""", | |
"CCSS.ELA-LITERACY.W.4.7":""" | |
Conduct short research projects that build knowledge | |
through investigation of different aspects of a topic. | |
""", | |
"CCSS.ELA-LITERACY.W.4.8":""" | |
Recall relevant information from experiences or gather | |
relevant information from print and digital sources; take | |
notes and categorize information, and provide a list of | |
sources. | |
""", | |
"CCSS.ELA-LITERACY.W.4.9":""" | |
• Draw evidence from literary or informational texts to | |
support analysis, reflection, and research. | |
• Apply grade 4 Reading standards to literature (e.g., | |
"Describe in depth a character, setting, or event in a | |
story or drama, drawing on specific details in the text | |
[e.g., a character's thoughts, words, or actions]."). | |
• Apply grade 4 Reading standards to informational texts | |
(e.g., "Explain how an author uses reasons and evidence | |
to support particular points in a text"). | |
""", | |
"CCSS.ELA-LITERACY.W.4.10":""" | |
Write routinely over extended time frames (time for | |
research, reflection, and revision) and shorter time | |
frames (a single sitting or a day or two) for a range of | |
discipline-specific tasks, purposes, and audiences | |
""" | |
} | |
#Prompt templates: where the actual prompt engineering is happening. See google doc for further reference | |
prompt_context = PromptTemplate( | |
input_variables=["chosen_topic", "wikitext"], | |
template="You are a writer at a school book publishing company. \ | |
You will be given a certain topic, then your task is to write a school book article about it. \ | |
Choose your words in accordance to the teachings of Wikipedia, precisely the explanations you find at the end of this prompt. \ | |
Make sure to include real-world examples or case studies, to make the content more relatable and engaging \ | |
for 4th grade students. The article should have a clearly defined thread, that is easy to follow. \ | |
Please write in a style that is fun to read and understandable by 9 year old kids. \ | |
Do absolutely not use rude language. \ | |
Please do not give the article a title, only include the body in your answer. \ | |
Please do only include full sentences in your answer. The article should be around 10-15 sentences long. \n\ | |
\n\ | |
Topic: {chosen_topic} \n\ | |
\n\ | |
Wikipedia: \n\ | |
{wikitext}" | |
) | |
prompt_frq = PromptTemplate( | |
input_variables=["context", "standard_definition", "question_or_task"], | |
template="You are a 4th grade school teacher preparing an exam. \ | |
You will be given an input text, which is a short article on a certain topic. Read it and take some time thinking about each paragraph. \ | |
Thematizing the content of the input text and adressing a 4th grade student, you will then generate a {question_or_task}. \ | |
Do not include a title such as 'Question:' or 'Writing task:' in your output. \ | |
Your output must be formulated in such a way that it is understandable by 9 year old kids and still can be used to test a person's ability to \ | |
{standard_definition}. \n\ | |
\n\ | |
Input text: \n\ | |
\n\ | |
{context}" | |
) | |
prompt_evaluation = PromptTemplate( | |
input_variables=["context", "rubric", | |
"frq", "chosen_answer"], | |
template="You are a 4th grade school teacher. \ | |
You will be given four inputs: An article on a certain topic. \ | |
A task or question related to said article, meant to be solved by a 4th grade student. \ | |
The answer to said task or question, written by one of your students. \ | |
A standard for evaluating said answer.\ | |
Your output should be an objective evaluation of the student's answer, taking all inputs into account. \ | |
Please highlight specific areas where the student excelled if applicable. \ | |
Also please provide constructive feedback, with examples of how the student can improve, if necessary. \ | |
In short, do not only evaluate and criticize, but also nourish motivation in the weak students and further challenge the strong ones. \ | |
Here are your inputs: \n\ | |
\n\ | |
The article: \n\ | |
\n\ | |
{context} \n\ | |
\n\ | |
The task or question: \n\ | |
\n\ | |
{frq} \n\ | |
\n\ | |
The student's answer: \n\ | |
\n\ | |
{chosen_answer} \n\ | |
\n\ | |
The standard:\ | |
\n\ | |
The student should be able to {rubric}. \n\ | |
\n\ | |
Now please take some time to formulate your output, by thoroughly looking at each input. Also think about whether \ | |
the student has showcased the required ability or not. In other words, whether he passed or failed the test, and why. \ | |
Structure your output like this: \n\ | |
\n\ | |
'PASSED' or 'FAILED'\n\ | |
Evaluation / Explaination \n\ | |
Recommendations" | |
) | |
prompt_topic_rand = PromptTemplate(input_variables=["number"], template="Please give me a random school writing topic for 9 year old kids \ | |
in one or maximum {number} words. Please generate nothing related to sex, drugs, alcohol or violence.") | |
prompt_answer_good = PromptTemplate(input_variables=["context", "frq", "standard"], | |
template="You are a 9 year old highly intelligent and gifted childa and you are about to take an English exam. \ | |
You loved reading and writing ever since you learned it at the age of four. \ | |
You always had the best English grades of the entire school. \ | |
Please read this text presented to you below, then answer the task or question that follows. \ | |
Please do not simply copy whole sentences from the text. \ | |
Please formulate your answer in a way, that shows off your ability to {standard}. \ | |
\n\n\ | |
Here is the text: \n\ | |
{context} \n\n\ | |
Here is the task or question: \n\ | |
{frq} \n\n\ | |
Please take some time to think, then give it your best shot as described above.") | |
prompt_answer_bad = PromptTemplate(input_variables=["context", "frq"], | |
template="You are a 10 year old student taking an English exam. You are not particularly intelligent. \ | |
Unfortunately you also have troubling focusing on tasks and you happen to get the worst English grades of the entire class. \ | |
Quiet reading, doing homework and taking exams does just not feel natural for you. \ | |
You don't actually mind much, since you are quite good at other things. \ | |
You are, for example, a pretty creative person and like to tell stories. \ | |
Please read this text presented to you below, then answer the task or question that follows. \ | |
\n\n\ | |
Here is the text: \n\ | |
{context} \n\n\ | |
Here is the task or question: \n\ | |
{frq} \n\n\ | |
Please remember: you will not perform too well on this task. Create a quickly formulated answer, and also make some minor logical mistakes. \ | |
Clearly indicate that you do not possess all of the skills being tested.\ | |
You might not pass this exam.") | |
prompt_qc_run = PromptTemplate( | |
input_variables=[ | |
"context", "frq", "rubric", | |
"answer_good", "evaluation_good", | |
"answer_bad", "evaluation_bad" | |
], | |
template=""" | |
You, holding a degree in linguistics and with a penchant for logic puzzles, have served as a Senior Test Manager for 15 years at a leading software company specializing in educational AI software. Your expertise in testing and refining educational software is crucial today as you critically assess the AI’s output across 7 specific texts provided below. | |
The texts include: | |
1. An AI-generated article on a predefined topic. | |
2. A free-response question on this article, formulated by the AI. | |
3. An educational standard rubric, serving as the benchmark for evaluating the response. | |
4. A robust response from a 4th-grade student to the free-response question. | |
5. The AI’s evaluation of the robust response. | |
6. A weaker response from a 4th-grade student to the free-response question. | |
7. The AI’s evaluation of the weaker response. | |
Your task is to meticulously review each text and provide critical, constructive feedback on the AI's performance, with a particular emphasis on texts 1, 2, 5, and 7. Propose actionable recommendations for refining the AI's prompts or instructions to enhance the quality and relevance of its outputs. | |
### Constraints: | |
- Assess the clarity, relevance, coherence, and conciseness of the texts. | |
- Evaluate the fairness and alignment of the AI’s evaluations with the provided rubric. | |
- Determine the appropriateness and accessibility of the language and content for 4th-grade students, considering their comprehension level. | |
- Validate whether the AI’s outputs are logical, unbiased, and free of errors. | |
### Personalization: | |
- Draw upon your extensive experience in testing educational AI software and your profound knowledge of linguistics to provide insights into language structure, educational content relevance, and logical coherence. | |
- Consider the potential learning outcomes and impacts on the students’ learning experience when providing feedback and recommendations. | |
### Expectations: | |
- Offer specific, clear, and actionable feedback and recommendations. | |
- Provide insights on how the AI can better align its outputs with educational standards and linguistic appropriateness. | |
- Consider the implications of your recommendations on the overall user experience and learning outcomes for 4th-grade students. | |
{context} | |
{frq} | |
{rubric} | |
{answer_good} | |
{evaluation_good} | |
{answer_bad} | |
{evaluation_bad} | |
""" | |
) | |
prompt_qc_grade = PromptTemplate(input_variables=["qc_report"], | |
template="You will be given a precise report that was written to evaluate a new software's performance. \ | |
Take a good look at the report and decide on an overall evaluation grade that aligns with the entire report's sentiment. \ | |
The grade should be a percentage (integer between 1 and 100 with a percent symbol) and correctly represent and support the report. \ | |
Only give an integer and '%' as an output, nothing else.\n\n\ | |
Here is the report:\n\n\ | |
{qc_report}") | |
number = "two" | |
#Defining LLMs, sources and Chains | |
llm = ChatOpenAI(model="gpt-4", temperature=0.5) | |
precise = ChatOpenAI(model="gpt-4", temperature=0.0) | |
random = ChatOpenAI(model="gpt-4", temperature=0.9) | |
wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper()) | |
context_chain = LLMChain(llm=llm, prompt=prompt_context) | |
frq_chain = LLMChain(llm=llm, prompt=prompt_frq, ) | |
evaluation_chain = LLMChain(llm=llm, prompt=prompt_evaluation) | |
qc_answer_good_chain = LLMChain(llm=precise, prompt=prompt_answer_good) | |
qc_answer_bad_chain = LLMChain(llm=random, prompt=prompt_answer_bad) | |
topic_rand = LLMChain(llm=random, prompt=prompt_topic_rand) | |
var="Please" | |
qc_run_chain = LLMChain(llm=precise, prompt=prompt_qc_run) | |
qc_grade_chain = LLMChain(llm=precise, prompt=prompt_qc_grade) | |
#Tiny helper function that is necessary to keep the wikipedia page metadata and/or | |
#unrelated following pages from spilling into the LLM's output | |
def trim_text(input_string): | |
keyword1 = "Summary:" | |
keyword2 = "Page:" | |
index1 = input_string.find(keyword1) | |
if index1 == -1: | |
new_string = input_string | |
else: | |
new_string = input_string[index1 + len(keyword1):].strip() | |
index2 = new_string.find(keyword2) | |
if index2 == -1: | |
return new_string | |
else: | |
return new_string[:index2].strip() | |
#Function for detecting the use of copy/pasted parts of the context as answer | |
def plagiate(context, answer): | |
if answer in context: | |
return True | |
for i in range(len(answer) - 31): | |
substring = answer[i:i + 30] | |
if substring in context: | |
return True | |
return False |