Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add improved and refactored filters #33

Merged
merged 5 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 40 additions & 33 deletions backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,57 +458,64 @@ def chat():
}

filtered_response = chat_completion_request(messages=user_messages, tools=tools)
print(filtered_response)
filtered_data = json.loads(filtered_response.choices[0].message.tool_calls[0].function.arguments)

filtered_data = json.loads(
filtered_response.choices[0].message.tool_calls[0].function.arguments
)
print("")
print("Completion Request: Filtered Response")
print(filtered_data)
print("")

natural_filter_subject = filtered_data.get("subject_code", None)
natural_filter_season_codes = filtered_data.get("season_code", None)
natural_filter_areas = filtered_data.get("areas", None)
natural_filter_skills = filtered_data.get("skills", None)


filter_skills = None

if not filter_subjects:
filter_subjects = filtered_data.get("subject", None)
if filter_subjects: filter_subjects = [filter_subjects]

if not filter_season_codes:
filter_season_codes = filtered_data.get("season_code", None)
if filter_season_codes: filter_season_codes = [filter_season_codes]

if not filter_areas:
filter_areas = filtered_data.get("areas", None)
if filter_areas: filter_areas = [filter_areas]
# Remove this elif if a skills dropdown filter is added to the chat interface
elif filter_areas == "QR" or filter_areas == "WR":
filter_skills = filtered_data.get("areas", None)

if not filter_skills:
filter_skills = filtered_data.get("skills", None)
if filter_skills: filter_skills = [filter_skills]

filters = []

# Apply filters for season codes
# Filter season codes
if filter_season_codes:
filters.append({"season_code": {"$in": filter_season_codes}})
elif natural_filter_season_codes:
filters.append({"season_code": {"$in": [natural_filter_season_codes]}})

# Apply filters for subjects
# Filter subject
if filter_subjects:
filters.append({"subject": {"$in": filter_subjects}})
elif natural_filter_subject:
filters.append({"subject": {"$in": [natural_filter_subject]}})

# Apply filters for areas

# Filter areas
if filter_areas:
filters.append({"areas": {"$in": filter_areas}})
elif natural_filter_areas:
filters.append({"areas": {"$in": [natural_filter_areas]}})

# Apply filters for skills
# Filter skills
if filter_skills:
filters.append({"skills": {"$in": filter_skills}})
elif natural_filter_skills:
filters.append({"skills": {"$in": [natural_filter_skills]}})


# If there are any filters, add them to the vectorSearch pipeline
if filters:
aggregate_pipeline["$vectorSearch"]["filter"] = {"$and": filters}


print()
print("Filters:")
print(aggregate_pipeline["$vectorSearch"]["filter"])

print()
# print(aggregate_pipeline)

database_response = collection.aggregate([aggregate_pipeline])
database_response = list(database_response)

recommended_courses = [
{
"season_code": course["season_code"],
Expand All @@ -532,17 +539,17 @@ def chat():

else:
recommendation_prompt = "Apologize to the user for not being able to fullfill their request, your response should begin with 'I'm sorry. I tried to search for courses that match your criteria but couldn't find any' verbatim"
recommendation_prompt += "Also suggest that the user should try widening their search or removing filters."

user_messages.append({"role": "system", "content": recommendation_prompt})

print(user_messages)

response = chat_completion_request(messages=user_messages)

response = response.choices[0].message.content

print()
print(user_messages)
print(response)
# print()
# print(user_messages)
# print(response)

json_response = {"response": response, "courses": recommended_courses}

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ export default function Chat() {
{ value: "Hu", label: "Hu" },
{ value: "Sc", label: "Sc" },
{ value: "So", label: "So" },
{ value: "Qr", label: "Qr" },
{ value: "Wr", label: "Wr" }
{ value: "QR", label: "QR" },
{ value: "WR", label: "WR" }
]}
styles={customStyles}
classNamePrefix="select"
Expand Down
Loading