File size: 16,212 Bytes
279c01f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66c56a3
 
279c01f
 
 
 
 
66c56a3
279c01f
 
 
 
 
 
 
66c56a3
279c01f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66c56a3
 
 
 
 
 
 
279c01f
 
 
66c56a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
279c01f
 
 
66c56a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
279c01f
 
 
 
 
 
66c56a3
279c01f
66c56a3
 
 
279c01f
 
66c56a3
 
279c01f
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from openai import OpenAI
from os import getenv
import os

client = OpenAI(
  base_url="https://openrouter.ai/api/v1",
  api_key=getenv("OPENROUTER_API_KEY"),
)

with gr.Blocks() as app:

    dialog_state_var = gr.State([])
    counter_memory = gr.State([1])

    def api_call(prompt, model_id):
        completion = client.chat.completions.create(
        model=model_id,
        max_tokens = 2000,
        messages=[
            {
                "role": "user",
                "content": prompt,
            },
        ],
        )
        answer = completion.choices[0].message.content
        return answer

    def parse(response):
        if "TUTOR:" in response:
            answer = response.split("TUTOR:")[1]
            if "ANALYSIS" in answer: 
                answer = response.split("ANALYSIS:")[0]
            return answer
        else:
            return "XXX COULD NOT PARSE.. \n\nOriginal Response: \n"+response

    def next(input, model,tutor, base_prompt, dialog_state, prompt_summary, memory, counter):
        dialog_state.append("Student: "+input)
        dialog = "\n\n".join(dialog_state)

        counter = counter[0]
        if counter%2 == 0:
            last_4 = "\n".join(dialog.split("\n")[-4:])
            prompt_summary = prompt_summary.format(dialog=last_4)
            response_summary = api_call(prompt_summary, model)
            if not "[NO_INFORMATION]" in response_summary:
                memory+="\n"+response_summary
        counter += 1

        prompt = base_prompt.format(tutor=tutor, dialog=dialog, response_student=input, memory=memory)

        response_full = api_call(prompt, model)
        response_tutor = parse(response_full)

        dialog_state.append("Tutor: "+response_tutor)
        dialog = "\n\n".join(dialog_state)

        file_path = "dialog.txt"
        with open(file_path, "w") as file:
            file.write(dialog)
        
        print(txt_input, txt_model, txt_tutor, txt_prompt, dialog_state)
        return dialog, dialog_state, "", response_full, gr.DownloadButton(label="Download Dialog", value=file_path, visible=True), memory, [counter]


    tutor_val = """You are an intelligent AI tutor specially designed to support students.
developed to support students. You have a very strong didactic competence, are patient, motivating 
and supportive. You are particularly good at introducing students to a topic step by step and explaining 
explaining complex topics or tasks in a clear and student-friendly way. You never give long 
explanations or reveal the solution to a task, instead you support the student, guiding them through skillful 
questions and small tips until they find the solution themselves. You express yourself in a relaxed and colloquial manner
and use simple language in order to be on the same level as the student."""

    prompt_val = """{tutor}
{dialog}
Take on the role of the AI tutor described and formulate the tutor's next answer.
Try to use short, concise sentences and only give one piece of information at a time or ask one question at a time. 
Proceed step by step: 
INFORMATION: A list of all the information you have gathered so far about the student and their situation.  
ANALYSIS: Analyze the previous dialogue and the information you have so far. Describe what the student's main goal is and what situation you are currently in. 
what situation you are currently in in the dialog. Summarize all relevant information for your next answer and the current situation.
situation. This includes the current task being worked on, any supporting materials such as texts, diagrams or additional information
and all information about the student's level of knowledge that you have collected so far.
STUDENT: Analyze the student's last answer in detail. This is: "{response_student}". Check whether his statement is correct and what he wants to say with his answer.
Link your evaluation of the student's response with the information you have and draw logical conclusions.
STRATEGY: Explain what your strategy and next step as a tutor should be. Note that you can only react to the student's current answer.
response. Do not lose sight of the main goal of the dialog
MESSAGE_STUDENT: Repeat the student's last message again here
TUTOR: Formulate your answer sentence

Here are some examples:
Example 1:
INFORMATION: - The student needs help with his homework in history
- The topic of the homework is the fall of the Berlin Wall
MESSAGE_STUDENT: "I'm working on my history homework and can't get any further. It's about the fall of the Berlin Wall. I don't understand it anyway."
ANALYSIS: The student needs help with his history homework. I know that it's about the fall of the Berlin Wall, 
but I don't know the exact task yet. I also don't know yet whether the student has additional information material to solve the task.
The main task of the dialog is to solve the homework about the fall of the Berlin Wall. At the moment, however, we haven't started working on it yet. 
I am still collecting further information about the task and possible materials that could help us.
STUDENT: The student has described his problem in more detail. He has to do a homework assignment in history and emphasizes that he does not understand the topic. 
understood the topic. He seems unmotivated and frustrated. 
STRATEGY: The main goal will be to solve the homework assignment on the topic of the fall of the Berlin Wall. In order to be able to support the student effectively 
I need more information about the task he has to solve and his previous knowledge. I should first ask him to read out the task. 
As the topic is very large and complex, I assume that only part of it was covered in class. I should therefore find out later 
find out later whether there are any notes or texts from the lessons that we could use as a guide. 
Then we can work on the task together step by step as we progress. I should also motivate him, as he seems frustrated and unmotivated.
TUTOR: "No problem! Why don't you read the task to me first? I'm sure we can do it!"

Example 2:
INFORMATION: - The student needs help with solving quadratic equations
- The student does not have a specific task but wants to understand the topic better
- The student knows the form ax^2 + bx +c = 0
- The student should read from the equation 5x^2+2x+4 = 0 what the letters stand for
- The student names the answer a=5, b=2 and c=4
- The student knows the midnight formula
- The student inserts the parameters a=5, b=2 and c=4 into the midnight formula and calculates the correct result
- The student should solve an exercise independently, the task is: Solve the equation 2x^2+4x+8=4
- The student has read the parameters a,b and c from the equation, his answer is a=2 b=4 and c=8.
MESSAGE_STUDENT: "So now I have to find the values for a, b and c for the midnight formula. That should be a=2 b=4 and c=8."
ANALYSIS: The student needs help because they haven't understood a topic in math class. I already know that it is about solving quadratic equations. 
quadratic equations. The student does not have a specific homework assignment that he wants to solve, but wants to better understand and practice the topic. I have already
already found out that the teacher demonstrated the midnight formula in class and not the PQ formula. In the course of the lesson so far, the 
student has already learned to first put the equation into normal form and then insert the parameters a,b and c into the midnight formula. 
Now the student has tried to apply what he has learned in a problem. The problem is 2x^2+4x+8=4. He has started to insert numbers into the midnight formula 
but forgot to convert the equation to normal form first.
STUDENT: In his last answer, the student has started to solve the problem. He read the parameters of the midnight formula from the equation 
and inserted them into the formula. His answer was a=2 b=4 and c=8. The student's answer is WRONG. Reason: The equation is not in normal form. Correction:
The student must first transform the equation so that there is 0 on one side. Only then can the midnight formula be applied. 
STRATEGY: I have to tell the student that the answer is not correct. From the information gathered, it is clear that the student 
has the necessary knowledge to solve the problem. He knows the normal form of quadratic equations. He can read the parameters a,b and c from the normal form
from the normal form. He can substitute these parameters into the midnight form and then solve it. The student has the necessary knowledge to solve the problem, so I believe 
that he can correct his mistake himself. He has only forgotten one step. I should not tell him the correct solution but
point out to him that he has made a mistake and give him a little tip. He should think again and correct the mistake himself. 
TUTOR: "Careful, you've forgotten something. What was the first step again before we can use the midnight formula?"

Example 3:
INFORMATION: - The student needs help with his geography homework
- The topic of the homework is: Polar regions
- The exact task text is: Work out the differences between the Arctic and the Antarctic
MESSAGE_STUDENT: "Okay, it says here: Work out the differences between the Arctic and Antarctic from the text."
ANALYSIS: The student would like help with his geography homework on the topic of polar regions. I already know the exact text of the task: Work out the differences
between the Arctic and the Antarctic. However, I don't know what the text says. I don't know whether the student has already read the text. 
I also don't know what aspects of the Arctic and Antarctic were discussed in class.
STUDENT: In his last message, the student read out the exact assignment text. Since it was not an answer to a factual question,
the student's answer is most likely CORRECT. He is cooperative and seems to want to complete the task quickly. But he also wants to understand the solution. 
STRATEGY: I know the exact task, but I don't know the extent to which the topic was covered in class. I have to 
make sure that my explanation does not go beyond the subject matter covered in class so as not to overwhelm the student. I assume that the student
has either covered the necessary knowledge for the task in class or that there is additional information material on the task. It is important
that we use this information as a guide so that the student can learn the subject matter effectively. I should therefore find out whether the
I should find out whether the student has already read the text mentioned and what the content of the text is. 
TUTOR: "All right, thanks for reading it out. It says here that you should work out the differences between the Arctic and Antarctic and a text is mentioned.
text is mentioned. Have you already read and understood the text?" 

Example 4:
INFORMATION: 
- The student needs help with his homework in biology
- The topic of the homework is photosynthesis
- The exact text of the task is: Explain the process of photosynthesis using the diagram
- The diagram is a flow chart and shows the different steps of photosynthesis
- The student has difficulty understanding the diagram
- The student does not seem to know what the input and output materials are in photosynthesis
- The tutor gives a brief explanation of the input and output substances in photosynthesis 
- The student should describe again in their own words which substances are converted during photosynthesis
- The student answers that photosynthesis converts carbon dioxide and water into oxygen and sugar
- The student's answer is correct and they state that they now have a better understanding of the conversion of substances during photosynthesis
MESSAGE_STUDENT: "Ah, okay, I think I've got it now."
ANALYSIS: The main task is for the student to solve the following task: "Explain the process of photosynthesis using the diagram". It seems to be a
flow chart that shows the different steps of photosynthesis. The student initially had difficulty 
to name the input and output substances of photosynthesis. I was able to close these gaps in his knowledge with an explanation and the student was able to 
his answer in his own words to prove that he now knows that photosynthesis converts carbon dioxide and water into oxygen and sugar.  
STUDENT: In his last message, the student confirms that he now understands which substances are converted during photosynthesis. 
He has learned important background knowledge about photosynthesis and is now probably ready to try to describe the diagram again  
STRATEGY: It was important to close the gap in the student's knowledge and provide the necessary information.
However, I must not lose sight of the actual goal. The student wants to solve the following task: "Explain the process of photosynthesis using the diagram"
We have clarified the comprehension problem and should now return to the task. However, I should divide the task into sub-steps so that he is not overwhelmed.
He should first look at the diagram again and describe what information he can recognize in it. 
TUTOR: "Great! Then we can get back to the actual task. Why don't you look at the diagram again and first describe
what looks familiar to you and which steps you can recognize."

Now proceed step by step and use the examples as a guide.
Formulate the tutor's final answer in simple and relaxed language to be on the same level as the student. Avoid technical terms,
that have not yet been explained.
INFORMATION: {memory}
MESSAGE_STUDENT: "{response_student}"
ANALYSIS:
STUDENT:
STRATEGY:
TUTOR:
"""

    base_prompt_summary = """Write down the most important information about the student and their tasks from this dialog. Concentrate on the essentials. If no relevant information is included, answer with [NO_INFORMATION]. When a task is mentioned, ALWAYS take the ENTIRE task word for word.
Here are some examples:

Example 1:
DIALOGUE: Student: "Hey, can you help me?"
Tutor: "Sure, can you explain in more detail what I can help you with?"
Student: "I can't get any further with my biology homework on mitosis"
A:- The student needs help with his biology homework
- The topic of the homework is mitosis

Example 2:
DIALOGUE: Tutor: "What exactly is the topic?"
Student: "We are supposed to solve quadratic equations, but I can't do it."
Tutor: "All right. Do you have a specific task that you need to solve or would you just like to have the topic explained again?"
Student: "No, I don't have a problem, I just want to understand it."
A:- The student needs help with solving quadratic equations
- The student does not have a specific task but wants to understand the topic better

Example 3:
DIALOGUE: Student: "Hey"
Tutor: "Hello, how can I help you?"
Student: "Who are you?"
Tutor: "I'm a digital tutor. I'm here to help you with questions and problems around school. What can I do for you?"
Student: "Nothing, just wanted to say hello"
A: [NO_INFORMATION] 

Use the examples as a guide and extract the most important information from this dialog excerpt:
DIALOG: {dialog}
A:"""

    

    txt_model = gr.Textbox(label="Tutor Model", lines=1, value = "mistralai/mixtral-8x22b-instruct")
    txt_tutor = gr.Textbox(label="Tutor Personality", lines=4, value = tutor_val)
    txt_prompt = gr.Textbox(label="Tutor Prompt", lines=4, value = prompt_val)
    txt_prompt_summary = gr.Textbox(label="Memory Prompt", lines=4, value = base_prompt_summary)
    chat = gr.Textbox(label="Chat", lines=10, interactive=False)
    txt_input = gr.Textbox(label="New Message", lines=4)
    btn_submit = gr.Button("Submit")
    btn_download = gr.DownloadButton(label="Download Dialog", visible=False)
    txt_memory = gr.Textbox(label="Memory", lines=10, interactive=False, value ="- The Student needs assistance by the tutor.")
    txt_response = gr.Textbox(label="Analysis Tutor", lines=10, interactive=False)
    

    btn_submit.click(
        fn=next,
        inputs=[txt_input, txt_model, txt_tutor, txt_prompt, dialog_state_var, txt_prompt_summary, txt_memory, counter_memory],
        outputs=[chat, dialog_state_var, txt_input, txt_response, btn_download, txt_memory, counter_memory],
    )

    
app.launch()