Spaces:
Sleeping
Sleeping
File size: 8,091 Bytes
233222c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | 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 = (
"<p>No, this is a paid hackathon.</p>" if is_paid else
"<p>Yes, this is a free hackathon. There is no registration fee required to participate.</p>"
)
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": "<p>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.</p>",
"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": "<p>Yes</p>",
"drillId": drill_info.get("drillId", ""),
"positionOrder": 2,
"question": "Will I receive a certificate for participation?",
"questionId": ""
},
{
"answer": f"<p>{start_date_answer}</p>",
"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() |