Skip to content

Commit

Permalink
Added ChatBotModel and created ChatBot delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
nicole-brewer committed Apr 3, 2024
1 parent c5f2df8 commit d1bd8e4
Show file tree
Hide file tree
Showing 9 changed files with 362 additions and 356 deletions.
2 changes: 2 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ dependencies:
- notebook
- sortedcontainers
- jupyter-vuetify
- pypdf
- pip
- pip:
- langchain-openai
- unstructured # for loading markdown files
18 changes: 12 additions & 6 deletions jupyter_mentor/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
'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.update_bot_template': ( 'chatbot.html#chatbotmodel.update_bot_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.EducatorChatBot': ( 'chatbot.html#educatorchatbot',
'jupyter_mentor/chatbot.py'),
'jupyter_mentor.chatbot.EducatorChatBot.__init__': ( 'chatbot.html#educatorchatbot.__init__',
Expand All @@ -20,12 +32,6 @@
'jupyter_mentor/educator_course_overview.py'),
'jupyter_mentor.educator_course_overview.EducatorCourseOverview.__init__': ( 'educator_course_overview.html#educatorcourseoverview.__init__',
'jupyter_mentor/educator_course_overview.py')},
'jupyter_mentor.educator_model': { 'jupyter_mentor.educator_model.EducatorModel': ( 'educator_model.html#educatormodel',
'jupyter_mentor/educator_model.py'),
'jupyter_mentor.educator_model.EducatorModel.__init__': ( 'educator_model.html#educatormodel.__init__',
'jupyter_mentor/educator_model.py'),
'jupyter_mentor.educator_model.EducatorModel.invoke': ( 'educator_model.html#educatormodel.invoke',
'jupyter_mentor/educator_model.py')},
'jupyter_mentor.educator_profile': { 'jupyter_mentor.educator_profile.EducatorProfile': ( 'educator_profile.html#educatorprofile',
'jupyter_mentor/educator_profile.py'),
'jupyter_mentor.educator_profile.EducatorProfile.__init__': ( 'educator_profile.html#educatorprofile.__init__',
Expand Down
111 changes: 73 additions & 38 deletions jupyter_mentor/chatbot.py
Original file line number Diff line number Diff line change
@@ -1,74 +1,102 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/05_chatbot.ipynb.

# %% auto 0
__all__ = ['ChatBot', 'StudentChatBot', 'EducatorChatBot']
__all__ = ['ChatBotModel', 'ChatBotView', 'ChatBot', 'StudentChatBot', 'EducatorChatBot']

# %% ../nbs/05_chatbot.ipynb 1
import ipywidgets as widgets
import traitlets
from ipywidgets import Textarea, Text, Layout, HBox

# %% ../nbs/05_chatbot.ipynb 2
class ChatBot(widgets.VBox):
from traitlets import HasTraits
import os
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_core.prompts.chat import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
SystemMessagePromptTemplate,
)
from langchain_openai import ChatOpenAI

# %% ../nbs/05_chatbot.ipynb 3
class ChatBotModel(HasTraits):

def __init__(self, llm, bot_template="You are playing the role of a tutor/educator"):
super().__init__()
self.llm = llm
self.human_template = "{input_text}"
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.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):
#prompt_val = prompt_template.invoke({"adjective": "funny", "content": "chickens"})
ret = self.llm.invoke(self.chat_prompt.format_prompt(input_text=input_text))
return ret.content

# %% ../nbs/05_chatbot.ipynb 5
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__()

