Skip to content

Commit

Permalink
Added madlibs to stack on chatbot_tab
Browse files Browse the repository at this point in the history
  • Loading branch information
nicole-brewer committed Apr 5, 2024
1 parent a329fc3 commit ac682be
Show file tree
Hide file tree
Showing 7 changed files with 386 additions and 245 deletions.
56 changes: 28 additions & 28 deletions jupyter_mentor/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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__',
Expand Down
85 changes: 1 addition & 84 deletions jupyter_mentor/chatbot.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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'
108 changes: 104 additions & 4 deletions jupyter_mentor/chatbot_tab.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
16 changes: 7 additions & 9 deletions jupyter_mentor/madlib.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,20 +12,23 @@

# %% ../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
setattr(self, key, value)
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()):
Expand All @@ -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())
Loading

0 comments on commit ac682be

Please sign in to comment.