diff --git a/backend/__pycache__/lib.cpython-311.pyc b/backend/__pycache__/lib.cpython-311.pyc deleted file mode 100644 index 55266ca8b..000000000 Binary files a/backend/__pycache__/lib.cpython-311.pyc and /dev/null differ diff --git a/backend/__pycache__/lib.cpython-312.pyc b/backend/__pycache__/lib.cpython-312.pyc index 24c8e2cf1..b9b70a6f6 100644 Binary files a/backend/__pycache__/lib.cpython-312.pyc and b/backend/__pycache__/lib.cpython-312.pyc differ diff --git a/backend/app.py b/backend/app.py index 72156b228..901604d91 100644 --- a/backend/app.py +++ b/backend/app.py @@ -8,7 +8,7 @@ COURSE_QUERY_LIMIT = 5 SAFETY_CHECK_ENABLED = False -DATABASE_RELEVANCY_CHECK_ENABLED = False +DATABASE_RELEVANCY_CHECK_ENABLED = True load_dotenv() @@ -42,6 +42,8 @@ def chat(): if message['role'] == 'ai': message['role'] = 'assistant' + print(user_messages) + if SAFETY_CHECK_ENABLED: # for safety check, not to be included in final response user_messages_safety_check = user_messages.copy() @@ -76,7 +78,7 @@ def chat(): 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".' + 'content': 'Will you be able to better answer my question with access to specific courses at Yale University? If you answer "yes", you will be provided with courses that are semantically similar to my question. Answer "yes" or "no".' }) user_messages_database_relevancy_check = chat_completion_request(messages=user_messages_database_relevancy_check) @@ -128,7 +130,7 @@ def chat(): recommendation_prompt = f'Here are some courses that might be relevant to the user request:\n\n' for course in recommended_courses: recommendation_prompt += f'{course["course_code"]}: {course["title"]}\n{course["description"]}\n\n' - recommendation_prompt += 'Provide a response to the user. Incorporate specific course information if it is relevant to the user request.' + recommendation_prompt += 'Provide a response to the user. Incorporate specific course information if it is relevant to the user request. If you include any course titles, make sure to wrap it in **double asterisks**. Do not order them in a list.' user_messages.append({ 'role': 'system', diff --git a/backend/chroma_demo.ipynb b/backend/chroma_demo.ipynb deleted file mode 100644 index fec7a23bd..000000000 --- a/backend/chroma_demo.ipynb +++ /dev/null @@ -1,219 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "!pip install chromadb\n", - "!pip install openai\n", - "!pip install python-dotenv" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", - "from dotenv import load_dotenv\n", - "load_dotenv()\n", - "import chromadb\n", - "import chromadb.utils.embedding_functions as embedding_functions\n", - "openai_ef = embedding_functions.OpenAIEmbeddingFunction(\n", - " api_key=os.getenv(\"OPENAI_API_KEY\"),\n", - " model_name=\"text-embedding-ada-002\"\n", - " )\n", - "chroma_client = chromadb.Client()\n", - "collection = chroma_client.create_collection(name=\"my_collection\", embedding_function=openai_ef)" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [], - "source": [ - "collection.add(\n", - " documents=[\"This class sucks!\", \"I love this class!\", \"I have learned NOTHING from this class\", \"This is a must take\"],\n", - " metadatas=[{\"source\": \"my_source\"}, {\"source\": \"my_source\"}, {\"source\": \"my_source\"}, {\"source\": \"my_source\"}],\n", - " ids=[\"id1\", \"id2\", \"id3\", \"id4\"]\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'ids': [['id4', 'id2']], 'distances': [[0.4128364324569702, 0.46640193462371826]], 'metadatas': [[{'source': 'my_source'}, {'source': 'my_source'}]], 'embeddings': None, 'documents': [['This is a must take', 'I love this class!']], 'uris': None, 'data': None}\n" - ] - } - ], - "source": [ - "results = collection.query(\n", - " query_texts=[\"positive reviews\"],\n", - " n_results=2\n", - ")\n", - "print(results)" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [], - "source": [ - "collection = chroma_client.create_collection(name=\"course_list\", embedding_function=openai_ef)\n", - "collection.add(\n", - " documents=[\"CPSC 439 Software Engineering\", \"CPSC 484 Human Computer Interaction\", \"CPSC 323 Introduction to Systems Programming\", \"CPSC 471 Trustworthy Deep Learning\"],\n", - " metadatas=[{\"source\": \"my_source\"}, {\"source\": \"my_source\"}, {\"source\": \"my_source\"}, {\"source\": \"my_source\"}],\n", - " ids=[\"id1\", \"id2\", \"id3\", \"id4\"]\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'ids': [['id1']],\n", - " 'distances': [[0.4114447236061096]],\n", - " 'metadatas': [[{'source': 'my_source'}]],\n", - " 'embeddings': None,\n", - " 'documents': [['CPSC 439 Software Engineering']],\n", - " 'uris': None,\n", - " 'data': None}" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "collection.query(\n", - " query_texts=[\"devops\"],\n", - " n_results=1\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'ids': [['id3']],\n", - " 'distances': [[0.3774021863937378]],\n", - " 'metadatas': [[{'source': 'my_source'}]],\n", - " 'embeddings': None,\n", - " 'documents': [['CPSC 323 Introduction to Systems Programming']],\n", - " 'uris': None,\n", - " 'data': None}" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "collection.query(\n", - " query_texts=[\"systems\"],\n", - " n_results=1\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'ids': [['id2']],\n", - " 'distances': [[0.3394881784915924]],\n", - " 'metadatas': [[{'source': 'my_source'}]],\n", - " 'embeddings': None,\n", - " 'documents': [['CPSC 484 Human Computer Interaction']],\n", - " 'uris': None,\n", - " 'data': None}" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "collection.query(\n", - " query_texts=[\"hci\"],\n", - " n_results=1\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'ids': [['id4']],\n", - " 'distances': [[0.4633175730705261]],\n", - " 'metadatas': [[{'source': 'my_source'}]],\n", - " 'embeddings': None,\n", - " 'documents': [['CPSC 471 Trustworthy Deep Learning']],\n", - " 'uris': None,\n", - " 'data': None}" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "collection.query(\n", - " query_texts=[\"DL\"],\n", - " n_results=1\n", - ")" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/frontend/src/app/page.module.css b/frontend/src/app/page.module.css index 27ee62fc6..1b3da7cab 100644 --- a/frontend/src/app/page.module.css +++ b/frontend/src/app/page.module.css @@ -85,7 +85,7 @@ } .message { - display: flex; + display: inline; flex-direction: column; word-wrap: break-word; padding: 0.5rem 1rem; diff --git a/frontend/src/app/page.tsx b/frontend/src/app/page.tsx index 5b5ea91d1..749e1e3b2 100644 --- a/frontend/src/app/page.tsx +++ b/frontend/src/app/page.tsx @@ -2,6 +2,7 @@ import React, { useState } from 'react'; import styles from './page.module.css'; // change to ur own directory +import { format } from 'path'; export default function Chat() { const [isTyping, setIsTyping] = useState(false); @@ -15,6 +16,8 @@ export default function Chat() { const handleSubmit = async (e: { preventDefault: () => void; }) => { e.preventDefault(); + + setInput(''); // add the user's message to the chat. const newUserMessage = { id: `user-${Date.now()}`, content: input, role: 'user' }; @@ -43,8 +46,6 @@ export default function Chat() { console.error('Failed to send message'); } - setInput(''); - }; const simulateTypingEffect = (message: string, role: string, messageId: string) => { @@ -80,6 +81,19 @@ export default function Chat() { setChatVisible(!chatVisible); }; + const formatMessage = (content: string) => { + const boldRegex = /\*\*(.*?)\*\*/g; + return content.split(boldRegex).map((part, index) => { + // Every even index is not bold, odd indices are the bold text between **. + if (index % 2 === 0) { + // Normal text + return part; + } else { + // Bold text + return {part}; + } + }); + }; return ( <> @@ -99,7 +113,7 @@ export default function Chat() {