Skip to content

Commit

Permalink
add format message
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 9a5b784 commit 3a1b9dc
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 226 deletions.
Binary file removed backend/__pycache__/lib.cpython-311.pyc
Binary file not shown.
Binary file modified backend/__pycache__/lib.cpython-312.pyc
Binary file not shown.
8 changes: 5 additions & 3 deletions backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

COURSE_QUERY_LIMIT = 5
SAFETY_CHECK_ENABLED = False
DATABASE_RELEVANCY_CHECK_ENABLED = False
DATABASE_RELEVANCY_CHECK_ENABLED = True

load_dotenv()

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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',
Expand Down
219 changes: 0 additions & 219 deletions backend/chroma_demo.ipynb

This file was deleted.

2 changes: 1 addition & 1 deletion frontend/src/app/page.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
}

.message {
display: flex;
display: inline;
flex-direction: column;
word-wrap: break-word;
padding: 0.5rem 1rem;
Expand Down
20 changes: 17 additions & 3 deletions frontend/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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' };
Expand Down Expand Up @@ -43,8 +46,6 @@ export default function Chat() {
console.error('Failed to send message');
}

setInput('');

};

const simulateTypingEffect = (message: string, role: string, messageId: string) => {
Expand Down Expand Up @@ -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 <strong key={index}>{part}</strong>;
}
});
};

return (
<>
Expand All @@ -99,7 +113,7 @@ export default function Chat() {
<div className={styles.messages}>
{messages.map((m) => (
<div key={m.id} className={`${styles.message} ${m.role === 'user' ? styles.user : styles.ai}`}>
{m.content}
{formatMessage(m.content)}
</div>
))}
{isTyping && (
Expand Down

0 comments on commit 3a1b9dc

Please sign in to comment.