Update main.py
Browse files
main.py
CHANGED
|
@@ -812,50 +812,66 @@ def _scrape_dashboard_info_brscraper():
|
|
| 812 |
logging.error(f"Error scraping dashboard info with BRScraper: {e}")
|
| 813 |
return dashboard_data
|
| 814 |
|
|
|
|
| 815 |
PERP_KEY = os.getenv("PERPLEXITY_API_KEY")
|
| 816 |
PERP_URL = "https://api.perplexity.ai/chat/completions"
|
| 817 |
|
| 818 |
NBA_ANALYST_SYSTEM_PROMPT = (
|
| 819 |
"You are a sharp, insightful NBA analyst AI with the tone and knowledge of a seasoned sports commentator. "
|
| 820 |
-
"Your expertise spans the entire history of
|
| 821 |
"advanced stats, trades, rivalries, and playoff lore. Speak with authority, depth, and the occasional flair of broadcast commentary. "
|
| 822 |
-
"Your job is to help users explore
|
| 823 |
"player tendencies, team dynamics, historical context, and front-office strategy. You may reference key metrics like PER, TS%, "
|
| 824 |
"on-off splits, or synergy data when relevant. You provide takes that are well-reasoned, never vague, and always rooted in basketball-specific insight. "
|
|
|
|
| 825 |
"Do not respond to questions outside the world of basketball. If asked, steer the conversation back to the NBA with finesse, "
|
| 826 |
"perhaps by connecting a topic metaphorically to hoops. Your personality is that of a knowledgeable but approachable analyst—"
|
| 827 |
"a cross between a basketball scout, play-by-play commentator, and sportswriter. You love the game, and it shows."
|
| 828 |
)
|
| 829 |
|
| 830 |
-
def ask_perp(prompt, system=NBA_ANALYST_SYSTEM_PROMPT
|
| 831 |
if not PERP_KEY:
|
| 832 |
logging.error("PERPLEXITY_API_KEY env var not set.")
|
| 833 |
return "Perplexity API key is not configured."
|
| 834 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 835 |
payload = {
|
| 836 |
-
"model":"sonar-pro",
|
| 837 |
-
"messages":[
|
| 838 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 839 |
}
|
|
|
|
| 840 |
try:
|
| 841 |
-
|
| 842 |
-
|
| 843 |
-
return
|
| 844 |
except requests.exceptions.RequestException as e:
|
| 845 |
error_message = f"Error communicating with Perplexity API: {e}"
|
| 846 |
if hasattr(e, 'response') and e.response is not None:
|
| 847 |
try:
|
| 848 |
error_detail = e.response.json().get("error", {}).get("message", e.response.text)
|
| 849 |
-
error_message = f"Perplexity API error: {
|
| 850 |
except ValueError:
|
| 851 |
-
error_message = f"Perplexity API error: {
|
| 852 |
logging.error(f"Perplexity API request failed: {error_message}")
|
| 853 |
return f"Error from AI: {error_message}"
|
| 854 |
except Exception as e:
|
| 855 |
logging.error(f"An unexpected error occurred with Perplexity API: {e}")
|
| 856 |
return f"An unexpected error occurred with AI: {str(e)}"
|
| 857 |
|
| 858 |
-
|
| 859 |
@app.route('/api/nba/players', methods=['GET'])
|
| 860 |
@cross_origin()
|
| 861 |
def get_players():
|
|
|
|
| 812 |
logging.error(f"Error scraping dashboard info with BRScraper: {e}")
|
| 813 |
return dashboard_data
|
| 814 |
|
| 815 |
+
|
| 816 |
PERP_KEY = os.getenv("PERPLEXITY_API_KEY")
|
| 817 |
PERP_URL = "https://api.perplexity.ai/chat/completions"
|
| 818 |
|
| 819 |
NBA_ANALYST_SYSTEM_PROMPT = (
|
| 820 |
"You are a sharp, insightful NBA analyst AI with the tone and knowledge of a seasoned sports commentator. "
|
| 821 |
+
"Your expertise spans the entire history of basketball—from hardwood legends to rising stars, tactical evolutions, "
|
| 822 |
"advanced stats, trades, rivalries, and playoff lore. Speak with authority, depth, and the occasional flair of broadcast commentary. "
|
| 823 |
+
"Your job is to help users explore basketball with analytical rigor and passion. You draw on statistics, game film analysis, "
|
| 824 |
"player tendencies, team dynamics, historical context, and front-office strategy. You may reference key metrics like PER, TS%, "
|
| 825 |
"on-off splits, or synergy data when relevant. You provide takes that are well-reasoned, never vague, and always rooted in basketball-specific insight. "
|
| 826 |
+
"CRITICAL: Stay strictly within basketball. When discussing 'rookie performances' or any basketball topic, ONLY reference NBA/basketball players and stats - never NFL or other sports. "
|
| 827 |
"Do not respond to questions outside the world of basketball. If asked, steer the conversation back to the NBA with finesse, "
|
| 828 |
"perhaps by connecting a topic metaphorically to hoops. Your personality is that of a knowledgeable but approachable analyst—"
|
| 829 |
"a cross between a basketball scout, play-by-play commentator, and sportswriter. You love the game, and it shows."
|
| 830 |
)
|
| 831 |
|
| 832 |
+
def ask_perp(prompt, system=NBA_ANALYST_SYSTEM_PROMPT, max_tokens=1000, temp=0.2):
|
| 833 |
if not PERP_KEY:
|
| 834 |
logging.error("PERPLEXITY_API_KEY env var not set.")
|
| 835 |
return "Perplexity API key is not configured."
|
| 836 |
+
|
| 837 |
+
headers = {
|
| 838 |
+
'Authorization': f'Bearer {PERP_KEY}',
|
| 839 |
+
'Content-Type': 'application/json'
|
| 840 |
+
}
|
| 841 |
+
|
| 842 |
payload = {
|
| 843 |
+
"model": "sonar-pro",
|
| 844 |
+
"messages": [
|
| 845 |
+
{"role": "system", "content": system},
|
| 846 |
+
{"role": "user", "content": f"BASKETBALL ONLY: {prompt}"}
|
| 847 |
+
],
|
| 848 |
+
"max_tokens": max_tokens,
|
| 849 |
+
"temperature": temp,
|
| 850 |
+
"web_search_options": {
|
| 851 |
+
"search_context_size": "high",
|
| 852 |
+
"search_domain_filter": ["nba.com", "espn.com", "basketball-reference.com"]
|
| 853 |
+
},
|
| 854 |
+
"emit_sources": True
|
| 855 |
}
|
| 856 |
+
|
| 857 |
try:
|
| 858 |
+
response = requests.post(PERP_URL, json=payload, headers=headers, timeout=45)
|
| 859 |
+
response.raise_for_status()
|
| 860 |
+
return response.json().get("choices", [])[0].get("message", {}).get("content", "")
|
| 861 |
except requests.exceptions.RequestException as e:
|
| 862 |
error_message = f"Error communicating with Perplexity API: {e}"
|
| 863 |
if hasattr(e, 'response') and e.response is not None:
|
| 864 |
try:
|
| 865 |
error_detail = e.response.json().get("error", {}).get("message", e.response.text)
|
| 866 |
+
error_message = f"Perplexity API error: {e.response.status_code} - {e.response.reason}"
|
| 867 |
except ValueError:
|
| 868 |
+
error_message = f"Perplexity API error: {e.response.status_code} - {e.response.reason}"
|
| 869 |
logging.error(f"Perplexity API request failed: {error_message}")
|
| 870 |
return f"Error from AI: {error_message}"
|
| 871 |
except Exception as e:
|
| 872 |
logging.error(f"An unexpected error occurred with Perplexity API: {e}")
|
| 873 |
return f"An unexpected error occurred with AI: {str(e)}"
|
| 874 |
|
|
|
|
| 875 |
@app.route('/api/nba/players', methods=['GET'])
|
| 876 |
@cross_origin()
|
| 877 |
def get_players():
|