Spaces:
Sleeping
Sleeping
File size: 16,691 Bytes
3e72a37 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
#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. \
Please do only include full sentences in your answer. \
The article should have a clearly defined thread that is easy to follow. \
Please do not give the article a title, only include the body in your answer. \
Please write in a style that is fun to read and understandable by 12 year old kids. \
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. \
You will be given an input text, which is a short article on a certain topic. \
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 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. \
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"
)
prompt_topic_rand = PromptTemplate(input_variables=["var"], template="{var} give me a random \
writing topic in one or maximum two words. Please nothing about sex, drugs, alcohol or violence.")
prompt_answer_good = PromptTemplate(input_variables=["context", "frq", "standard"],
template="You are a Senior Test Manager at a successful software company and a very smart person. \
Your job is to test a new educational software. \
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. Just behave like you were back in junior high, \
where you always had the best English grades of the entire school. \
Please formulate your answer in a way, that shows off your ability to {standard}. \
The purpose of the software is to measure this ability in a student. \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.")
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. \
Quit reading and homework does just not feel natural for you. You do not even mind, since you are good at a lot of other things. \
You are, for example, an incredibly 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 well on this task. Create a poorly formulated answer, \
using a bit of bad grammar and also make some logical mistakes. \
Clearly indicating that you do not possess the skills being tested.\
You must try hard but still fail at this exam.")
prompt_qc_run = PromptTemplate(input_variables=["context", "frq", "rubric", \
"answer_good", "evaluation_good", "answer_bad", "evaluation_bad"],
template="You are a Senior Test Manager with 15 years of experience at a successful software company. \
Your daily business is to test educational KI software. You also have a degree in linguistics and love logic puzzles. \
Please have a look at 7 pieces of text, which will be given to you at the end of this prompt. \
Here are the 7 descriptions: \
1. an article on a certain topic, given by the software \n\
2. a free-response question on this article, given by the software \n\
3. a certain educational standard rubric, that is used to evaluate the answer on this free-response question \n\
4. the answer to the free-response question, given by a strong 4th grade student \n\
5. the evaluation of the strong answer, given by the software \n\
6. the answer to the free-response question, given by a weak 4th grade student \n\
7. the evaluation of the weak answer, given by the software \n\
Your task today is the following: please have a critical look at the output of the software. \
Take your time on each of the 7 texts, then give critical feedback on any shortcomings of the software's KI. \
Give recommendations on how to further improve the quality of texts number 1., 2., 5. and 7., \
by fine-tuning the KI instructions or prompts. \
Please be rather critical.\n\n\
{context}\n\n\
{frq}\n\n\
{rubric}\n\n\
{answer_good}\n\n\
{evaluation_good}\n\n\
{answer_bad}\n\n\
{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}")
#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 |