Spaces:
Sleeping
Sleeping
File size: 14,580 Bytes
7644eac |
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
"""OpenAI-powered resource search helper.
This module queries the OpenAI Chat Completion endpoint to retrieve
real, high-quality learning resources (videos, articles, docs) that a user
can click to continue learning. It returns a simple list of dictionaries so
upstream code can map them into `ResourceItem` Pydantic objects.
If the `OPENAI_API_KEY` environment variable is missing, or the API call
fails, we fall back to a single placeholder so the rest of the app
continues to work.
"""
from __future__ import annotations
import json
import logging
import os
import re
import time
from typing import Dict, List
from openai import OpenAI
from langsmith import traceable as langsmith_traceable
from src.utils.observability import get_observability_manager
from src.utils.config import (
PERPLEXITY_PROMPT_COST_PER_1K,
PERPLEXITY_COMPLETION_COST_PER_1K,
)
# Initialize OpenAI client
client = None
def _stub_resources() -> List[Dict[str, str]]:
"""Return a static placeholder when real search is unavailable."""
return [
{
"type": "article",
"url": "https://example.com/placeholder-resource",
"description": "Add your OpenAI API key to see real learning resources.",
}
]
def _extract_keywords(query: str) -> List[str]:
"""Collect meaningful keywords from the query for simple relevance filtering."""
tokens = re.findall(r"[\w']+", query.lower())
stopwords = {
"the",
"with",
"your",
"from",
"this",
"that",
"about",
"topic",
"learn",
"learning",
"skill",
"skills",
"path",
"guide",
"study",
"course",
"for",
"into",
"using",
"based",
"mastery",
"introduction",
"advanced",
"beginner",
"intermediate",
}
# Extract keywords, prioritizing the first word (usually the main topic)
keywords = [tok for tok in tokens if len(tok) > 3 and tok not in stopwords]
# If query has a colon (e.g., "Mandarin: Pronunciation"), extract both parts
if ":" in query:
parts = query.split(":")
main_topic = parts[0].strip().lower()
# Main topic is critical - add all its words
main_tokens = re.findall(r"[\w']+", main_topic)
keywords.extend([tok for tok in main_tokens if len(tok) > 3 and tok not in stopwords])
return list(set(keywords)) # Remove duplicates
def _filter_by_keywords(resources: List[Dict[str, str]], query: str) -> List[Dict[str, str]]:
"""Filter out resources that do not mention any significant query keywords."""
keywords = _extract_keywords(query)
if not keywords:
return resources
# Extract main topic (first word or word before colon)
main_topic = query.split(":")[0].strip().lower() if ":" in query else query.split()[0].lower()
filtered: List[Dict[str, str]] = []
for item in resources:
haystack = " ".join(
[item.get("url", ""), item.get("description", ""), item.get("type", "")]
).lower()
# STRICT: Main topic MUST be present
if main_topic not in haystack:
logging.info(f"β οΈ Filtered out resource (missing main topic '{main_topic}'): {item.get('description', '')[:50]}")
continue
# Also check if any other keyword matches
if any(keyword in haystack for keyword in keywords):
filtered.append(item)
# If everything was filtered out, keep the originals to avoid empty lists
if not filtered:
logging.warning(f"All resources filtered out for query '{query}'. Keeping originals.")
return filtered or resources
@langsmith_traceable(name="perplexity_resource_search")
def search_resources(query: str, k: int = 3, timeout: int = 45, trusted_sources: Dict[str, List[str]] = None) -> List[Dict[str, str]]:
"""Search for learning resources using Perplexity (with OpenAI fallback).
Each dict has keys: `type`, `url`, `description`.
Args:
query: The search query/milestone title
k: Number of resources to return
timeout: API timeout in seconds
trusted_sources: Dict with 'youtube' and 'websites' lists of trusted sources
"""
# Build source-specific instructions
source_instruction = ""
if trusted_sources:
youtube_channels = trusted_sources.get('youtube', [])
websites = trusted_sources.get('websites', [])
if youtube_channels or websites:
source_instruction = "\n\nπ― CRITICAL - SEARCH ONLY IN THESE CURATED SOURCES:\n"
if youtube_channels:
source_instruction += f"β
APPROVED YouTube Channels (search ONLY these): {', '.join(youtube_channels)}\n"
source_instruction += " - Go to each channel's videos page\n"
source_instruction += " - Find videos that match the query topic\n"
source_instruction += " - Return DIRECT video watch URLs (youtube.com/watch?v=...)\n"
if websites:
source_instruction += f"β
APPROVED Websites (search ONLY these): {', '.join(websites)}\n"
source_instruction += " - Search within these domains for relevant content\n"
source_instruction += " - Return direct article/tutorial URLs, not homepages\n"
source_instruction += "\nβ FORBIDDEN: Do NOT search or suggest content from ANY other sources\n"
source_instruction += "β FORBIDDEN: Do NOT make up or hallucinate URLs\n"
source_instruction += "β
REQUIRED: Every URL must be from the approved list above\n"
source_instruction += "β
REQUIRED: Every URL must be a real, existing page you found by searching\n"
prompt = (
f"Search the web and find {k} real, working FREE learning resources SPECIFICALLY for: '{query}'. "
"\n"
"π― CRITICAL REQUIREMENTS:\n"
"1. PRIORITIZE FREE CONTENT: YouTube videos, free tutorials, open documentation\n"
"2. AVOID PAID COURSES: Do NOT suggest Udemy, Coursera, or any paid platforms unless they have free content\n"
"3. DIRECT VIDEO LINKS ONLY: For YouTube, provide DIRECT VIDEO LINKS (youtube.com/watch?v=...), NOT:\n"
" - Channel homepages\n"
" - Playlist pages\n"
" - Search result pages\n"
"4. SPECIFIC ARTICLES: For websites, link to the SPECIFIC PAGE/ARTICLE, not homepages\n"
"5. EXACT TOPIC MATCH: Every resource MUST be directly about the EXACT topic in the query\n"
"6. VERIFY RELEVANCE: The resource title/description must explicitly mention the main topic\n"
"7. PREFER COMPREHENSIVE CONTENT: Look for 'full course', 'complete tutorial', 'crash course'\n"
f"{source_instruction}"
"\n"
"πΊ YOUTUBE PRIORITY: At least 60% of resources should be YouTube videos with direct watch links\n"
"\n"
"Return ONLY valid JSON array (no markdown, no code blocks) with format: "
'[{"type": "video", "url": "https://youtube.com/watch?v=...", "description": "Full Course Title by Channel Name"}, ...]'
"\n"
"β
VALIDATION: Each URL must be:\n"
"- A real, working link that exists right now\n"
"- Directly clickable and accessible\n"
)
obs_manager = get_observability_manager()
# Try Perplexity first (real-time web search)
perplexity_key = os.getenv("PERPLEXITY_API_KEY")
if perplexity_key:
try:
logging.info("Searching for resources using Perplexity (web search)...")
client = OpenAI(
api_key=perplexity_key,
base_url="https://api.perplexity.ai"
)
start_time = time.time()
completion = client.chat.completions.create(
model="sonar-pro", # Online search model
messages=[
{
"role": "system",
"content": "You are a helpful assistant that searches the web for real learning resources. Always return valid JSON with actual, working URLs.",
},
{"role": "user", "content": prompt},
],
temperature=0.2,
max_tokens=500,
timeout=timeout,
)
latency_ms = (time.time() - start_time) * 1000
content = completion.choices[0].message.content.strip()
# Remove markdown code blocks if present
if content.startswith("```"):
content = content.split("```")[1]
if content.startswith("json"):
content = content[4:]
content = content.strip()
resources: List[Dict[str, str]] = json.loads(content)
cleaned: List[Dict[str, str]] = []
for item in resources[:k]:
cleaned.append({
"type": item.get("type", "article"),
"url": item.get("url", ""),
"description": item.get("description", ""),
})
if cleaned:
cleaned = _filter_by_keywords(cleaned, query)
logging.info(f"β
Found {len(cleaned)} resources via Perplexity")
# Extract usage (token counts) if provided
prompt_tokens = 0
completion_tokens = 0
total_tokens = 0
usage = getattr(completion, "usage", None)
if usage:
prompt_tokens = getattr(usage, "prompt_tokens", 0) or getattr(usage, "input_tokens", 0)
completion_tokens = getattr(usage, "completion_tokens", 0) or getattr(usage, "output_tokens", 0)
total_tokens = getattr(usage, "total_tokens", 0) or (prompt_tokens + completion_tokens)
else:
# Fallback: some clients expose model_dump / dict style
usage_payload = None
if hasattr(completion, "model_dump") and callable(completion.model_dump):
usage_payload = completion.model_dump().get("usage")
elif isinstance(completion, dict):
usage_payload = completion.get("usage")
if usage_payload:
prompt_tokens = usage_payload.get("prompt_tokens", usage_payload.get("input_tokens", 0))
completion_tokens = usage_payload.get("completion_tokens", usage_payload.get("output_tokens", 0))
total_tokens = usage_payload.get("total_tokens", prompt_tokens + completion_tokens)
# Estimate cost using configured pricing (per 1K tokens)
perplexity_cost = 0.0
if PERPLEXITY_PROMPT_COST_PER_1K > 0 or PERPLEXITY_COMPLETION_COST_PER_1K > 0:
perplexity_cost = (
(prompt_tokens / 1000.0) * PERPLEXITY_PROMPT_COST_PER_1K
+ (completion_tokens / 1000.0) * PERPLEXITY_COMPLETION_COST_PER_1K
)
# Log to observability platforms
obs_manager.log_llm_call(
prompt=prompt,
response=content,
model="perplexity-sonar-pro",
metadata={
"provider": "perplexity",
"query": query,
"trusted_sources": trusted_sources or {},
},
latency_ms=latency_ms,
token_count=total_tokens or None,
cost=perplexity_cost or None,
)
obs_manager.log_metric(
"perplexity_latency_ms",
float(latency_ms),
{
"query": query,
"result_count": len(cleaned),
},
)
if prompt_tokens:
obs_manager.log_metric(
"perplexity_prompt_tokens",
float(prompt_tokens),
{"query": query},
)
if completion_tokens:
obs_manager.log_metric(
"perplexity_completion_tokens",
float(completion_tokens),
{"query": query},
)
if perplexity_cost:
obs_manager.log_metric(
"perplexity_cost_usd",
perplexity_cost,
{"query": query},
)
return cleaned
except Exception as exc:
logging.warning(f"Perplexity resource search failed: {exc}. Falling back to OpenAI...")
# Fallback to OpenAI
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
logging.info("OPENAI_API_KEY not set; returning stub resources")
return _stub_resources()
model = os.getenv("DEFAULT_MODEL", "gpt-4o-mini")
try:
client = OpenAI(api_key=api_key)
completion = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": "You are a helpful research assistant that provides real, working URLs to learning resources."},
{"role": "user", "content": prompt},
],
temperature=0.2,
max_tokens=400,
timeout=timeout,
)
content = completion.choices[0].message.content.strip()
# Remove markdown code blocks if present
if content.startswith("```"):
content = content.split("```")[1]
if content.startswith("json"):
content = content[4:]
content = content.strip()
resources: List[Dict[str, str]] = json.loads(content)
cleaned: List[Dict[str, str]] = []
for item in resources[:k]:
cleaned.append({
"type": item.get("type", "article"),
"url": item.get("url", ""),
"description": item.get("description", ""),
})
cleaned = _filter_by_keywords(cleaned, query)
return cleaned or _stub_resources()
except Exception as exc:
logging.warning("OpenAI resource search failed: %s", exc)
return _stub_resources()
|