diff --git a/app/managers/request_handler.py b/app/managers/request_handler.py index 6557cdd..4253ddc 100644 --- a/app/managers/request_handler.py +++ b/app/managers/request_handler.py @@ -25,7 +25,7 @@ def handle_question(self, question: str, classification: str, language: str): sample_questions = self.weaviate_manager.get_relevant_sample_questions(question=question, language=language) sample_questions_formatted = self.prompt_manager.format_sample_questions(sample_questions, language) messages = self.prompt_manager.create_messages(general_context, specific_context, sample_questions_formatted, - question, language) + question, language, classification) return self.model.complete(messages) @@ -46,7 +46,7 @@ def handle_question_test_mode(self, question: str, classification: str, language sample_questions = self.weaviate_manager.get_relevant_sample_questions(question=question, language=language) sample_questions_formatted = self.prompt_manager.format_sample_questions(sample_questions, language) messages = self.prompt_manager.create_messages(general_context, specific_context, sample_questions_formatted, - question, language) + question, language, classification) answer, tokens = self.model.complete_with_tokens(messages) return answer, tokens, general_context_list, specific_context_list @@ -76,7 +76,8 @@ def handle_chat(self, messages: List[ChatMessage], study_program: str): question=last_message, history=history_formatted, sample_questions=sample_questions_formatted, - language=lang + language=lang, + study_program=study_program ) # Generate and return the answer diff --git a/app/prompt/prompt_manager.py b/app/prompt/prompt_manager.py index cee3252..08a4b2c 100644 --- a/app/prompt/prompt_manager.py +++ b/app/prompt/prompt_manager.py @@ -1,5 +1,6 @@ from typing import List import logging +import re from app.data.user_requests import ChatMessage from app.managers.weaviate_manager import SampleQuestion @@ -31,6 +32,8 @@ def __init__(self): -------------------- **Study Program Specific Information:** + {study_program} + ----- {specific_context} -------------------- @@ -80,6 +83,8 @@ def __init__(self): -------------------- **Studiengangspezifische Informationen:** + {study_program} + ----- {specific_context} -------------------- @@ -140,6 +145,8 @@ def __init__(self): -------------------- **Study Program Specific Information:** + {study_program} + ----- {specific_context} -------------------- @@ -196,6 +203,8 @@ def __init__(self): -------------------- **Studiengangspezifische Informationen:** + {study_program} + ----- {specific_context} -------------------- @@ -219,16 +228,17 @@ def __init__(self): """ def create_messages(self, general_context: str, specific_context: str, sample_questions: str, question: str, - language: str): + language: str, study_program): """Converts the template into a message format suitable for LLMs like OpenAI's GPT.""" - + study_program_text = self.format_study_program(study_program, language) # Construct the system prompt if language.lower() == "english": user_content = self.answer_prompt_template.format( general_context=general_context, specific_context=specific_context or "No specific context available.", question=question, - sample_questions=sample_questions + sample_questions=sample_questions, + study_program=study_program_text ) system_content = "You are an intelligent assistant that helps students of the Technical University of Munich (TUM) with questions related to their studies." @@ -237,7 +247,8 @@ def create_messages(self, general_context: str, specific_context: str, sample_qu general_context=general_context, specific_context=specific_context or "Kein studienfachspezifischer Kontext verfügbar.", question=question, - sample_questions=sample_questions + sample_questions=sample_questions, + study_program=study_program_text ) system_content = "Sie sind ein intelligenter Assistent, der den Studierenden der Technischen Universität München (TUM) bei Fragen rund um ihr Studium hilft" @@ -251,8 +262,9 @@ def create_messages(self, general_context: str, specific_context: str, sample_qu def create_messages_with_history(self, general_context: str, specific_context: str, question: str, history: str, - sample_questions: str, language: str): + sample_questions: str, language: str, study_program: str): """Converts the template into a message format suitable for LLMs like OpenAI's GPT.""" + study_program_text = self.format_study_program(study_program, language) # Construct the system prompt including history if language.lower() == "english": user_content = self.answer_prompt_template_with_history.format( @@ -260,7 +272,8 @@ def create_messages_with_history(self, general_context: str, specific_context: s specific_context=specific_context or "No specific context available.", question=question, history=history or "No conversation history available.", - sample_questions=sample_questions + sample_questions=sample_questions, + study_program=study_program_text ) system_content = "You are an intelligent assistant that helps students of the Technical University of Munich (TUM) with questions related to their studies." @@ -270,7 +283,8 @@ def create_messages_with_history(self, general_context: str, specific_context: s specific_context=specific_context or "Kein studienfachspezifischer Kontext verfügbar.", question=question, history=history or "Kein Verlauf verfügbar.", - sample_questions=sample_questions + sample_questions=sample_questions, + study_program=study_program_text ) system_content = "Sie sind ein intelligenter Assistent, der den Studierenden der Technischen Universität München (TUM) bei Fragen rund um ihr Studium hilft" @@ -320,3 +334,16 @@ def format_chat_history(self, chat_messages: List[ChatMessage], language: str) - # Join formatted messages with separator combined_string = "\n\n".join(formatted_strings) return combined_string + + # Format the study program + def format_study_program(self, study_program: str, language: str) -> str: + if not study_program or study_program.lower() == "general": + return "No study program specified" if language.lower() == "english" else "Kein Studiengang angegeben" + + # Capitalize first letter of each word and replace hyphens with spaces + formatted_program = re.sub(r'-', ' ', study_program).title() + + if language.lower() == "english": + return f"The study program of the student is {formatted_program}" + else: + return f"Der Studiengang des Studenten ist {formatted_program}" \ No newline at end of file