import json from typing import List from datetime import datetime from langchain_core.prompts import ChatPromptTemplate from langchain_core.output_parsers import StrOutputParser from langchain_core.runnables import RunnableSequence from langchain_core.messages import SystemMessage, HumanMessage from .core import get_llm def generate_drill_description(drill_info: dict, llm=None) -> str: """Generate a short description for the event based on the drillPurpose.""" if llm is None: llm = get_llm() drill_purpose = drill_info.get("drillPurpose", "Innovation") description_prompt = f""" Generate a short and engaging description for the following event: - Name: {drill_info["drillName"]} - Type: {drill_info["drillType"]} - Purpose: {drill_purpose} The description should clearly reflect the purpose of the event and provide an overview that aligns with the event's goals. """ prompt_template = ChatPromptTemplate.from_messages([ ("system", description_prompt), ("human", "Please generate the description based on the above details.") ]) response = (prompt_template | llm).invoke({}) return response.content.strip() def generate_drill_overview_description(drill_info: dict, llm=None) -> str: """Generate an overview description for the event.""" if llm is None: llm = get_llm() description_prompt = f""" Generate a single, concise overview paragraph (max 2-3 sentences) for this event: Name: {drill_info["drillName"]} Type: {drill_info["drillType"]} Purpose: {drill_info["drillPurpose"]} Start Date: {drill_info["phaseStartDt"]} End Date: {drill_info["phaseEndDt"]} Registration Start Date: {drill_info["drillRegistrationStartDt"]} Registration End Date: {drill_info["drillRegistrationEndDt"]} Is Paid: {"Yes" if drill_info["isDrillPaid"] else "No"} Rules: - Create ONLY ONE paragraph with 2-3 sentences maximum - Focus on the event's purpose and key details - Keep it professional and engaging - Do not list dates or repeat information verbatim - Do not use bullet points or multiple paragraphs - The response must be a single concise paragraph only """ chain = ( ChatPromptTemplate.from_messages([ ("system", description_prompt), ("human", "Generate a single overview paragraph.") ]) | llm | StrOutputParser() ) return chain.invoke({}) def generate_random_event_names(llm=None) -> List[str]: """Generate random creative event names when the user is unsure.""" if llm is None: llm = get_llm() prompt = """ Generate 5 random, creative, and professional event names. Only return the names, each on a new line. """ response = llm.invoke(prompt) suggestions = response.content.strip().split("\n") return [name.strip() for name in suggestions if name.strip()] def generate_faq_answers(drill_info: dict, llm=None) -> List[dict]: """Generate FAQ answers based on the drill details.""" drill_title = drill_info.get("drillName", "the hackathon") is_paid = drill_info.get("isDrillPaid", False) def format_date(date_str): if not date_str: return "" try: if "T" in date_str: dt = datetime.strptime(date_str.split('T')[0], "%Y-%m-%d") return dt.strftime("%d %B %Y") else: dt = datetime.strptime(date_str, "%d-%m-%Y") return dt.strftime("%d %B %Y") except Exception: return date_str drill_phase = json.loads(drill_info.get("drillPhase", "{}")) if isinstance(drill_info.get("drillPhase"), str) else drill_info.get("drillPhase", {}) phase_schedule = drill_phase.get("schedule", [{}])[0] if drill_phase.get("schedule") else {} phase_start_dt = format_date(phase_schedule.get("phaseStartDt") or drill_info.get("phaseStartDt", "")) phase_end_dt = format_date(phase_schedule.get("phaseEndDt") or drill_info.get("phaseEndDt", "")) registration_start_dt = format_date(drill_info.get("drillRegistrationStartDt", "")) registration_end_dt = format_date(drill_info.get("drillRegistrationEndDt", "")) payment_type = "paid" if is_paid else "free" payment_question = "Is this a free hackathon?" payment_answer = ( "

No, this is a paid hackathon.

" if is_paid else "

Yes, this is a free hackathon. There is no registration fee required to participate.

" ) if phase_start_dt and phase_end_dt: start_date_answer = ( f"The hackathon will start on {phase_start_dt} and end on {phase_end_dt}. " f"Registration is open from {registration_start_dt} to {registration_end_dt}." ) else: start_date_answer = "Start date to be announced" print(f"Debug - Dates: Start={phase_start_dt}, End={phase_end_dt}, Reg Start={registration_start_dt}, Reg End={registration_end_dt}") print(f"Debug - Final answer: {start_date_answer}") faq_list = [ { "answer": "

To Participate – Click on the \"Login & Participate\" button and sign up using your email or login with your existing account. If you are already logged in then click on the \"Participate\" button to continue to join the hackathon.

", "drillId": drill_info.get("drillId", ""), "positionOrder": 0, "question": f"How do I participate in {drill_title}?", "questionId": "" }, { "answer": payment_answer, "drillId": drill_info.get("drillId", ""), "positionOrder": 1, "question": payment_question, "questionId": "" }, { "answer": "

Yes

", "drillId": drill_info.get("drillId", ""), "positionOrder": 2, "question": "Will I receive a certificate for participation?", "questionId": "" }, { "answer": f"

{start_date_answer}

", "drillId": drill_info.get("drillId", ""), "positionOrder": 3, "question": "When will the hackathon start?", "questionId": "" } ] return faq_list def create_naming_assistant(llm=None) -> RunnableSequence: """Creates a naming assistant chain using ChatPromptTemplate""" if llm is None: llm = get_llm() prompt = ChatPromptTemplate.from_messages([ ("system", """You are a conversational naming assistant helping users name their technology related event. Key Behaviors: 1. Guide the conversation naturally to help users select a name 2. Keep your responses decent and engaging. 3. When user is uncertain, first ask about their event's theme/focus 4. Based on their response, provide 5 creative, relevant name suggestions 5. Handle user responses intelligently: - If they select a suggestion (by number or name),or write a name from the suggestions shown, confirm with "SELECTED_NAME: [name]" - If they want more options, provide new suggestions - If they propose their own name, evaluate it and either accept ("SELECTED_NAME: [name]") or suggest alternatives - If they're unsure, encourage them with refined suggestions Format your suggestions as: "Based on [their theme/focus], here are some suggestions: 1. [Name 1] 2. [Name 2] 3. [Name 3] 4. [Name 4] 5. [Name 5] You can select one of these numbers, suggest your own name, or ask for more options." IMPORTANT: Once a name is selected, immediately respond with "SELECTED_NAME: [name]" to signal completion. Conversation History: {history} Current User Input: {input} """), ("human", "{input}") ]) return prompt | llm | StrOutputParser()