Skip to content

Commit 408c64d

Browse files
committed
overhaul frontend - integrate prompt data, link cards, etc
1 parent ac940a8 commit 408c64d

13 files changed

+322
-199
lines changed

chatbot/chat.py

+67-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Chat views.
33
"""
4-
from flask import Blueprint, render_template, request, session, current_app
4+
from flask import Blueprint, render_template, request, session, current_app, abort
55
from wtforms import Form, StringField, validators
66

77
from chatbot.auth import login_required
@@ -15,23 +15,33 @@
1515
def overview():
1616
""" Route for the chat overview
1717
"""
18-
return render_template('chat/overview.html')
18+
return render_template('chat/overview.html', prompts=current_app.prompts)
1919

2020

2121
@bp.route('/<chat_scenario>', methods=('GET', 'POST'))
2222
@login_required
2323
def chat(chat_scenario):
2424
""" Route for chat with variable scenarios
2525
"""
26-
return render_template('chat/chat.html', chat_scenario=chat_scenario)
26+
form = ChatForm(request.form)
2727

28+
# Return 404 if chat scenario not found
29+
promptExists = chat_scenario in current_app.prompts.keys()
30+
if not promptExists:
31+
abort(404)
32+
# return render_template('404.html'), 404
2833

29-
@bp.route('/general', methods=('GET', 'POST'))
30-
@login_required
31-
def general():
32-
""" Route for general simple chat.
33-
"""
34-
form = ChatForm(request.form)
34+
# If the users opens the chat from somewhere else, clear chat history
35+
userOpensChatFirstTime = request.url != request.referrer
36+
if userOpensChatFirstTime:
37+
clear_chat_history()
38+
39+
# request.referrer.split('/')[-1]
40+
41+
prompt = current_app.prompts[chat_scenario]
42+
prompt_text = prompt['text']
43+
# if chat_scenario
44+
# prompt = current_app.prompts
3545

3646
# Get chat history
3747
chat_history = session.get('chat_history')
@@ -56,14 +66,57 @@ def general():
5666
# Clear form
5767
form.text_input.data = ""
5868
# Get response from bot
59-
chat_history = get_bot_response(chat_history)
69+
chat_history = get_bot_response(chat_history, prompt_text)
6070
# Update chat history
6171
session['chat_history'] = chat_history
72+
73+
74+
return render_template('chat/chat.html',
75+
# chat_scenario=chat_scenario,
76+
prompt=prompt,
77+
form=form,
78+
loc = userOpensChatFirstTime
79+
)
80+
81+
82+
# @bp.route('/general', methods=('GET', 'POST'))
83+
# @login_required
84+
# def general():
85+
# """ Route for general simple chat.
86+
# """
87+
# form = ChatForm(request.form)
88+
89+
# # Get chat history
90+
# chat_history = session.get('chat_history')
91+
# # Create if not exists
92+
# if not chat_history:
93+
# chat_history = []
94+
95+
# # On text form submission
96+
# if request.method == 'POST':
97+
# if request.form['submit_button'] == 'Clear chat':
98+
# # Clear chat history
99+
# clear_chat_history()
100+
# elif request.form['submit_button'] == 'Send text' and form.validate():
101+
# # Add text to chat history
102+
# chat_history.append(
103+
# {
104+
# 'sender': 'user',
105+
# 'text': form.text_input.data
106+
# }
107+
# )
108+
109+
# # Clear form
110+
# form.text_input.data = ""
111+
# # Get response from bot
112+
# chat_history = get_bot_response(chat_history)
113+
# # Update chat history
114+
# session['chat_history'] = chat_history
62115

63-
return render_template('chat/general_chat.html', form=form)
116+
# return render_template('chat/general_chat.html', form=form)
64117

65118

