Skip to content

Commit

Permalink
Merge pull request #11 from ls1intum/add-study-program-to-prompt
Browse files Browse the repository at this point in the history
Add study program to prompt
  • Loading branch information
ninori9 authored Oct 29, 2024
2 parents e7d4613 + 8e20512 commit b561edb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
7 changes: 4 additions & 3 deletions app/managers/request_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
41 changes: 34 additions & 7 deletions app/prompt/prompt_manager.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -31,6 +32,8 @@ def __init__(self):
--------------------
**Study Program Specific Information:**
{study_program}
-----
{specific_context}
--------------------
Expand Down Expand Up @@ -80,6 +83,8 @@ def __init__(self):
--------------------
**Studiengangspezifische Informationen:**
{study_program}
-----
{specific_context}
--------------------
Expand Down Expand Up @@ -140,6 +145,8 @@ def __init__(self):
--------------------
**Study Program Specific Information:**
{study_program}
-----
{specific_context}
--------------------
Expand Down Expand Up @@ -196,6 +203,8 @@ def __init__(self):
--------------------
**Studiengangspezifische Informationen:**
{study_program}
-----
{specific_context}
--------------------
Expand All @@ -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."

Expand All @@ -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"

Expand All @@ -251,16 +262,18 @@ 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(
general_context=general_context,
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."

Expand All @@ -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"

Expand Down Expand Up @@ -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}"

0 comments on commit b561edb

Please sign in to comment.