diff --git a/jupyter_mentor/_modidx.py b/jupyter_mentor/_modidx.py index 2935d13..2dd6c9e 100644 --- a/jupyter_mentor/_modidx.py +++ b/jupyter_mentor/_modidx.py @@ -5,28 +5,29 @@ 'doc_host': 'https://Jupyter4Science.github.io', 'git_url': 'https://github.com/Jupyter4Science/jupyter-mentor', 'lib_path': 'jupyter_mentor'}, - 'syms': { 'jupyter_mentor.chatbot': { 'jupyter_mentor.chatbot.ChatBot': ('chatbot.html#chatbot', 'jupyter_mentor/chatbot.py'), - 'jupyter_mentor.chatbot.ChatBot.__init__': ( 'chatbot.html#chatbot.__init__', - 'jupyter_mentor/chatbot.py'), - 'jupyter_mentor.chatbot.ChatBot.on_click': ( 'chatbot.html#chatbot.on_click', - 'jupyter_mentor/chatbot.py'), - 'jupyter_mentor.chatbot.ChatBotModel': ('chatbot.html#chatbotmodel', 'jupyter_mentor/chatbot.py'), - 'jupyter_mentor.chatbot.ChatBotModel.__init__': ( 'chatbot.html#chatbotmodel.__init__', - 'jupyter_mentor/chatbot.py'), - 'jupyter_mentor.chatbot.ChatBotModel.prompt': ( 'chatbot.html#chatbotmodel.prompt', - 'jupyter_mentor/chatbot.py'), - 'jupyter_mentor.chatbot.ChatBotModel.prompt_course_files': ( 'chatbot.html#chatbotmodel.prompt_course_files', - 'jupyter_mentor/chatbot.py'), - 'jupyter_mentor.chatbot.ChatBotModel.prompt_with_template': ( 'chatbot.html#chatbotmodel.prompt_with_template', - 'jupyter_mentor/chatbot.py'), - 'jupyter_mentor.chatbot.ChatBotModel.update_bot_template': ( 'chatbot.html#chatbotmodel.update_bot_template', - 'jupyter_mentor/chatbot.py'), - 'jupyter_mentor.chatbot.ChatBotModel.update_human_template': ( 'chatbot.html#chatbotmodel.update_human_template', - 'jupyter_mentor/chatbot.py'), - 'jupyter_mentor.chatbot.ChatBotView': ('chatbot.html#chatbotview', 'jupyter_mentor/chatbot.py'), - 'jupyter_mentor.chatbot.ChatBotView.__init__': ( 'chatbot.html#chatbotview.__init__', - 'jupyter_mentor/chatbot.py')}, - 'jupyter_mentor.chatbot_tab': { 'jupyter_mentor.chatbot_tab.EducatorChatBot': ( 'chatbot.html#educatorchatbot', + 'syms': { 'jupyter_mentor.chatbot': {}, + 'jupyter_mentor.chatbot_tab': { 'jupyter_mentor.chatbot_tab.ChatBot': ('chatbot.html#chatbot', 'jupyter_mentor/chatbot_tab.py'), + 'jupyter_mentor.chatbot_tab.ChatBot.__init__': ( 'chatbot.html#chatbot.__init__', + 'jupyter_mentor/chatbot_tab.py'), + 'jupyter_mentor.chatbot_tab.ChatBot.on_click': ( 'chatbot.html#chatbot.on_click', + 'jupyter_mentor/chatbot_tab.py'), + 'jupyter_mentor.chatbot_tab.ChatBotModel': ( 'chatbot.html#chatbotmodel', + 'jupyter_mentor/chatbot_tab.py'), + 'jupyter_mentor.chatbot_tab.ChatBotModel.__init__': ( 'chatbot.html#chatbotmodel.__init__', + 'jupyter_mentor/chatbot_tab.py'), + 'jupyter_mentor.chatbot_tab.ChatBotModel.prompt': ( 'chatbot.html#chatbotmodel.prompt', + 'jupyter_mentor/chatbot_tab.py'), + 'jupyter_mentor.chatbot_tab.ChatBotModel.prompt_with_template': ( 'chatbot.html#chatbotmodel.prompt_with_template', + 'jupyter_mentor/chatbot_tab.py'), + 'jupyter_mentor.chatbot_tab.ChatBotModel.update_bot_template': ( 'chatbot.html#chatbotmodel.update_bot_template', + 'jupyter_mentor/chatbot_tab.py'), + 'jupyter_mentor.chatbot_tab.ChatBotModel.update_human_template': ( 'chatbot.html#chatbotmodel.update_human_template', + 'jupyter_mentor/chatbot_tab.py'), + 'jupyter_mentor.chatbot_tab.ChatBotView': ( 'chatbot.html#chatbotview', + 'jupyter_mentor/chatbot_tab.py'), + 'jupyter_mentor.chatbot_tab.ChatBotView.__init__': ( 'chatbot.html#chatbotview.__init__', + 'jupyter_mentor/chatbot_tab.py'), + 'jupyter_mentor.chatbot_tab.EducatorChatBot': ( 'chatbot.html#educatorchatbot', 'jupyter_mentor/chatbot_tab.py'), 'jupyter_mentor.chatbot_tab.EducatorChatBot.__init__': ( 'chatbot.html#educatorchatbot.__init__', 'jupyter_mentor/chatbot_tab.py'), @@ -66,13 +67,12 @@ 'jupyter_mentor.llm.LLM.__init__': ('llm.html#llm.__init__', 'jupyter_mentor/llm.py')}, 'jupyter_mentor.login': { 'jupyter_mentor.login.Login': ('login.html#login', 'jupyter_mentor/login.py'), 'jupyter_mentor.login.Login.__init__': ('login.html#login.__init__', 'jupyter_mentor/login.py')}, - 'jupyter_mentor.madlib': { 'jupyter_mentor.madlib.MadLib': ('madlib.html#madlib', 'jupyter_mentor/madlib.py'), - 'jupyter_mentor.madlib.MadLib.__init__': ('madlib.html#madlib.__init__', 'jupyter_mentor/madlib.py'), - 'jupyter_mentor.madlib.MadLib.observe_submit_button': ( 'madlib.html#madlib.observe_submit_button', - 'jupyter_mentor/madlib.py'), - 'jupyter_mentor.madlib.MadLibModel': ('madlib.html#madlibmodel', 'jupyter_mentor/madlib.py'), + 'jupyter_mentor.madlib': { 'jupyter_mentor.madlib.MadLibModel': ('madlib.html#madlibmodel', 'jupyter_mentor/madlib.py'), 'jupyter_mentor.madlib.MadLibModel.__init__': ( 'madlib.html#madlibmodel.__init__', - 'jupyter_mentor/madlib.py')}, + 'jupyter_mentor/madlib.py'), + 'jupyter_mentor.madlib.MadLibView': ('madlib.html#madlibview', 'jupyter_mentor/madlib.py'), + 'jupyter_mentor.madlib.MadLibView.__init__': ( 'madlib.html#madlibview.__init__', + 'jupyter_mentor/madlib.py')}, 'jupyter_mentor.student_course_overview': { 'jupyter_mentor.student_course_overview.StudentCourseOverview': ( 'student_course_overview.html#studentcourseoverview', 'jupyter_mentor/student_course_overview.py'), 'jupyter_mentor.student_course_overview.StudentCourseOverview.__init__': ( 'student_course_overview.html#studentcourseoverview.__init__', diff --git a/jupyter_mentor/chatbot.py b/jupyter_mentor/chatbot.py index 6065a1f..61268b7 100644 --- a/jupyter_mentor/chatbot.py +++ b/jupyter_mentor/chatbot.py @@ -1,7 +1,7 @@ # AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/04_chatbot.ipynb. # %% auto 0 -__all__ = ['ChatBotModel', 'ChatBotView', 'ChatBot'] +__all__ = [] # %% ../nbs/04_chatbot.ipynb 1 import ipywidgets as widgets @@ -18,86 +18,3 @@ ) from langchain_openai import ChatOpenAI from .llm import FileModel - -# %% ../nbs/04_chatbot.ipynb 2 -class ChatBotModel(FileModel): - - def __init__(self, bot_template="You are playing the role of a tutor/educator", human_template="{input_text}"): - super().__init__() - self.human_template = human_template - self.human_message_prompt = HumanMessagePromptTemplate.from_template(self.human_template) - self.update_bot_template(bot_template) - - def update_bot_template(self, bot_template): - self.bot_template = bot_template - self.bot_message_prompt = SystemMessagePromptTemplate.from_template(self.bot_template) - self.chat_prompt = ChatPromptTemplate.from_messages([self.bot_message_prompt, self.human_message_prompt]) - - def update_human_template(self, human_template): - self.human_template = human_template - self.human_message_prompt = HumanMessagePromptTemplate.from_template(self.human_template) - self.chat_prompt = ChatPromptTemplate.from_messages([self.bot_message_prompt, self.human_message_prompt]) - - def prompt(self, input_text): - ret = self.llm.invoke(input_text) - return ret.content - - def prompt_with_template(self, kwargs): - ret = self.llm.invoke(self.chat_prompt.format_prompt(**kwargs)) - return ret.content - - def prompt_course_files(self, input_text, k = 4): - if self.db: - try: - docs = self.db.similarity_search(input_text, k=1) - docs_content = " ".join([d.page_content for d in docs]) - self.update_bot_template("You are an educator/tutor. Answer the question based on the following information: {docs}") - ret = self.llm.invoke(self.chat_prompt.format_prompt(docs=docs, input_text=input_text)) - return "\n\n".join([ret.content + '\n\nSourced from ' + docs[0].metadata['source'] + ': \n', docs_content]) - except Exception as e: - return docs[0].page_content - else: - return self.prompt(input_text) - -# %% ../nbs/04_chatbot.ipynb 6 -class ChatBotView(widgets.VBox): - - def __init__(self): - # If you forget to call the superconstructor on an extended widget - # you will get an AttributeError: object has no attribute '_model_id' - super().__init__() - - self.chat = Textarea( - disabled = True, - layout=Layout(width='90%', height='400px') - ) - self.user_input_and_submit = HBox() - self.user_input = widgets.Text( - placeholder='Message AI chatbot...', - #layout=Layout(width='100%') - ) - self.submit_button = widgets.Button( - value=False, - disabled=False, - button_style='success', - icon='arrow-circle-right' - ) - self.user_input_and_submit.children = (self.user_input, self.submit_button) - - self.children = (self.chat, self.user_input_and_submit) - -# %% ../nbs/04_chatbot.ipynb 8 -class ChatBot(ChatBotView): - - def __init__(self, model): - # If you forget to call the superconstructor on an extended widget - # you will get an AttributeError: object has no attribute '_model_id' - super().__init__() - self.submit_button.on_click(self.on_click) - self.model =model - - def on_click(self, change): - self.chat.value = self.chat.value + "USER: " + self.user_input.value + '\n\n' - self.user_input.value = '' - ret = self.model.prompt_course_files(self.user_input.value) - self.chat.value = self.chat.value + "CHATBOT: " + ret + '\n\n' diff --git a/jupyter_mentor/chatbot_tab.py b/jupyter_mentor/chatbot_tab.py index 614668e..45d9cfa 100644 --- a/jupyter_mentor/chatbot_tab.py +++ b/jupyter_mentor/chatbot_tab.py @@ -1,7 +1,7 @@ # AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/06_chatbot.ipynb. # %% auto 0 -__all__ = ['StudentChatBot', 'EducatorChatBot'] +__all__ = ['ChatBotModel', 'ChatBotView', 'ChatBot', 'StudentChatBot', 'EducatorChatBot'] # %% ../nbs/06_chatbot.ipynb 1 import ipywidgets as widgets @@ -17,9 +17,109 @@ SystemMessagePromptTemplate, ) from langchain_openai import ChatOpenAI -from .chatbot import ChatBot, ChatBotModel +#from jupyter_mentor.chatbot import ChatBot, ChatBotModel, ChatBotView +from .madlib import MadLibModel, MadLibView +from .llm import FileModel -# %% ../nbs/06_chatbot.ipynb 7 +# %% ../nbs/06_chatbot.ipynb 2 +class ChatBotModel(FileModel): + + def __init__(self, madlibs_list): + super().__init__() + self.human_template = "{input_text}" + self.bot_template = "You are playing the role of a tutor/educator" + self.human_message_prompt = HumanMessagePromptTemplate.from_template(self.human_template) + self.update_bot_template(self.bot_template) + + self.madlib_models = [] + for value in madlibs_list: + model = MadLibModel(**value) + self.madlib_models.append(model) + + def update_bot_template(self, bot_template): + self.bot_template = bot_template + self.bot_message_prompt = SystemMessagePromptTemplate.from_template(self.bot_template) + self.chat_prompt = ChatPromptTemplate.from_messages([self.bot_message_prompt, self.human_message_prompt]) + + def update_human_template(self, human_template): + self.human_template = human_template + self.human_message_prompt = HumanMessagePromptTemplate.from_template(self.human_template) + self.chat_prompt = ChatPromptTemplate.from_messages([self.bot_message_prompt, self.human_message_prompt]) + + def prompt(self, input_text): + ret = self.llm.invoke(input_text) + return ret.content + + def prompt_with_template(self, kwargs): + ret = self.llm.invoke(self.chat_prompt.format_prompt(**kwargs)) + return ret.content + +# %% ../nbs/06_chatbot.ipynb 6 +class ChatBotView(widgets.VBox): + """ This view also exists in 04_chatbot, but wea + are overwriting it here + """ + + def __init__(self): + # If you forget to call the superconstructor on an extended widget + # you will get an AttributeError: object has no attribute '_model_id' + super().__init__() + + self.chat = Textarea( + disabled = True, + layout=Layout(width='90%', height='400px') + ) + self.radio_buttons = widgets.RadioButtons() + self.hbox = widgets.HBox() + self.default = widgets.Text( + placeholder='Message AI chatbot...', + #layout=Layout(width='100%') + ) + self.stack = widgets.Stack(children = (self.default, )) + self.stack.selected_index = 0 + + self.submit_button = widgets.Button( + value=False, + disabled=False, + button_style='success', + icon='arrow-circle-right' + ) + self.hbox.children = (self.stack, self.submit_button) + self.children = (self.chat, self.radio_buttons, self.hbox ) + +# %% ../nbs/06_chatbot.ipynb 8 +class ChatBot(ChatBotView): + """ This view also exists in 04_chatbot, but wea + are overwriting it here + """ + selected_index = traitlets.Int() + + def __init__(self, model): + # If you forget to call the superconstructor on an extended widget + # you will get an AttributeError: object has no attribute '_model_id' + super().__init__() + self.model = model + + self.selected_model = 0 + + madlib_views = [] + for model in self.model.madlib_models: + view = MadLibView(model) + madlib_views.append(view) + self.stack.children += (view, ) + + #self.selected_index = 0 + traitlets.link((self, 'selected_index'), (self.stack, 'selected_index')) + + self.submit_button.on_click(self.on_click) + + def on_click(self, change): + self.chat.value = self.chat.value + "USER: " + self.stack.children[0].value + '\n\n' + self.stack.children[0].value = '' + ret = self.model.prompt(self.stack.children[0].value) + self.chat.value = self.chat.value + "CHATBOT: " + ret + '\n\n' + +# %% ../nbs/06_chatbot.ipynb 11 class StudentChatBot(widgets.VBox): #user = traitlets.CUnicode() @@ -60,7 +160,7 @@ def __init__(self, chatbot_model): self.suggestion_buttons.children = (self.step_by_step, self.metaphor, self.hints, self.guided_questions) self.children = (self.chat_bot, self.suggestion_buttons) -# %% ../nbs/06_chatbot.ipynb 9 +# %% ../nbs/06_chatbot.ipynb 13 class EducatorChatBot(widgets.VBox): #user = traitlets.CUnicode() diff --git a/jupyter_mentor/madlib.py b/jupyter_mentor/madlib.py index 4c6a253..0d98d74 100644 --- a/jupyter_mentor/madlib.py +++ b/jupyter_mentor/madlib.py @@ -1,7 +1,7 @@ # AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/05_madlib.ipynb. # %% auto 0 -__all__ = ['MadLibModel', 'MadLib'] +__all__ = ['MadLibModel', 'MadLibView'] # %% ../nbs/05_madlib.ipynb 1 import ipywidgets as widgets @@ -12,7 +12,9 @@ # %% ../nbs/05_madlib.ipynb 2 class MadLibModel(object): + def __init__(self, description, variables, template, **kwargs): + self.description = description # two ways to save variables... for key, value in variables.items(): # ... as attributes @@ -20,12 +22,13 @@ def __init__(self, description, variables, template, **kwargs): self.variables = variables # ...or as a dictionary, ex. { 'input_text': 'value' } self.template = template # string: '{input_text}' + # %% ../nbs/05_madlib.ipynb 5 -class MadLib(HBox): +class MadLibView(HBox): def __init__(self, model, **kwargs): super().__init__() - + self.model = model self.variables = {} for i, (key, value) in enumerate(self.model.variables.items()): @@ -38,9 +41,4 @@ def __init__(self, model, **kwargs): button_style='success', icon='arrow-circle-right' ) - self.submit_button.on_click(self.observe_submit_button) - self.children = list(self.variables.values()) + [self.submit_button] - - def observe_submit_button(self, change): - for key, value in self.variables.items(): - setattr(self.model, key, value.value) + self.children = list(self.variables.values()) diff --git a/nbs/04_chatbot.ipynb b/nbs/04_chatbot.ipynb index 576216d..a7e97d8 100644 --- a/nbs/04_chatbot.ipynb +++ b/nbs/04_chatbot.ipynb @@ -41,43 +41,44 @@ "metadata": {}, "outputs": [], "source": [ - "#|export\n", "class ChatBotModel(FileModel):\n", "\n", - " def __init__(self, bot_template=\"You are playing the role of a tutor/educator\", human_template=\"{input_text}\"):\n", + " bot_identity = traitlets.Unicode()\n", + " human_identity = traitlets.Unicode()\n", + "\n", + " def __init__(self, bot_template=\"\", human_template=\"{input_text}\"):\n", " super().__init__()\n", - " self.human_template = human_template\n", - " self.human_message_prompt = HumanMessagePromptTemplate.from_template(self.human_template)\n", + " self.bot_identity = \"You are playing the role of a tutor/educator\"\n", + " self.human_identity = \"I am a student\"\n", " self.update_bot_template(bot_template)\n", + " self.update_human_template(human_template)\n", "\n", " def update_bot_template(self, bot_template):\n", - " self.bot_template = bot_template\n", - " self.bot_message_prompt = SystemMessagePromptTemplate.from_template(self.bot_template)\n", - " self.chat_prompt = ChatPromptTemplate.from_messages([self.bot_message_prompt, self.human_message_prompt])\n", + " self.bot_message_prompt = SystemMessagePromptTemplate.from_template(\"\\n\\n\".join([self.bot_identity, bot_template]))\n", "\n", " def update_human_template(self, human_template):\n", - " self.human_template = human_template\n", - " self.human_message_prompt = HumanMessagePromptTemplate.from_template(self.human_template)\n", - " self.chat_prompt = ChatPromptTemplate.from_messages([self.bot_message_prompt, self.human_message_prompt]) \n", + " self.human_message_prompt = HumanMessagePromptTemplate.from_template(\"\\n\\n\".join([self.human_identity, human_template]))\n", "\n", " def prompt(self, input_text):\n", " ret = self.llm.invoke(input_text)\n", " return ret.content\n", " \n", " def prompt_with_template(self, kwargs):\n", + " self.chat_prompt = ChatPromptTemplate.from_messages([self.bot_message_prompt, self.human_message_prompt]) \n", " ret = self.llm.invoke(self.chat_prompt.format_prompt(**kwargs))\n", " return ret.content\n", "\n", - " def prompt_course_files(self, input_text, k = 4):\n", + " def prompt_course_files(self, input_text):\n", " if self.db:\n", " try:\n", " docs = self.db.similarity_search(input_text, k=1)\n", " docs_content = \" \".join([d.page_content for d in docs])\n", - " self.update_bot_template(\"You are an educator/tutor. Answer the question based on the following information: {docs}\")\n", + " self.update_bot_template(self.bot_identity + \"\\n\\nAnswer the question based on the following information: {docs}\\n\\n{input_text}\")\n", + " self.chat_prompt = ChatPromptTemplate.from_messages([self.bot_message_prompt, self.human_message_prompt]) \n", " ret = self.llm.invoke(self.chat_prompt.format_prompt(docs=docs, input_text=input_text)) \n", " return \"\\n\\n\".join([ret.content + '\\n\\nSourced from ' + docs[0].metadata['source'] + ': \\n', docs_content])\n", " except Exception as e: \n", - " return docs[0].page_content\n", + " return e\n", " else:\n", " return self.prompt(input_text)" ] @@ -91,7 +92,7 @@ "source": [ "chatbot_model = ChatBotModel()\n", "#chatbot_model.prompt('hello')\n", - "#echatbot_model.prompt_with_template({'input_text': 'hi'})" + "#chatbot_model.prompt_with_template({'input_text': 'hi'})" ] }, { @@ -101,7 +102,7 @@ "metadata": {}, "outputs": [], "source": [ - "#chatbot_model.load_pdf_to_db(\"course_files/STP 420 spring 2024 course syllabus.pdf\")" + "chatbot_model.load_pdf_to_db(\"course_files/STP 420 spring 2024 course syllabus.pdf\")" ] }, { @@ -111,7 +112,7 @@ "metadata": {}, "outputs": [], "source": [ - "#chatbot_model.prompt_course_files(\"what is this course about?\")" + "chatbot_model.prompt_course_files(\"when will we learn about integration?\", )" ] }, { @@ -121,7 +122,6 @@ "metadata": {}, "outputs": [], "source": [ - "#| export\n", "class ChatBotView(widgets.VBox):\n", " \n", " def __init__(self):\n", @@ -167,7 +167,6 @@ "metadata": {}, "outputs": [], "source": [ - "#| export\n", "class ChatBot(ChatBotView):\n", "\n", " def __init__(self, model):\n", diff --git a/nbs/05_madlib.ipynb b/nbs/05_madlib.ipynb index b064661..42377e9 100644 --- a/nbs/05_madlib.ipynb +++ b/nbs/05_madlib.ipynb @@ -34,13 +34,15 @@ "source": [ "#| export\n", "class MadLibModel(object):\n", + "\n", " def __init__(self, description, variables, template, **kwargs):\n", + " \n", " self.description = description\n", " # two ways to save variables... \n", " for key, value in variables.items(): # ... as attributes\n", " setattr(self, key, value)\n", " self.variables = variables # ...or as a dictionary, ex. { 'input_text': 'value' } \n", - " self.template = template # string: '{input_text}'" + " self.template = template # string: '{input_text}'\n" ] }, { @@ -79,11 +81,11 @@ "outputs": [], "source": [ "#| export\n", - "class MadLib(HBox):\n", + "class MadLibView(HBox):\n", " \n", " def __init__(self, model, **kwargs):\n", " super().__init__()\n", - "\n", + " \n", " self.model = model\n", " self.variables = {}\n", " for i, (key, value) in enumerate(self.model.variables.items()):\n", @@ -96,12 +98,7 @@ " button_style='success',\n", " icon='arrow-circle-right' \n", " )\n", - " self.submit_button.on_click(self.observe_submit_button)\n", - " self.children = list(self.variables.values()) + [self.submit_button]\n", - "\n", - " def observe_submit_button(self, change):\n", - " for key, value in self.variables.items():\n", - " setattr(self.model, key, value.value)" + " self.children = list(self.variables.values()) " ] }, { @@ -111,7 +108,7 @@ "metadata": {}, "outputs": [], "source": [ - "madlib = MadLib(madlib_model)\n", + "madlib = MadLibView(madlib_model)\n", "madlib" ] }, @@ -122,9 +119,17 @@ "metadata": {}, "outputs": [], "source": [ - "madlib.variables['input_text'].value = 'hiii'\n", - "madlib.observe_submit_button(None)\n", - "print(madlib.model.input_text)" + "madlib.variables['input_text'].value = 'hiii'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f5aae3a7-d025-4145-a6d0-54ccc66a4d2a", + "metadata": {}, + "outputs": [], + "source": [ + "madlib_model.variables" ] }, { @@ -142,16 +147,17 @@ "metadata": {}, "outputs": [], "source": [ - "madlibs_dict = {\n", - " 'Open Prompt': {\n", - " 'description': '',\n", - " 'variables': {\n", - " 'input_text': 'value'\n", + "madlibs_list = [\n", + " {'name': 'Open Prompt',\n", + " 'description': '',\n", + " 'variables': {\n", + " 'input_text': 'value'\n", " }, \n", " 'template': '{input_text}' ,\n", " 'placeholder': 'Message AI chatbot...',\n", - " }, \n", - " 'Metaphor': {\n", + " },\n", + " {\n", + " 'name': 'Metaphor',\n", " 'variables': {\n", " 'concept': 'value'\n", " }, \n", @@ -159,12 +165,12 @@ " 'description': \"I'm having trouble understanding...\", \n", " 'placeholder': 'why the sky is blue'\n", " }\n", - "}\n", + "]\n", "\n", - "madlibs = {}\n", - "for key, value in madlibs_dict.items():\n", + "madlibs = []\n", + "for value in madlibs_list:\n", " model = MadLibModel(**value)\n", - " madlibs[key] = MadLib(model)" + " madlibs.append(MadLibView(model))" ] }, { @@ -174,7 +180,7 @@ "metadata": {}, "outputs": [], "source": [ - "madlibs['Metaphor']" + "madlibs[1]" ] }, { @@ -187,7 +193,50 @@ "class MadLibsView(Stack):\n", "\n", " def __init__(self):\n", - " super().__init__()\n" + " super().__init__()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "aec28861-3f40-4a25-9830-53f92f0efa19", + "metadata": {}, + "outputs": [], + "source": [ + "madlibs_view = MadLibsView()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dc2486a5-0267-4ac7-831f-62544b771d64", + "metadata": {}, + "outputs": [], + "source": [ + "for value in madlibs_list:\n", + " model = MadLibModel(**value)\n", + " madlibs.append(MadLib(model))\n", + " madlibs_view.children += (MadLib(model), )" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "22acb08a-9c77-48d7-b79e-df1fcff1614d", + "metadata": {}, + "outputs": [], + "source": [ + "madlibs_view" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3680f384-96a8-46a7-b6b8-afd04d709a31", + "metadata": {}, + "outputs": [], + "source": [ + "madlibs_view?" ] }, { @@ -204,7 +253,7 @@ { "cell_type": "code", "execution_count": null, - "id": "662f28f2-7c29-432b-be4e-80575793ec66", + "id": "fd280ef1-0227-458e-b51d-3a371e10e020", "metadata": {}, "outputs": [], "source": [] diff --git a/nbs/06_chatbot.ipynb b/nbs/06_chatbot.ipynb index 70ac595..5f7de76 100644 --- a/nbs/06_chatbot.ipynb +++ b/nbs/06_chatbot.ipynb @@ -31,32 +31,9 @@ " SystemMessagePromptTemplate,\n", ")\n", "from langchain_openai import ChatOpenAI\n", - "from jupyter_mentor.chatbot import ChatBot, ChatBotModel" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "246e149a-2461-4b40-8efb-0a476be12b80", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Hello! How can I assist you today?'" - ] - }, - "execution_count": null, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "from jupyter_mentor.chatbot import ChatBotModel\n", - "chatbot_model = ChatBotModel()\n", - "chatbot_model.prompt('hello')\n", - "\n", - "## Extend ChatBotModel below" + "#from jupyter_mentor.chatbot import ChatBot, ChatBotModel, ChatBotView\n", + "from jupyter_mentor.madlib import MadLibModel, MadLibView\n", + "from jupyter_mentor.llm import FileModel" ] }, { @@ -66,14 +43,20 @@ "metadata": {}, "outputs": [], "source": [ - "\n", + "#|export\n", "class ChatBotModel(FileModel):\n", "\n", - " def __init__(self, bot_template=\"You are playing the role of a tutor/educator\", human_template=\"{input_text}\"):\n", + " def __init__(self, madlibs_list):\n", " super().__init__()\n", - " self.human_template = human_template\n", + " self.human_template = \"{input_text}\"\n", + " self.bot_template = \"You are playing the role of a tutor/educator\"\n", " self.human_message_prompt = HumanMessagePromptTemplate.from_template(self.human_template)\n", - " self.update_bot_template(bot_template)\n", + " self.update_bot_template(self.bot_template)\n", + "\n", + " self.madlib_models = []\n", + " for value in madlibs_list:\n", + " model = MadLibModel(**value)\n", + " self.madlib_models.append(model)\n", "\n", " def update_bot_template(self, bot_template):\n", " self.bot_template = bot_template\n", @@ -97,52 +80,163 @@ { "cell_type": "code", "execution_count": null, - "id": "a57225dd-d6de-43ee-982f-c1467203dcd3", + "id": "cdf37b3e-7dc8-4af6-abc0-1620c5447cd5", + "metadata": {}, + "outputs": [], + "source": [ + "madlibs_list = [\n", + " {'name': 'Open Prompt',\n", + " 'description': '',\n", + " 'variables': {\n", + " 'input_text': 'value'\n", + " }, \n", + " 'template': '{input_text}' ,\n", + " 'placeholder': 'Message AI chatbot...',\n", + " },\n", + " {\n", + " 'name': 'Metaphor',\n", + " 'variables': {\n", + " 'concept': 'value'\n", + " }, \n", + " 'template': \"I'm having trouble understanding {concept}. Explain it as a metaphor\",\n", + " 'description': \"I'm having trouble understanding...\", \n", + " 'placeholder': 'why the sky is blue'\n", + " }\n", + "]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "246e149a-2461-4b40-8efb-0a476be12b80", + "metadata": {}, + "outputs": [], + "source": [ + "chatbot_model = ChatBotModel(madlibs_list)\n", + "chatbot_model.prompt('hello')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "142e2664-b0b2-4a10-8901-67ec87a562c1", + "metadata": {}, + "outputs": [], + "source": [ + "chatbot_model.madlib_models[0].variables" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9a4bfc4a-16e7-4c66-9963-b0174d832326", "metadata": {}, "outputs": [], "source": [ - "# read openapi key and set model\n", - "with open('OPENAI_API_KEY', 'r') as file:\n", - " openai_api_key = file.read().strip()\n", + "#|export\n", + "class ChatBotView(widgets.VBox):\n", + " \"\"\" This view also exists in 04_chatbot, but wea\n", + " are overwriting it here\n", + " \"\"\"\n", + " \n", + " def __init__(self):\n", + " # If you forget to call the superconstructor on an extended widget\n", + " # you will get an AttributeError: object has no attribute '_model_id'\n", + " super().__init__()\n", "\n", - "llm = ChatOpenAI(openai_api_key = openai_api_key, model_name=\"gpt-3.5-turbo\")" + " self.chat = Textarea(\n", + " disabled = True,\n", + " layout=Layout(width='90%', height='400px')\n", + " )\n", + " self.radio_buttons = widgets.RadioButtons()\n", + " self.hbox = widgets.HBox()\n", + " self.default = widgets.Text(\n", + " placeholder='Message AI chatbot...',\n", + " #layout=Layout(width='100%')\n", + " )\n", + " self.stack = widgets.Stack(children = (self.default, ))\n", + " self.stack.selected_index = 0\n", + " \n", + " self.submit_button = widgets.Button(\n", + " value=False,\n", + " disabled=False,\n", + " button_style='success',\n", + " icon='arrow-circle-right' \n", + " )\n", + " self.hbox.children = (self.stack, self.submit_button)\n", + " self.children = (self.chat, self.radio_buttons, self.hbox ) " ] }, { "cell_type": "code", "execution_count": null, - "id": "a553a55b-ddce-4c09-b5d5-f5cac9a6fbd9", + "id": "61722b3b-4731-4a32-98bc-ea94c5237807", "metadata": {}, "outputs": [], "source": [ - "student_template_options = widgets.RadioButtons(\n", - " options={'Open Prompt': '{input_text}',\n", - " 'Metaphor':\"I'm having trouble understanding {input_text}. Explain it as a metaphor.\"},\n", - " description='Prompt Options:',\n", - ")\n", - "student_template_options" + "chatbotview = ChatBotView()\n", + "chatbotview" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2c4f64d8-ad50-455b-b308-68cc8e9cae07", + "metadata": {}, + "outputs": [], + "source": [ + "#|export\n", + "class ChatBot(ChatBotView):\n", + " \"\"\" This view also exists in 04_chatbot, but wea\n", + " are overwriting it here\n", + " \"\"\"\n", + " selected_index = traitlets.Int()\n", + " \n", + " def __init__(self, model):\n", + " # If you forget to call the superconstructor on an extended widget\n", + " # you will get an AttributeError: object has no attribute '_model_id'\n", + " super().__init__()\n", + " self.model = model\n", + " \n", + " self.selected_model = 0\n", + "\n", + " madlib_views = []\n", + " for model in self.model.madlib_models:\n", + " view = MadLibView(model)\n", + " madlib_views.append(view)\n", + " self.stack.children += (view, )\n", + "\n", + " #self.selected_index = 0\n", + " traitlets.link((self, 'selected_index'), (self.stack, 'selected_index'))\n", + "\n", + " self.submit_button.on_click(self.on_click)\n", + "\n", + " def on_click(self, change):\n", + " self.chat.value = self.chat.value + \"USER: \" + self.stack.children[0].value + '\\n\\n'\n", + " self.stack.children[0].value = ''\n", + " ret = self.model.prompt(self.stack.children[0].value)\n", + " self.chat.value = self.chat.value + \"CHATBOT: \" + ret + '\\n\\n'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "157aa5c9-7068-4f5c-a8f6-8c27286f8a80", + "metadata": {}, + "outputs": [], + "source": [ + "chatbot = ChatBot(chatbot_model)\n", + "chatbot" ] }, { "cell_type": "code", "execution_count": null, - "id": "5a2e9467-8af1-4f42-83ff-27c77d9133ef", + "id": "eb40de44-b1f4-4654-a3d8-8626dd9e192e", "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'student_template_options' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[2], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mstudent_template_options\u001b[49m\u001b[38;5;241m.\u001b[39mvalue\n", - "\u001b[0;31mNameError\u001b[0m: name 'student_template_options' is not defined" - ] - } - ], + "outputs": [], "source": [ - "student_template_options.value" + "chatbot.selected_index = 0" ] }, { @@ -199,23 +293,7 @@ "execution_count": null, "id": "b4e598d6-0f71-423e-afa6-723d52a68926", "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "77cb790799a34780b325d893189111e7", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "StudentChatBot(children=(ChatBot(children=(Textarea(value='', disabled=True, layout=Layout(height='400px', wid…" - ] - }, - "execution_count": null, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "student_chatbot = StudentChatBot(chatbot_model)\n", "student_chatbot" @@ -228,7 +306,7 @@ "metadata": {}, "outputs": [], "source": [ - "#| export\n", + "#|export\n", "class EducatorChatBot(widgets.VBox):\n", "\n", " #user = traitlets.CUnicode()\n",