Spaces:
Running
refactor: remove GitHub Models + Anthropic; unify LLM provider chain
Browse filesDrop GitHub Models (GITHUB_TOKEN) and Anthropic (ANTHROPIC_API_KEY) entirely —
neither is in active use. All LLM calls now use a single OpenRouter → Groq →
HuggingFace waterfall, consistent across ai_forecast.py and news_sentiment.py
(previously news_sentiment only had GitHub + OpenRouter).
- Delete model_picker.py (GitHub Models-only utility, not imported anywhere)
- ai_forecast.py: remove _try_github(), _GITHUB_DISABLED_UNTIL, update provider
order to [openrouter, groq, huggingface]
- news_sentiment.py: remove GitHub block; add Groq + HF providers to match
ai_forecast.py's 3-provider chain
- app.py: remove github_token from provider status dict
- research/loop_backtest.py: replace GitHub readiness check with Groq + HF
- research/compare_predictors.py: update CLI defaults to openrouter/groq
- .env.example: remove GITHUB_TOKEN and ANTHROPIC_API_KEY entries
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- .env.example +3 -12
- ai_forecast.py +9 -56
- app.py +0 -1
- model_picker.py +0 -389
- news_sentiment.py +73 -33
- research/compare_predictors.py +6 -6
- research/loop_backtest.py +7 -4
|
@@ -1,13 +1,7 @@
|
|
| 1 |
# ── AI Backends ───────────────────────────────────────────────────────────────
|
| 2 |
-
# GitHub Personal Access Token (with 'models:read' scope)
|
| 3 |
-
# Used as PRIMARY AI backend (GPT-4o via GitHub Models)
|
| 4 |
-
# Create one at: https://github.com/settings/tokens → "Generate new token (classic)"
|
| 5 |
-
# Required scopes: "models:read" (under the Models section)
|
| 6 |
-
GITHUB_TOKEN=ghp_your_github_token_here
|
| 7 |
-
|
| 8 |
# Hugging Face token — two roles:
|
| 9 |
# 1. Database sync: export_env_secrets.py pushes secrets to V1deh/PaperTrade Space
|
| 10 |
-
# 2. LLM Inference API:
|
| 11 |
# Get your token at: https://huggingface.co/settings/tokens
|
| 12 |
HF_TOKEN=hf_your_huggingface_token_here
|
| 13 |
|
|
@@ -18,11 +12,8 @@ HF_TOKEN=hf_your_huggingface_token_here
|
|
| 18 |
# HF_INFERENCE_MODEL=meta-llama/Llama-3.1-8B-Instruct
|
| 19 |
# HF_INFERENCE_FALLBACK_MODELS=mistralai/Mistral-7B-Instruct-v0.3,google/gemma-2-9b-it,HuggingFaceH4/zephyr-7b-beta
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
# ── OpenRouter (optional — backtest-only fallback provider) ────────────────────
|
| 25 |
-
# Used in research/backtest.py for LLM prompt accuracy testing
|
| 26 |
# Get your free key at: https://openrouter.ai/keys
|
| 27 |
OPENROUTER_API_KEY=sk-or-v1-your_openrouter_key_here
|
| 28 |
# Verified working free models as of 2026-06-29
|
|
|
|
| 1 |
# ── AI Backends ───────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
# Hugging Face token — two roles:
|
| 3 |
# 1. Database sync: export_env_secrets.py pushes secrets to V1deh/PaperTrade Space
|
| 4 |
+
# 2. LLM Inference API: 3rd fallback AI provider (free serverless, non-Chinese models)
|
| 5 |
# Get your token at: https://huggingface.co/settings/tokens
|
| 6 |
HF_TOKEN=hf_your_huggingface_token_here
|
| 7 |
|
|
|
|
| 12 |
# HF_INFERENCE_MODEL=meta-llama/Llama-3.1-8B-Instruct
|
| 13 |
# HF_INFERENCE_FALLBACK_MODELS=mistralai/Mistral-7B-Instruct-v0.3,google/gemma-2-9b-it,HuggingFaceH4/zephyr-7b-beta
|
| 14 |
|
| 15 |
+
# ── OpenRouter (PRIMARY AI provider) ──────────────────────────────────────────
|
| 16 |
+
# Used for all LLM calls: predictions, news sentiment, backtest
|
|
|
|
|
|
|
|
|
|
| 17 |
# Get your free key at: https://openrouter.ai/keys
|
| 18 |
OPENROUTER_API_KEY=sk-or-v1-your_openrouter_key_here
|
| 19 |
# Verified working free models as of 2026-06-29
|
|
@@ -40,8 +40,8 @@ logger = logging.getLogger(__name__)
|
|
| 40 |
|
| 41 |
def _resolve_source_metadata() -> tuple[str, str, str]:
|
| 42 |
"""Resolve predictor source metadata from environment overrides."""
|
| 43 |
-
provider = (os.getenv("AI_FORECAST_SOURCE_PROVIDER") or "
|
| 44 |
-
model = (os.getenv("AI_FORECAST_SOURCE_MODEL") or "gpt-
|
| 45 |
source_prefix = (os.getenv("AI_FORECAST_SOURCE_PREFIX") or "ai_forecast").strip() or "ai_forecast"
|
| 46 |
return f"{source_prefix}:{model}", provider, model
|
| 47 |
|
|
@@ -268,8 +268,7 @@ def _ensure_non_degenerate_range(
|
|
| 268 |
# LLM INFRASTRUCTURE
|
| 269 |
# ============================================================================
|
| 270 |
|
| 271 |
-
# Rate-limit circuit breakers —
|
| 272 |
-
_GITHUB_DISABLED_UNTIL: float = 0.0 # set on GitHub Models 429; does NOT block OpenRouter
|
| 273 |
_OPENROUTER_DISABLED_UNTIL: float = 0.0 # set when all OpenRouter free models 429 simultaneously
|
| 274 |
_GROQ_DISABLED_UNTIL: float = 0.0 # set on Groq 429; does NOT block the global cooldown
|
| 275 |
_HF_DISABLED_UNTIL: float = 0.0 # set on HF Inference API 429; does NOT block global cooldown
|
|
@@ -285,7 +284,7 @@ _LLM_SEMAPHORE = threading.Semaphore(4) # one slot per provider (was 3)
|
|
| 285 |
# ── Provider routing state (guarded by _LLM_LOCK) ──────────────────────────
|
| 286 |
# Default waterfall order — also used as key set for _PROVIDER_LAST_SUCCESS.
|
| 287 |
# Must match the literal provider strings returned by each _try_* function.
|
| 288 |
-
_PROVIDER_ORDER: list = ["
|
| 289 |
|
| 290 |
# Wall-clock timestamp of last successful call per provider.
|
| 291 |
# Higher = more recent = tried first within non-cooldown group.
|
|
@@ -307,64 +306,21 @@ def _make_chat_call(
|
|
| 307 |
) -> tuple[str, str, str]:
|
| 308 |
"""
|
| 309 |
Call an LLM with the given messages. Tries providers in order:
|
| 310 |
-
1.
|
| 311 |
-
2.
|
| 312 |
-
3.
|
| 313 |
-
4. Hugging Face Inference API (HF_TOKEN)
|
| 314 |
|
| 315 |
Returns (content, provider, model). Raises RuntimeError if all fail.
|
| 316 |
|
| 317 |
The semaphore is released BEFORE any inter-pass sleep so other callers are
|
| 318 |
not blocked while this call waits for a rate-limit cooldown to expire.
|
| 319 |
"""
|
| 320 |
-
global
|
| 321 |
|
| 322 |
with _LLM_LOCK:
|
| 323 |
if time.time() < _LLM_DISABLED_UNTIL:
|
| 324 |
raise RuntimeError("LLM providers on cooldown — rate limit hit recently")
|
| 325 |
|
| 326 |
-
def _try_github() -> tuple[str, str, str] | None:
|
| 327 |
-
global _GITHUB_DISABLED_UNTIL # must be global — inner fn both reads and writes it
|
| 328 |
-
token = os.environ.get("GITHUB_TOKEN", "").strip()
|
| 329 |
-
if not token:
|
| 330 |
-
logger.debug("GitHub Models skipped — GITHUB_TOKEN not set")
|
| 331 |
-
return None
|
| 332 |
-
with _LLM_LOCK:
|
| 333 |
-
if time.time() < _GITHUB_DISABLED_UNTIL:
|
| 334 |
-
return None # GitHub on cooldown; fall through to OpenRouter
|
| 335 |
-
model = os.environ.get("AI_FORECAST_GITHUB_MODEL", "gpt-4o-mini").strip() or "gpt-4o-mini"
|
| 336 |
-
try:
|
| 337 |
-
resp = requests.post(
|
| 338 |
-
"https://models.inference.ai.azure.com/chat/completions",
|
| 339 |
-
headers={"Authorization": f"Bearer {token}", "Content-Type": "application/json"},
|
| 340 |
-
json={"model": model, "messages": messages, "temperature": temperature, "max_tokens": max_tokens},
|
| 341 |
-
timeout=20,
|
| 342 |
-
)
|
| 343 |
-
if resp.status_code == 429:
|
| 344 |
-
# Always set cooldown and fall through to OpenRouter.
|
| 345 |
-
# fast_fail_on_rate_limit=True uses a short cooldown (5s) instead of 10min.
|
| 346 |
-
with _LLM_LOCK:
|
| 347 |
-
cooldown = 5 if fast_fail_on_rate_limit else _LLM_COOLDOWN_SECS
|
| 348 |
-
_GITHUB_DISABLED_UNTIL = time.time() + cooldown
|
| 349 |
-
logger.warning("GitHub Models rate-limited (429) — falling through to OpenRouter")
|
| 350 |
-
return None
|
| 351 |
-
if resp.status_code == 401:
|
| 352 |
-
logger.error("GitHub Models auth failed (401) — check GITHUB_TOKEN has models:read scope")
|
| 353 |
-
return None
|
| 354 |
-
if resp.status_code == 403:
|
| 355 |
-
logger.error("GitHub Models forbidden (403) — token may lack models:read scope or usage limit hit")
|
| 356 |
-
return None
|
| 357 |
-
if resp.status_code == 200:
|
| 358 |
-
content = (((resp.json().get("choices") or [{}])[0].get("message") or {}).get("content") or "").strip()
|
| 359 |
-
if content:
|
| 360 |
-
return content, "github", model
|
| 361 |
-
logger.warning("GitHub Models unexpected status %s — body: %s", resp.status_code, resp.text[:200])
|
| 362 |
-
except RuntimeError:
|
| 363 |
-
raise
|
| 364 |
-
except Exception as e:
|
| 365 |
-
logger.debug("GitHub Models call failed: %s", e)
|
| 366 |
-
return None
|
| 367 |
-
|
| 368 |
def _try_openrouter() -> tuple[str, str, str] | None:
|
| 369 |
global _OPENROUTER_DISABLED_UNTIL
|
| 370 |
api_key = os.environ.get("OPENROUTER_API_KEY", "").strip()
|
|
@@ -544,7 +500,6 @@ def _make_chat_call(
|
|
| 544 |
|
| 545 |
# Map provider name → closure (defined above, captures this call's messages/params).
|
| 546 |
_PROVIDER_FN_MAP: dict = {
|
| 547 |
-
"github": _try_github,
|
| 548 |
"openrouter": _try_openrouter,
|
| 549 |
"groq": _try_groq,
|
| 550 |
"huggingface": _try_huggingface,
|
|
@@ -563,7 +518,6 @@ def _make_chat_call(
|
|
| 563 |
with _LLM_LOCK:
|
| 564 |
_ls = dict(_PROVIDER_LAST_SUCCESS)
|
| 565 |
_disabled = {
|
| 566 |
-
"github": _GITHUB_DISABLED_UNTIL,
|
| 567 |
"openrouter": _OPENROUTER_DISABLED_UNTIL,
|
| 568 |
"groq": _GROQ_DISABLED_UNTIL,
|
| 569 |
"huggingface": _HF_DISABLED_UNTIL,
|
|
@@ -603,7 +557,6 @@ def _make_chat_call(
|
|
| 603 |
with _LLM_LOCK:
|
| 604 |
active_cooldowns = [
|
| 605 |
t for t in [
|
| 606 |
-
_GITHUB_DISABLED_UNTIL,
|
| 607 |
_OPENROUTER_DISABLED_UNTIL,
|
| 608 |
_GROQ_DISABLED_UNTIL,
|
| 609 |
_HF_DISABLED_UNTIL,
|
|
@@ -1249,7 +1202,7 @@ def get_ai_forecast(
|
|
| 1249 |
get_ai_forecast(ticker, company, tf_label, ml, nifty_ok, macro_ok, vix_level, news,
|
| 1250 |
current_price=..., indicators=..., ohlcv_df=..., ...)
|
| 1251 |
|
| 1252 |
-
Provider chain:
|
| 1253 |
Source label: "{provider}:{model}+debate+fund" | "+debate" | "" | "heuristic".
|
| 1254 |
"""
|
| 1255 |
# ── Parse arguments ────────────────────────────────────────────────────────
|
|
|
|
| 40 |
|
| 41 |
def _resolve_source_metadata() -> tuple[str, str, str]:
|
| 42 |
"""Resolve predictor source metadata from environment overrides."""
|
| 43 |
+
provider = (os.getenv("AI_FORECAST_SOURCE_PROVIDER") or "openrouter").strip() or "openrouter"
|
| 44 |
+
model = (os.getenv("AI_FORECAST_SOURCE_MODEL") or "openai/gpt-oss-120b:free").strip() or "openai/gpt-oss-120b:free"
|
| 45 |
source_prefix = (os.getenv("AI_FORECAST_SOURCE_PREFIX") or "ai_forecast").strip() or "ai_forecast"
|
| 46 |
return f"{source_prefix}:{model}", provider, model
|
| 47 |
|
|
|
|
| 268 |
# LLM INFRASTRUCTURE
|
| 269 |
# ============================================================================
|
| 270 |
|
| 271 |
+
# Rate-limit circuit breakers — per-provider and global (last-resort).
|
|
|
|
| 272 |
_OPENROUTER_DISABLED_UNTIL: float = 0.0 # set when all OpenRouter free models 429 simultaneously
|
| 273 |
_GROQ_DISABLED_UNTIL: float = 0.0 # set on Groq 429; does NOT block the global cooldown
|
| 274 |
_HF_DISABLED_UNTIL: float = 0.0 # set on HF Inference API 429; does NOT block global cooldown
|
|
|
|
| 284 |
# ── Provider routing state (guarded by _LLM_LOCK) ──────────────────────────
|
| 285 |
# Default waterfall order — also used as key set for _PROVIDER_LAST_SUCCESS.
|
| 286 |
# Must match the literal provider strings returned by each _try_* function.
|
| 287 |
+
_PROVIDER_ORDER: list = ["openrouter", "groq", "huggingface"]
|
| 288 |
|
| 289 |
# Wall-clock timestamp of last successful call per provider.
|
| 290 |
# Higher = more recent = tried first within non-cooldown group.
|
|
|
|
| 306 |
) -> tuple[str, str, str]:
|
| 307 |
"""
|
| 308 |
Call an LLM with the given messages. Tries providers in order:
|
| 309 |
+
1. OpenRouter (OPENROUTER_API_KEY)
|
| 310 |
+
2. Groq (GROQ_API_KEY)
|
| 311 |
+
3. Hugging Face Inference API (HF_TOKEN)
|
|
|
|
| 312 |
|
| 313 |
Returns (content, provider, model). Raises RuntimeError if all fail.
|
| 314 |
|
| 315 |
The semaphore is released BEFORE any inter-pass sleep so other callers are
|
| 316 |
not blocked while this call waits for a rate-limit cooldown to expire.
|
| 317 |
"""
|
| 318 |
+
global _OPENROUTER_DISABLED_UNTIL, _GROQ_DISABLED_UNTIL, _HF_DISABLED_UNTIL, _LLM_DISABLED_UNTIL
|
| 319 |
|
| 320 |
with _LLM_LOCK:
|
| 321 |
if time.time() < _LLM_DISABLED_UNTIL:
|
| 322 |
raise RuntimeError("LLM providers on cooldown — rate limit hit recently")
|
| 323 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
def _try_openrouter() -> tuple[str, str, str] | None:
|
| 325 |
global _OPENROUTER_DISABLED_UNTIL
|
| 326 |
api_key = os.environ.get("OPENROUTER_API_KEY", "").strip()
|
|
|
|
| 500 |
|
| 501 |
# Map provider name → closure (defined above, captures this call's messages/params).
|
| 502 |
_PROVIDER_FN_MAP: dict = {
|
|
|
|
| 503 |
"openrouter": _try_openrouter,
|
| 504 |
"groq": _try_groq,
|
| 505 |
"huggingface": _try_huggingface,
|
|
|
|
| 518 |
with _LLM_LOCK:
|
| 519 |
_ls = dict(_PROVIDER_LAST_SUCCESS)
|
| 520 |
_disabled = {
|
|
|
|
| 521 |
"openrouter": _OPENROUTER_DISABLED_UNTIL,
|
| 522 |
"groq": _GROQ_DISABLED_UNTIL,
|
| 523 |
"huggingface": _HF_DISABLED_UNTIL,
|
|
|
|
| 557 |
with _LLM_LOCK:
|
| 558 |
active_cooldowns = [
|
| 559 |
t for t in [
|
|
|
|
| 560 |
_OPENROUTER_DISABLED_UNTIL,
|
| 561 |
_GROQ_DISABLED_UNTIL,
|
| 562 |
_HF_DISABLED_UNTIL,
|
|
|
|
| 1202 |
get_ai_forecast(ticker, company, tf_label, ml, nifty_ok, macro_ok, vix_level, news,
|
| 1203 |
current_price=..., indicators=..., ohlcv_df=..., ...)
|
| 1204 |
|
| 1205 |
+
Provider chain: OpenRouter → Groq → HuggingFace → heuristic fallback.
|
| 1206 |
Source label: "{provider}:{model}+debate+fund" | "+debate" | "" | "heuristic".
|
| 1207 |
"""
|
| 1208 |
# ── Parse arguments ────────────────────────────────────────────────────────
|
|
@@ -545,7 +545,6 @@ def db_diag():
|
|
| 545 |
except Exception as e:
|
| 546 |
download_result = f"FAILED: {e}"
|
| 547 |
providers = {
|
| 548 |
-
"github_token": bool(os.environ.get("GITHUB_TOKEN", "").strip()),
|
| 549 |
"openrouter_api_key": bool(os.environ.get("OPENROUTER_API_KEY", "").strip()),
|
| 550 |
"groq_api_key": bool(os.environ.get("GROQ_API_KEY", "").strip()),
|
| 551 |
"hf_token": bool(os.environ.get("HF_TOKEN", "").strip()),
|
|
|
|
| 545 |
except Exception as e:
|
| 546 |
download_result = f"FAILED: {e}"
|
| 547 |
providers = {
|
|
|
|
| 548 |
"openrouter_api_key": bool(os.environ.get("OPENROUTER_API_KEY", "").strip()),
|
| 549 |
"groq_api_key": bool(os.environ.get("GROQ_API_KEY", "").strip()),
|
| 550 |
"hf_token": bool(os.environ.get("HF_TOKEN", "").strip()),
|
|
@@ -1,389 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
model_picker.py - GitHub Models availability checker and intelligent model selector.
|
| 3 |
-
|
| 4 |
-
Tests all available models against your GitHub token and selects the best available
|
| 5 |
-
model based on availability, rate limits, and performance tier.
|
| 6 |
-
|
| 7 |
-
Fetches model list dynamically from GitHub Models API.
|
| 8 |
-
|
| 9 |
-
Usage:
|
| 10 |
-
from model_picker import get_best_model, test_all_models, fetch_available_models
|
| 11 |
-
|
| 12 |
-
# Get all available models from API
|
| 13 |
-
available = fetch_available_models() # Returns ['gpt-4o', 'gpt-4o-mini', ...]
|
| 14 |
-
|
| 15 |
-
model = get_best_model() # Returns best available model
|
| 16 |
-
available = test_all_models() # Tests all models, returns detailed report
|
| 17 |
-
"""
|
| 18 |
-
|
| 19 |
-
import os
|
| 20 |
-
import requests
|
| 21 |
-
import logging
|
| 22 |
-
from typing import Dict, List, Tuple
|
| 23 |
-
|
| 24 |
-
logging.basicConfig(level=logging.INFO)
|
| 25 |
-
logger = logging.getLogger(__name__)
|
| 26 |
-
|
| 27 |
-
# GitHub Models API endpoint
|
| 28 |
-
GITHUB_MODELS_API = "https://models.inference.ai.azure.com/chat/completions"
|
| 29 |
-
GITHUB_MODELS_LIST_API = "https://models.inference.ai.azure.com/models"
|
| 30 |
-
|
| 31 |
-
# Model tier ranking (higher = better)
|
| 32 |
-
# Keys match the actual `name` field returned by the GitHub Models API.
|
| 33 |
-
MODEL_TIERS = {
|
| 34 |
-
"gpt-4o": 10, # Best reasoning/instruction
|
| 35 |
-
"gpt-4o-mini": 9, # Fast + cheap
|
| 36 |
-
"gpt-4-turbo": 8,
|
| 37 |
-
"gpt-4": 7,
|
| 38 |
-
"claude-3-5-sonnet": 6, # Anthropic via GitHub (if added)
|
| 39 |
-
"Meta-Llama-3.1-405B-Instruct": 5, # Large open-source, capable
|
| 40 |
-
"Meta-Llama-3.1-70B-Instruct": 4,
|
| 41 |
-
"Meta-Llama-3.1-8B-Instruct": 3, # Smallest Llama
|
| 42 |
-
"llama-3.1-405b": 5, # legacy friendly-name aliases
|
| 43 |
-
"llama-3.1-8b": 3,
|
| 44 |
-
"llama-2-70b": 2,
|
| 45 |
-
"mistral-7b-instruct": 1,
|
| 46 |
-
}
|
| 47 |
-
|
| 48 |
-
# Friendly model name mappings (from Azure registry paths)
|
| 49 |
-
AZURE_REGISTRY_MAP = {
|
| 50 |
-
"azure-openai/models/gpt-4o-mini": "gpt-4o-mini",
|
| 51 |
-
"azure-openai/models/gpt-4o": "gpt-4o",
|
| 52 |
-
"azure-openai/models/gpt-4-turbo": "gpt-4-turbo",
|
| 53 |
-
"meta/models/Meta-Llama-3.1-405B": "llama-3.1-405b",
|
| 54 |
-
"meta/models/Meta-Llama-3.1-8B": "llama-3.1-8b",
|
| 55 |
-
"cohere/models/Cohere-command-r-plus": "cohere-command-r-plus",
|
| 56 |
-
}
|
| 57 |
-
|
| 58 |
-
# Pattern-based model extraction from GitHub model metadata
|
| 59 |
-
MODEL_PATTERNS = [
|
| 60 |
-
("gpt-5", "gpt-5"),
|
| 61 |
-
("codex", "codex"),
|
| 62 |
-
("gpt-4o-mini", "gpt-4o-mini"),
|
| 63 |
-
("gpt-4o", "gpt-4o"),
|
| 64 |
-
("gpt-4-turbo", "gpt-4-turbo"),
|
| 65 |
-
("gpt-4", "gpt-4"),
|
| 66 |
-
("llama-3.1-405b", "llama-3.1-405b"),
|
| 67 |
-
("llama-3.1-8b", "llama-3.1-8b"),
|
| 68 |
-
("llama-2-70b", "llama-2-70b"),
|
| 69 |
-
("mistral-7b-instruct", "mistral-7b-instruct"),
|
| 70 |
-
]
|
| 71 |
-
|
| 72 |
-
# Fallback models to test if dynamic fetch fails
|
| 73 |
-
FALLBACK_MODELS = [
|
| 74 |
-
"gpt-4o",
|
| 75 |
-
"gpt-4o-mini",
|
| 76 |
-
"gpt-4-turbo",
|
| 77 |
-
"gpt-4",
|
| 78 |
-
"claude-3-5-sonnet",
|
| 79 |
-
"llama-2-70b",
|
| 80 |
-
"mistral-7b-instruct",
|
| 81 |
-
"llama-2-7b",
|
| 82 |
-
"phi-3-medium",
|
| 83 |
-
"phi-3-mini",
|
| 84 |
-
]
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
def _get_github_token() -> str:
|
| 89 |
-
"""Load GitHub token from .env or environment."""
|
| 90 |
-
token = os.getenv("GITHUB_TOKEN", "").strip()
|
| 91 |
-
if not token:
|
| 92 |
-
# Try loading from .env manually
|
| 93 |
-
try:
|
| 94 |
-
with open('.env', 'r') as f:
|
| 95 |
-
for line in f:
|
| 96 |
-
line = line.strip()
|
| 97 |
-
if line.startswith('GITHUB_TOKEN='):
|
| 98 |
-
token = line.split('=', 1)[1].strip()
|
| 99 |
-
break
|
| 100 |
-
except Exception:
|
| 101 |
-
pass
|
| 102 |
-
return token
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
def fetch_available_models(github_token: str) -> List[str]:
|
| 106 |
-
"""
|
| 107 |
-
Fetch list of chat-completion models from GitHub Models API.
|
| 108 |
-
|
| 109 |
-
Uses the `name` field directly from the API response — no pattern-matching
|
| 110 |
-
needed. Only models with task="chat-completion" are returned.
|
| 111 |
-
|
| 112 |
-
Returns:
|
| 113 |
-
List of model names exactly as the API reports them
|
| 114 |
-
(e.g., ['gpt-4o', 'gpt-4o-mini', 'Meta-Llama-3.1-405B-Instruct', ...])
|
| 115 |
-
"""
|
| 116 |
-
try:
|
| 117 |
-
headers = {
|
| 118 |
-
"Authorization": f"Bearer {github_token}",
|
| 119 |
-
"Content-Type": "application/json",
|
| 120 |
-
}
|
| 121 |
-
|
| 122 |
-
resp = requests.get(GITHUB_MODELS_LIST_API, headers=headers, timeout=10)
|
| 123 |
-
|
| 124 |
-
if resp.status_code == 200:
|
| 125 |
-
data = resp.json()
|
| 126 |
-
models: List[str] = []
|
| 127 |
-
|
| 128 |
-
if isinstance(data, list):
|
| 129 |
-
for item in data:
|
| 130 |
-
if not isinstance(item, dict):
|
| 131 |
-
continue
|
| 132 |
-
# Only include models that support chat/completions
|
| 133 |
-
task = item.get("task", "")
|
| 134 |
-
if task not in ("chat-completion", "chat_completion", "completions", ""):
|
| 135 |
-
# Skip embedding / image / audio models
|
| 136 |
-
if task and task not in ("chat-completion", "chat_completion"):
|
| 137 |
-
continue
|
| 138 |
-
name = item.get("name", "").strip()
|
| 139 |
-
if name:
|
| 140 |
-
models.append(name)
|
| 141 |
-
|
| 142 |
-
unique_models = sorted(set(models))
|
| 143 |
-
logger.info(f"📦 Fetched {len(unique_models)} chat-completion models from GitHub PAT")
|
| 144 |
-
for m in unique_models:
|
| 145 |
-
logger.info(f" • {m}")
|
| 146 |
-
return unique_models if unique_models else FALLBACK_MODELS
|
| 147 |
-
else:
|
| 148 |
-
logger.warning(f"Could not fetch model list (status {resp.status_code}), using fallback")
|
| 149 |
-
return FALLBACK_MODELS
|
| 150 |
-
except Exception as e:
|
| 151 |
-
logger.warning(f"Model list fetch failed: {e}, using fallback models")
|
| 152 |
-
return FALLBACK_MODELS
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
def test_model(model_name: str, github_token: str, timeout: int = 5) -> Dict:
|
| 157 |
-
"""
|
| 158 |
-
Test if a model is available and accessible.
|
| 159 |
-
|
| 160 |
-
Returns:
|
| 161 |
-
{
|
| 162 |
-
"model": str,
|
| 163 |
-
"available": bool,
|
| 164 |
-
"status_code": int,
|
| 165 |
-
"reason": str (e.g. "WORKING", "RATE_LIMITED", "NOT_FOUND", "ERROR"),
|
| 166 |
-
"rate_limit_remaining": int or None,
|
| 167 |
-
"tier": int
|
| 168 |
-
}
|
| 169 |
-
"""
|
| 170 |
-
headers = {
|
| 171 |
-
"Authorization": f"Bearer {github_token}",
|
| 172 |
-
"Content-Type": "application/json"
|
| 173 |
-
}
|
| 174 |
-
|
| 175 |
-
payload = {
|
| 176 |
-
"model": model_name,
|
| 177 |
-
"messages": [{"role": "user", "content": "ok"}],
|
| 178 |
-
"max_tokens": 5
|
| 179 |
-
}
|
| 180 |
-
|
| 181 |
-
# Tier lookup: check exact name first, then try lower-case and simplified variants
|
| 182 |
-
tier = MODEL_TIERS.get(model_name, 0)
|
| 183 |
-
if tier == 0:
|
| 184 |
-
lower = model_name.lower()
|
| 185 |
-
for key, val in MODEL_TIERS.items():
|
| 186 |
-
if key.lower() in lower or lower in key.lower():
|
| 187 |
-
tier = val
|
| 188 |
-
break
|
| 189 |
-
|
| 190 |
-
result = {
|
| 191 |
-
"model": model_name,
|
| 192 |
-
"available": False,
|
| 193 |
-
"status_code": None,
|
| 194 |
-
"reason": "UNKNOWN",
|
| 195 |
-
"rate_limit_remaining": None,
|
| 196 |
-
"tier": tier,
|
| 197 |
-
}
|
| 198 |
-
|
| 199 |
-
try:
|
| 200 |
-
resp = requests.post(GITHUB_MODELS_API, headers=headers, json=payload, timeout=timeout)
|
| 201 |
-
result["status_code"] = resp.status_code
|
| 202 |
-
|
| 203 |
-
# Check rate limit headers
|
| 204 |
-
if "x-ratelimit-remaining-requests" in resp.headers:
|
| 205 |
-
try:
|
| 206 |
-
result["rate_limit_remaining"] = int(resp.headers["x-ratelimit-remaining-requests"])
|
| 207 |
-
except:
|
| 208 |
-
pass
|
| 209 |
-
|
| 210 |
-
if resp.status_code == 200:
|
| 211 |
-
result["available"] = True
|
| 212 |
-
result["reason"] = "WORKING"
|
| 213 |
-
elif resp.status_code == 429:
|
| 214 |
-
result["available"] = False
|
| 215 |
-
result["reason"] = "RATE_LIMITED"
|
| 216 |
-
# Still might work if we wait/retry
|
| 217 |
-
elif resp.status_code == 401:
|
| 218 |
-
result["reason"] = "UNAUTHORIZED"
|
| 219 |
-
elif resp.status_code == 403:
|
| 220 |
-
result["reason"] = "FORBIDDEN"
|
| 221 |
-
elif resp.status_code == 400:
|
| 222 |
-
result["reason"] = "NOT_FOUND"
|
| 223 |
-
else:
|
| 224 |
-
result["reason"] = f"HTTP_{resp.status_code}"
|
| 225 |
-
|
| 226 |
-
except requests.exceptions.Timeout:
|
| 227 |
-
result["reason"] = "TIMEOUT"
|
| 228 |
-
except Exception as e:
|
| 229 |
-
result["reason"] = f"ERROR: {str(e)[:30]}"
|
| 230 |
-
|
| 231 |
-
return result
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
def test_all_models(verbose: bool = True) -> Dict[str, Dict]:
|
| 235 |
-
"""
|
| 236 |
-
Test all available models and return availability report.
|
| 237 |
-
|
| 238 |
-
Returns:
|
| 239 |
-
{
|
| 240 |
-
"gpt-4o": {"available": True, "status": "WORKING", ...},
|
| 241 |
-
"gpt-4-mini": {"available": False, "status": "RATE_LIMITED", ...},
|
| 242 |
-
...
|
| 243 |
-
}
|
| 244 |
-
"""
|
| 245 |
-
github_token = _get_github_token()
|
| 246 |
-
if not github_token:
|
| 247 |
-
logger.error("❌ GITHUB_TOKEN not found in .env or environment")
|
| 248 |
-
return {}
|
| 249 |
-
|
| 250 |
-
# Fetch dynamic model list
|
| 251 |
-
models_to_test = fetch_available_models(github_token)
|
| 252 |
-
|
| 253 |
-
results = {}
|
| 254 |
-
|
| 255 |
-
if verbose:
|
| 256 |
-
print("\n" + "="*70)
|
| 257 |
-
print("🔍 Testing GitHub Models Availability")
|
| 258 |
-
print("="*70 + "\n")
|
| 259 |
-
|
| 260 |
-
for model_name in models_to_test:
|
| 261 |
-
result = test_model(model_name, github_token)
|
| 262 |
-
results[model_name] = result
|
| 263 |
-
|
| 264 |
-
status_icon = "✅" if result["available"] else "❌" if result["reason"] == "NOT_FOUND" else "⏳"
|
| 265 |
-
if verbose:
|
| 266 |
-
print(f"{status_icon} {model_name:<30} → {result['reason']:<15}", end="")
|
| 267 |
-
if result["rate_limit_remaining"] is not None:
|
| 268 |
-
print(f" (remaining: {result['rate_limit_remaining']})", end="")
|
| 269 |
-
print()
|
| 270 |
-
|
| 271 |
-
if verbose:
|
| 272 |
-
print("\n" + "="*70)
|
| 273 |
-
print("📊 Summary")
|
| 274 |
-
print("="*70)
|
| 275 |
-
available = [m for m, r in results.items() if r["available"]]
|
| 276 |
-
rate_limited = [m for m, r in results.items() if r["reason"] == "RATE_LIMITED"]
|
| 277 |
-
not_found = [m for m, r in results.items() if r["reason"] == "NOT_FOUND"]
|
| 278 |
-
|
| 279 |
-
print(f"✅ Working: {len(available)}")
|
| 280 |
-
if available:
|
| 281 |
-
for m in available:
|
| 282 |
-
print(f" • {m}")
|
| 283 |
-
else:
|
| 284 |
-
print(" none")
|
| 285 |
-
|
| 286 |
-
if rate_limited:
|
| 287 |
-
print(f"⏳ Rate Limited: {len(rate_limited)}")
|
| 288 |
-
for m in rate_limited:
|
| 289 |
-
print(f" • {m}")
|
| 290 |
-
|
| 291 |
-
if not_found:
|
| 292 |
-
print(f"❌ Not Found: {len(not_found)}")
|
| 293 |
-
for m in not_found[:3]:
|
| 294 |
-
print(f" • {m}")
|
| 295 |
-
print()
|
| 296 |
-
|
| 297 |
-
return results
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
def get_best_model(fallback_to_rate_limited: bool = True) -> Tuple[str, Dict]:
|
| 302 |
-
"""
|
| 303 |
-
Select the best available model based on tier and availability.
|
| 304 |
-
|
| 305 |
-
Strategy:
|
| 306 |
-
1. Prefer working models (gpt-4o > gpt-4o-mini > others)
|
| 307 |
-
2. Fall back to rate-limited models if requested
|
| 308 |
-
3. Use rate-limited gpt-4o with retry logic over gpt-4o-mini
|
| 309 |
-
|
| 310 |
-
Returns:
|
| 311 |
-
(model_name, test_result)
|
| 312 |
-
|
| 313 |
-
Priority:
|
| 314 |
-
1. Working models (sorted by tier)
|
| 315 |
-
2. Rate-limited models if fallback_to_rate_limited=True (may retry later)
|
| 316 |
-
3. None if no models available
|
| 317 |
-
"""
|
| 318 |
-
github_token = _get_github_token()
|
| 319 |
-
if not github_token:
|
| 320 |
-
logger.warning("⚠️ GITHUB_TOKEN not set, returning None")
|
| 321 |
-
return None, {}
|
| 322 |
-
|
| 323 |
-
results = test_all_models(verbose=False)
|
| 324 |
-
|
| 325 |
-
# 1. Find working models (prefer gpt-4o-mini since gpt-4o is usually rate-limited)
|
| 326 |
-
working = [
|
| 327 |
-
(name, res) for name, res in results.items()
|
| 328 |
-
if res["available"]
|
| 329 |
-
]
|
| 330 |
-
working.sort(key=lambda x: -x[1]["tier"]) # Sort by tier (descending)
|
| 331 |
-
|
| 332 |
-
if working:
|
| 333 |
-
best_name, best_result = working[0]
|
| 334 |
-
logger.info(f"✅ Selected model: {best_name} (available, tier {best_result['tier']})")
|
| 335 |
-
return best_name, best_result
|
| 336 |
-
|
| 337 |
-
# 2. Find rate-limited models (can retry with backoff)
|
| 338 |
-
if fallback_to_rate_limited:
|
| 339 |
-
rate_limited = [
|
| 340 |
-
(name, res) for name, res in results.items()
|
| 341 |
-
if res["reason"] == "RATE_LIMITED"
|
| 342 |
-
]
|
| 343 |
-
rate_limited.sort(key=lambda x: -x[1]["tier"])
|
| 344 |
-
|
| 345 |
-
if rate_limited:
|
| 346 |
-
best_name, best_result = rate_limited[0]
|
| 347 |
-
logger.warning(
|
| 348 |
-
f"⚠️ No working models. {best_name} is rate-limited "
|
| 349 |
-
f"but available (tier {best_result['tier']}). Retry with exponential backoff."
|
| 350 |
-
)
|
| 351 |
-
return best_name, best_result
|
| 352 |
-
|
| 353 |
-
# 3. No models available
|
| 354 |
-
logger.error("❌ No models available for this token")
|
| 355 |
-
return None, {}
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
def get_model_for_backtest() -> str:
|
| 360 |
-
"""Get best model for backtesting (prefers rate-limited but high-tier models)."""
|
| 361 |
-
model, _ = get_best_model(fallback_to_rate_limited=True)
|
| 362 |
-
return model or "gpt-4o" # Fallback to gpt-4o with retry logic
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
if __name__ == "__main__":
|
| 366 |
-
github_token = _get_github_token()
|
| 367 |
-
if not github_token:
|
| 368 |
-
print("❌ GITHUB_TOKEN not found in .env")
|
| 369 |
-
exit(1)
|
| 370 |
-
|
| 371 |
-
# Fetch and display dynamic model list
|
| 372 |
-
print("\n🔍 Discovering available models from GitHub Models API...\n")
|
| 373 |
-
dynamic_models = fetch_available_models(github_token)
|
| 374 |
-
print(f"📦 Found {len(dynamic_models)} models:")
|
| 375 |
-
for model in dynamic_models:
|
| 376 |
-
print(f" • {model}")
|
| 377 |
-
|
| 378 |
-
# Show available models
|
| 379 |
-
print("\n" + "="*70)
|
| 380 |
-
results = test_all_models(verbose=True)
|
| 381 |
-
|
| 382 |
-
# Show best selection
|
| 383 |
-
best_model, best_info = get_best_model()
|
| 384 |
-
if best_model:
|
| 385 |
-
print(f"\n🎯 Best model for forecasting: {best_model}")
|
| 386 |
-
print(f" Status: {best_info['reason']}")
|
| 387 |
-
print(f" Tier: {best_info['tier']}/10")
|
| 388 |
-
else:
|
| 389 |
-
print("\n❌ No suitable model found")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -2,8 +2,7 @@
|
|
| 2 |
"""
|
| 3 |
news_sentiment.py — AI-powered news sentiment for NSE stocks.
|
| 4 |
|
| 5 |
-
|
| 6 |
-
Fallback: OpenRouter (OPENROUTER_API_KEY), then keyword scoring.
|
| 7 |
|
| 8 |
News fetch strategy:
|
| 9 |
1. Google News RSS using company name (most relevant for NSE stocks)
|
|
@@ -47,14 +46,13 @@ _NEG = {
|
|
| 47 |
"selloff", "slump", "disappointing", "bleak", "warning", "breach", "lawsuit",
|
| 48 |
}
|
| 49 |
|
| 50 |
-
# ── CLAUDE API SENTIMENT ──────────────────────────────────────────────────────
|
| 51 |
def analyze_news(
|
| 52 |
headlines: list[str],
|
| 53 |
ticker: str,
|
| 54 |
company: str = "",
|
| 55 |
) -> dict:
|
| 56 |
"""
|
| 57 |
-
Analyze news headlines
|
| 58 |
Returns { label, score, summary, key_headline, source }.
|
| 59 |
Falls back to keyword scoring if no API key is set.
|
| 60 |
"""
|
|
@@ -80,9 +78,10 @@ def _llm_analyze(headlines: list[str], ticker: str, company: str) -> dict | None
|
|
| 80 |
_LLM_PROBE_IN_FLIGHT = True
|
| 81 |
|
| 82 |
try:
|
| 83 |
-
gh_token = os.environ.get("GITHUB_TOKEN", "").strip()
|
| 84 |
or_key = os.environ.get("OPENROUTER_API_KEY", "").strip()
|
| 85 |
-
|
|
|
|
|
|
|
| 86 |
return None
|
| 87 |
|
| 88 |
numbered = "\n".join(f"{i+1}. {h}" for i, h in enumerate(headlines[:8]))
|
|
@@ -119,34 +118,9 @@ Rules:
|
|
| 119 |
"source": data.get("source", "llm"),
|
| 120 |
}
|
| 121 |
|
| 122 |
-
# Provider 1:
|
| 123 |
-
if gh_token:
|
| 124 |
-
gh_model = os.environ.get("NEWS_SENTIMENT_GITHUB_MODEL", "gpt-4o-mini").strip() or "gpt-4o-mini"
|
| 125 |
-
gh_resp = requests.post(
|
| 126 |
-
"https://models.inference.ai.azure.com/chat/completions",
|
| 127 |
-
headers={
|
| 128 |
-
"Authorization": f"Bearer {gh_token}",
|
| 129 |
-
"Content-Type": "application/json",
|
| 130 |
-
},
|
| 131 |
-
json={
|
| 132 |
-
"model": gh_model,
|
| 133 |
-
"messages": [{"role": "user", "content": prompt}],
|
| 134 |
-
"temperature": 0.2,
|
| 135 |
-
"max_tokens": 250,
|
| 136 |
-
},
|
| 137 |
-
timeout=12,
|
| 138 |
-
)
|
| 139 |
-
if gh_resp.status_code == 200:
|
| 140 |
-
payload = gh_resp.json()
|
| 141 |
-
content = (((payload.get("choices") or [{}])[0].get("message") or {}).get("content") or "")
|
| 142 |
-
parsed = _parse_chat_json(content)
|
| 143 |
-
if parsed:
|
| 144 |
-
parsed["source"] = f"github:{gh_model}"
|
| 145 |
-
return parsed
|
| 146 |
-
|
| 147 |
-
# Provider 2: OpenRouter fallback
|
| 148 |
if or_key:
|
| 149 |
-
or_model = os.environ.get("OPENROUTER_BEST_FREE_MODEL", "
|
| 150 |
or_resp = requests.post(
|
| 151 |
"https://openrouter.ai/api/v1/chat/completions",
|
| 152 |
headers={
|
|
@@ -169,6 +143,72 @@ Rules:
|
|
| 169 |
parsed["source"] = f"openrouter:{or_model}"
|
| 170 |
return parsed
|
| 171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
with _LLM_LOCK:
|
| 173 |
_LLM_DISABLED_UNTIL = time.time() + _LLM_COOLDOWN_SECS
|
| 174 |
return None
|
|
|
|
| 2 |
"""
|
| 3 |
news_sentiment.py — AI-powered news sentiment for NSE stocks.
|
| 4 |
|
| 5 |
+
Provider chain: OpenRouter → Groq → HuggingFace → keyword scoring.
|
|
|
|
| 6 |
|
| 7 |
News fetch strategy:
|
| 8 |
1. Google News RSS using company name (most relevant for NSE stocks)
|
|
|
|
| 46 |
"selloff", "slump", "disappointing", "bleak", "warning", "breach", "lawsuit",
|
| 47 |
}
|
| 48 |
|
|
|
|
| 49 |
def analyze_news(
|
| 50 |
headlines: list[str],
|
| 51 |
ticker: str,
|
| 52 |
company: str = "",
|
| 53 |
) -> dict:
|
| 54 |
"""
|
| 55 |
+
Analyze news headlines using OpenRouter → Groq → HuggingFace.
|
| 56 |
Returns { label, score, summary, key_headline, source }.
|
| 57 |
Falls back to keyword scoring if no API key is set.
|
| 58 |
"""
|
|
|
|
| 78 |
_LLM_PROBE_IN_FLIGHT = True
|
| 79 |
|
| 80 |
try:
|
|
|
|
| 81 |
or_key = os.environ.get("OPENROUTER_API_KEY", "").strip()
|
| 82 |
+
groq_key = os.environ.get("GROQ_API_KEY", "").strip()
|
| 83 |
+
hf_token = os.environ.get("HF_TOKEN", "").strip()
|
| 84 |
+
if not or_key and not groq_key and not hf_token:
|
| 85 |
return None
|
| 86 |
|
| 87 |
numbered = "\n".join(f"{i+1}. {h}" for i, h in enumerate(headlines[:8]))
|
|
|
|
| 118 |
"source": data.get("source", "llm"),
|
| 119 |
}
|
| 120 |
|
| 121 |
+
# Provider 1: OpenRouter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
if or_key:
|
| 123 |
+
or_model = os.environ.get("OPENROUTER_BEST_FREE_MODEL", "openai/gpt-oss-120b:free").strip()
|
| 124 |
or_resp = requests.post(
|
| 125 |
"https://openrouter.ai/api/v1/chat/completions",
|
| 126 |
headers={
|
|
|
|
| 143 |
parsed["source"] = f"openrouter:{or_model}"
|
| 144 |
return parsed
|
| 145 |
|
| 146 |
+
# Provider 2: Groq
|
| 147 |
+
if groq_key:
|
| 148 |
+
groq_primary = "llama-3.3-70b-versatile"
|
| 149 |
+
groq_fallback_raw = os.environ.get("GROQ_FREE_MODELS", "")
|
| 150 |
+
groq_models = [groq_primary] + [m.strip() for m in groq_fallback_raw.split(",") if m.strip() and m.strip() != groq_primary][:3]
|
| 151 |
+
for groq_model in groq_models:
|
| 152 |
+
try:
|
| 153 |
+
groq_resp = requests.post(
|
| 154 |
+
"https://api.groq.com/openai/v1/chat/completions",
|
| 155 |
+
headers={
|
| 156 |
+
"Authorization": f"Bearer {groq_key}",
|
| 157 |
+
"Content-Type": "application/json",
|
| 158 |
+
},
|
| 159 |
+
json={
|
| 160 |
+
"model": groq_model,
|
| 161 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 162 |
+
"temperature": 0.2,
|
| 163 |
+
"max_tokens": 250,
|
| 164 |
+
},
|
| 165 |
+
timeout=15,
|
| 166 |
+
)
|
| 167 |
+
if groq_resp.status_code == 429:
|
| 168 |
+
continue
|
| 169 |
+
if groq_resp.status_code == 200:
|
| 170 |
+
payload = groq_resp.json()
|
| 171 |
+
content = (((payload.get("choices") or [{}])[0].get("message") or {}).get("content") or "")
|
| 172 |
+
parsed = _parse_chat_json(content)
|
| 173 |
+
if parsed:
|
| 174 |
+
parsed["source"] = f"groq:{groq_model}"
|
| 175 |
+
return parsed
|
| 176 |
+
except Exception:
|
| 177 |
+
continue
|
| 178 |
+
|
| 179 |
+
# Provider 3: HuggingFace
|
| 180 |
+
if hf_token:
|
| 181 |
+
hf_primary = (os.environ.get("HF_INFERENCE_MODEL") or "meta-llama/Llama-3.1-8B-Instruct").strip()
|
| 182 |
+
hf_fallback_raw = os.environ.get("HF_INFERENCE_FALLBACK_MODELS", "")
|
| 183 |
+
hf_models = [hf_primary] + [m.strip() for m in hf_fallback_raw.split(",") if m.strip() and m.strip() != hf_primary][:3]
|
| 184 |
+
for hf_model in hf_models:
|
| 185 |
+
try:
|
| 186 |
+
hf_resp = requests.post(
|
| 187 |
+
"https://router.huggingface.co/novita/v3/openai/chat/completions",
|
| 188 |
+
headers={
|
| 189 |
+
"Authorization": f"Bearer {hf_token}",
|
| 190 |
+
"Content-Type": "application/json",
|
| 191 |
+
},
|
| 192 |
+
json={
|
| 193 |
+
"model": hf_model.lower(),
|
| 194 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 195 |
+
"temperature": 0.2,
|
| 196 |
+
"max_tokens": 250,
|
| 197 |
+
},
|
| 198 |
+
timeout=30,
|
| 199 |
+
)
|
| 200 |
+
if hf_resp.status_code == 429:
|
| 201 |
+
continue
|
| 202 |
+
if hf_resp.status_code == 200:
|
| 203 |
+
payload = hf_resp.json()
|
| 204 |
+
content = (((payload.get("choices") or [{}])[0].get("message") or {}).get("content") or "")
|
| 205 |
+
parsed = _parse_chat_json(content)
|
| 206 |
+
if parsed:
|
| 207 |
+
parsed["source"] = f"huggingface:{hf_model}"
|
| 208 |
+
return parsed
|
| 209 |
+
except Exception:
|
| 210 |
+
continue
|
| 211 |
+
|
| 212 |
with _LLM_LOCK:
|
| 213 |
_LLM_DISABLED_UNTIL = time.time() + _LLM_COOLDOWN_SECS
|
| 214 |
return None
|
|
@@ -10,8 +10,8 @@ while overriding source metadata via environment variables:
|
|
| 10 |
|
| 11 |
Example:
|
| 12 |
python research/compare_predictors.py \
|
| 13 |
-
--a-provider
|
| 14 |
-
--b-provider
|
| 15 |
--use-cache
|
| 16 |
"""
|
| 17 |
|
|
@@ -117,10 +117,10 @@ def _print_compare(a_label: str, a: dict, b_label: str, b: dict) -> None:
|
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
| 119 |
p = argparse.ArgumentParser()
|
| 120 |
-
p.add_argument("--a-provider", default="
|
| 121 |
-
p.add_argument("--a-model", default="gpt-
|
| 122 |
-
p.add_argument("--b-provider", default="
|
| 123 |
-
p.add_argument("--b-model", default="
|
| 124 |
p.add_argument("--use-cache", action="store_true")
|
| 125 |
p.add_argument("--refresh-cache", action="store_true")
|
| 126 |
p.add_argument("--cache-dir", default=str(RESEARCH / "cache"))
|
|
|
|
| 10 |
|
| 11 |
Example:
|
| 12 |
python research/compare_predictors.py \
|
| 13 |
+
--a-provider openrouter --a-model openai/gpt-oss-120b:free \
|
| 14 |
+
--b-provider groq --b-model llama-3.3-70b-versatile \
|
| 15 |
--use-cache
|
| 16 |
"""
|
| 17 |
|
|
|
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
| 119 |
p = argparse.ArgumentParser()
|
| 120 |
+
p.add_argument("--a-provider", default="openrouter")
|
| 121 |
+
p.add_argument("--a-model", default="openai/gpt-oss-120b:free")
|
| 122 |
+
p.add_argument("--b-provider", default="groq")
|
| 123 |
+
p.add_argument("--b-model", default="llama-3.3-70b-versatile")
|
| 124 |
p.add_argument("--use-cache", action="store_true")
|
| 125 |
p.add_argument("--refresh-cache", action="store_true")
|
| 126 |
p.add_argument("--cache-dir", default=str(RESEARCH / "cache"))
|
|
@@ -361,16 +361,19 @@ def _model_status() -> str:
|
|
| 361 |
"""Return a one-line string showing which LLM providers are available."""
|
| 362 |
try:
|
| 363 |
import ai_forecast as _aif
|
| 364 |
-
github_ready = bool(os.environ.get("GITHUB_TOKEN", ""))
|
| 365 |
or_ready = bool(os.environ.get("OPENROUTER_API_KEY", ""))
|
|
|
|
|
|
|
| 366 |
parts = []
|
| 367 |
-
if github_ready:
|
| 368 |
-
parts.append("GitHub Models: ready")
|
| 369 |
if or_ready:
|
| 370 |
model = os.environ.get("OPENROUTER_BEST_FREE_MODEL", "openai/gpt-oss-120b:free")
|
| 371 |
parts.append(f"OpenRouter ({model}): ready")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 372 |
if not parts:
|
| 373 |
-
parts.append("No LLM provider configured (set
|
| 374 |
return " Models: " + " | ".join(parts)
|
| 375 |
except Exception as e:
|
| 376 |
return f" Models: (status check failed: {e})"
|
|
|
|
| 361 |
"""Return a one-line string showing which LLM providers are available."""
|
| 362 |
try:
|
| 363 |
import ai_forecast as _aif
|
|
|
|
| 364 |
or_ready = bool(os.environ.get("OPENROUTER_API_KEY", ""))
|
| 365 |
+
groq_ready = bool(os.environ.get("GROQ_API_KEY", ""))
|
| 366 |
+
hf_ready = bool(os.environ.get("HF_TOKEN", ""))
|
| 367 |
parts = []
|
|
|
|
|
|
|
| 368 |
if or_ready:
|
| 369 |
model = os.environ.get("OPENROUTER_BEST_FREE_MODEL", "openai/gpt-oss-120b:free")
|
| 370 |
parts.append(f"OpenRouter ({model}): ready")
|
| 371 |
+
if groq_ready:
|
| 372 |
+
parts.append("Groq: ready")
|
| 373 |
+
if hf_ready:
|
| 374 |
+
parts.append("HuggingFace: ready")
|
| 375 |
if not parts:
|
| 376 |
+
parts.append("No LLM provider configured (set OPENROUTER_API_KEY, GROQ_API_KEY, or HF_TOKEN)")
|
| 377 |
return " Models: " + " | ".join(parts)
|
| 378 |
except Exception as e:
|
| 379 |
return f" Models: (status check failed: {e})"
|