Skip to content

Commit

Permalink
add optional debug flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Lin authored and Paul Lin committed Mar 31, 2024
1 parent 9b50984 commit 5d29533
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 145 deletions.
4 changes: 3 additions & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.env
.env
__pycache__/
bluebook_env/
55 changes: 0 additions & 55 deletions backend/README.md

This file was deleted.

82 changes: 42 additions & 40 deletions backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from pymongo.mongo_client import MongoClient

COURSE_QUERY_LIMIT = 5
SAFETY_CHECK_ENABLED = False
DATABASE_RELEVANCY_CHECK_ENABLED = False

load_dotenv()

Expand Down Expand Up @@ -40,28 +42,27 @@ def chat():
if message['role'] == 'ai':
message['role'] = 'assistant'

print(user_messages)

# for safety check, not to be included in final response
user_messages_safety_check = user_messages.copy()
user_messages_safety_check.append({
'role': 'user',
'content': 'Am I asking for help with courses or academics? Answer "yes" or "no".'
})
if SAFETY_CHECK_ENABLED:
# for safety check, not to be included in final response
user_messages_safety_check = user_messages.copy()
user_messages_safety_check.append({
'role': 'user',
'content': 'Am I asking for help with courses or academics? Answer "yes" or "no".'
})

response_safety_check = chat_completion_request(messages=user_messages_safety_check)
response_safety_check = response_safety_check.choices[0].message.content

if 'no' in response_safety_check.lower():
response = 'I am sorry, but I can only assist with questions related to courses or academics at this time.'
json_response = {
'response': response,
'courses': []
}
print('failed safety check')
return jsonify(json_response)
else:
print('passed safety check')
response_safety_check = chat_completion_request(messages=user_messages_safety_check)
response_safety_check = response_safety_check.choices[0].message.content
if 'no' in response_safety_check.lower():
response = 'I am sorry, but I can only assist with questions related to courses or academics at this time.'
json_response = {
'response': response,
'courses': []
}
print('failed safety check')
return jsonify(json_response)
else:
print('passed safety check')

# adding system message if user message does not include a system message header
if user_messages[0]['role'] != 'system':
Expand All @@ -70,27 +71,28 @@ def chat():
'content': 'Your name is Eli. You are a helpful assistant for Yale University students to ask questions about courses and academics.'
})

# checking if database query is necessary
user_messages_database_relevancy_check = user_messages.copy()
user_messages_database_relevancy_check.append({
'role': 'user',
'content': 'Will you be able to better answer my questions with information about specific courses related to the user query at Yale University? You should answer "yes" if you need information about courses at Yale that you don\'t have, otherwise you should answer "no".'
})
if DATABASE_RELEVANCY_CHECK_ENABLED:
# checking if database query is necessary
user_messages_database_relevancy_check = user_messages.copy()
user_messages_database_relevancy_check.append({
'role': 'user',
'content': 'Will you be able to better answer my questions with information about specific courses related to the user query at Yale University? You should answer "yes" if you need information about courses at Yale that you don\'t have, otherwise you should answer "no".'
})

user_messages_database_relevancy_check = chat_completion_request(messages=user_messages_database_relevancy_check)
response_user_messages_database_relevancy_check = user_messages_database_relevancy_check.choices[0].message.content
user_messages_database_relevancy_check = chat_completion_request(messages=user_messages_database_relevancy_check)
response_user_messages_database_relevancy_check = user_messages_database_relevancy_check.choices[0].message.content

if 'no' in response_user_messages_database_relevancy_check.lower():
response = chat_completion_request(messages=user_messages)
response = response.choices[0].message.content
json_response = {
'response': response,
'courses': []
}
print('no need to query database for course information')
return jsonify(json_response)
else:
print('need to query database for course information')
if 'no' in response_user_messages_database_relevancy_check.lower():
response = chat_completion_request(messages=user_messages)
response = response.choices[0].message.content
json_response = {
'response': response,
'courses': []
}
print('no need to query database for course information')
return jsonify(json_response)
else:
print('need to query database for course information')

# create embedding for user message to query against vector index
query_vector = create_embedding(user_messages[-1]['content'])
Expand Down
49 changes: 0 additions & 49 deletions backend/environment.yml

This file was deleted.

0 comments on commit 5d29533

Please sign in to comment.