open-learning-ai-tutor 0.0.0__tar.gz

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.
@@ -0,0 +1,137 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+ .pytest_cache/
6
+
7
+ # C extensions
8
+ *.so
9
+
10
+ # Distribution / packaging
11
+ .Python
12
+ build/
13
+ build-exports/
14
+ develop-eggs/
15
+ dist/
16
+ downloads/
17
+ eggs/
18
+ .eggs/
19
+ ./lib/
20
+ ./lib64/
21
+ parts/
22
+ sdist/
23
+ var/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+
28
+ # PyInstaller
29
+ # Usually these files are written by a python script from a template
30
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
31
+ *.manifest
32
+ *.spec
33
+
34
+ # Installer logs
35
+ pip-log.txt
36
+ pip-delete-this-directory.txt
37
+
38
+ # Unit test / coverage reports
39
+ htmlcov/
40
+ .tox/
41
+ .coverage
42
+ .coverage.*
43
+ .cache
44
+ nosetests.xml
45
+ coverage.xml
46
+ *,cover
47
+ .hypothesis/
48
+ coverage/
49
+ .nyc_output/
50
+
51
+ # Translations
52
+ *.mo
53
+ *.pot
54
+
55
+ # Django stuff:
56
+ *.log
57
+ celerybeat-schedule.db
58
+
59
+ # Sphinx documentation
60
+ docs/_build/
61
+
62
+ # PyBuilder
63
+ target/
64
+
65
+ #Ipython/Jupyter Notebook
66
+ .ipynb_checkpoints
67
+ *.ipynb
68
+
69
+ # Heroku/Foreman
70
+ .env
71
+
72
+ # webpack
73
+ static/bundles
74
+ node_modules
75
+
76
+
77
+ # Editor stuff
78
+ *.swp
79
+ *.orig
80
+
81
+ /nbproject
82
+ .idea/
83
+ .redcar/
84
+ codekit-config.json
85
+ .pycharm_helpers/
86
+
87
+ .project
88
+ .pydevproject
89
+
90
+ *.DS_Store
91
+
92
+ # Django static
93
+ staticfiles/
94
+ static/hash.txt
95
+
96
+ .venv
97
+ release-notes-checklist
98
+
99
+ # Webpack
100
+ webpack-stats.json
101
+ webpack-stats/
102
+ !scripts/test/data/webpack-stats/
103
+
104
+
105
+ # Celery Beat
106
+ celerybeat-schedule
107
+
108
+ # for testing out NPM packages
109
+ *.tgz
110
+
111
+ # docs/bundler/jekyll
112
+ docs/_site
113
+
114
+ .pnp.*
115
+ frontends/.yarn
116
+ !frontends/.yarn/releases
117
+
118
+ .yarn/*
119
+ !.yarn/releases
120
+
121
+ # Typescript
122
+ tsconfig.tsbuildinfo
123
+
124
+ # VS-Code
125
+ .vscode
126
+
127
+ # GitHub Pages static site
128
+ github-pages/public
129
+
130
+ # Storybook
131
+ storybook-static/
132
+
133
+ /**/.yarn/cache
134
+ .swc
135
+
136
+ # Streamlit secrets
137
+ .streamlit
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Romain Puech
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,19 @@
1
+ Metadata-Version: 2.4
2
+ Name: open_learning_ai_tutor
3
+ Version: 0.0.0
4
+ Summary: AI powered tutor
5
+ Author-email: MIT Open Learning Engineering <odl-devops@mit.edu>
6
+ License-File: LICENSE
7
+ Requires-Dist: e2b-code-interpreter
8
+ Requires-Dist: langchain
9
+ Requires-Dist: langchain-anthropic
10
+ Requires-Dist: langchain-chroma
11
+ Requires-Dist: langchain-community
12
+ Requires-Dist: langchain-core
13
+ Requires-Dist: langchain-experimental
14
+ Requires-Dist: langchain-openai
15
+ Requires-Dist: langchain-together
16
+ Requires-Dist: langgraph
17
+ Requires-Dist: openai
18
+ Requires-Dist: pygithub
19
+ Requires-Dist: tiktoken
@@ -0,0 +1,5 @@
1
+ # Open_Learning_AI_Tutor
2
+ AI tutor MIT open Learning
3
+ `open_learning_ai_tutor` contains the code used for the demo at: https://uai-tutor-demo.streamlit.app/.
4
+
5
+ Paper for pedagogical engine: https://arxiv.org/pdf/2410.03781
@@ -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,3 @@
1
+ analytics_book = r"""
2
+ A big book.
3
+ """