Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0442d18

Browse files
committed
update web assistant tutorial to make better UI & use chatgpt api
1 parentf398822 commit0442d18

File tree

4 files changed

+64
-48
lines changed

4 files changed

+64
-48
lines changed
Lines changed: 16 additions & 24 deletions
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
<divclass="row justify-content-center my-4">
7-
85
<divclass="col-md-7 mt-4">
9-
106
<divclass="card">
117
<h1class="card-header text-center">A.I WEB ASSISTANT</h1>
12-
138
<divclass="card-body">
14-
15-
<pre>Hello, am your web assistant here to help you, what's on your mind?</pre>
16-
9+
<divclass="d-flex justify-content-end">
10+
<buttontype="button"class="btn btn-primary mb-3"onclick="location.href='{% url 'new_chat' %}'">New Chat +</button>
11+
</div>
12+
<divclass="chat-history mb-3">
13+
{% for message in messages %}
14+
<divclass="card mb-2 {% if message.role == 'assistant' %}bg-success text-white{% endif %}">
15+
<divclass="card-body p-2">
16+
<strong>{{ message.role|title }}:</strong> {{ message.content|linebreaksbr }}
17+
</div>
18+
</div>
19+
{% endfor %}
20+
</div>
1721
<formaction="."method="POST">
1822
<!-- this secures the form from malicious attacks during submission -->
1923
{% csrf_token %}
20-
2124
<inputclass="form-control mb-2"requiredtype="text"autofocus="autofocus"name="prompt"value="{{ prompt }}"id="">
22-
25+
<labelfor="temperature"class="form-label">Temperature:</label>
26+
<inputclass="form-control mb-2"type="number"step="0.01"min="0"max="2"name="temperature"value="{{ temperature }}"id="temperature">
2327
<buttonclass="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‎

Lines changed: 2 additions & 1 deletion
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‎

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,66 @@
44
importopenai
55
# import the generated API key from the secret_key file
66
from .secret_keyimportAPI_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
defhome(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'notinrequest.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
ifrequest.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
returnrender(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-
returnrender(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+
returnrender(request,'assistant/home.html',context)
55+
exceptExceptionase:
56+
print(e)
57+
# if there is an error, redirect to the error handler
4158
returnredirect('error_handler')
4259

4360

61+
defnew_chat(request):
62+
# clear the messages list
63+
request.session.pop('messages',None)
64+
returnredirect('home')
65+
66+
4467
# this is the view for handling errors
4568
deferror_handler(request):
4669
returnrender(request,'assistant/404.html')

‎web-programming/webassistant/webassistant/urls.py‎

Lines changed: 1 addition & 1 deletion
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)

[8]ページ先頭

©2009-2025 Movatter.jp