use real MCP data fix
Browse files26 unmodified lines27282930303132333436353637383940414344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071084243444563 unmodified lines109110111178112113114115156 unmodified lines27227327434127527627727827928028134834928228328428528635328728828929030 unmodified lines32132232339032432532632721 unmodified lines34935035135235335435535635735810 unmodified lines36937037143437237337443637537643843944037737837938044244344444544638138238338438538638738845038939039139234 unmodified lines4274284294914304314324334342 unmodified lines43743843950050150250350450550644044144244344444544644744844912 unmodified lines4624634645255265275284654664674684695304704714724735344744754764771 unmodified line4794804815424824835444844854864874884895504904914924934 unmodified lines4984995005615625015025035645045055065075685085095705715105115125135141 unmodified line5165175185795195205215223 unmodified lines52652752858959059159259359459559659759859960060160260352953060560653153253353453553653753853954054154254354454554660961061161254754854955055155221 unmodified lines5745755766405775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246496506256266276286296566306316326336346616356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786676796806816 unmodified lines68868969068068168268368468568669169268969069369469569669716 unmodified lines71471571671371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579671771871972072172272372472572672772872979879980080180280380480580680780880981081181273073173273373473573673773873974074174274374474574674774874975075175275375475581481581681781881982082182282382482582682782882983083183283383483583683783883984084184275675775875976076176284484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787876376476576676776817 unmodified lines786787788902789790791792134 unmodified lines927928929104393093193293393493510499369379381051939940105494194294394430 unmodified lines97597697710919789799809817 unmodified lines9899909911105992993110799499599699799899910001114100110021003100410051119100610071008100912 unmodified lines102210231024113810251026102710288 unmodified lines1037103810391153104010411156104210431158104410451046104724 unmodified lines107210731074118911901075107610771078107930 unmodified lines1110111111121227111311141230111511161117111834 unmodified lines1153115411551271127212731274115611571158115911601161116242 unmodified lines1205120612071323120812091210121114 unmodified lines122612271228134413451346134713481349135012291230123163 unmodified lines129512961297142012981299130033 unmodified lines1334133513361460133713381339134082 unmodified lines1423142414251549142614271428142916 unmodified lines1446144714481572144914501451145226 unmodified lines
@dataclassclass UserProfile: skills: List[str] experience_level: str time_available: str interests: List[str]
class UltimateTopcoderMCPEngine: """FIXED: Real MCP Integration - More Aggressive Connection""" """FIXED: Real MCP Integration - No Mock/Fallback Data"""
def __init__(self): print("π Initializing ULTIMATE Topcoder MCP Engine...") self.base_url = "https://api.topcoder.com/v6/mcp" self.session_id = None self.is_connected = False self.mock_challenges = self._create_enhanced_fallback_challenges() print(f"β
Loaded fallback system with {len(self.mock_challenges)} premium challenges")
def _create_enhanced_fallback_challenges(self) -> List[Challenge]: return [ Challenge( id="30174840", title="React Component Library Development", description="Build a comprehensive React component library with TypeScript support and Storybook documentation. Perfect for developers looking to create reusable UI components.", technologies=["React", "TypeScript", "Storybook", "CSS", "Jest"], difficulty="Intermediate", prize="$3,000", time_estimate="14 days", registrants=45 ), Challenge( id="30174841", title="Python API Performance Optimization", description="Optimize existing Python FastAPI application for better performance and scalability. Focus on database queries, caching strategies, and async processing.", technologies=["Python", "FastAPI", "PostgreSQL", "Redis", "Docker"], difficulty="Advanced", prize="$5,000", time_estimate="21 days", registrants=28 ), Challenge( id="30174842", title="Mobile App UI/UX Design", description="Design modern, accessible mobile app interface with dark mode support and responsive layouts for both iOS and Android platforms.", technologies=["Figma", "UI/UX", "Mobile Design", "Accessibility", "Prototyping"], difficulty="Beginner", prize="$2,000", time_estimate="10 days", registrants=67 ), Challenge( id="30174843", title="Blockchain Smart Contract Development", description="Develop secure smart contracts for DeFi applications with comprehensive testing suite and gas optimization techniques.", technologies=["Solidity", "Web3", "JavaScript", "Hardhat", "Testing"], difficulty="Advanced", prize="$7,500", time_estimate="28 days", registrants=19 ), Challenge( id="30174844", title="Data Visualization Dashboard", description="Create interactive data visualization dashboard using modern charting libraries with real-time data updates and export capabilities.", technologies=["D3.js", "JavaScript", "HTML", "CSS", "Chart.js"], difficulty="Intermediate", prize="$4,000", time_estimate="18 days", registrants=33 ), Challenge( id="30174845", title="Machine Learning Model Deployment", description="Deploy ML models to production with API endpoints, monitoring, and auto-scaling capabilities using cloud platforms.", technologies=["Python", "TensorFlow", "Docker", "Kubernetes", "AWS"], difficulty="Advanced", prize="$6,000", time_estimate="25 days", registrants=24 ) ] print(f"β
MCP Engine initialized with live data connection")
def parse_sse_response(self, sse_text: str) -> Dict[str, Any]: """Parse Server-Sent Events response"""63 unmodified lines print("β οΈ MCP connection succeeded but no session ID found")
except Exception as e: print(f"β οΈ MCP connection failed, using enhanced fallback: {e}") print(f"β οΈ MCP connection failed: {e}")
return False
156 unmodified lines sort_by: str = None, sort_order: str = None, ) -> List[Challenge]: """FIXED: More aggressive real challenge fetching""" """FIXED: Only fetch real challenges, no mock/fallback""" # Always try to connect print(f"π Attempting to fetch REAL challenges (limit: {limit})") connection_success = await self.initialize_connection() if not connection_success: print("β Could not establish MCP connection, using fallback") return [] print("β Could not establish MCP connection") raise Exception("Unable to connect to Topcoder MCP server. Please try again later.")
# Build comprehensive query parameters skill_keywords = self.extract_technologies_from_query( query + " " + " ".join(user_profile.skills + user_profile.interests) query + " " + " ".join(user_profile.interests) # FIXED: Only using interests, not skills )
mcp_query = {30 unmodified lines result = await self.call_tool("query-tc-challenges", mcp_query) if not result: print("β No result from MCP tool call") return [] raise Exception("No data received from Topcoder MCP server. Please try again later.")
print(f"π Raw MCP result type: {type(result)}") if isinstance(result, dict):21 unmodified lines except json.JSONDecodeError: pass
if not challenge_data_list: print("β No challenge data found in MCP response") raise Exception("No challenges found matching your criteria. Please try different filters.")
challenges = [] for item in challenge_data_list: if isinstance(item, dict):10 unmodified lines def calculate_advanced_compatibility_score(self, challenge: Challenge, user_profile: UserProfile, query: str) -> tuple: score = 0.0 factors = [] user_skills_lower = [skill.lower().strip() for skill in user_profile.skills] # FIXED: Only using interests, not skills user_interests_lower = [interest.lower().strip() for interest in user_profile.interests] challenge_techs_lower = [tech.lower() for tech in challenge.technologies] skill_matches = len(set(user_skills_lower) & set(challenge_techs_lower)) interest_matches = len(set(user_interests_lower) & set(challenge_techs_lower)) if len(challenge.technologies) > 0: exact_match_score = (skill_matches / len(challenge.technologies)) * 30 coverage_bonus = min(skill_matches * 10, 10) skill_score = exact_match_score + coverage_bonus exact_match_score = (interest_matches / len(challenge.technologies)) * 30 coverage_bonus = min(interest_matches * 10, 10) interest_score = exact_match_score + coverage_bonus else: skill_score = 30 score += skill_score if skill_matches > 0: matched_skills = [t for t in challenge.technologies if t.lower() in user_skills_lower] factors.append(f"Strong match: uses your {', '.join(matched_skills[:2])} expertise") interest_score = 30 score += interest_score if interest_matches > 0: matched_interests = [t for t in challenge.technologies if t.lower() in user_interests_lower] factors.append(f"Strong match: uses your {', '.join(matched_interests[:2])} interests") elif len(challenge.technologies) > 0: factors.append(f"Growth opportunity: learn {', '.join(challenge.technologies[:2])}") else: factors.append("Versatile challenge suitable for multiple skill levels") factors.append("Versatile challenge suitable for multiple skill/interest levels") level_mapping = {'beginner': 1, 'intermediate': 2, 'advanced': 3} user_level_num = level_mapping.get(user_profile.experience_level.lower(), 2) challenge_level_num = level_mapping.get(challenge.difficulty.lower(), 2)34 unmodified lines return min(score, 100.0), factors
def get_user_insights(self, user_profile: UserProfile) -> Dict: skills = user_profile.skills # FIXED: Only using interests, not skills interests = user_profile.interests level = user_profile.experience_level time_available = user_profile.time_available frontend_skills = ['react', 'javascript', 'css', 'html', 'vue', 'angular', 'typescript']2 unmodified lines devops_skills = ['docker', 'kubernetes', 'aws', 'azure', 'terraform', 'jenkins'] design_skills = ['figma', 'ui/ux', 'design', 'prototyping', 'accessibility'] blockchain_skills = ['solidity', 'web3', 'ethereum', 'blockchain', 'smart contracts', 'nft'] user_skills_lower = [skill.lower() for skill in skills] frontend_count = sum(1 for skill in user_skills_lower if any(fs in skill for fs in frontend_skills)) backend_count = sum(1 for skill in user_skills_lower if any(bs in skill for bs in backend_skills)) data_count = sum(1 for skill in user_skills_lower if any(ds in skill for ds in data_skills)) devops_count = sum(1 for skill in user_skills_lower if any(ds in skill for ds in devops_skills)) design_count = sum(1 for skill in user_skills_lower if any(ds in skill for ds in design_skills)) blockchain_count = sum(1 for skill in user_skills_lower if any(bs in skill for bs in blockchain_skills)) user_interests_lower = [interest.lower() for interest in interests] frontend_count = sum(1 for interest in user_interests_lower if any(fs in interest for fs in frontend_skills)) backend_count = sum(1 for interest in user_interests_lower if any(bs in interest for bs in backend_skills)) data_count = sum(1 for interest in user_interests_lower if any(ds in interest for ds in data_skills)) devops_count = sum(1 for interest in user_interests_lower if any(ds in interest for ds in devops_skills)) design_count = sum(1 for interest in user_interests_lower if any(ds in interest for ds in design_skills)) blockchain_count = sum(1 for interest in user_interests_lower if any(bs in interest for bs in blockchain_skills)) if blockchain_count >= 2: profile_type = "Blockchain Developer" elif frontend_count >= 2 and backend_count >= 1:12 unmodified lines profile_type = "Versatile Developer" insights = { 'profile_type': profile_type, 'strengths': f"Strong {profile_type.lower()} with expertise in {', '.join(skills[:3]) if skills else 'multiple technologies'}", 'growth_areas': self._suggest_growth_areas(user_skills_lower, frontend_count, backend_count, data_count, devops_count, blockchain_count), 'skill_progression': f"Ready for {level.lower()} to advanced challenges based on current skill set", 'market_trends': self._get_market_trends(skills), 'strengths': f"Strong {profile_type.lower()} with expertise in {', '.join(interests[:3]) if interests else 'multiple technologies'}", 'growth_areas': self._suggest_growth_areas(user_interests_lower, frontend_count, backend_count, data_count, devops_count, blockchain_count), 'skill_progression': f"Ready for {level.lower()} to advanced challenges based on current skill/interest set", 'market_trends': self._get_market_trends(interests), 'time_optimization': f"With {time_available}, you can complete 1-2 medium challenges or 1 large project", 'success_probability': self._calculate_success_probability(level, len(skills)) 'success_probability': self._calculate_success_probability(level, len(interests)) } return insights
def _suggest_growth_areas(self, user_skills: List[str], frontend: int, backend: int, data: int, devops: int, blockchain: int) -> str: def _suggest_growth_areas(self, user_interests: List[str], frontend: int, backend: int, data: int, devops: int, blockchain: int) -> str: suggestions = [] if blockchain < 1 and (frontend >= 1 or backend >= 1): suggestions.append("blockchain and Web3 technologies")1 unmodified line suggestions.append("cloud technologies (AWS, Docker)") if data < 1 and backend >= 1: suggestions.append("database optimization and analytics") if frontend >= 1 and "typescript" not in str(user_skills): if frontend >= 1 and "typescript" not in str(user_interests): suggestions.append("TypeScript for enhanced development") if backend >= 1 and "api" not in str(user_skills): if backend >= 1 and "api" not in str(user_interests): suggestions.append("API design and microservices") if not suggestions: suggestions = ["AI/ML integration", "system design", "performance optimization"] return "Consider exploring " + ", ".join(suggestions[:3])
def _get_market_trends(self, skills: List[str]) -> str: def _get_market_trends(self, interests: List[str]) -> str: hot_skills = { 'react': 'React dominates frontend with 75% job market share', 'python': 'Python leads in AI/ML and backend development growth',4 unmodified lines 'ai': 'AI integration skills in highest demand for 2024', 'kubernetes': 'Container orchestration critical for enterprise roles' } for skill in skills: skill_lower = skill.lower() for interest in interests: interest_lower = interest.lower() for hot_skill, trend in hot_skills.items(): if hot_skill in skill_lower: if hot_skill in interest_lower: return trend return "Full-stack and cloud skills show strongest market demand"
def _calculate_success_probability(self, level: str, skill_count: int) -> str: def _calculate_success_probability(self, level: str, interest_count: int) -> str: base_score = {'beginner': 60, 'intermediate': 75, 'advanced': 85}.get(level.lower(), 70) skill_bonus = min(skill_count * 3, 15) total = base_score + skill_bonus interest_bonus = min(interest_count * 3, 15) total = base_score + interest_bonus if total >= 90: return f"{total}% - Outstanding success potential" elif total >= 80:1 unmodified line elif total >= 70: return f"{total}% - Good probability of success" else: return f"{total}% - Consider skill development first" return f"{total}% - Consider skill/interest development first"
async def get_personalized_recommendations( self, user_profile: UserProfile, query: str = "",3 unmodified lines limit: int = 50 ) -> Dict[str, Any]: start_time = datetime.now() print(f"π― Analyzing profile: {user_profile.skills} | Level: {user_profile.experience_level}")
# FIXED: More aggressive real data fetching real_challenges = await self.fetch_real_challenges( user_profile=user_profile, query=query, limit=limit, status=status, prize_min=prize_min, prize_max=prize_max, challenge_type=challenge_type, track=track, sort_by=sort_by, sort_order=sort_order, ) print(f"π― Analyzing profile: {user_profile.interests} | Level: {user_profile.experience_level}")
if real_challenges: challenges = real_challenges # FIXED: Only fetch real challenges, no mock/fallback try: challenges = await self.fetch_real_challenges( user_profile=user_profile, query=query, limit=limit, status=status, prize_min=prize_min, prize_max=prize_max, challenge_type=challenge_type, track=track, sort_by=sort_by, sort_order=sort_order, ) data_source = "π₯ REAL Topcoder MCP Server (4,596+ challenges)" print(f"π Using {len(challenges)} REAL Topcoder challenges!") else: challenges = self.mock_challenges data_source = "β¨ Enhanced Intelligence Engine (Premium Dataset)" print(f"β‘ Using {len(challenges)} premium challenges with advanced algorithms") except Exception as e: print(f"β Error fetching challenges: {str(e)}") raise Exception(f"Unable to fetch challenges from Topcoder MCP: {str(e)}")
scored_challenges = [] for challenge in challenges:21 unmodified lines "session_active": bool(self.session_id), "mcp_connected": self.is_connected, "algorithm_version": "Advanced Multi-Factor v2.0", "topcoder_total": "4,596+ live challenges" if real_challenges else "Premium dataset" "topcoder_total": "4,596+ live challenges" } }
class EnhancedLLMChatbot: """FIXED: Enhanced LLM Chatbot with OpenAI Integration + HF Secrets""" LLM_INSTRUCTIONS = """You are an expert Topcoder Challenge Intelligence Assistant with REAL-TIME access to live challenge data through MCP integration.
Your capabilities:- Access to 4,596+ live Topcoder challenges through real MCP integration- Advanced challenge matching algorithms with multi-factor scoring- Real-time prize information, difficulty levels, and technology requirements- Comprehensive skill & interest analysis and career guidance- Market intelligence and technology trend insights
Guidelines:- Use the REAL challenge data provided above in your responses- Reference actual challenge titles, prizes, and technologies when relevant- Provide specific, actionable advice based on real data- Mention that your data comes from live MCP integration with Topcoder- Be enthusiastic about the real-time data capabilities- If asked about specific technologies, reference actual challenges that use them- For skill & interest questions, suggest real challenges that match their level- Keep responses concise but informative (max 300 words)
Provide a helpful, intelligent response using the real challenge data context."""
FOOTER_TEXT = "π€ Powered by OpenAI GPT-4 + Real MCP Data"
LLM_TOOLS = [ { "type": "function", "name": "get_challenge_context", "description": "Query challenges via Topcoder API", "parameters": { "type": "object", "properties": { "query": {"type": "string", "description": "Search query for challenges. e.g. python, react, etc."}, "limit": {"type": "integer", "description": "Maximum number of challenges to return", "default": 10} }, "required": ["query"] } } ]
def __init__(self, mcp_engine): self.mcp_engine = mcp_engine self.conversation_context = [] self.user_preferences = {} # FIXED: Use Hugging Face Secrets (environment variables) self.openai_api_key = os.getenv("OPENAI_API_KEY", "") if not self.openai_api_key: print("β οΈ OpenAI API key not found in HF secrets. Using enhanced fallback responses.") print("β οΈ OpenAI API key not found in HF secrets. Chat will show error messages.") self.llm_available = False else: self.llm_available = True print("β
OpenAI API key loaded from HF secrets for intelligent responses")
async def generate_openai_response(self, input_list: List[Dict]) -> Dict: """Reusable function to call the OpenAI API.""" headers = { "Content-Type": "application/json", "Authorization": f"Bearer {self.openai_api_key}" } body = { "model": "gpt-4o-mini", "input": input_list, "store": False, "tools": self.LLM_TOOLS, "instructions": self.LLM_INSTRUCTIONS } print("π Sending request to OpenAI API...") async with httpx.AsyncClient(timeout=30.0) as client: response = await client.post( "https://api.openai.com/v1/responses", headers=headers, json=body ) print(f"π‘ Received OpenAI response with status: {response.status_code}") if response.status_code == 200: return response.json() else: print(f"OpenAI API error: {response.status_code} - {response.text}") raise Exception(f"β **OpenAI API Error** (Status {response.status_code}): Unable to generate response. Please try again later or check your API key configuration.")
def extract_response_text(self, data: Dict) -> str: """Safely extracts the response text from the API data.""" print("π Parsing OpenAI response text...") try: response_text = data["output"][0]["content"][0]["text"] print("β
Successfully extracted response text.") return response_text except (KeyError, IndexError): print("β οΈ Failed to extract response text, returning default message.") return "I apologize, but I couldn't generate a response. Please try again."
async def get_challenge_context(self, query: str, limit: int = 10) -> str: """Get relevant challenge data for LLM context""" try: # Create a basic profile for context basic_profile = UserProfile( skills=['Python', 'JavaScript'], experience_level='Intermediate', time_available='4-8 hours', interests=[query]6 unmodified lines limit=limit ) if not challenges: # Try fallback challenges challenges = self.mcp_engine.mock_challenges[:limit] context_source = "Enhanced Intelligence Engine" else: context_source = "Real MCP Server" # Create rich context from real data context_data = { "total_challenges_available": "4,596+" if challenges == self.mcp_engine.mock_challenges else f"{len(challenges)}+", "data_source": context_source, "total_challenges_available": "4,596+", "data_source": "Real MCP Server", "sample_challenges": [] } 16 unmodified lines return f"Challenge data temporarily unavailable: {str(e)}" async def generate_llm_response(self, user_message: str, chat_history: List) -> str: """FIXED: Generate intelligent response using OpenAI API with real MCP data""" # Get real challenge context challenge_context = await self.get_challenge_context(user_message) # Build conversation context recent_history = chat_history[-4:] if len(chat_history) > 4 else chat_history history_text = "\n".join([f"User: {h[0]}\nAssistant: {h[1]}" for h in recent_history]) # Create comprehensive prompt for LLM system_prompt = f"""You are an expert Topcoder Challenge Intelligence Assistant with REAL-TIME access to live challenge data through MCP integration.
REAL CHALLENGE DATA CONTEXT:{challenge_context}
Your capabilities:- Access to 4,596+ live Topcoder challenges through real MCP integration- Advanced challenge matching algorithms with multi-factor scoring- Real-time prize information, difficulty levels, and technology requirements- Comprehensive skill analysis and career guidance- Market intelligence and technology trend insights
CONVERSATION HISTORY:{history_text}
Guidelines:- Use the REAL challenge data provided above in your responses- Reference actual challenge titles, prizes, and technologies when relevant- Provide specific, actionable advice based on real data- Mention that your data comes from live MCP integration with Topcoder- Be enthusiastic about the real-time data capabilities- If asked about specific technologies, reference actual challenges that use them- For skill questions, suggest real challenges that match their level- Keep responses concise but informative (max 300 words)
User's current question: {user_message}
Provide a helpful, intelligent response using the real challenge data context."""
# FIXED: Try OpenAI API if available if self.llm_available: try: async with httpx.AsyncClient(timeout=30.0) as client: response = await client.post( "https://api.openai.com/v1/chat/completions", # FIXED: Correct OpenAI endpoint headers={ "Content-Type": "application/json", "Authorization": f"Bearer {self.openai_api_key}" # FIXED: Proper auth header }, json={ "model": "gpt-4o-mini", # Fast and cost-effective "messages": [ {"role": "system", "content": "You are an expert Topcoder Challenge Intelligence Assistant with real MCP data access."}, {"role": "user", "content": system_prompt} ], "max_tokens": 800, "temperature": 0.7 } ) if response.status_code == 200: data = response.json() llm_response = data["choices"][0]["message"]["content"] # Add real-time data indicators llm_response += f"\n\n*π€ Powered by OpenAI GPT-4 + Real MCP Data β’ {len(challenge_context)} chars of live context*" return llm_response else: print(f"OpenAI API error: {response.status_code} - {response.text}") return await self.get_fallback_response_with_context(user_message, challenge_context) except Exception as e: print(f"OpenAI API error: {e}") return await self.get_fallback_response_with_context(user_message, challenge_context) # Fallback to enhanced responses with real data return await self.get_fallback_response_with_context(user_message, challenge_context) async def get_fallback_response_with_context(self, user_message: str, challenge_context: str) -> str: """Enhanced fallback using real challenge data""" message_lower = user_message.lower() # Parse challenge context for intelligent responses """Send a message to the conversation using Responses API""" if not self.llm_available: raise Exception("OpenAI API key not configured. Please set it in Hugging Face Secrets.")
input_list = [] for user_msg, bot_resp in chat_history: bot_resp_cleaned = bot_resp.split(f"\n\n*{self.FOOTER_TEXT}")[0] input_list.append({"role": "user", "content": user_msg}) input_list.append({"role": "assistant", "content": bot_resp_cleaned}) input_list.append({"role": "user", "content": user_message})
print("π€ Generating LLM response...") try: context_data = json.loads(challenge_context) challenges = context_data.get("sample_challenges", []) except: challenges = [] # Technology-specific responses using real data tech_keywords = ['python', 'react', 'javascript', 'blockchain', 'ai', 'ml', 'java', 'nodejs', 'angular', 'vue'] matching_tech = [tech for tech in tech_keywords if tech in message_lower] if matching_tech: relevant_challenges = [] for challenge in challenges: challenge_techs = [tech.lower() for tech in challenge.get('technologies', [])] if any(tech in challenge_techs for tech in matching_tech): relevant_challenges.append(challenge) data = await self.generate_openai_response(input_list) input_list += data.get("output", [])
tool_result = None function_call_found = False for item in data.get("output", []): if item.get("type") == "function_call" and item.get("name") == "get_challenge_context": print("π Function call detected, processing tool...") function_call_found = True tool_args = json.loads(item.get("arguments", "{}")) query = tool_args.get("query", "") limit = tool_args.get("limit", 10) tool_result = await self.get_challenge_context(query, limit) print(f"π§ Tool result: {json.dumps(tool_result, indent=2) if tool_result else 'No data returned'}") input_list.append({ "type": "function_call_output", "call_id": item.get("call_id"), "output": json.dumps({"challenges": tool_result}) })
if function_call_found: data = await self.generate_openai_response(input_list)
llm_response = self.extract_response_text(data) if relevant_challenges: response = f"Great question about {', '.join(matching_tech)}! π Based on my real MCP data access, here are actual challenges:\n\n" for i, challenge in enumerate(relevant_challenges[:3], 1): response += f"π― **{challenge['title']}**\n" response += f" π° Prize: {challenge['prize']}\n" response += f" π οΈ Technologies: {', '.join(challenge['technologies'])}\n" response += f" π Difficulty: {challenge['difficulty']}\n" response += f" π₯ Registrants: {challenge['registrants']}\n\n" response += f"*These are REAL challenges from my live MCP connection to Topcoder's database of 4,596+ challenges!*" return response # Prize/earning questions with real data if any(word in message_lower for word in ['prize', 'money', 'earn', 'pay', 'salary', 'income']): if challenges: response = f"π° Based on real MCP data, current Topcoder challenges offer:\n\n" for i, challenge in enumerate(challenges[:3], 1): response += f"{i}. **{challenge['title']}** - {challenge['prize']}\n" response += f" π Difficulty: {challenge['difficulty']} | π₯ Competition: {challenge['registrants']} registered\n\n" response += f"*This is live prize data from {context_data.get('total_challenges_available', '4,596+')} real challenges!*" return response # Career/skill questions if any(word in message_lower for word in ['career', 'skill', 'learn', 'beginner', 'advanced', 'help']): if challenges: sample_challenge = challenges[0] return f"""I'm your intelligent Topcoder assistant with REAL MCP integration! π
I currently have live access to {context_data.get('total_challenges_available', '4,596+')} real challenges. For example, right now there's: footer_text = self.FOOTER_TEXT if tool_result: footer_text += f" β’ {len(str(tool_result))} chars of live context" llm_response += f"\n\n*{footer_text}*" print("β
LLM response generated successfully.") return llm_response
π― **"{sample_challenge['title']}"** π° Prize: **{sample_challenge['prize']}** π οΈ Technologies: {', '.join(sample_challenge['technologies'][:3])}π Difficulty: {sample_challenge['difficulty']}
I can help you with:π― Find challenges matching your specific skillsπ° Compare real prize amounts and competition levels π Analyze difficulty levels and technology requirementsπ Career guidance based on market demand
Try asking me about specific technologies like "Python challenges" or "React opportunities"!
*Powered by live MCP connection to Topcoder's challenge database*""" # Default intelligent response with real data if challenges: return f"""Hi! I'm your intelligent Topcoder assistant! π€
I have REAL MCP integration with live access to **{context_data.get('total_challenges_available', '4,596+')} challenges** from Topcoder's database.
**Currently active challenges include:**β’ **{challenges[0]['title']}** ({challenges[0]['prize']})β’ **{challenges[1]['title']}** ({challenges[1]['prize']}) β’ **{challenges[2]['title']}** ({challenges[2]['prize']})
Ask me about:π― Specific technologies (Python, React, blockchain, etc.)π° Prize ranges and earning potentialπ Difficulty levels and skill requirementsπ Career advice and skill development
*All responses powered by real-time Topcoder MCP data!*""" return "I'm your intelligent Topcoder assistant with real MCP data access! Ask me about challenges, skills, or career advice and I'll help you using live data from 4,596+ real challenges! π" except Exception as e: print(f"Chat error: {e}") raise Exception(f"β **Chat Error**: {str(e)}")
# FIXED: Properly placed standalone functions with correct signaturesasync def chat_with_enhanced_llm_agent(message: str, history: List[Tuple[str, str]], mcp_engine) -> Tuple[List[Tuple[str, str]], str]:17 unmodified lines return history, "" except Exception as e: error_response = f"I encountered an issue processing your request: {str(e)}. However, I can still help you with challenge recommendations using my real MCP data! Try asking about specific technologies or challenge types." error_response = f"I encountered an issue processing your request: {str(e)}." history.append((message, error_response)) return history, ""
134 unmodified lines """
async def get_ultimate_recommendations_async( skills_input: str, experience_level: str, time_available: str, interests: str, experience_level: str, time_available: str, interests: str, status: str, prize_min: int, prize_max: int, challenge_type: str, track: str, sort_by: str, sort_order: str) -> Tuple[str, str]: start_time = time.time() try: skills = [skill.strip() for skill in skills_input.split(',') if skill.strip()] # FIXED: Removed skills_input parameter, only using interests interest_list = [interest.strip() for interest in interests.split(',') if interest.strip()] user_profile = UserProfile( skills=skills, experience_level=experience_level, time_available=time_available, interests=[interests] if interests else [] interests=interest_list ) # Pass all new filter params to get_personalized_recommendations recommendations_data = await intelligence_engine.get_personalized_recommendations(30 unmodified lines <div style='background:linear-gradient(135deg,#fdcb6e,#e17055);color:white;padding:25px;border-radius:12px;text-align:center;box-shadow:0 8px 25px rgba(253,203,110,0.3);'> <div style='font-size:3em;margin-bottom:15px;'>π</div> <div style='font-size:1.3em;font-weight:600;margin-bottom:10px;'>No perfect matches found</div> <div style='opacity:0.9;font-size:1em;'>Try adjusting your skills, experience level, or interests for better results</div> <div style='opacity:0.9;font-size:1em;'>Try adjusting your interests, experience level, or filters for better results</div> </div> """ # Generate insights panel7 unmodified lines error_msg = f""" <div style='background:linear-gradient(135deg,#e17055,#d63031);color:white;padding:25px;border-radius:12px;text-align:center;box-shadow:0 8px 25px rgba(225,112,85,0.3);'> <div style='font-size:3em;margin-bottom:15px;'>β </div> <div style='font-size:1.3em;font-weight:600;margin-bottom:10px;'>Processing Error</div> <div style='font-size:1.3em;font-weight:600;margin-bottom:10px;'>No recommendations found</div> <div style='opacity:0.9;font-size:0.9em;'>{str(e)}</div> <div style='opacity:0.8;font-size:0.85em;margin-top:10px;'>Please try again or contact support</div> <div style='opacity:0.8;font-size:0.85em;margin-top:10px;'>If problem persists, contact support.</div> </div> """ print(f"β Error processing ULTIMATE request: {str(e)}") return error_msg, ""
def get_ultimate_recommendations_sync( skills_input: str, experience_level: str, time_available: str, interests: str, experience_level: str, time_available: str, interests: str, status: str, prize_min: int, prize_max: int, challenge_type: str, track: str, sort_by: str, sort_order: str) -> Tuple[str, str]: return asyncio.run(get_ultimate_recommendations_async( skills_input, experience_level, time_available, interests, experience_level, time_available, interests, status, prize_min, prize_max, challenge_type, track, sort_by, sort_order ))12 unmodified lines # Test 1: MCP Connection Test results.append("π Test 1: Real MCP Connection Status") start = time.time() mcp_status = "β
CONNECTED" if intelligence_engine.is_connected else "β οΈ FALLBACK MODE" mcp_status = "β
CONNECTED" if intelligence_engine.is_connected else "β οΈ NOT CONNECTED" session_status = f"Session: {intelligence_engine.session_id[:8]}..." if intelligence_engine.session_id else "No session" test1_time = round(time.time() - start, 3) results.append(f" {mcp_status} ({test1_time}s)")8 unmodified lines # Create async test async def test_recommendations(): test_profile = UserProfile( skills=['Python', 'React', 'AWS'], experience_level='Intermediate', time_available='4-8 hours', interests=['web development', 'cloud computing'] interests=['python', 'react', 'cloud'] ) return await intelligence_engine.get_personalized_recommendations(test_profile, 'python react cloud') return await intelligence_engine.get_personalized_recommendations(test_profile, 'python') try: # Run async test24 unmodified lines results.append(f" π€ LLM Integration: Available") results.append(f" π§ Enhanced Chat: Enabled") else: results.append(f" π€ LLM Integration: Fallback mode") results.append(f" π§ Enhanced Chat: Basic responses") results.append(f" π€ LLM Integration: Not Available") results.append(f" π§ Enhanced Chat: Not Available") results.append("") # Summary30 unmodified lines # Test basic recommendation speed async def quick_test(): test_profile = UserProfile( skills=['Python', 'React'], experience_level='Intermediate', time_available='4-8 hours', interests=['web development'] interests=['web development', 'Python', 'React'] ) return await intelligence_engine.get_personalized_recommendations(test_profile) 34 unmodified lines results.append("π― Features: Real-time challenge data") results.append("β‘ Performance: Sub-second response times") else: results.append("β οΈ Status: FALLBACK MODE") results.append("π Using: Enhanced premium dataset") results.append("π― Features: Advanced algorithms active") results.append("π‘ Note: Still provides excellent recommendations") results.append("β οΈ Status: NOT CONNECTED") results.append("π Using: No data available") results.append("π― Features: MCP connection required") results.append("π‘ Note: Please check your connection") # Check OpenAI API Key has_openai = bool(os.getenv("OPENAI_API_KEY"))42 unmodified lines ### **π₯ REAL MCP Integration + Advanced AI Intelligence + OpenAI LLM** Experience the **world's most advanced** Topcoder challenge discovery system! Powered by **live Model Context Protocol integration** with access to **4,596+ real challenges**, **OpenAI GPT-4 intelligence**, and sophisticated AI algorithms that deliver **personalized recommendations** tailored to your exact skills and career goals. Experience the **world's most advanced** Topcoder challenge discovery system! Powered by **live Model Context Protocol integration** with access to **4,596+ real challenges**, **OpenAI GPT-4 intelligence**, and sophisticated AI algorithms that deliver **personalized recommendations** tailored to your exact skills, interests and career goals. **π― What Makes This ULTIMATE:** - **π₯ Real MCP Data**: Live connection to Topcoder's official MCP server14 unmodified lines with gr.Row(): with gr.Column(scale=1): gr.Markdown("**π€ Tell the AI about yourself and filter challenges:**")
skills_input = gr.Textbox( label="π οΈ Your Skills & Technologies", placeholder="Python, React, JavaScript, AWS, Docker, Blockchain, UI/UX...", lines=3, value="Python, JavaScript, React" ) experience_level = gr.Dropdown( choices=["Beginner", "Intermediate", "Advanced"], label="π Experience Level",63 unmodified lines ultimate_recommend_btn.click( get_ultimate_recommendations_sync, inputs=[ skills_input, experience_level, time_available, interests,33 unmodified lines with gr.Row(): enhanced_chat_input = gr.Textbox( placeholder="Ask me about challenges, skills, career advice, or anything else!", placeholder="Ask me about challenges, skills, interests, career advice, or anything else!", container=False, scale=4, show_label=False82 unmodified lines - **API Key Status**: {"β
Configured via HF Secrets" if os.getenv("OPENAI_API_KEY") else "β οΈ Set OPENAI_API_KEY in HF Secrets for full features"} #### π§ **Enhanced AI Intelligence Engine v4.0** - **Multi-Factor Scoring**: 40% skill match + 30% experience + 20% interest + 10% market factors - **Multi-Factor Scoring**: 40% interest match + 30% experience + 20% query + 10% market factors - **Natural Language Processing**: Understands your goals and matches with relevant opportunities - **Enhanced Market Intelligence**: Real-time insights on trending technologies and career paths - **Success Prediction**: Enhanced algorithms calculate your probability of success16 unmodified lines ```python # SECURE: Hugging Face Secrets integration openai_api_key = os.getenv("OPENAI_API_KEY", "") endpoint = "https://api.openai.com/v1/chat/completions" endpoint = "https://api.openai.com/v1/responses" model = "gpt-4o-mini" # Fast and cost-effective context = "Real MCP challenge data + conversation history" ```app.py+219-342