Spaces:
Sleeping
Sleeping
| import json | |
| from config import client_ai | |
| from db_service import record_chat | |
| from datetime import datetime | |
| tools = [{ | |
| "type": "function", | |
| "function": { | |
| "name": "record_chat", | |
| "description": "Save the user and bot conversation to MongoDB", | |
| "parameters": { | |
| "type": "object", | |
| "properties": { | |
| "user_id": {"type": "string"}, | |
| "user_message": {"type": "string"}, | |
| "bot_reply": {"type": "string"} | |
| }, | |
| "required": ["user_id", "user_message", "bot_reply"] | |
| } | |
| } | |
| }] | |
| class CareerBot: | |
| def __init__(self): | |
| self.openai = client_ai | |
| self.name = "Career Guidance Bot" | |
| def get_current_greeting(self): | |
| """Return the correct greeting based on current time""" | |
| hour = datetime.now().hour | |
| if 5 <= hour < 12: | |
| return "Good morning" | |
| elif 12 <= hour < 17: | |
| return "Good afternoon" | |
| elif 17 <= hour < 21: | |
| return "Good evening" | |
| else: | |
| return "Good night" | |
| def system_prompt(self): | |
| return """You are CareerBot, a friendly and knowledgeable global career guidance assistant. | |
| β¨ Your behavior rules: | |
| - If the user greets you (e.g., "hello", "hey", "hi", "good morning", "good evening", "namaste"), | |
| check the current system time and respond with the correct greeting. | |
| - If the user used the wrong greeting for the time of day, gently correct them and provide the right one. | |
| Example: If user says "Good morning" but itβs afternoon β reply: | |
| "βοΈ Actually, itβs afternoon now π. Good afternoon! Welcome to CareerBot..." | |
| - If the question is about **career, studies, jobs, or skills** β give accurate and detailed guidance with examples, career roadmaps, and reliable resources. | |
| - When sharing **links**: | |
| - Use **only trusted, official websites** (Coursera, edX, LinkedIn Learning, YouTube official channels, Investopedia, Khan Academy, government education sites, etc.). | |
| - Never invent or guess links. | |
| - If no reliable link exists, guide the user to **search on trusted platforms** (e.g., βYou can explore this on Coursera by searching *Financial Accounting Fundamentals*β). | |
| - If the question is **technical** β provide step-by-step practical help (with examples, code snippets, or references to official docs). | |
| - If the question is about **general knowledge, global topics, or personal development** β answer clearly and helpfully. | |
| - Only if the question is completely irrelevant β reply: "This is not the right question." | |
| - Always keep your tone friendly, professional, and supportive. Use emojis when appropriate to make answers more engaging. | |
| """ | |
| def chat(self, message, history): | |
| # Normalize | |
| current_greeting = self.get_current_greeting() | |
| normalized_msg = message.lower() | |
| # Greeting words | |
| simple_greetings = ["hello", "hey", "hi", "namaste"] | |
| time_based_greetings = ["good morning", "good afternoon", "good evening", "good night"] | |
| # Case 1: simple greetings (hey, hi, hello, namaste) | |
| if any(greet in normalized_msg for greet in simple_greetings): | |
| bot_reply = f"{current_greeting}! π Welcome to CareerBot. How can I help you with your career today?" | |
| record_chat( message, bot_reply) | |
| return bot_reply | |
| # Case 2: time-based greetings (good morning, etc.) | |
| if any(greet in normalized_msg for greet in time_based_greetings): | |
| if current_greeting.lower() not in normalized_msg: | |
| bot_reply = f"βοΈ Actually, it's {current_greeting.lower()} now π. {current_greeting}! Welcome to CareerBot. I'm here to help you with career guidance." | |
| else: | |
| bot_reply = f"{current_greeting}! π Welcome to CareerBot. How can I guide you today?" | |
| record_chat( message, bot_reply) | |
| return bot_reply | |
| # Default: pass to OpenAI | |
| messages = [{"role": "system", "content": self.system_prompt()}] + history + [{"role": "user", "content": message}] | |
| response = self.openai.chat.completions.create( | |
| model="gpt-4o-mini", | |
| messages=messages, | |
| tools=tools | |
| ) | |
| choice = response.choices[0] | |
| bot_reply = choice.message.content.strip() if choice.message.content else "This is not the right question." | |
| record_chat( message, bot_reply) | |
| return bot_reply | |