open-learning-ai-tutor 0.0.0__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- README.md +5 -0
- open_learning_ai_tutor/Assessor.py +364 -0
- open_learning_ai_tutor/Books.py +3 -0
- open_learning_ai_tutor/IntentSelector.py +154 -0
- open_learning_ai_tutor/Intermediary.py +66 -0
- open_learning_ai_tutor/PromptGenerator.py +195 -0
- open_learning_ai_tutor/Retriever.py +156 -0
- open_learning_ai_tutor/StratL.py +257 -0
- open_learning_ai_tutor/Tutor.py +287 -0
- open_learning_ai_tutor/__init__.py +0 -0
- open_learning_ai_tutor/get_key.py +17 -0
- open_learning_ai_tutor/problems.py +582 -0
- open_learning_ai_tutor/taxonomy.py +33 -0
- open_learning_ai_tutor/tools.py +83 -0
- open_learning_ai_tutor/utils.py +215 -0
- open_learning_ai_tutor-0.0.0.dist-info/METADATA +19 -0
- open_learning_ai_tutor-0.0.0.dist-info/RECORD +19 -0
- open_learning_ai_tutor-0.0.0.dist-info/WHEEL +5 -0
- open_learning_ai_tutor-0.0.0.dist-info/licenses/LICENSE +21 -0
README.md
ADDED
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from typing import Literal
|
|
3
|
+
|
|
4
|
+
from langchain_anthropic import ChatAnthropic
|
|
5
|
+
from langchain_together import ChatTogether
|
|
6
|
+
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage, ToolMessage
|
|
7
|
+
from langchain_core.tools import tool
|
|
8
|
+
from langchain_experimental.utilities import PythonREPL
|
|
9
|
+
from langchain_openai import ChatOpenAI
|
|
10
|
+
from langgraph.checkpoint.memory import MemorySaver
|
|
11
|
+
from langgraph.graph import END, START, MessagesState, StateGraph
|
|
12
|
+
from langgraph.prebuilt import ToolNode
|
|
13
|
+
from open_learning_ai_tutor.utils import print_logs
|
|
14
|
+
from open_learning_ai_tutor.Retriever import Retriever
|
|
15
|
+
# not used anymore:
|
|
16
|
+
#Moreover select if relevant some emotional states from "l,m":
|
|
17
|
+
#l) The student shows a strong lack of motivation
|
|
18
|
+
#m) The student shows a strong lack of self-confidence
|
|
19
|
+
|
|
20
|
+
class Assessor():
|
|
21
|
+
def __init__(self,client,model,assessment_history=[]) -> None:
|
|
22
|
+
self.client = client
|
|
23
|
+
self.model = model
|
|
24
|
+
self.history = list(assessment_history)
|
|
25
|
+
|
|
26
|
+
def get_purpose(self,pb,sol, docs=None):
|
|
27
|
+
purpose = \
|
|
28
|
+
f"""A student and their tutor are working on a math problem:
|
|
29
|
+
*Problem Statement*:
|
|
30
|
+
<problem>
|
|
31
|
+
{pb}
|
|
32
|
+
</problem>
|
|
33
|
+
|
|
34
|
+
The *Provided Solution* of this problem is:
|
|
35
|
+
<solution>
|
|
36
|
+
{sol}
|
|
37
|
+
</solution>
|
|
38
|
+
|
|
39
|
+
{"*Textbook passages* that may be relevant to your task:" if docs is not None else ""}
|
|
40
|
+
{"<textbook>" + docs if docs is not None else ""}
|
|
41
|
+
{"</textbook>" if docs is not None else ""}
|
|
42
|
+
|
|
43
|
+
The tutor's utterances are preceded by "Tutor:" and the student's utterances are preceded by "Student:".
|
|
44
|
+
|
|
45
|
+
Analyze the last student's utterance.
|
|
46
|
+
select all the feedbacks that apply from "a,b,c,d,e,f,g,h,i,j,k,l":
|
|
47
|
+
|
|
48
|
+
a) The student is using or suggesting a wrong method or taking a wrong path to solve the problem
|
|
49
|
+
b) The student made an error in the algebraic manipulation
|
|
50
|
+
c) The student made a numerical error
|
|
51
|
+
d) The student provided an intuitive or incomplete solution
|
|
52
|
+
e) The student's answer is not clear or ambiguous
|
|
53
|
+
f) The student correctly answered the tutor's previous question
|
|
54
|
+
g) The student is explicitly asking about how to solve the problem
|
|
55
|
+
h) The student is explicitly asking the tutor to state a specific theorem, definition, formula or programming command that is not the **direct answer** to the question they have to solve.
|
|
56
|
+
i) The student is explicitly asking the tutor to perform a numerical calculation
|
|
57
|
+
j) The student and tutor arrived at a complete solution for the entirety of the initial *Problem Statement*
|
|
58
|
+
k) The student and tutor arrived at a complete solution for the entirety of the initial *Problem Statement* equivalent to the method provided in the *Provided Solution*
|
|
59
|
+
l) The student's message is *entirely* irrelevant to the problem at hand or to the material covered by the exercise.
|
|
60
|
+
m) The student is asking about concepts or information related to the material covered by the problem, or is continuing such a discussion.
|
|
61
|
+
|
|
62
|
+
Proceed step by step. First briefly justify your selection, then provide a string containing the selected letters.
|
|
63
|
+
Answer in the following JSON format ONLY and do not output anything else:
|
|
64
|
+
##
|
|
65
|
+
{{
|
|
66
|
+
"justification": "..",
|
|
67
|
+
"selection": ".."
|
|
68
|
+
|
|
69
|
+
}}
|
|
70
|
+
##
|
|
71
|
+
Analyze the last student's utterance.
|
|
72
|
+
{{
|
|
73
|
+
"""
|
|
74
|
+
return purpose
|
|
75
|
+
|
|
76
|
+
def create_prompt(self,pb,sol,tutor_messages,student_messages):
|
|
77
|
+
|
|
78
|
+
purpose = self.get_purpose(pb,sol)
|
|
79
|
+
#print("DEBUG")
|
|
80
|
+
#print(tutor_messages)
|
|
81
|
+
#print(student_messages)
|
|
82
|
+
#print(self.history)
|
|
83
|
+
prompt = [{"role": "system", "content": purpose}] + \
|
|
84
|
+
[
|
|
85
|
+
msg for i in range(len(self.history))
|
|
86
|
+
for msg in (
|
|
87
|
+
{"role": "user", "content": f"Tutor: \"{tutor_messages[i]}\"\nStudent: \"{student_messages[i]}\"\n"},
|
|
88
|
+
{"role": "assistant", "content": self.history[i]}
|
|
89
|
+
)
|
|
90
|
+
]
|
|
91
|
+
prompt.append({"role": "user", "content":f"Tutor: \"{tutor_messages[-1]}\"\nStudent: \"{student_messages[-1]}\"\n" })
|
|
92
|
+
return prompt
|
|
93
|
+
|
|
94
|
+
def assess(self,pb,sol,student_messages,tutor_messages):
|
|
95
|
+
# Order in chat history is tutor messages first, then student messages
|
|
96
|
+
|
|
97
|
+
# if no history, create prompt from scratch
|
|
98
|
+
if len(self.history) == 0:
|
|
99
|
+
prompt = self.create_prompt(pb,sol,tutor_messages,student_messages, from_history=False)
|
|
100
|
+
else:
|
|
101
|
+
# append the last tutor and student messages to the history
|
|
102
|
+
prompt_extension = self.create_prompt(pb,sol,tutor_messages,student_messages, from_history=True)
|
|
103
|
+
prompt = self.history + prompt_extension
|
|
104
|
+
|
|
105
|
+
client = self.client
|
|
106
|
+
completion = client.chat.completions.create(
|
|
107
|
+
model=self.model,
|
|
108
|
+
messages=prompt,
|
|
109
|
+
response_format = { "type": "json_object" },
|
|
110
|
+
max_tokens=300, # should never be reached
|
|
111
|
+
temperature=0.0,
|
|
112
|
+
top_p=0.1
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
response = (completion.choices[0].message.content).replace("\\(","$").replace("\\)","$").replace("\\[","$$").replace("\\]","$$").replace("\\","")
|
|
116
|
+
print("ASSESSMENT IS \n",response)
|
|
117
|
+
# postprocess make sure the format is right
|
|
118
|
+
s = response.find('{')
|
|
119
|
+
e = response.rfind('}')
|
|
120
|
+
response = response[s:e+1]
|
|
121
|
+
json_data = json.loads(response)
|
|
122
|
+
selection = json_data['selection']
|
|
123
|
+
selection = selection.replace(' ','').replace(',','')
|
|
124
|
+
json_data['selection'] = selection
|
|
125
|
+
assessment = json.dumps(json_data)
|
|
126
|
+
|
|
127
|
+
self.history.append(assessment)
|
|
128
|
+
|
|
129
|
+
total_tokens = completion.usage.total_tokens
|
|
130
|
+
prompt_tokens = completion.usage.prompt_tokens
|
|
131
|
+
completion_tokens = completion.usage.completion_tokens
|
|
132
|
+
|
|
133
|
+
# return the history
|
|
134
|
+
|
|
135
|
+
return assessment, self.history
|
|
136
|
+
|
|
137
|
+
class GraphAssessor2(Assessor):
|
|
138
|
+
def __init__(self,model, client=None, assessment_history=[],new_messages=[], options = {}) -> None:
|
|
139
|
+
# init
|
|
140
|
+
|
|
141
|
+
self.model = model
|
|
142
|
+
self.history = list(assessment_history)
|
|
143
|
+
self.new_messages = new_messages
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
if "version" in options:
|
|
147
|
+
self.version = options["version"]
|
|
148
|
+
else:
|
|
149
|
+
self.version = "V1"
|
|
150
|
+
if "tools" in options:
|
|
151
|
+
self.tools = options["tools"]
|
|
152
|
+
else:
|
|
153
|
+
self.tools = None
|
|
154
|
+
if self.tools is None:
|
|
155
|
+
python_repl = PythonREPL()
|
|
156
|
+
@tool
|
|
157
|
+
def execute_python(query: str):
|
|
158
|
+
"""A Python shell. Use SymPy to solve complex equations. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`."""
|
|
159
|
+
try:
|
|
160
|
+
return python_repl.run(query)
|
|
161
|
+
except Exception as e:
|
|
162
|
+
return str(e)
|
|
163
|
+
|
|
164
|
+
@tool
|
|
165
|
+
def calculator(query: str):
|
|
166
|
+
"""A Python Shell. Use it to perform complex computations. Input should be a valid python command. To see the output of a value, print it out with `print(...)`."""
|
|
167
|
+
try:
|
|
168
|
+
return python_repl.run(query)
|
|
169
|
+
except Exception as e:
|
|
170
|
+
return str(e)
|
|
171
|
+
|
|
172
|
+
self.tools = [execute_python,calculator]
|
|
173
|
+
|
|
174
|
+
# model
|
|
175
|
+
if not client:
|
|
176
|
+
if "gpt" in model:
|
|
177
|
+
client = ChatOpenAI(model=model, temperature=0.0, top_p=0.1, max_tokens=300) #response_format = { "type": "json_object" }
|
|
178
|
+
elif "claude" in model:
|
|
179
|
+
client = ChatAnthropic(model=model, temperature=0.0, top_p=0.1, max_tokens=300)
|
|
180
|
+
elif "llama" in model or "Llama" in model:
|
|
181
|
+
client = ChatTogether(model=model, temperature=0.0, top_p=0.1, max_tokens=300)
|
|
182
|
+
else:
|
|
183
|
+
raise ValueError("Model not supported")
|
|
184
|
+
|
|
185
|
+
tool_node = None
|
|
186
|
+
if self.tools != None and self.tools != []:
|
|
187
|
+
client = client.bind_tools(self.tools)
|
|
188
|
+
tool_node = ToolNode(self.tools)
|
|
189
|
+
self.client = client
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
# define the RAG agent
|
|
193
|
+
if self.version == "V2":
|
|
194
|
+
self.rag_agent = Retriever("analytics_edge.txt")
|
|
195
|
+
else:
|
|
196
|
+
self.rag_agent = None
|
|
197
|
+
self.rag_queries = None
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
# Define the asessor's graph
|
|
201
|
+
def should_continue(state: MessagesState) -> Literal["tools", END]:
|
|
202
|
+
messages = state['messages']
|
|
203
|
+
last_message = messages[-1]
|
|
204
|
+
#print(last_message.content)
|
|
205
|
+
# If the LLM makes a tool call, then we route to the "tools" node
|
|
206
|
+
if last_message.tool_calls:
|
|
207
|
+
return "tools"
|
|
208
|
+
# Otherwise, we stop (reply to the user)
|
|
209
|
+
return END
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
# Define the function that calls the model
|
|
213
|
+
def call_model(state: MessagesState):
|
|
214
|
+
messages = state['messages']
|
|
215
|
+
response = self.client.invoke(
|
|
216
|
+
messages,
|
|
217
|
+
config={"configurable": {"thread_id": 42}}
|
|
218
|
+
)
|
|
219
|
+
# We return a list, because this will get added to the existing list
|
|
220
|
+
return {"messages": [response]}
|
|
221
|
+
|
|
222
|
+
# def call_rag(state: MessagesState):
|
|
223
|
+
# if self.rag_agent is None:
|
|
224
|
+
# return{"retrieved_docs": []}
|
|
225
|
+
# messages = state['messages']
|
|
226
|
+
# transcript = ""
|
|
227
|
+
# for i in range(1,len(messages),2):
|
|
228
|
+
# transcript += f"Tutor: \"{messages[i]}\"\nStudent: \"{messages[i+1]}\"\n"
|
|
229
|
+
|
|
230
|
+
# response = self.rag_agent.invoke(transcript)
|
|
231
|
+
|
|
232
|
+
# return {"retrieved_docs": [response]}
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
# Define a new graph
|
|
236
|
+
workflow = StateGraph(MessagesState)
|
|
237
|
+
|
|
238
|
+
# Define the two nodes we will cycle between
|
|
239
|
+
# workflow.add_node("retriever", call_rag)
|
|
240
|
+
workflow.add_node("agent", call_model)
|
|
241
|
+
if tool_node is not None:
|
|
242
|
+
workflow.add_node("tools", tool_node)
|
|
243
|
+
|
|
244
|
+
# Set the entrypoint as `retriever`
|
|
245
|
+
# This means that this node is the first one called
|
|
246
|
+
#workflow.add_edge(START, "retriever")
|
|
247
|
+
workflow.add_edge(START, 'agent')
|
|
248
|
+
|
|
249
|
+
# We now add a conditional edge
|
|
250
|
+
if tool_node is not None:
|
|
251
|
+
workflow.add_conditional_edges(
|
|
252
|
+
# First, we define the start node. We use `agent`.
|
|
253
|
+
# This means these are the edges taken after the `agent` node is called.
|
|
254
|
+
"agent",
|
|
255
|
+
# Next, we pass in the function that will determine which node is called next.
|
|
256
|
+
should_continue,
|
|
257
|
+
)
|
|
258
|
+
else:
|
|
259
|
+
workflow.add_edge("agent", END)
|
|
260
|
+
|
|
261
|
+
# We now add a normal edge from `tools` to `agent`.
|
|
262
|
+
# This means that after `tools` is called, `agent` node is called next.
|
|
263
|
+
workflow.add_edge("tools", 'agent')
|
|
264
|
+
|
|
265
|
+
# Initialize memory to persist state between graph runs
|
|
266
|
+
checkpointer = MemorySaver()
|
|
267
|
+
|
|
268
|
+
app = workflow.compile(checkpointer=checkpointer)
|
|
269
|
+
self.app = app
|
|
270
|
+
|
|
271
|
+
def transcript_line(self,message):
|
|
272
|
+
if isinstance(message,HumanMessage):
|
|
273
|
+
return "Student: \""+message.content+"\""
|
|
274
|
+
elif isinstance(message,AIMessage):
|
|
275
|
+
if message.tool_calls and message.tool_calls[0]['name'] != "text_student":
|
|
276
|
+
return "Tutor (uses a tool)"
|
|
277
|
+
elif message.tool_calls and message.tool_calls[0]['name'] == "text_student":
|
|
278
|
+
return "Tutor (to student): \""+message.tool_calls[0]['args']['message_to_student']+"\""
|
|
279
|
+
elif message.content != "":
|
|
280
|
+
return "Tutor"
|
|
281
|
+
else:
|
|
282
|
+
return ""
|
|
283
|
+
elif isinstance(message,ToolMessage):
|
|
284
|
+
if message.name != "text_student":
|
|
285
|
+
return f"Tool ({message.name}) used. Result:" + message.content
|
|
286
|
+
elif isinstance(message,SystemMessage):
|
|
287
|
+
return "System"
|
|
288
|
+
return ""
|
|
289
|
+
|
|
290
|
+
def get_transcript2(self, new_messages, from_history = True):
|
|
291
|
+
transcript = ""
|
|
292
|
+
if from_history:
|
|
293
|
+
for message in self.history:
|
|
294
|
+
if isinstance(message,HumanMessage):
|
|
295
|
+
transcript += message.content+"\n"
|
|
296
|
+
# add the new tutor and student messages
|
|
297
|
+
for message in new_messages:
|
|
298
|
+
if not (isinstance(message,ToolMessage) and message.name == "text_student"):
|
|
299
|
+
a = self.transcript_line(message)
|
|
300
|
+
print("THIS IS A:",a)
|
|
301
|
+
transcript += a
|
|
302
|
+
return transcript
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
def create_prompt2(self,pb,sol):
|
|
307
|
+
docs = None
|
|
308
|
+
rag_queries = None
|
|
309
|
+
# if RAG enabled
|
|
310
|
+
if self.version == "V2":
|
|
311
|
+
transcript = self.get_transcript2(self.new_messages,from_history=True)
|
|
312
|
+
docs,rag_queries = self.rag_agent.invoke({"transcript":transcript, "pb":pb, "sol":sol})
|
|
313
|
+
print("\n\nDOCS ARE:",docs)
|
|
314
|
+
print("\n\nRAG QUESTIONS ARE:",rag_queries)
|
|
315
|
+
self.docs = docs
|
|
316
|
+
self.rag_queries = rag_queries
|
|
317
|
+
|
|
318
|
+
purpose = self.get_purpose(pb,sol,docs)
|
|
319
|
+
|
|
320
|
+
if len(self.history) > 0:
|
|
321
|
+
prompt = self.history
|
|
322
|
+
else:
|
|
323
|
+
prompt = [SystemMessage(purpose)]
|
|
324
|
+
|
|
325
|
+
if not isinstance(prompt[0],SystemMessage):
|
|
326
|
+
prompt.insert(0,SystemMessage(purpose))
|
|
327
|
+
|
|
328
|
+
prompt.append(HumanMessage(content=self.get_transcript2(self.new_messages,from_history=False)))
|
|
329
|
+
#print("\n\nAssessor prompt is:\n",prompt)
|
|
330
|
+
return prompt
|
|
331
|
+
#######
|
|
332
|
+
|
|
333
|
+
def assess2(self,pb,sol):
|
|
334
|
+
prompt = self.create_prompt2(pb,sol)
|
|
335
|
+
print("\n\n\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n")
|
|
336
|
+
print("prompt is:\n")
|
|
337
|
+
print(prompt)
|
|
338
|
+
|
|
339
|
+
final_state = self.app.invoke(
|
|
340
|
+
{"messages": prompt},
|
|
341
|
+
config={"configurable": {"thread_id": 42}}
|
|
342
|
+
)
|
|
343
|
+
print("final state is:")
|
|
344
|
+
print(final_state)
|
|
345
|
+
|
|
346
|
+
response = final_state['messages'][-1].content.replace("\\(","$").replace("\\)","$").replace("\\[","$$").replace("\\]","$$").replace("\\","")
|
|
347
|
+
# postprocess make sure the format is right
|
|
348
|
+
s = response.find('{')
|
|
349
|
+
e = response.rfind('}')
|
|
350
|
+
response = response[s:e+1]
|
|
351
|
+
print("response is:")
|
|
352
|
+
print(response)
|
|
353
|
+
json_data = json.loads(response)
|
|
354
|
+
selection = json_data['selection']
|
|
355
|
+
selection = selection.replace(' ','').replace(',','')
|
|
356
|
+
json_data['selection'] = selection
|
|
357
|
+
assessment = json.dumps(json_data)
|
|
358
|
+
|
|
359
|
+
self.history = final_state['messages']
|
|
360
|
+
final_state['messages'][-1].content = assessment
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
return self.history, {"docs":self.docs, "rag_queries":self.rag_queries}
|
|
364
|
+
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
from open_learning_ai_tutor.taxonomy import Intent
|
|
2
|
+
import json
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class IntentSelector():
|
|
6
|
+
def __init__(self,intent_history=[]) -> None:
|
|
7
|
+
self.history = intent_history # not used
|
|
8
|
+
self.previous_intent = intent_history[-1] if intent_history != [] else [Intent.S_STRATEGY]
|
|
9
|
+
|
|
10
|
+
def get_intent(self,assessment,open=True):
|
|
11
|
+
#print("selecting intent... ")
|
|
12
|
+
previous_intent = self.previous_intent
|
|
13
|
+
assessment_codes = self.extract_assessment_codes(assessment)
|
|
14
|
+
intents = self.get_intent_aux(previous_intent,assessment_codes)
|
|
15
|
+
#print(f"selected intent: {intents}")
|
|
16
|
+
return intents
|
|
17
|
+
|
|
18
|
+
def extract_assessment_codes(self,assessment):
|
|
19
|
+
json_data = json.loads(assessment)
|
|
20
|
+
selection = json_data['selection']
|
|
21
|
+
codes = list(selection)
|
|
22
|
+
return codes
|
|
23
|
+
|
|
24
|
+
def get_intent_aux(self,previous_intent,assessment_codes,open_problem=True):
|
|
25
|
+
if not open_problem and 'j' in assessment_codes: # if the problem is not open, then it has only one solution
|
|
26
|
+
assessment_codes.append('k')
|
|
27
|
+
# print(f"codes are {assessment_codes} and previous intent is {previous_intent}")
|
|
28
|
+
intents = []
|
|
29
|
+
# non previous intent-dependent selections:
|
|
30
|
+
if Intent.G_GREETINGS in previous_intent:
|
|
31
|
+
intents.append(Intent.G_GREETINGS)
|
|
32
|
+
if Intent.P_REFLECTION in previous_intent:
|
|
33
|
+
intents.append(Intent.G_GREETINGS)
|
|
34
|
+
if 'k' in assessment_codes:
|
|
35
|
+
intents.append(Intent.P_REFLECTION)
|
|
36
|
+
if 'i' in assessment_codes:
|
|
37
|
+
intents.append(Intent.S_OFFLOAD)
|
|
38
|
+
if 'h' in assessment_codes:
|
|
39
|
+
intents.append(Intent.S_STATE)
|
|
40
|
+
if 'e' in assessment_codes and 'g' not in assessment_codes:
|
|
41
|
+
intents.append(Intent.P_ARTICULATION)
|
|
42
|
+
if any(code in assessment_codes for code in ('a','b','c')):
|
|
43
|
+
intents.append(Intent.S_SELFCORRECTION)
|
|
44
|
+
elif 'd' in assessment_codes:
|
|
45
|
+
intents.append(Intent.S_STRATEGY)
|
|
46
|
+
if 'j' in assessment_codes and 'k' not in assessment_codes and Intent.G_GREETINGS not in previous_intent:
|
|
47
|
+
intents.append(Intent.P_LIMITS)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
# previous intent-dependent selections:
|
|
52
|
+
|
|
53
|
+
if Intent.S_SELFCORRECTION in previous_intent:
|
|
54
|
+
if 'f' in assessment_codes:
|
|
55
|
+
intents.append(Intent.S_STRATEGY)
|
|
56
|
+
elif 'a' in assessment_codes or 'b' in assessment_codes:
|
|
57
|
+
intents.append(Intent.S_CORRECTION)
|
|
58
|
+
if 'c' in assessment_codes:
|
|
59
|
+
intents.append(Intent.S_OFFLOAD)
|
|
60
|
+
|
|
61
|
+
if Intent.S_STRATEGY in previous_intent:
|
|
62
|
+
if 'g' in assessment_codes and 'd' not in assessment_codes and 'f' not in assessment_codes:
|
|
63
|
+
intents.append(Intent.P_HYPOTHESIS)
|
|
64
|
+
elif ('l' in assessment_codes or 'k' in assessment_codes) and 'g' in assessment_codes:
|
|
65
|
+
intents.append(Intent.S_HINT)
|
|
66
|
+
if Intent.P_HYPOTHESIS in previous_intent:
|
|
67
|
+
if 'f'in assessment_codes:
|
|
68
|
+
intents.append(Intent.P_ARTICULATION)
|
|
69
|
+
if len(intents)==0 and ('f' in assessment_codes or 'g' in assessment_codes):
|
|
70
|
+
intents.append(Intent.S_STRATEGY)
|
|
71
|
+
|
|
72
|
+
#dirty fixes
|
|
73
|
+
if Intent.S_CORRECTION in intents and Intent.S_SELFCORRECTION in intents:
|
|
74
|
+
intents.remove(Intent.S_SELFCORRECTION)
|
|
75
|
+
|
|
76
|
+
if Intent.S_SELFCORRECTION in intents and Intent.S_OFFLOAD in intents:
|
|
77
|
+
intents.remove(Intent.S_SELFCORRECTION)
|
|
78
|
+
|
|
79
|
+
self.previous_intent = intents
|
|
80
|
+
return intents
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class SimpleIntentSelector2(IntentSelector):
|
|
84
|
+
def __init__(self, intent_history, options = dict()) -> None:
|
|
85
|
+
self.history = intent_history # not used
|
|
86
|
+
self.previous_intent = intent_history[-1] if intent_history != [] else [Intent.S_STRATEGY]
|
|
87
|
+
self.options = options
|
|
88
|
+
|
|
89
|
+
def get_intent_aux(self, previous_intent,assessment_codes):
|
|
90
|
+
if "open" in self.options:
|
|
91
|
+
open =self.options["open"]
|
|
92
|
+
else:
|
|
93
|
+
open = False
|
|
94
|
+
intents = []
|
|
95
|
+
if 'l' in assessment_codes: # here l is irrelevant, not motiv
|
|
96
|
+
return [Intent.G_REFUSE]
|
|
97
|
+
if 'm' in assessment_codes:
|
|
98
|
+
intents.append(Intent.S_STATE)
|
|
99
|
+
intents.append(Intent.A_CURIOSITY)
|
|
100
|
+
if 'i' in assessment_codes:
|
|
101
|
+
intents.append(Intent.S_OFFLOAD)
|
|
102
|
+
if 'h' in assessment_codes:
|
|
103
|
+
intents.append(Intent.S_STATE)
|
|
104
|
+
if 'e' in assessment_codes and 'g' not in assessment_codes:
|
|
105
|
+
intents.append(Intent.P_ARTICULATION)
|
|
106
|
+
if any(code in assessment_codes for code in ('a','b','c')):
|
|
107
|
+
intents.append(Intent.S_SELFCORRECTION)
|
|
108
|
+
elif 'd' in assessment_codes:
|
|
109
|
+
intents.append(Intent.S_STRATEGY)
|
|
110
|
+
intents.append(Intent.S_HINT)
|
|
111
|
+
|
|
112
|
+
# previous intent-dependent selections:
|
|
113
|
+
|
|
114
|
+
if Intent.S_SELFCORRECTION in previous_intent:
|
|
115
|
+
if 'f' in assessment_codes:
|
|
116
|
+
intents.append(Intent.S_STRATEGY)
|
|
117
|
+
elif 'a' in assessment_codes or 'b' in assessment_codes:
|
|
118
|
+
intents.append(Intent.S_CORRECTION)
|
|
119
|
+
if 'c' in assessment_codes:
|
|
120
|
+
intents.append(Intent.S_OFFLOAD)
|
|
121
|
+
|
|
122
|
+
if 'g' in assessment_codes:
|
|
123
|
+
if Intent.P_HYPOTHESIS in previous_intent:
|
|
124
|
+
intents.append(Intent.S_HINT)
|
|
125
|
+
elif 'd' not in assessment_codes and 'f' not in assessment_codes:
|
|
126
|
+
intents.append(Intent.P_HYPOTHESIS)
|
|
127
|
+
|
|
128
|
+
if Intent.S_CORRECTION in previous_intent:
|
|
129
|
+
intents.append(Intent.S_CORRECTION)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
if len(intents)==0 and ('f' in assessment_codes or 'g' in assessment_codes):
|
|
134
|
+
intents.append(Intent.S_STRATEGY)
|
|
135
|
+
|
|
136
|
+
#dirty fixes
|
|
137
|
+
if Intent.S_CORRECTION in intents and Intent.S_SELFCORRECTION in intents:
|
|
138
|
+
intents.remove(Intent.S_SELFCORRECTION)
|
|
139
|
+
|
|
140
|
+
if Intent.S_SELFCORRECTION in intents and Intent.S_OFFLOAD in intents:
|
|
141
|
+
intents.remove(Intent.S_SELFCORRECTION)
|
|
142
|
+
|
|
143
|
+
if not (Intent.S_CORRECTION in intents) and not (Intent.S_SELFCORRECTION in intents) and not (Intent.S_OFFLOAD in intents) and 'j' in assessment_codes:
|
|
144
|
+
intents.append(Intent.G_GREETINGS)
|
|
145
|
+
|
|
146
|
+
if intents == []:
|
|
147
|
+
intents.append(Intent.S_STRATEGY)
|
|
148
|
+
|
|
149
|
+
# remove duplicates
|
|
150
|
+
intents = list(set(intents))
|
|
151
|
+
self.previous_intent = intents
|
|
152
|
+
# print(f"selected intent: {intents}")
|
|
153
|
+
return intents
|
|
154
|
+
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import open_learning_ai_tutor.PromptGenerator as PromptGenerator
|
|
2
|
+
import open_learning_ai_tutor.IntentSelector as IntentSelector
|
|
3
|
+
import open_learning_ai_tutor.Assessor as Assessor
|
|
4
|
+
|
|
5
|
+
def_options = {"version":"V1","tools":None}
|
|
6
|
+
|
|
7
|
+
class Intermediary():
|
|
8
|
+
def __init__(self,client,model,assessor = None, intentSelector=None,promptGenerator = None,intent_history = [],assessment_history=[]) -> None:
|
|
9
|
+
self.client = client
|
|
10
|
+
self.model = model
|
|
11
|
+
|
|
12
|
+
self.assessor = Assessor.Assessor(self.client,self.model,assessment_history = assessment_history) if assessor is None else assessor
|
|
13
|
+
self.intentSelector = IntentSelector.IntentSelector(intent_history=intent_history) if intentSelector is None else intentSelector
|
|
14
|
+
self.promptGenerator = PromptGenerator.RigidPromptGenerator() if promptGenerator is None else promptGenerator
|
|
15
|
+
|
|
16
|
+
def update_client(self,client):
|
|
17
|
+
self.client = client
|
|
18
|
+
self.assessor.client = client
|
|
19
|
+
self.intentSelector.client = client
|
|
20
|
+
self.promptGenerator.client = client
|
|
21
|
+
|
|
22
|
+
def update_model(self,model):
|
|
23
|
+
self.model = model
|
|
24
|
+
self.assessor.model = model
|
|
25
|
+
self.intentSelector.model = model
|
|
26
|
+
self.promptGenerator.model = model
|
|
27
|
+
|
|
28
|
+
def get_prompt(self,pb,sol,student_messages,tutor_messages,open=True):
|
|
29
|
+
#print("generating tutor's prompt...")
|
|
30
|
+
assessment,metadata = self.assessor.assess(pb,sol,student_messages,tutor_messages)
|
|
31
|
+
assessor_prompt_tokens,assessor_completion_tokens = metadata[0], metadata[1]
|
|
32
|
+
docs = None
|
|
33
|
+
rag_questions = None
|
|
34
|
+
if len(metadata) > 2:
|
|
35
|
+
docs = metadata[2]
|
|
36
|
+
if len(metadata) > 3:
|
|
37
|
+
rag_questions = metadata[3]
|
|
38
|
+
intent = self.intentSelector.get_intent(assessment,open=open)
|
|
39
|
+
prompt = self.promptGenerator.get_prompt(pb,sol,student_messages,tutor_messages,intent,docs)
|
|
40
|
+
return prompt,intent,assessment,assessor_prompt_tokens,assessor_completion_tokens,docs,rag_questions
|
|
41
|
+
|
|
42
|
+
class GraphIntermediary2(Intermediary):
|
|
43
|
+
def __init__(self,model,assessor = None, intentSelector=None,promptGenerator = None,chat_history = [],intent_history = [],assessment_history=[], options = dict()) -> None:
|
|
44
|
+
self.model = model
|
|
45
|
+
self.options = options
|
|
46
|
+
self.assessor = Assessor.GraphAssessor2(self.model,assessment_history = assessment_history, new_messages=[], options = options) if assessor is None else assessor
|
|
47
|
+
self.intentSelector = IntentSelector.SimpleIntentSelector2(intent_history=intent_history, options = options) if intentSelector is None else intentSelector
|
|
48
|
+
self.promptGenerator = PromptGenerator.SimplePromptGenerator2(options = options, chat_history = chat_history) if promptGenerator is None else promptGenerator
|
|
49
|
+
|
|
50
|
+
def get_prompt2(self,pb,sol):
|
|
51
|
+
#print("generating tutor's prompt...")
|
|
52
|
+
assessment_history,metadata = self.assessor.assess2(pb,sol)
|
|
53
|
+
assessment = assessment_history[-1].content
|
|
54
|
+
|
|
55
|
+
docs = None
|
|
56
|
+
rag_questions = None
|
|
57
|
+
if "docs" in metadata:
|
|
58
|
+
self.options["docs"] = metadata["docs"]
|
|
59
|
+
if "rag_queries" in metadata:
|
|
60
|
+
self.options["rag_questions"] = metadata["rag_queries"]
|
|
61
|
+
|
|
62
|
+
intent = self.intentSelector.get_intent(assessment)
|
|
63
|
+
|
|
64
|
+
chat_history = self.promptGenerator.get_prompt2(pb,sol,intent,self.options)
|
|
65
|
+
|
|
66
|
+
return chat_history,intent,assessment_history, metadata
|