Ilayda-j commited on
Commit
dd3611a
β€’
1 Parent(s): 6abb9ab

Upload 7 files

Browse files
IOHelperUtilities.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/helper_utilities.ipynb.
2
+
3
+ # %% auto 0
4
+ __all__ = ['check_is_colab', 'MultiFileChooser', 'setup_drives']
5
+
6
+ # %% ../nbs/helper_utilities.ipynb 3
7
+ import ipywidgets as widgets
8
+ from IPython.display import display, clear_output
9
+ from functools import partial
10
+ from ipyfilechooser import FileChooser
11
+ import os
12
+
13
+ # %% ../nbs/helper_utilities.ipynb 4
14
+ def check_is_colab():
15
+ """
16
+ Check if the current environment is Google Colab.
17
+ """
18
+ try:
19
+ import google.colab
20
+ return True
21
+ except:
22
+ return False
23
+
24
+ # %% ../nbs/helper_utilities.ipynb 7
25
+ class MultiFileChooser:
26
+ def __init__(self):
27
+ self.fc = FileChooser('.')
28
+ self.fc.title = "Use the following file chooser to add each file individually.\n You can remove files by clicking the remove button."
29
+ self.fc.use_dir_icons = True
30
+ self.fc.show_only_dirs = False
31
+ self.selected_files = []
32
+
33
+ self.fc.register_callback(self.file_selected)
34
+
35
+ self.output = widgets.Output()
36
+
37
+ def file_selected(self, chooser):
38
+ if self.fc.selected is not None and self.fc.selected not in self.selected_files:
39
+ self.selected_files.append(self.fc.selected)
40
+ self.update_display()
41
+
42
+ def update_display(self):
43
+ with self.output:
44
+ clear_output()
45
+ for this_file in self.selected_files:
46
+ remove_button = widgets.Button(description="Remove", tooltip="Remove this file")
47
+ remove_button.on_click(partial(self.remove_file, file=this_file))
48
+ display(widgets.HBox([widgets.Label(value=this_file), remove_button]))
49
+
50
+ def remove_file(self, button, this_file):
51
+ if this_file in self.selected_files:
52
+ self.selected_files.remove(this_file)
53
+ self.update_display()
54
+
55
+ def display(self):
56
+ display(self.fc, self.output)
57
+
58
+ def get_selected_files(self):
59
+ return self.selected_files
60
+
61
+ # %% ../nbs/helper_utilities.ipynb 12
62
+ def setup_drives(upload_set):
63
+
64
+ upload_set = upload_set.lower()
65
+ uploaded = None
66
+
67
+ # allow them to mount the drive if they chose Google Colab.
68
+ if upload_set == 'google drive':
69
+ if check_is_colab():
70
+ from google.colab import drive
71
+ drive.mount('/content/drive')
72
+ else:
73
+ raise ValueError("It looks like you're not on Google Colab. Google Drive mounting is currently only implemented for Google Colab.")
74
+
75
+ # Everything else means that they'll need to use a file chooser (including Google Drive)
76
+ if check_is_colab():
77
+ from google.colab import files
78
+ uploaded = files.upload()
79
+ else:
80
+ # Create file chooser and interact
81
+ mfc = MultiFileChooser()
82
+ mfc.display()
83
+ uploaded = mfc.get_selected_files()
84
+
85
+ return uploaded
MediaVectorStores.py ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/media_stores.ipynb.
2
+
3
+ # %% auto 0
4
+ __all__ = ['rawtext_to_doc_split', 'files_to_text', 'youtube_to_text', 'save_text', 'get_youtube_transcript',
5
+ 'website_to_text_web', 'website_to_text_unstructured', 'get_document_segments', 'create_local_vector_store']
6
+
7
+ # %% ../nbs/media_stores.ipynb 3
8
+ # import libraries here
9
+ import os
10
+ import itertools
11
+
12
+ from langchain.embeddings import OpenAIEmbeddings
13
+
14
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
15
+ from langchain.document_loaders.unstructured import UnstructuredFileLoader
16
+ from langchain.document_loaders.generic import GenericLoader
17
+ from langchain.document_loaders.parsers import OpenAIWhisperParser
18
+ from langchain.document_loaders.blob_loaders.youtube_audio import YoutubeAudioLoader
19
+ from langchain.document_loaders import WebBaseLoader, UnstructuredURLLoader
20
+ from langchain.docstore.document import Document
21
+
22
+ from langchain.vectorstores import Chroma
23
+ from langchain.chains import RetrievalQAWithSourcesChain
24
+
25
+ # %% ../nbs/media_stores.ipynb 8
26
+ def rawtext_to_doc_split(text, chunk_size=1500, chunk_overlap=150):
27
+
28
+ # Quick type checking
29
+ if not isinstance(text, list):
30
+ text = [text]
31
+
32
+ # Create splitter
33
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size,
34
+ chunk_overlap=chunk_overlap,
35
+ add_start_index = True)
36
+
37
+ #Split into docs segments
38
+ if isinstance(text[0], Document):
39
+ doc_segments = text_splitter.split_documents(text)
40
+ else:
41
+ doc_segments = text_splitter.split_documents(text_splitter.create_documents(text))
42
+
43
+ # Make into one big list
44
+ doc_segments = list(itertools.chain(*doc_segments)) if isinstance(doc_segments[0], list) else doc_segments
45
+
46
+ return doc_segments
47
+
48
+ # %% ../nbs/media_stores.ipynb 16
49
+ ## A single File
50
+ def _file_to_text(single_file, chunk_size = 1000, chunk_overlap=150):
51
+
52
+ # Create loader and get segments
53
+ loader = UnstructuredFileLoader(single_file)
54
+ doc_segments = loader.load_and_split(RecursiveCharacterTextSplitter(chunk_size=chunk_size,
55
+ chunk_overlap=chunk_overlap,
56
+ add_start_index=True))
57
+ return doc_segments
58
+
59
+
60
+ ## Multiple files
61
+ def files_to_text(files_list, chunk_size=1000, chunk_overlap=150):
62
+
63
+ # Quick type checking
64
+ if not isinstance(files_list, list):
65
+ files_list = [files_list]
66
+
67
+ # This is currently a fix because the UnstructuredFileLoader expects a list of files yet can't split them correctly yet
68
+ all_segments = [_file_to_text(single_file, chunk_size=chunk_size, chunk_overlap=chunk_overlap) for single_file in files_list]
69
+ all_segments = list(itertools.chain(*all_segments)) if isinstance(all_segments[0], list) else all_segments
70
+
71
+ return all_segments
72
+
73
+ # %% ../nbs/media_stores.ipynb 20
74
+ def youtube_to_text(urls, save_dir = "content"):
75
+ # Transcribe the videos to text
76
+ # save_dir: directory to save audio files
77
+
78
+ if not isinstance(urls, list):
79
+ urls = [urls]
80
+
81
+ youtube_loader = GenericLoader(YoutubeAudioLoader(urls, save_dir), OpenAIWhisperParser())
82
+ youtube_docs = youtube_loader.load()
83
+
84
+ return youtube_docs
85
+
86
+ # %% ../nbs/media_stores.ipynb 24
87
+ def save_text(text, text_name = None):
88
+ if not text_name:
89
+ text_name = text[:20]
90
+ text_path = os.path.join("/content",text_name+".txt")
91
+
92
+ with open(text_path, "x") as f:
93
+ f.write(text)
94
+ # Return the location at which the transcript is saved
95
+ return text_path
96
+
97
+ # %% ../nbs/media_stores.ipynb 25
98
+ def get_youtube_transcript(yt_url, save_transcript = False, temp_audio_dir = "sample_data"):
99
+ # Transcribe the videos to text and save to file in /content
100
+ # save_dir: directory to save audio files
101
+
102
+ youtube_docs = youtube_to_text(yt_url, save_dir = temp_audio_dir)
103
+
104
+ # Combine doc
105
+ combined_docs = [doc.page_content for doc in youtube_docs]
106
+ combined_text = " ".join(combined_docs)
107
+
108
+ # Save text to file
109
+ video_path = youtube_docs[0].metadata["source"]
110
+ youtube_name = os.path.splitext(os.path.basename(video_path))[0]
111
+
112
+ save_path = None
113
+ if save_transcript:
114
+ save_path = save_text(combined_text, youtube_name)
115
+
116
+ return youtube_docs, save_path
117
+
118
+ # %% ../nbs/media_stores.ipynb 27
119
+ def website_to_text_web(url, chunk_size = 1500, chunk_overlap=100):
120
+
121
+ # Url can be a single string or list
122
+ website_loader = WebBaseLoader(url)
123
+ website_raw = website_loader.load()
124
+
125
+ website_data = rawtext_to_doc_split(website_raw, chunk_size = chunk_size, chunk_overlap=chunk_overlap)
126
+
127
+ # Combine doc
128
+ return website_data
129
+
130
+ # %% ../nbs/media_stores.ipynb 33
131
+ def website_to_text_unstructured(web_urls, chunk_size = 1500, chunk_overlap=100):
132
+
133
+ # Make sure it's a list
134
+ if not isinstance(web_urls, list):
135
+ web_urls = [web_urls]
136
+
137
+ # Url can be a single string or list
138
+ website_loader = UnstructuredURLLoader(web_urls)
139
+ website_raw = website_loader.load()
140
+
141
+ website_data = rawtext_to_doc_split(website_raw, chunk_size = chunk_size, chunk_overlap=chunk_overlap)
142
+
143
+ # Return individual docs or list
144
+ return website_data
145
+
146
+ # %% ../nbs/media_stores.ipynb 45
147
+ def get_document_segments(context_info, data_type, chunk_size = 1500, chunk_overlap=100):
148
+
149
+ load_fcn = None
150
+ addtnl_params = {'chunk_size': chunk_size, 'chunk_overlap': chunk_overlap}
151
+
152
+ # Define function use to do the loading
153
+ if data_type == 'text':
154
+ load_fcn = rawtext_to_doc_split
155
+ elif data_type == 'web_page':
156
+ load_fcn = website_to_text_unstructured
157
+ elif data_type == 'youtube_video':
158
+ load_fcn = youtube_to_text
159
+ else:
160
+ load_fcn = files_to_text
161
+
162
+ # Get the document segments
163
+ doc_segments = load_fcn(context_info, **addtnl_params)
164
+
165
+ return doc_segments
166
+
167
+ # %% ../nbs/media_stores.ipynb 47
168
+ def create_local_vector_store(document_segments, **retriever_kwargs):
169
+ embeddings = OpenAIEmbeddings()
170
+ db = Chroma.from_documents(document_segments, embeddings)
171
+ retriever = db.as_retriever(**retriever_kwargs)
172
+
173
+ return db, retriever
PromptInteractionBase.py ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/prompt_interaction_base.ipynb.
2
+
3
+ # %% auto 0
4
+ __all__ = ['SYSTEM_TUTOR_TEMPLATE', 'HUMAN_RESPONSE_TEMPLATE', 'HUMAN_RETRIEVER_RESPONSE_TEMPLATE', 'DEFAULT_ASSESSMENT_MSG',
5
+ 'DEFAULT_LEARNING_OBJS_MSG', 'DEFAULT_CONDENSE_PROMPT_TEMPLATE', 'DEFAULT_QUESTION_PROMPT_TEMPLATE',
6
+ 'DEFAULT_COMBINE_PROMPT_TEMPLATE', 'create_model', 'set_openai_key', 'create_base_tutoring_prompt',
7
+ 'get_tutoring_prompt', 'get_tutoring_answer', 'create_tutor_mdl_chain']
8
+
9
+ # %% ../nbs/prompt_interaction_base.ipynb 3
10
+ from langchain.chat_models import ChatOpenAI
11
+ from langchain.llms import OpenAI
12
+
13
+ from langchain import PromptTemplate
14
+ from langchain.prompts import ChatPromptTemplate, PromptTemplate
15
+ from langchain.prompts import SystemMessagePromptTemplate, HumanMessagePromptTemplate
16
+ from langchain.chains import LLMChain, ConversationalRetrievalChain, RetrievalQAWithSourcesChain
17
+ from langchain.chains.base import Chain
18
+
19
+ from getpass import getpass
20
+
21
+ import os
22
+
23
+ # %% ../nbs/prompt_interaction_base.ipynb 5
24
+ def create_model(openai_mdl='gpt-3.5-turbo-16k', temperature=0.1, **chatopenai_kwargs):
25
+ llm = ChatOpenAI(model_name = openai_mdl, temperature=temperature, **chatopenai_kwargs)
26
+
27
+ return llm
28
+
29
+ # %% ../nbs/prompt_interaction_base.ipynb 6
30
+ def set_openai_key():
31
+ openai_api_key = getpass()
32
+ os.environ["OPENAI_API_KEY"] = openai_api_key
33
+
34
+ return
35
+
36
+ # %% ../nbs/prompt_interaction_base.ipynb 10
37
+ # Create system prompt template
38
+ SYSTEM_TUTOR_TEMPLATE = ("You are a world-class tutor helping students to perform better on oral and written exams though interactive experiences. " +
39
+ "When assessing and evaluating students, you always ask one question at a time, and wait for the student's response before " +
40
+ "providing them with feedback. Asking one question at a time, waiting for the student's response, and then commenting " +
41
+ "on the strengths and weaknesses of their responses (when appropriate) is what makes you such a sought-after, world-class tutor.")
42
+
43
+ # Create a human response template
44
+ HUMAN_RESPONSE_TEMPLATE = ("I'm trying to better understand the text provided below. {assessment_request} The learning objectives to be assessed are: " +
45
+ "{learning_objectives}. Although I may request more than one assessment question, you should " +
46
+ "only provide ONE question in you initial response. Do not include the answer in your response. " +
47
+ "If I get an answer wrong, provide me with an explanation of why it was incorrect, and then give me additional " +
48
+ "chances to respond until I get the correct choice. Explain why the correct choice is right. " +
49
+ "The text that you will base your questions on is as follows: {context}.")
50
+
51
+ HUMAN_RETRIEVER_RESPONSE_TEMPLATE = ("I want to master the topics based on the excerpts of the text below. Given the following extracted text from long documents, {assessment_request} The learning objectives to be assessed are: " +
52
+ "{learning_objectives}. Although I may request more than one assessment question, you should " +
53
+ "only provide ONE question in you initial response. Do not include the answer in your response. " +
54
+ "If I get an answer wrong, provide me with an explanation of why it was incorrect, and then give me additional " +
55
+ "chances to respond until I get the correct choice. Explain why the correct choice is right. " +
56
+ "The extracted text from long documents are as follows: {summaries}.")
57
+
58
+ def create_base_tutoring_prompt(system_prompt=None, human_prompt=None):
59
+
60
+ #setup defaults using defined values
61
+ if system_prompt == None:
62
+ system_prompt = PromptTemplate(template = SYSTEM_TUTOR_TEMPLATE,
63
+ input_variables = [])
64
+
65
+ if human_prompt==None:
66
+ human_prompt = PromptTemplate(template = HUMAN_RESPONSE_TEMPLATE,
67
+ input_variables=['assessment_request', 'learning_objectives', 'context'])
68
+
69
+ # Create prompt messages
70
+ system_tutor_msg = SystemMessagePromptTemplate(prompt=system_prompt)
71
+ human_tutor_msg = HumanMessagePromptTemplate(prompt= human_prompt)
72
+
73
+ # Create ChatPromptTemplate
74
+ chat_prompt = ChatPromptTemplate.from_messages([system_tutor_msg, human_tutor_msg])
75
+
76
+ return chat_prompt
77
+
78
+ # %% ../nbs/prompt_interaction_base.ipynb 14
79
+ DEFAULT_ASSESSMENT_MSG = 'Please design a 5 question short answer quiz about the provided text.'
80
+ DEFAULT_LEARNING_OBJS_MSG = 'Identify and comprehend the important topics and underlying messages and connections within the text'
81
+
82
+ def get_tutoring_prompt(context, chat_template=None, assessment_request = None, learning_objectives = None, **kwargs):
83
+
84
+ # set defaults
85
+ if chat_template is None:
86
+ chat_template = create_base_tutoring_prompt()
87
+ else:
88
+ if not all([prompt_var in chat_template.input_variables
89
+ for prompt_var in ['context', 'assessment_request', 'learning_objectives']]):
90
+ raise KeyError('''It looks like you may have a custom chat_template. Either include context, assessment_request, and learning objectives
91
+ as input variables or create your own tutoring prompt.''')
92
+
93
+ if assessment_request is None and 'assessment_request':
94
+ assessment_request = DEFAULT_ASSESSMENT_MSG
95
+
96
+ if learning_objectives is None:
97
+ learning_objectives = DEFAULT_LEARNING_OBJS_MSG
98
+
99
+ # compose final prompt
100
+ tutoring_prompt = chat_template.format_prompt(context=context,
101
+ assessment_request = assessment_request,
102
+ learning_objectives = learning_objectives,
103
+ **kwargs)
104
+
105
+ return tutoring_prompt
106
+
107
+
108
+ # %% ../nbs/prompt_interaction_base.ipynb 18
109
+ def get_tutoring_answer(context, tutor_mdl, chat_template=None, assessment_request=None, learning_objectives=None, return_dict=False, call_kwargs={}, input_kwargs={}):
110
+
111
+ # Get answer from chat
112
+
113
+ # set defaults
114
+ if assessment_request is None:
115
+ assessment_request = DEFAULT_ASSESSMENT_MSG
116
+ if learning_objectives is None:
117
+ learning_objectives = DEFAULT_LEARNING_OBJS_MSG
118
+
119
+ common_inputs = {'assessment_request':assessment_request, 'learning_objectives':learning_objectives}
120
+
121
+ # get answer based on interaction type
122
+ if isinstance(tutor_mdl, ChatOpenAI):
123
+ human_ask_prompt = get_tutoring_prompt(context, chat_template, assessment_request, learning_objectives)
124
+ tutor_answer = tutor_mdl(human_ask_prompt.to_messages())
125
+
126
+ if not return_dict:
127
+ final_answer = tutor_answer.content
128
+
129
+ elif isinstance(tutor_mdl, Chain):
130
+ if isinstance(tutor_mdl, RetrievalQAWithSourcesChain):
131
+ if 'question' not in input_kwargs.keys():
132
+ common_inputs['question'] = assessment_request
133
+ final_inputs = {**common_inputs, **input_kwargs}
134
+ else:
135
+ common_inputs['context'] = context
136
+ final_inputs = {**common_inputs, **input_kwargs}
137
+
138
+ # get answer
139
+ tutor_answer = tutor_mdl(final_inputs, **call_kwargs)
140
+ final_answer = tutor_answer
141
+
142
+ if not return_dict:
143
+ final_answer = final_answer['answer']
144
+
145
+ else:
146
+ raise NotImplementedError(f"tutor_mdl of type {type(tutor_mdl)} is not supported.")
147
+
148
+ return final_answer
149
+
150
+ # %% ../nbs/prompt_interaction_base.ipynb 19
151
+ DEFAULT_CONDENSE_PROMPT_TEMPLATE = ("Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question, " +
152
+ "in its original language.\n\nChat History:\n{chat_history}\nFollow Up Input: {question}\nStandalone question:")
153
+
154
+ DEFAULT_QUESTION_PROMPT_TEMPLATE = ("Use the following portion of a long document to see if any of the text is relevant to creating a response to the question." +
155
+ "\nReturn any relevant text verbatim.\n{context}\nQuestion: {question}\nRelevant text, if any:")
156
+
157
+ DEFAULT_COMBINE_PROMPT_TEMPLATE = ("Given the following extracted parts of a long document and the given prompt, create a final answer with references ('SOURCES'). "+
158
+ "If you don't have a response, just say that you are unable to come up with a response. "+
159
+ "\nSOURCES:\n\nQUESTION: {question}\n=========\n{summaries}\n=========\nFINAL ANSWER:'")
160
+
161
+ def create_tutor_mdl_chain(kind='llm', mdl=None, prompt_template = None, **kwargs):
162
+
163
+ #Validate parameters
164
+ if mdl is None:
165
+ mdl = create_model()
166
+ kind = kind.lower()
167
+
168
+ #Create model chain
169
+ if kind == 'llm':
170
+ if prompt_template is None:
171
+ prompt_template = create_base_tutoring_prompt()
172
+ mdl_chain = LLMChain(llm=mdl, prompt=prompt_template, **kwargs)
173
+ elif kind == 'conversational':
174
+ if prompt_template is None:
175
+ prompt_template = PromptTemplate.from_template(DEFAULT_CONDENSE_PROMPT_TEMPLATE)
176
+ mdl_chain = ConversationalRetrieverChain.from_llm(mdl, condense_question_prompt = prompt_template, **kwargs)
177
+ elif kind == 'retrieval_qa':
178
+ if prompt_template is None:
179
+
180
+ #Create custom human prompt to take in summaries
181
+ human_prompt = PromptTemplate(template = HUMAN_RETRIEVER_RESPONSE_TEMPLATE,
182
+ input_variables=['assessment_request', 'learning_objectives', 'summaries'])
183
+ prompt_template = create_base_tutoring_prompt(human_prompt=human_prompt)
184
+
185
+ #Create the combination prompt and model
186
+ question_template = PromptTemplate.from_template(DEFAULT_QUESTION_PROMPT_TEMPLATE)
187
+ mdl_chain = RetrievalQAWithSourcesChain.from_llm(llm=mdl, question_prompt=question_template, combine_prompt = prompt_template, **kwargs)
188
+ else:
189
+ raise NotImplementedError(f"Model kind {kind} not implemented")
190
+
191
+ return mdl_chain
SelfStudyPrompts.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/self_study_prompts.ipynb.
2
+
3
+ # %% auto 0
4
+ __all__ = ['MC_QUIZ_DEFAULT', 'SHORT_ANSWER_DEFAULT', 'FILL_BLANK_DEFAULT', 'SEQUENCING_DEFAULT', 'RELATIONSHIP_DEFAULT',
5
+ 'CONCEPTS_DEFAULT', 'REAL_WORLD_EXAMPLE_DEFAULT', 'RANDOMIZED_QUESTIONS_DEFAULT', 'SELF_STUDY_PROMPT_NAMES',
6
+ 'SELF_STUDY_DEFAULTS', 'list_all_self_study_prompt_keys', 'list_all_self_study_prompts',
7
+ 'list_default_self_prompt_varnames', 'print_all_self_study_prompts']
8
+
9
+ # %% ../nbs/self_study_prompts.ipynb 4
10
+ # used for pretty display
11
+ import pandas as pd
12
+
13
+ # %% ../nbs/self_study_prompts.ipynb 5
14
+ MC_QUIZ_DEFAULT = "Please design a 5 question multiple choice quiz about the provided text."
15
+
16
+ SHORT_ANSWER_DEFAULT = ("Please design a 5 question short answer quiz about the provided text. "
17
+ "The question types should be short answer. Expect the correct answers to be a few sentences long.")
18
+
19
+ FILL_BLANK_DEFAULT = """Create a 5 question fill in the blank quiz referencing parts of the provided text.
20
+ The "blank" part of the question should appear as "________". The answers should reflect what word(s) should go in the blank an accurate statement.
21
+ An example is as follows: "The author of the book is ______." The question should be a statement.
22
+ """
23
+
24
+ SEQUENCING_DEFAULT = """Create a 5 question questionnaire that will ask me to recall the steps or sequence of events
25
+ in the provided text."""
26
+
27
+ RELATIONSHIP_DEFAULT = ("Create a 5 question quiz for the student that asks the student to identify relationships between"
28
+ "topics or concepts that are important to understanding this text.")
29
+
30
+ CONCEPTS_DEFAULT = """ Design a 5 question quiz that asks me about definitions or concepts of importance in the provided text."""
31
+
32
+ REAL_WORLD_EXAMPLE_DEFAULT = """Demonstrate how the provided context can be applied to solve a real world problem.
33
+ Ask me questions about how the demonstration you provided relates to solving a real world problem."""
34
+
35
+ RANDOMIZED_QUESTIONS_DEFAULT = """Generate a high-quality assessment consisting of 5 varied questions,
36
+ each of different types (open-ended, multiple choice, short answer, analogies, etc.)"""
37
+
38
+ SELF_STUDY_PROMPT_NAMES = ['MC_QUIZ_DEFAULT',
39
+ 'SHORT_ANSWER_DEFAULT',
40
+ 'FILL_BLANK_DEFAULT',
41
+ 'SEQUENCING_DEFAULT',
42
+ 'RELATIONSHIP_DEFAULT',
43
+ 'CONCEPTS_DEFAULT',
44
+ 'REAL_WORLD_EXAMPLE_DEFAULT',
45
+ 'RANDOMIZED_QUESTIONS_DEFAULT']
46
+
47
+ # %% ../nbs/self_study_prompts.ipynb 7
48
+ # Define self study dictionary for lookup
49
+ SELF_STUDY_DEFAULTS = {'mc': MC_QUIZ_DEFAULT,
50
+ 'short_answer': SHORT_ANSWER_DEFAULT,
51
+ 'fill_blank': FILL_BLANK_DEFAULT,
52
+ 'sequencing': SEQUENCING_DEFAULT,
53
+ 'relationships': RELATIONSHIP_DEFAULT,
54
+ 'concepts': CONCEPTS_DEFAULT,
55
+ 'real_world_example': REAL_WORLD_EXAMPLE_DEFAULT,
56
+ 'randomized_questions': RANDOMIZED_QUESTIONS_DEFAULT
57
+ }
58
+
59
+ # Return list of all self study prompts
60
+ def list_all_self_study_prompt_keys():
61
+ return list(SELF_STUDY_DEFAULTS.keys())
62
+
63
+ def list_all_self_study_prompts():
64
+ return list(SELF_STUDY_DEFAULTS.values())
65
+
66
+ # Return list of all self study variable names
67
+ def list_default_self_prompt_varnames():
68
+ return SELF_STUDY_PROMPT_NAMES
69
+
70
+ # Print as a table
71
+ def print_all_self_study_prompts():
72
+ with pd.option_context('max_colwidth', None):
73
+ display(pd.DataFrame({'SELF_STUDY_DEFAULTS key': list(SELF_STUDY_DEFAULTS.keys()),
74
+ 'Prompt': list(SELF_STUDY_DEFAULTS.values())}))
75
+
__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ __version__ = "0.0.1"
_modidx.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Autogenerated by nbdev
2
+
3
+ d = { 'settings': { 'branch': 'main',
4
+ 'doc_baseurl': '/lo-achievement',
5
+ 'doc_host': 'https://vanderbilt-data-science.github.io',
6
+ 'git_url': 'https://github.com/vanderbilt-data-science/lo-achievement',
7
+ 'lib_path': 'ai_classroom_suite'},
8
+ 'syms': { 'ai_classroom_suite.IOHelperUtilities': { 'ai_classroom_suite.IOHelperUtilities.MultiFileChooser': ( 'helper_utilities.html#multifilechooser',
9
+ 'ai_classroom_suite/IOHelperUtilities.py'),
10
+ 'ai_classroom_suite.IOHelperUtilities.MultiFileChooser.__init__': ( 'helper_utilities.html#multifilechooser.__init__',
11
+ 'ai_classroom_suite/IOHelperUtilities.py'),
12
+ 'ai_classroom_suite.IOHelperUtilities.MultiFileChooser.display': ( 'helper_utilities.html#multifilechooser.display',
13
+ 'ai_classroom_suite/IOHelperUtilities.py'),
14
+ 'ai_classroom_suite.IOHelperUtilities.MultiFileChooser.file_selected': ( 'helper_utilities.html#multifilechooser.file_selected',
15
+ 'ai_classroom_suite/IOHelperUtilities.py'),
16
+ 'ai_classroom_suite.IOHelperUtilities.MultiFileChooser.get_selected_files': ( 'helper_utilities.html#multifilechooser.get_selected_files',
17
+ 'ai_classroom_suite/IOHelperUtilities.py'),
18
+ 'ai_classroom_suite.IOHelperUtilities.MultiFileChooser.remove_file': ( 'helper_utilities.html#multifilechooser.remove_file',
19
+ 'ai_classroom_suite/IOHelperUtilities.py'),
20
+ 'ai_classroom_suite.IOHelperUtilities.MultiFileChooser.update_display': ( 'helper_utilities.html#multifilechooser.update_display',
21
+ 'ai_classroom_suite/IOHelperUtilities.py'),
22
+ 'ai_classroom_suite.IOHelperUtilities.check_is_colab': ( 'helper_utilities.html#check_is_colab',
23
+ 'ai_classroom_suite/IOHelperUtilities.py'),
24
+ 'ai_classroom_suite.IOHelperUtilities.setup_drives': ( 'helper_utilities.html#setup_drives',
25
+ 'ai_classroom_suite/IOHelperUtilities.py')},
26
+ 'ai_classroom_suite.MediaVectorStores': { 'ai_classroom_suite.MediaVectorStores._file_to_text': ( 'media_stores.html#_file_to_text',
27
+ 'ai_classroom_suite/MediaVectorStores.py'),
28
+ 'ai_classroom_suite.MediaVectorStores.create_local_vector_store': ( 'media_stores.html#create_local_vector_store',
29
+ 'ai_classroom_suite/MediaVectorStores.py'),
30
+ 'ai_classroom_suite.MediaVectorStores.files_to_text': ( 'media_stores.html#files_to_text',
31
+ 'ai_classroom_suite/MediaVectorStores.py'),
32
+ 'ai_classroom_suite.MediaVectorStores.get_document_segments': ( 'media_stores.html#get_document_segments',
33
+ 'ai_classroom_suite/MediaVectorStores.py'),
34
+ 'ai_classroom_suite.MediaVectorStores.get_youtube_transcript': ( 'media_stores.html#get_youtube_transcript',
35
+ 'ai_classroom_suite/MediaVectorStores.py'),
36
+ 'ai_classroom_suite.MediaVectorStores.rawtext_to_doc_split': ( 'media_stores.html#rawtext_to_doc_split',
37
+ 'ai_classroom_suite/MediaVectorStores.py'),
38
+ 'ai_classroom_suite.MediaVectorStores.save_text': ( 'media_stores.html#save_text',
39
+ 'ai_classroom_suite/MediaVectorStores.py'),
40
+ 'ai_classroom_suite.MediaVectorStores.website_to_text_unstructured': ( 'media_stores.html#website_to_text_unstructured',
41
+ 'ai_classroom_suite/MediaVectorStores.py'),
42
+ 'ai_classroom_suite.MediaVectorStores.website_to_text_web': ( 'media_stores.html#website_to_text_web',
43
+ 'ai_classroom_suite/MediaVectorStores.py'),
44
+ 'ai_classroom_suite.MediaVectorStores.youtube_to_text': ( 'media_stores.html#youtube_to_text',
45
+ 'ai_classroom_suite/MediaVectorStores.py')},
46
+ 'ai_classroom_suite.PromptInteractionBase': { 'ai_classroom_suite.PromptInteractionBase.create_base_tutoring_prompt': ( 'prompt_interaction_base.html#create_base_tutoring_prompt',
47
+ 'ai_classroom_suite/PromptInteractionBase.py'),
48
+ 'ai_classroom_suite.PromptInteractionBase.create_model': ( 'prompt_interaction_base.html#create_model',
49
+ 'ai_classroom_suite/PromptInteractionBase.py'),
50
+ 'ai_classroom_suite.PromptInteractionBase.create_tutor_mdl_chain': ( 'prompt_interaction_base.html#create_tutor_mdl_chain',
51
+ 'ai_classroom_suite/PromptInteractionBase.py'),
52
+ 'ai_classroom_suite.PromptInteractionBase.get_tutoring_answer': ( 'prompt_interaction_base.html#get_tutoring_answer',
53
+ 'ai_classroom_suite/PromptInteractionBase.py'),
54
+ 'ai_classroom_suite.PromptInteractionBase.get_tutoring_prompt': ( 'prompt_interaction_base.html#get_tutoring_prompt',
55
+ 'ai_classroom_suite/PromptInteractionBase.py'),
56
+ 'ai_classroom_suite.PromptInteractionBase.set_openai_key': ( 'prompt_interaction_base.html#set_openai_key',
57
+ 'ai_classroom_suite/PromptInteractionBase.py')},
58
+ 'ai_classroom_suite.SelfStudyPrompts': { 'ai_classroom_suite.SelfStudyPrompts.list_all_self_study_prompt_keys': ( 'self_study_prompts.html#list_all_self_study_prompt_keys',
59
+ 'ai_classroom_suite/SelfStudyPrompts.py'),
60
+ 'ai_classroom_suite.SelfStudyPrompts.list_all_self_study_prompts': ( 'self_study_prompts.html#list_all_self_study_prompts',
61
+ 'ai_classroom_suite/SelfStudyPrompts.py'),
62
+ 'ai_classroom_suite.SelfStudyPrompts.list_default_self_prompt_varnames': ( 'self_study_prompts.html#list_default_self_prompt_varnames',
63
+ 'ai_classroom_suite/SelfStudyPrompts.py'),
64
+ 'ai_classroom_suite.SelfStudyPrompts.print_all_self_study_prompts': ( 'self_study_prompts.html#print_all_self_study_prompts',
65
+ 'ai_classroom_suite/SelfStudyPrompts.py')},
66
+ 'ai_classroom_suite.self_study_app': { 'ai_classroom_suite.self_study_app.SlightlyDelusionalTutor': ( 'gradio_application.html#slightlydelusionaltutor',
67
+ 'ai_classroom_suite/self_study_app.py'),
68
+ 'ai_classroom_suite.self_study_app.SlightlyDelusionalTutor.__init__': ( 'gradio_application.html#slightlydelusionaltutor.__init__',
69
+ 'ai_classroom_suite/self_study_app.py'),
70
+ 'ai_classroom_suite.self_study_app.SlightlyDelusionalTutor.add_user_message': ( 'gradio_application.html#slightlydelusionaltutor.add_user_message',
71
+ 'ai_classroom_suite/self_study_app.py'),
72
+ 'ai_classroom_suite.self_study_app.SlightlyDelusionalTutor.forget_conversation': ( 'gradio_application.html#slightlydelusionaltutor.forget_conversation',
73
+ 'ai_classroom_suite/self_study_app.py'),
74
+ 'ai_classroom_suite.self_study_app.SlightlyDelusionalTutor.get_sources_memory': ( 'gradio_application.html#slightlydelusionaltutor.get_sources_memory',
75
+ 'ai_classroom_suite/self_study_app.py'),
76
+ 'ai_classroom_suite.self_study_app.SlightlyDelusionalTutor.get_tutor_reply': ( 'gradio_application.html#slightlydelusionaltutor.get_tutor_reply',
77
+ 'ai_classroom_suite/self_study_app.py'),
78
+ 'ai_classroom_suite.self_study_app.SlightlyDelusionalTutor.initialize_llm': ( 'gradio_application.html#slightlydelusionaltutor.initialize_llm',
79
+ 'ai_classroom_suite/self_study_app.py'),
80
+ 'ai_classroom_suite.self_study_app.add_user_message': ( 'gradio_application.html#add_user_message',
81
+ 'ai_classroom_suite/self_study_app.py'),
82
+ 'ai_classroom_suite.self_study_app.create_reference_store': ( 'gradio_application.html#create_reference_store',
83
+ 'ai_classroom_suite/self_study_app.py'),
84
+ 'ai_classroom_suite.self_study_app.disable_until_done': ( 'gradio_application.html#disable_until_done',
85
+ 'ai_classroom_suite/self_study_app.py'),
86
+ 'ai_classroom_suite.self_study_app.embed_key': ( 'gradio_application.html#embed_key',
87
+ 'ai_classroom_suite/self_study_app.py'),
88
+ 'ai_classroom_suite.self_study_app.get_tutor_reply': ( 'gradio_application.html#get_tutor_reply',
89
+ 'ai_classroom_suite/self_study_app.py'),
90
+ 'ai_classroom_suite.self_study_app.prompt_select': ( 'gradio_application.html#prompt_select',
91
+ 'ai_classroom_suite/self_study_app.py'),
92
+ 'ai_classroom_suite.self_study_app.save_chatbot_dialogue': ( 'gradio_application.html#save_chatbot_dialogue',
93
+ 'ai_classroom_suite/self_study_app.py')}}}
self_study_app.py ADDED
@@ -0,0 +1,358 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/gradio_application.ipynb.
2
+
3
+ # %% auto 0
4
+ __all__ = ['save_pdf', 'save_json', 'save_txt', 'save_csv', 'num_sources', 'css', 'save_chatbot_dialogue',
5
+ 'SlightlyDelusionalTutor', 'embed_key', 'create_reference_store', 'prompt_select', 'add_user_message',
6
+ 'get_tutor_reply', 'disable_until_done']
7
+
8
+ # %% ../nbs/gradio_application.ipynb 9
9
+ import gradio as gr
10
+ from functools import partial
11
+ import pandas as pd
12
+ import os
13
+
14
+ from .PromptInteractionBase import *
15
+ from .IOHelperUtilities import *
16
+ from .SelfStudyPrompts import *
17
+ from .MediaVectorStores import *
18
+
19
+ # %% ../nbs/gradio_application.ipynb 13
20
+ def save_chatbot_dialogue(chat_tutor, save_type):
21
+
22
+ formatted_convo = pd.DataFrame(chat_tutor.conversation_memory, columns=['user', 'chatbot'])
23
+
24
+ output_fname = f'tutoring_conversation.{save_type}'
25
+
26
+ if save_type == 'csv':
27
+ formatted_convo.to_csv(output_fname, index=False)
28
+ elif save_type == 'json':
29
+ formatted_convo.to_json(output_fname, orient='records')
30
+ elif save_type == 'txt':
31
+ temp = formatted_convo.apply(lambda x: 'User: {0}\nAI: {1}'.format(x[0], x[1]), axis=1)
32
+ temp = '\n\n'.join(temp.tolist())
33
+ with open(output_fname, 'w') as f:
34
+ f.write(temp)
35
+ else:
36
+ gr.update(value=None, visible=False)
37
+
38
+ return gr.update(value=output_fname, visible=True)
39
+
40
+ save_pdf = partial(save_chatbot_dialogue, save_type='pdf')
41
+ save_json = partial(save_chatbot_dialogue, save_type='json')
42
+ save_txt = partial(save_chatbot_dialogue, save_type='txt')
43
+ save_csv = partial(save_chatbot_dialogue, save_type='csv')
44
+
45
+
46
+ # %% ../nbs/gradio_application.ipynb 16
47
+ class SlightlyDelusionalTutor:
48
+ # create basic initialization function
49
+ def __init__(self, model_name = None):
50
+
51
+ # create default model name
52
+ if model_name is None:
53
+ self.model_name = 'gpt-3.5-turbo-16k'
54
+
55
+ self.chat_llm = None
56
+ self.tutor_chain = None
57
+ self.vector_store = None
58
+ self.vs_retriever = None
59
+ self.conversation_memory = []
60
+ self.sources_memory = []
61
+ self.flattened_conversation = ''
62
+ self.api_key_valid = False
63
+ self.learning_objectives = None
64
+ self.openai_auth = ''
65
+
66
+ def initialize_llm(self):
67
+
68
+ if self.openai_auth:
69
+ try:
70
+ self.chat_llm = create_model(self.model_name, openai_api_key = self.openai_auth)
71
+ self.api_key_valid = True
72
+ except Exception as e:
73
+ print(e)
74
+ self.api_key_valid = False
75
+ else:
76
+ print("Please provide an OpenAI API key and press Enter.")
77
+
78
+ def add_user_message(self, user_message):
79
+ self.conversation_memory.append([user_message, None])
80
+ self.flattened_conversation = self.flattened_conversation + '\n\n' + 'User: ' + user_message
81
+
82
+ def get_tutor_reply(self, **input_kwargs):
83
+
84
+ if not self.conversation_memory:
85
+ return "Please type something to start the conversation."
86
+
87
+ # we want to have a different vector comparison for reference lookup after the topic is first used
88
+ if len(self.conversation_memory) > 1:
89
+ if 'question' in input_kwargs.keys():
90
+ if input_kwargs['question']:
91
+ input_kwargs['question'] = self.conversation_memory[-1][0] + ' keeping in mind I want to learn about ' + input_kwargs['question']
92
+ else:
93
+ input_kwargs['question'] = self.conversation_memory[-1][0]
94
+
95
+ # get tutor message
96
+ tutor_message = get_tutoring_answer(None,
97
+ self.tutor_chain,
98
+ assessment_request = self.flattened_conversation + 'First, please provide your feedback on my previous answer if I was answering a question, otherwise, respond appropriately to my statement. Then, help me with the following:' + self.conversation_memory[-1][0],
99
+ learning_objectives = self.learning_objectives,
100
+ return_dict=True,
101
+ **input_kwargs)
102
+
103
+ # add tutor message to conversation memory
104
+ self.conversation_memory[-1][1] = tutor_message['answer']
105
+ self.flattened_conversation = self.flattened_conversation + '\nAI: ' + tutor_message['answer']
106
+ self.sources_memory.append(tutor_message['source_documents'])
107
+ #print(self.flattened_conversation, '\n\n')
108
+ print(tutor_message['source_documents'])
109
+
110
+ def get_sources_memory(self):
111
+ # retrieve last source
112
+ last_sources = self.sources_memory[-1]
113
+
114
+ # get page_content keyword from last_sources
115
+ doc_contents = ['Source ' + str(ind+1) + '\n"' + doc.page_content + '"\n\n' for ind, doc in enumerate(last_sources)]
116
+ doc_contents = ''.join(doc_contents)
117
+
118
+ return doc_contents
119
+
120
+ def forget_conversation(self):
121
+ self.conversation_memory = []
122
+ self.sources_memory = []
123
+ self.flattened_conversation = ''
124
+
125
+ # %% ../nbs/gradio_application.ipynb 18
126
+ def embed_key(openai_api_key, chat_tutor):
127
+ if not openai_api_key:
128
+ return chat_tutor
129
+
130
+ # Otherwise, update key
131
+ os.environ["OPENAI_API_KEY"] = openai_api_key
132
+
133
+ #update tutor
134
+ chat_tutor.openai_auth = openai_api_key
135
+
136
+ if not chat_tutor.api_key_valid:
137
+ chat_tutor.initialize_llm()
138
+
139
+ return chat_tutor
140
+
141
+ # %% ../nbs/gradio_application.ipynb 20
142
+ def create_reference_store(chat_tutor, vs_button, text_cp, upload_files, reference_vs, openai_auth, learning_objs):
143
+
144
+ text_segs = []
145
+ upload_segs = []
146
+
147
+ if reference_vs:
148
+ raise NotImplementedError("Reference Vector Stores are not yet implemented")
149
+
150
+ if text_cp.strip():
151
+ text_segs = get_document_segments(text_cp, 'text', chunk_size=700, chunk_overlap=100)
152
+ [doc.metadata.update({'source':'text box'}) for doc in text_segs];
153
+
154
+ if upload_files:
155
+ print(upload_files)
156
+ upload_fnames = [f.name for f in upload_files]
157
+ upload_segs = get_document_segments(upload_fnames, 'file', chunk_size=700, chunk_overlap=100)
158
+
159
+ # get the full list of everything
160
+ all_segs = text_segs + upload_segs
161
+ print(all_segs)
162
+
163
+ # create the vector store and update tutor
164
+ vs_db, vs_retriever = create_local_vector_store(all_segs, search_kwargs={"k": 2})
165
+ chat_tutor.vector_store = vs_db
166
+ chat_tutor.vs_retriever = vs_retriever
167
+
168
+ # create the tutor chain
169
+ if not chat_tutor.api_key_valid or not chat_tutor.openai_auth:
170
+ chat_tutor = embed_key(openai_auth, chat_tutor)
171
+ qa_chain = create_tutor_mdl_chain(kind="retrieval_qa", mdl=chat_tutor.chat_llm, retriever = chat_tutor.vs_retriever, return_source_documents=True)
172
+ chat_tutor.tutor_chain = qa_chain
173
+
174
+ # store learning objectives
175
+ chat_tutor.learning_objectives = learning_objs
176
+
177
+ # return the story
178
+ return chat_tutor, gr.update(interactive=True, value='Tutor Initialized!')
179
+
180
+ # %% ../nbs/gradio_application.ipynb 22
181
+ ### Gradio Called Functions ###
182
+
183
+ def prompt_select(selection, number, length):
184
+ if selection == "Random":
185
+ prompt = f"Please design a {number} question quiz based on the context provided and the inputted learning objectives (if applicable). The types of questions should be randomized (including multiple choice, short answer, true/false, short answer, etc.). Provide one question at a time, and wait for my response before providing me with feedback. Again, while the quiz may ask for multiple questions, you should only provide 1 question in you initial response. Do not include the answer in your response. If I get an answer wrong, provide me with an explanation of why it was incorrect, and then give me additional chances to respond until I get the correct choice. Explain why the correct choice is right."
186
+ elif selection == "Fill in the Blank":
187
+ prompt = f"Create a {number} question fill in the blank quiz refrencing the context provided. The quiz should reflect the learning objectives (if inputted). The 'blank' part of the question should appear as '________'. The answers should reflect what word(s) should go in the blank an accurate statement. An example is the follow: 'The author of the article is ______.' The question should be a statement. Provide one question at a time, and wait for my response before providing me with feedback. Again, while the quiz may ask for multiple questions, you should only provide ONE question in you initial response. Do not include the answer in your response. If I get an answer wrong, provide me with an explanation of why it was incorrect,and then give me additional chances to respond until I get the correct choice. Explain why the correct choice is right."
188
+ elif selection == "Short Answer":
189
+ prompt = f"Please design a {number} question quiz about which reflects the learning objectives (if inputted). The questions should be short answer. Expect the correct answers to be {length} sentences long. Provide one question at a time, and wait for my response before providing me with feedback. Again, while the quiz may ask for multiple questions, you should only provide ONE question in you initial response. Do not include the answer in your response. If I get an answer wrong, provide me with an explanation of why it was incorrect, and then give me additional chances to respond until I get the correct choice. Explain why the correct answer is right."
190
+ else:
191
+ prompt = f"Please design a {number} question {selection.lower()} quiz based on the context provided and the inputted learning objectives (if applicable). Provide one question at a time, and wait for my response before providing me with feedback. Again, while the quiz may ask for multiple questions, you should only provide 1 question in you initial response. Do not include the answer in your response. If I get an answer wrong, provide me with an explanation of why it was incorrect, and then give me additional chances to respond until I get the correct choice. Explain why the correct choice is right."
192
+ return prompt, prompt
193
+
194
+
195
+ # %% ../nbs/gradio_application.ipynb 24
196
+ ### Chatbot Functions ###
197
+
198
+ def add_user_message(user_message, chat_tutor):
199
+ """Display user message and update chat history to include it.
200
+ Also disables user text input until bot is finished (call to reenable_chat())
201
+ See https://gradio.app/creating-a-chatbot/"""
202
+ chat_tutor.add_user_message(user_message)
203
+ return gr.update(value="", interactive=False), chat_tutor.conversation_memory, chat_tutor
204
+
205
+ def get_tutor_reply(learning_topic, chat_tutor):
206
+ chat_tutor.get_tutor_reply(input_kwargs={'question':learning_topic})
207
+ return gr.update(value="", interactive=True), gr.update(visible=True, value=chat_tutor.get_sources_memory()), chat_tutor.conversation_memory, chat_tutor
208
+
209
+ num_sources = 2
210
+
211
+ # %% ../nbs/gradio_application.ipynb 25
212
+ def disable_until_done(obj_in):
213
+ return gr.update(interactive=False)
214
+
215
+ # %% ../nbs/gradio_application.ipynb 27
216
+ # See https://gradio.app/custom-CSS-and-JS/
217
+ css="""
218
+ #sources-container {
219
+ overflow: scroll !important; /* Needs to override default formatting */
220
+ /*max-height: 20em; */ /* Arbitrary value */
221
+ }
222
+ #sources-container > div { padding-bottom: 1em !important; /* Arbitrary value */ }
223
+ .short-height > * > * { min-height: 0 !important; }
224
+ .translucent { opacity: 0.5; }
225
+ .textbox_label { padding-bottom: .5em; }
226
+ """
227
+ #srcs = [] # Reset sources (db and qa are kept the same for ease of testing)
228
+
229
+ with gr.Blocks(css=css, analytics_enabled=False) as demo:
230
+
231
+ #initialize tutor (with state)
232
+ study_tutor = gr.State(SlightlyDelusionalTutor())
233
+
234
+ # Title
235
+ gr.Markdown("# Studying with a Slightly Delusional Tutor")
236
+
237
+ # API Authentication functionality
238
+ with gr.Box():
239
+ gr.Markdown("### OpenAI API Key ")
240
+ gr.HTML("""<span>Embed your OpenAI API key below; if you haven't created one already, visit
241
+ <a href="https://platform.openai.com/account/api-keys">platform.openai.com/account/api-keys</a>
242
+ to sign up for an account and get your personal API key</span>""",
243
+ elem_classes="textbox_label")
244
+ api_input = gr.Textbox(show_label=False, type="password", container=False, autofocus=True,
245
+ placeholder="●●●●●●●●●●●●●●●●●", value='')
246
+ api_input.submit(fn=embed_key, inputs=[api_input, study_tutor], outputs=study_tutor)
247
+ api_input.blur(fn=embed_key, inputs=[api_input, study_tutor], outputs=study_tutor)
248
+
249
+ # Reference document functionality (building vector stores)
250
+ with gr.Box():
251
+ gr.Markdown("### Add Reference Documents")
252
+ # TODO Add entry for path to vector store (should be disabled for now)
253
+ with gr.Row(equal_height=True):
254
+ text_input = gr.TextArea(label='Copy and paste your text below',
255
+ lines=2)
256
+
257
+ file_input = gr.Files(label="Load a .txt or .pdf file",
258
+ file_types=['.pdf', '.txt'], type="file",
259
+ elem_classes="short-height")
260
+
261
+ instructor_input = gr.TextArea(label='Enter vector store URL, if given by instructor (WIP)', value='',
262
+ lines=2, interactive=False, elem_classes="translucent")
263
+
264
+ # Adding the learning objectives
265
+ with gr.Box():
266
+ gr.Markdown("### Optional: Enter Your Learning Objectives")
267
+ learning_objectives = gr.Textbox(label='If provided by your instructor, please input your learning objectives for this session', value='')
268
+
269
+ # Adding the button to submit all of the settings and create the Chat Tutor Chain.
270
+ with gr.Row():
271
+ vs_build_button = gr.Button(value = 'Start Studying with Your Tutor!', scale=1)
272
+ vs_build_button.click(disable_until_done, vs_build_button, vs_build_button) \
273
+ .then(create_reference_store, [study_tutor, vs_build_button, text_input, file_input, instructor_input, api_input, learning_objectives],
274
+ [study_tutor, vs_build_button])
275
+
276
+
277
+
278
+ # Premade question prompts
279
+ with gr.Box():
280
+ gr.Markdown("""
281
+ ## Generate a Premade Prompt
282
+ Select your type and number of desired questions. Click "Generate Prompt" to get your premade prompt,
283
+ and then "Insert Prompt into Chat" to copy the text into the chat interface below. \
284
+ You can also copy the prompt using the icon in the upper right corner and paste directly into the input box when interacting with the model.
285
+ """)
286
+ with gr.Row():
287
+ with gr.Column():
288
+ question_type = gr.Dropdown(["Multiple Choice", "True or False", "Short Answer", "Fill in the Blank", "Random"], label="Question Type")
289
+ number_of_questions = gr.Textbox(label="Enter desired number of questions")
290
+ sa_desired_length = gr.Dropdown(["1-2", "3-4", "5-6", "6 or more"], label = "For short answer questions only, choose the desired sentence length for answers. The default value is 1-2 sentences.")
291
+ with gr.Column():
292
+ prompt_button = gr.Button("Generate Prompt")
293
+ premade_prompt_output = gr.Textbox(label="Generated prompt (save or copy)", show_copy_button=True)
294
+
295
+
296
+ # Chatbot interface
297
+ gr.Markdown("## Chat with the Model")
298
+ topic_input = gr.Textbox(label="What topic or concept are you trying to learn more about?")
299
+ with gr.Row(equal_height=True):
300
+ with gr.Column(scale=2):
301
+ chatbot = gr.Chatbot()
302
+ with gr.Row():
303
+ user_chat_input = gr.Textbox(label="User input", scale=9)
304
+ user_chat_submit = gr.Button("Ask/answer model", scale=1)
305
+
306
+ # sources
307
+ with gr.Box(elem_id="sources-container", scale=1):
308
+ # TODO: Display document sources in a nicer format?
309
+ gr.HTML(value="<h3 id='sources'>Referenced Sources</h3>")
310
+ sources_output = gr.Textbox(value='', interactive=False, visible=False, show_label=False)
311
+ #sources_output = []
312
+ #for i in range(num_sources):
313
+ # source_elem = gr.HTML(visible=False)
314
+ # sources_output.append(source_elem)
315
+
316
+ #define the behavior of prompt button later since it depends on user_chat_input
317
+ prompt_button.click(prompt_select,
318
+ inputs=[question_type, number_of_questions, sa_desired_length],
319
+ outputs=[premade_prompt_output, user_chat_input])
320
+
321
+ # Display input and output in three-ish parts
322
+ # (using asynchronous functions):
323
+ # First show user input, then show model output when complete
324
+ # Then wait until the bot provides response and return the result
325
+ # Finally, allow the user to ask a new question by reenabling input
326
+ async_response = user_chat_submit.click(add_user_message,
327
+ [user_chat_input, study_tutor],
328
+ [user_chat_input, chatbot, study_tutor], queue=False) \
329
+ .then(get_tutor_reply, [topic_input, study_tutor], [user_chat_input, sources_output, chatbot, study_tutor], queue=True)
330
+
331
+ async_response_b = user_chat_input.submit(add_user_message,
332
+ [user_chat_input, study_tutor],
333
+ [user_chat_input, chatbot, study_tutor], queue=False) \
334
+ .then(get_tutor_reply, [topic_input, study_tutor], [user_chat_input, sources_output, chatbot, study_tutor], queue=True)
335
+
336
+ with gr.Blocks():
337
+ gr.Markdown("""
338
+ ## Export Your Chat History
339
+ Export your chat history as a .json, PDF file, .txt, or .csv file
340
+ """)
341
+ with gr.Row():
342
+ export_dialogue_button_json = gr.Button("JSON")
343
+ export_dialogue_button_pdf = gr.Button("PDF")
344
+ export_dialogue_button_txt = gr.Button("TXT")
345
+ export_dialogue_button_csv = gr.Button("CSV")
346
+
347
+ file_download = gr.Files(label="Download here",
348
+ file_types=['.pdf', '.txt', '.csv', '.json'], type="file", visible=False)
349
+
350
+ export_dialogue_button_json.click(save_json, study_tutor, file_download, show_progress=True)
351
+ export_dialogue_button_pdf.click(save_pdf, study_tutor, file_download, show_progress=True)
352
+ export_dialogue_button_txt.click(save_txt, study_tutor, file_download, show_progress=True)
353
+ export_dialogue_button_csv.click(save_csv, study_tutor, file_download, show_progress=True)
354
+
355
+ demo.queue()
356
+ demo.launch(debug=True)
357
+ #demo.launch()
358
+ #gr.close_all()