forked from x4nth055/pythoncode-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
46 lines (39 loc) · 1.73 KB
/
Copy pathviews.py
File metadata and controls
46 lines (39 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# importing render and redirect
from django.shortcuts import render, redirect
# importing the openai API
import openai
# import the generated API key from the secret_key file
from .secret_key import API_KEY
# loading the API key from the secret_key file
openai.api_key = API_KEY
# this is the home view for handling home page logic
def home(request):
# the try statement is for sending request to the API and getting back the response
# formatting it and rendering it in the template
try:
# checking if the request method is POST
if request.method == 'POST':
# getting prompt data from the form
prompt = request.POST.get('prompt')
# making a request to the API
response = openai.Completion.create(model="text-davinci-003", prompt=prompt, temperature=1, max_tokens=1000)
# formatting the response input
formatted_response = response['choices'][0]['text']
# bundling everything in the context
context = {
'formatted_response': formatted_response,
'prompt': prompt
}
# this will render the results in the home.html template
return render(request, 'assistant/home.html', context)
# this runs if the request method is GET
else:
# this will render when there is no request POST or after every POST request
return render(request, 'assistant/home.html')
# the except statement will capture any error
except:
# this will redirect to the 404 page after any error is caught
return redirect('error_handler')
# this is the view for handling errors
def error_handler(request):
return render(request, 'assistant/404.html')