66-
def get_bot_response(chat_history):
119+
def get_bot_response(chat_history, prompt_text):
67120
""" Get response from the bot """
68121
# Here we have to get the response from the bot
69122
# chat_history should be sent to the model class
@@ -79,7 +132,7 @@ def get_bot_response(chat_history):
79132
# )
80133
if current_app.config['LOAD_GRAMMAR_MODEL']:
81134
chat_history = current_app.grammar_correction.add_correction_to_chat_history(chat_history)
82-
chat_history = current_app.language_model.add_response_to_chat_history(chat_history)
135+
chat_history = current_app.language_model.add_response_to_chat_history(chat_history, prompt_text)
83136

84137
return chat_history
85138

@@ -91,5 +144,5 @@ def clear_chat_history():
91144
class ChatForm(Form):
92145
text_input = StringField('Input',
93146
[validators.Length(min=4, max=300)],
94-
render_kw={'class': 'chat_text_input_field'}
147+
render_kw={'class': 'form-control chat_text_input_field'}
95148
)

chatbot/dashboard.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Dashboard view.
33
"""
4-
from flask import Blueprint, render_template
4+
from flask import Blueprint, render_template, current_app
55

66
from chatbot.auth import login_required
77
from chatbot.utils import get_activity_plot, get_error_distribution_plot
@@ -16,8 +16,16 @@ def dashboard():
1616
"""
1717

1818
fig_activity_html = get_activity_plot()
19+
20+
21+
featured_prompts = {key: current_app.prompts[key] for key in
22+
['general_chat_beginner', 'scenario_restaurant', 'persona_shakespeare']
23+
}
1924

20-
return render_template('dashboard/dashboard.html', plot=fig_activity_html)
25+
return render_template('dashboard/dashboard.html',
26+
plot=fig_activity_html,
27+
featured_prompts=featured_prompts
28+
)
2129

2230

2331

@@ -30,9 +38,15 @@ def statistics():
3038
fig_activity_html = get_activity_plot()
3139

3240
fig_grammar_html = get_error_distribution_plot()
41+
42+
# suggested_prompts =
43+
suggested_prompts = {key: current_app.prompts[key] for key in
44+
['general_chat_intermediate', 'scenario_bakery', 'scenario_restaurant']
45+
}
3346

3447
return render_template('dashboard/statistics.html',
3548
plot_activity=fig_activity_html,
3649
plot_grammar=fig_grammar_html,
50+
suggested_prompts=suggested_prompts
3751
)
3852

