From 840628b452031e67fd0d72df078edeaf5aac6b89 Mon Sep 17 00:00:00 2001 From: github-actions Date: Sat, 18 Nov 2023 02:12:21 +0000 Subject: [PATCH] Automated autoyapf fixes --- application.py | 14 ++++++++------ utilities.py | 41 ++++++++++++++++++++++++++++++----------- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/application.py b/application.py index 0df91336..0b4f3332 100644 --- a/application.py +++ b/application.py @@ -19,7 +19,6 @@ from utilities import get_response import time - # Set the OpenAI API key openai.api_key = c.openAI_api_key @@ -56,7 +55,6 @@ # Initialize chat history chat_history = '' - app = Flask(__name__) app.secret_key = 'secret' app.config['MONGO_URI'] = 'mongodb://127.0.0.1:27017/test' @@ -455,20 +453,24 @@ def bmi_calci(): return render_template("bmi_cal.html", bmi=bmi, bmi_category=bmi_category) + @app.route('/chatbot', methods=['GET', 'POST']) def chatbot(): return render_template("chatbot.html") -@app.route("/get", methods=['GET', 'POST']) +@app.route("/get", methods=['GET', 'POST']) # Function for the bot response def get_bot_response(): userText = request.args.get('msg') - return str(get_response(chat_history, name, chatgpt_output, userText, history_file, impersonated_role, explicit_input)) + return str( + get_response(chat_history, name, chatgpt_output, userText, + history_file, impersonated_role, explicit_input)) + -@app.route('/refresh', methods=['GET', 'POST']) +@app.route('/refresh', methods=['GET', 'POST']) def refresh(): - time.sleep(600) # Wait for 10 minutes + time.sleep(600) # Wait for 10 minutes return redirect('/refresh') diff --git a/utilities.py b/utilities.py index d32b7c11..138c5747 100644 --- a/utilities.py +++ b/utilities.py @@ -30,11 +30,14 @@ def get_random_string(self, length): print("Random string of length", length, "is:", result_str) return result_str + import openai import time - + + # Function to complete chat input using OpenAI's GPT-3.5 Turbo -def chatcompletion(user_input, impersonated_role, explicit_input, chat_history): +def chatcompletion(user_input, impersonated_role, explicit_input, + chat_history): output = openai.ChatCompletion.create( model="gpt-3.5-turbo-0301", temperature=1, @@ -42,29 +45,45 @@ def chatcompletion(user_input, impersonated_role, explicit_input, chat_history): frequency_penalty=0, max_tokens=2000, messages=[ - {"role": "system", "content": f"{impersonated_role}. Conversation history: {chat_history}"}, - {"role": "user", "content": f"{user_input}. {explicit_input}"}, - ] - ) + { + "role": + "system", + "content": + f"{impersonated_role}. Conversation history: {chat_history}" + }, + { + "role": "user", + "content": f"{user_input}. {explicit_input}" + }, + ]) for item in output['choices']: chatgpt_output = item['message']['content'] return chatgpt_output + # Function to handle user chat input -def chat(chat_history, name, chatgpt_output, user_input, history_file, impersonated_role, explicit_input): +def chat(chat_history, name, chatgpt_output, user_input, history_file, + impersonated_role, explicit_input): current_day = time.strftime("%d/%m", time.localtime()) current_time = time.strftime("%H:%M:%S", time.localtime()) chat_history += f'\nUser: {user_input}\n' - chatgpt_raw_output = chatcompletion(user_input, impersonated_role, explicit_input, chat_history).replace(f'{name}:', '') + chatgpt_raw_output = chatcompletion(user_input, impersonated_role, + explicit_input, + chat_history).replace(f'{name}:', '') chatgpt_output = f'{name}: {chatgpt_raw_output}' chat_history += chatgpt_output + '\n' with open(history_file, 'a') as f: - f.write('\n'+ current_day+ ' '+ current_time+ ' User: ' +user_input +' \n' + current_day+ ' ' + current_time+ ' ' + chatgpt_output + '\n') + f.write('\n' + current_day + ' ' + current_time + ' User: ' + + user_input + ' \n' + current_day + ' ' + current_time + ' ' + + chatgpt_output + '\n') f.close() return chatgpt_raw_output + # Function to get a response from the chatbot -def get_response(chat_history, name, chatgpt_output, userText, history_file,impersonated_role, explicit_input): - return chat(chat_history, name, chatgpt_output, userText, history_file, impersonated_role, explicit_input) +def get_response(chat_history, name, chatgpt_output, userText, history_file, + impersonated_role, explicit_input): + return chat(chat_history, name, chatgpt_output, userText, history_file, + impersonated_role, explicit_input)