Skip to content

Commit 0442d18

Browse files
committed
update web assistant tutorial to make better UI & use chatgpt api
1 parent f398822 commit 0442d18

File tree

4 files changed

+64
-48
lines changed

4 files changed

+64
-48
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,35 @@
11
{% extends 'assistant/base.html' %}
2-
32
{% block title %} Home {% endblock %}
4-
53
{% block content %}
64
<div class="row justify-content-center my-4">
7-
85
<div class="col-md-7 mt-4">
9-
106
<div class="card">
117
<h1 class="card-header text-center">A.I WEB ASSISTANT</h1>
12-
138
<div class="card-body">
14-
15-
<pre>Hello, am your web assistant here to help you, what's on your mind?</pre>
16-
9+
<div class="d-flex justify-content-end">
10+
<button type="button" class="btn btn-primary mb-3" onclick="location.href='{% url 'new_chat' %}'">New Chat +</button>
11+
</div>
12+
<div class="chat-history mb-3">
13+
{% for message in messages %}
14+
<div class="card mb-2 {% if message.role == 'assistant' %}bg-success text-white{% endif %}">
15+
<div class="card-body p-2">
16+
<strong>{{ message.role|title }}:</strong> {{ message.content|linebreaksbr }}
17+
</div>
18+
</div>
19+
{% endfor %}
20+
</div>
1721
<form action="." method="POST">
1822
<!-- this secures the form from malicious attacks during submission -->
1923
{% csrf_token %}
20-
2124
<input class="form-control mb-2" required type="text" autofocus="autofocus" name="prompt" value="{{ prompt }}" id="">
22-
25+
<label for="temperature" class="form-label">Temperature:</label>
26+
<input class="form-control mb-2" type="number" step="0.01" min="0" max="2" name="temperature" value="{{ temperature }}" id="temperature">
2327
<button class="btn btn-success fw-bold" type="submit">
24-
GENARATE
28+
GENERATE
2529
</button>
26-
2730
</form>
28-
29-
<hr>
30-
31-
<pre>
32-
{{ formatted_response }}
33-
</pre>
34-
3531
</div>
36-
3732
</div>
38-
3933
</div>
40-
41-
</div>
4234
</div>
43-
{% endblock %}
35+
{% endblock %}

web-programming/webassistant/assistant/urls.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
# a list of all the urls
77
urlpatterns = [
8-
path('home/', views.home, name='home'),
8+
path('', views.home, name='home'),
9+
path('new_chat/', views.new_chat, name='new_chat'),
910
path('error-handler/', views.error_handler, name='error_handler'),
1011
]

web-programming/webassistant/assistant/views.py

+45-22
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,66 @@
44
import openai
55
# import the generated API key from the secret_key file
66
from .secret_key import API_KEY
7-
8-
97
# loading the API key from the secret_key file
108
openai.api_key = API_KEY
119

12-
1310
# this is the home view for handling home page logic
1411
def home(request):
15-
# the try statement is for sending request to the API and getting back the response
16-
# formatting it and rendering it in the template
1712
try:
18-
# checking if the request method is POST
13+
# if the session does not have a messages key, create one
14+
if 'messages' not in request.session:
15+
request.session['messages'] = [
16+
{"role": "system", "content": "You are now chatting with a user, provide them with comprehensive, short and concise answers."},
17+
]
18+
1919
if request.method == 'POST':
20-
# getting prompt data from the form
20+
# get the prompt from the form
2121
prompt = request.POST.get('prompt')
22-
# making a request to the API
23-
response = openai.Completion.create(model="text-davinci-003", prompt=prompt, temperature=1, max_tokens=1000)
24-
# formatting the response input
25-
formatted_response = response['choices'][0]['text']
26-
# bundling everything in the context
22+
# get the temperature from the form
23+
temperature = float(request.POST.get('temperature', 0.1))
24+
# append the prompt to the messages list
25+
request.session['messages'].append({"role": "user", "content": prompt})
26+
# set the session as modified
27+
request.session.modified = True
28+
# call the openai API
29+
response = openai.ChatCompletion.create(
30+
model="gpt-3.5-turbo",
31+
messages=request.session['messages'],
32+
temperature=temperature,
33+
max_tokens=1000,
34+
)
35+
# format the response
36+
formatted_response = response['choices'][0]['message']['content']
37+
# append the response to the messages list
38+
request.session['messages'].append({"role": "assistant", "content": formatted_response})
39+
request.session.modified = True
40+
# redirect to the home page
2741
context = {
28-
'formatted_response': formatted_response,
29-
'prompt': prompt
42+
'messages': request.session['messages'],
43+
'prompt': '',
44+
'temperature': temperature,
3045
}
31-
# this will render the results in the home.html template
3246
return render(request, 'assistant/home.html', context)
33-
# this runs if the request method is GET
3447
else:
35-
# this will render when there is no request POST or after every POST request
36-
return render(request, 'assistant/home.html')
37-
38-
# the except statement will capture any error
39-
except:
40-
# this will redirect to the 404 page after any error is caught
48+
# if the request is not a POST request, render the home page
49+
context = {
50+
'messages': request.session['messages'],
51+
'prompt': '',
52+
'temperature': 0.1,
53+
}
54+
return render(request, 'assistant/home.html', context)
55+
except Exception as e:
56+
print(e)
57+
# if there is an error, redirect to the error handler
4158
return redirect('error_handler')
4259

4360

61+
def new_chat(request):
62+
# clear the messages list
63+
request.session.pop('messages', None)
64+
return redirect('home')
65+
66+
4467
# this is the view for handling errors
4568
def error_handler(request):
4669
return render(request, 'assistant/404.html')

web-programming/webassistant/webassistant/urls.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
# the url to the admin site
77
path('admin/', admin.site.urls),
88
# registering all the assistant application urls
9-
path('webassistant/', include('assistant.urls')),
9+
path('', include('assistant.urls')),
1010
]

0 commit comments

Comments
 (0)