chatbot/data/prompts.json

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
{
2-
"general_chat_easy": {
2+
"general_chat_beginner": {
3+
"category": "general_chat",
4+
"label": "Beginner",
5+
"description_short": "This is a placeholder for the short description of the chat type.",
6+
"description_long": "This is a placeholder for the longer description of the chat type.<br>It is intended to be displayed when a user starts a scenario, for example",
37
"text": "In this conversation, the AI Teacher is discussing everyday topics with the student. Examples of topics are: greetings, introducing each other, telling time, talking about family and hobbies, counting. The teacher uses beginner level English. The conversation is made with short and simple sentences. After each interaction, the AI Teacher asks a new question.\nTeacher: Tell me, what would you like to talk about today?\nStudent: What do you suggest?"
48
},
59

610
"general_chat_intermediate": {
7-
"text": "In this conversation, the AI Teacher is discussing everyday topics with the student. Examples of topics are: greetings, introducing each other, telling time, talking about family and hobbies, counting. The teacher uses beginner level English. The conversation is made with short and simple sentences. After each interaction, the AI Teacher asks a new question.\nTeacher: Tell me, what would you like to talk about today?\nStudent: What do you suggest?"
11+
"category": "general_chat",
12+
"label": "Intermediate",
13+
"description_short": "This is a placeholder for the short description of the chat type.",
14+
"text": "In this conversation, the AI Teacher is discussing everyday topics with the student. Examples of topics are: greetings, introducing each other, telling time, talking about family and hobbies, counting. The teacher uses beginner level English. The conversation is made with short and simple sentences. After each interaction, the AI Teacher asks a new question.\nTeacher: Tell me, what would you like to talk about today?\nStudent: What do you suggest?"
815
},
916

1017
"general_chat_advanced": {
18+
"category": "general_chat",
19+
"label": "Advanced",
20+
"description_short": "This is a placeholder for the short description of the chat type.",
1121
"text": "In the following exchange, the AI Teacher is having a casual conversation with the student. The teacher uses high-level complex academic English. The AI Teacher talks in long sentences and uses uncommon words and synonyms. It sometimes asks hypothetical questions to the student and uses present, past and future tenses. The AI Teacher asks the student for his opinion on complex topics such as personal values, metaphysical questions, religion, current affairs, etc. After each interaction, the AI Teacher asks a new question.\nTeacher: Tell me, what would you like to talk about today?"
1222
},
1323

1424
"scenario_bakery": {
25+
"category": "scenario",
26+
"label": "Bakery",
27+
"description_short": "This is a placeholder for the short description of the chat type.",
28+
"description_long": "This is a placeholder for the longer description of the chat type.<br>It is intended to be displayed when a user starts a scenario, for example",
1529
"text": "The following is a conversation with Lola the baker. Lola is helpful, clever, and very friendly.\nHuman: Good morning!\nLola: Hello, how can I help you?",
1630
"tasks": [
1731
"Ask what types of bread they have and select one to buy",
@@ -23,6 +37,10 @@
2337
},
2438

2539
"scenario_store": {
40+
"category": "scenario",
41+
"label": "Store",
42+
"description_short": "This is a placeholder for the short description of the chat type.",
43+
"description_long": "This is a placeholder for the longer description of the chat type.<br>It is intended to be displayed when a user starts a scenario, for example",
2644
"text": "The following is a conversation between a Human and a store clerk called Artemis. The conversation takes place at a store. The Human needs help finding some items in the store as well as the price of those items. Artemis helps him out.\nArtemis: Hi, my name is Artemis. May I help you?",
2745
"tasks": [
2846
"Ask the clerk to help you find 2 cleaning related objects.",
@@ -34,6 +52,10 @@
3452
},
3553

3654
"scenario_restaurant": {
55+
"category": "scenario",
56+
"label": "Restaurant",
57+
"description_short": "This is a placeholder for the short description of the chat type.",
58+
"description_long": "This is a placeholder for the longer description of the chat type.<br>It is intended to be displayed when a user starts a scenario, for example",
3759
"text": "The following is a conversation between a waiter named Artemis and a client. The interaction takes place in a restaurant at lunch time. Artemis is helpful clever, and very friendly. He wants to make sure the client has a nice experience at the restaurant. \nArtemis: Hello my name is Artemis, I'll be your waiter today.",
3860
"tasks": [
3961
"Ask to see the menu",
@@ -44,5 +66,13 @@
4466
"Ask for the bill",
4567
"The meal they brought you is not what you ordered, let the waiter know and ask them to bring you a new one."
4668
]
69+
},
70+
71+
"persona_shakespeare": {
72+
"category": "persona",
73+
"label": "Shakespeare",
74+
"description_short": "This is a placeholder for the short description of the chat type.",
75+
"description_long": "This is a placeholder for the longer description of the chat type.<br>It is intended to be displayed when a user starts a scenario, for example",
76+
"text": "The following is a conversation between a a human and the famous playwright William Shakespeare."
4777
}
4878
}

chatbot/language_model.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def __init__(self,
2626
# Load the prompts for all scenarios
2727

2828

29-
def add_response_to_chat_history(self, chat_history):
29+
def add_response_to_chat_history(self, chat_history, prompt_text):
3030
""" Generate a response from the bot and append to chat history.
3131
3232
Converts the chat history to a flat string of ids,
@@ -36,7 +36,7 @@ def add_response_to_chat_history(self, chat_history):
3636
return chat_history
3737

3838

39-
reply_text = self.get_response_from_GPT3(chat_history)
39+
reply_text = self.get_response_from_GPT3(chat_history, prompt_text)
4040

4141
if reply_text:
4242
chat_history.append(
@@ -47,7 +47,7 @@ def add_response_to_chat_history(self, chat_history):
4747
)
4848
return chat_history
4949

50-
def get_response_from_GPT3(self, chat_history):
50+
def get_response_from_GPT3(self, chat_history, prompt_text):
5151
""" Get a reply from GPT3
5252
5353
Returns:
@@ -56,7 +56,7 @@ def get_response_from_GPT3(self, chat_history):
5656
A text string containing just the reply from the model.
5757
Example: "I'm fine, how are you?"
5858
"""
59-
prompt_with_dialog = self.create_prompt_with_dialog(chat_history)
59+
prompt_with_dialog = self.create_prompt_with_dialog(chat_history, prompt_text)
6060

6161
response = openai.Completion.create(
6262
engine=self.engine,
@@ -88,17 +88,16 @@ def clean_reply_text(self, reply_raw):
8888
return reply
8989

9090

91-
def create_prompt_with_dialog(self, chat_history,
92-
chat_type='general_chat_intermediate') -> str:
91+
def create_prompt_with_dialog(self, chat_history, prompt_text) -> str:
9392
""" Create a prompt to get a response from GPT-3.
9493
9594
Combines the base prompt and the recent chat history
9695
to a prompt with dialog for GPT3 to create the next sentence.
9796
"""
98-
prompts = current_app.prompts
99-
assert chat_type in prompts.keys()
100-
prompt = prompts[chat_type]
101-
prompt_text = prompt['text']
97+
# prompts = current_app.prompts
98+
# assert chat_type in prompts.keys()
99+
# prompt = prompts[chat_type]
100+
# prompt_text = prompt['text']
102101

103102
# Limit the chat_history to the past 100 messages
104103
chat_history = chat_history[-100:]

chatbot/static/style.css

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ html {
2626
.btn-primary:active,
2727
.btn-primary:visited {
2828
background-color: rgb(var(--blue-rgb)) !important;
29-
box-shadow: 0 0 0 0.25rem rgb(var(--blue-rgb)) !important;
29+
/* box-shadow: 0 0 0 0.25rem rgb(var(--blue-rgb)) !important; */
3030
border-color: rgb(var(--blue-rgb)) !important;
3131
}
3232

@@ -44,14 +44,14 @@ html {
4444
) !important;
4545
}
4646

47-
.bg-situation-header {
47+
.bg-scenario-header {
4848
background-color: rgba(
4949
var(--red-rgb),
5050
var(--bg-card-header-opacity)
5151
) !important;
5252
}
5353

54-
.bg-situation-body {
54+
.bg-scenario-body {
5555
background-color: rgba(
5656
var(--red-rgb),
5757
var(--bg-card-body-opacity)

chatbot/templates/base.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<!-- Favicon-->
1010
<link rel="icon" type="image/x-icon" href="{{ url_for('static', filename='favicon.ico') }}" />
1111
<!-- Bootstrap icons-->
12-
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.4.1/font/bootstrap-icons.css" rel="stylesheet" />
12+
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.0/font/bootstrap-icons.css" rel="stylesheet" />
1313
<!-- Core theme CSS (includes Bootstrap)-->
1414
<link href="{{ url_for('static', filename='bootstrap_template_styles.css') }}" rel="stylesheet" />
1515

@@ -62,7 +62,7 @@
6262

6363
<!-- Footer-->
6464
<footer class="py-5">
65-
<div class="container px-5"><p class="m-0 text-center text-dark">Copyright &copy; Your Website 2021</p></div>
65+
<div class="container px-5"><p class="m-0 text-center text-dark">Copyright &copy; ConvAF 2022</p></div>
6666
</footer>
6767
<!-- Bootstrap core JS-->
6868
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>

0 commit comments

Comments
 (0)