|
4 | 4 | importopenai |
5 | 5 | # import the generated API key from the secret_key file |
6 | 6 | from .secret_keyimportAPI_KEY |
7 | | - |
8 | | - |
9 | 7 | # loading the API key from the secret_key file |
10 | 8 | openai.api_key=API_KEY |
11 | 9 |
|
12 | | - |
13 | 10 | # this is the home view for handling home page logic |
14 | 11 | 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 |
17 | 12 | 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 | + |
19 | 19 | ifrequest.method=='POST': |
20 | | -#getting prompt data from the form |
| 20 | +#get the prompt from the form |
21 | 21 | 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 |
27 | 41 | context= { |
28 | | -'formatted_response':formatted_response, |
29 | | -'prompt':prompt |
| 42 | +'messages':request.session['messages'], |
| 43 | +'prompt':'', |
| 44 | +'temperature':temperature, |
30 | 45 | } |
31 | | -# this will render the results in the home.html template |
32 | 46 | returnrender(request,'assistant/home.html',context) |
33 | | -# this runs if the request method is GET |
34 | 47 | 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 |
41 | 58 | returnredirect('error_handler') |
42 | 59 |
|
43 | 60 |
|
| 61 | +defnew_chat(request): |
| 62 | +# clear the messages list |
| 63 | +request.session.pop('messages',None) |
| 64 | +returnredirect('home') |
| 65 | + |
| 66 | + |
44 | 67 | # this is the view for handling errors |
45 | 68 | deferror_handler(request): |
46 | 69 | returnrender(request,'assistant/404.html') |