Skip to content

Commit

Permalink
Automated autoyapf fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Nov 18, 2023
1 parent 5852d57 commit 840628b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
14 changes: 8 additions & 6 deletions application.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from utilities import get_response
import time


# Set the OpenAI API key
openai.api_key = c.openAI_api_key

Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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')


Expand Down
41 changes: 30 additions & 11 deletions utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,60 @@ 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,
presence_penalty=0,
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)

0 comments on commit 840628b

Please sign in to comment.