example_convo = '''
User: Hello, bot!
Bot: Hi there! How can I assist you today?
User: I need help setting up my email account. Can you guide me through the process?
Bot: Of course! Could you please provide me with the email service provider you're using?
User: I use Gmail.
Bot: Great! First, go to the Gmail website and click on the "Create account" button.
User: Okay, I'm there. What's next?
Bot: Enter your personal information such as your name, desired email address, password, and recovery information.
User: Done! What's next?
Bot: Follow the prompts to verify your phone number and agree to the terms of service.
User: All set! Thank you for your help, bot!
Bot: You're welcome! If you have any more questions, feel free to ask.
'''
self.chat = Textarea(
disabled = True,
value = example_convo,
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 = widgets.ToggleButton(
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)
self.user_input_and_submit.children = (self.user_input, self.submit_button)

self.children = (self.chat, self.user_input_and_submit)

# %% ../nbs/05_chatbot.ipynb 4
# %% ../nbs/05_chatbot.ipynb 7
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(self.user_input.value)
self.chat.value = self.chat.value + "CHATBOT: " + ret + '\n\n'

# %% ../nbs/05_chatbot.ipynb 9
class StudentChatBot(widgets.VBox):

#user = traitlets.CUnicode()
#response = traitlets.CUnicode()
#step_by_step = traitlets.Bool()
#metaphor = traitlets.Bool()
#hints = traitlets.Bool()
#guided_questions = traitlets.Bool()

def __init__(self):
def __init__(self, chatbot_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.chat_bot = ChatBot()
self.chat_bot = ChatBot(chatbot_model)

self.suggestion_buttons = HBox()
self.step_by_step = widgets.Button(
Expand All @@ -94,15 +122,22 @@ def __init__(self):
self.suggestion_buttons.children = (self.step_by_step, self.metaphor, self.hints, self.guided_questions)
self.children = (self.chat_bot, self.suggestion_buttons)

# %% ../nbs/05_chatbot.ipynb 6
# %% ../nbs/05_chatbot.ipynb 11
class EducatorChatBot(widgets.VBox):

#user = traitlets.CUnicode()
#response = traitlets.CUnicode()
#step_by_step = traitlets.Bool()
#metaphor = traitlets.Bool()
#hints = traitlets.Bool()
#guided_questions = traitlets.Bool()

def __init__(self, ):
def __init__(self, chatbot_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.chat_bot = ChatBot()
self.chat_bot = ChatBot(chatbot_model)

self.suggestion_buttons = HBox()
self.exam_questions = widgets.Button(
Expand Down
21 changes: 0 additions & 21 deletions jupyter_mentor/educator_model.py

This file was deleted.

52 changes: 45 additions & 7 deletions nbs/00_main.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,44 @@
"from jupyter_mentor.student_profile import StudentProfile\n",
"from jupyter_mentor.educator_course_overview import EducatorCourseOverview\n",
"from jupyter_mentor.student_course_overview import StudentCourseOverview\n",
"from jupyter_mentor.chatbot import StudentChatBot, EducatorChatBot\n",
"from jupyter_mentor.chatbot import StudentChatBot, EducatorChatBot, ChatBotModel\n",
"from jupyter_mentor.file_viewer import FileViewer"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "af80f333-81cd-4f2d-8f92-195b79a8e307",
"metadata": {},
"outputs": [],
"source": [
"import langchain\n",
"from langchain_openai import ChatOpenAI\n",
"from langchain_core.prompts import ChatPromptTemplate\n",
"from langchain_core.output_parsers import StrOutputParser\n",
"from langchain_community.document_loaders import PyPDFLoader\n",
"from langchain_community.vectorstores import FAISS\n",
"from langchain_openai import OpenAIEmbeddings\n",
"import os"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "94f92367-e2c2-4ce5-9c19-48ac5ae357e7",
"metadata": {},
"outputs": [],
"source": [
"from langchain_openai import ChatOpenAI\n",
"import os\n",
"# read openapi key and set model\n",
"with open('../OPENAI_API_KEY', 'r') as file:\n",
" env_value = file.read().strip()\n",
"\n",
"os.environ['OPENAI_API_KEY'] = env_value\n",
"llm = ChatOpenAI(model_name=\"gpt-3.5-turbo\")"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -46,18 +80,21 @@
"#| export\n",
"class EducatorMain(widgets.Tab):\n",
" \n",
" def __init__(self):\n",
" def __init__(self, llm):\n",
" super().__init__()\n",
" \n",
" # initialize models\n",
" self.llm = llm\n",
" # self.model = DataModel(FILENAME)\n",
" self.educator_chatbot_model = ChatBotModel(llm)\n",
"\n",
" \n",
" # initialize views\n",
" self.first = Login()\n",
" self.second = EducatorProfile()\n",
" self.file_viewer = FileViewer()\n",
" self.third = EducatorCourseOverview(self.file_viewer)\n",
" self.fourth = EducatorChatBot()\n",
" self.fourth = EducatorChatBot(self.educator_chatbot_model)\n",
" \n",
" # Add tabs to the Tab widget\n",
" self.children = (self.first, self.second, self.third, self.fourth)\n",
Expand Down Expand Up @@ -91,7 +128,7 @@
"metadata": {},
"outputs": [],
"source": [
"educator_main = EducatorMain()\n",
"educator_main = EducatorMain(llm)\n",
"educator_main"
]
},
Expand All @@ -109,15 +146,16 @@
" super().__init__()\n",
" \n",
" # initialize models\n",
" self.llm = llm\n",
" # self.model = EducatorModel(FILENAME)\n",
" \n",
" self.student_chatbot_model = ChatBotModel(llm)\n",
"\n",
" # initialize views\n",
" self.first = Login()\n",
" self.second = StudentProfile()\n",
" self.file_viewer = FileViewer()\n",
"\n",
" self.third = StudentCourseOverview(self.file_viewer)\n",
" self.fourth = StudentChatBot()\n",
" self.fourth = StudentChatBot(self.student_chatbot_model)\n",
"\n",
" \n",
" # Add tabs to the Tab widget\n",
Expand Down
Loading

0 comments on commit d1bd8e4

Please sign in to comment.