File size: 5,661 Bytes
070daf8 | 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 | #!/usr/bin/env python3
"""Validate the OpenRouter setup in the backend."""
import json
import os
import sys
from pathlib import Path
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
def check_env_variables():
"""Check that required environment variables are set."""
print("=" * 60)
print("Checking Environment Variables")
print("=" * 60)
required = ["OPENROUTER_API_KEY", "HF_TOKEN"]
optional = ["OPENROUTER_MODEL", "OPENROUTER_REFERER", "OPENROUTER_APP_TITLE"]
all_good = True
for var in required:
value = os.environ.get(var)
if value:
masked = value[:20] + "..." if len(value) > 20 else value
print(f"β
{var}: {masked}")
else:
print(f"β {var}: NOT SET (Required)")
all_good = False
for var in optional:
value = os.environ.get(var)
if value:
print(f"β
{var}: {value}")
else:
print(f"β οΈ {var}: Not set (Optional)")
return all_good
def check_config_file():
"""Check that the config file is properly configured."""
print("\n" + "=" * 60)
print("Checking Config File")
print("=" * 60)
config_path = Path(__file__).parent / "configs" / "main_agent_config.json"
if not config_path.exists():
print(f"β Config file not found: {config_path}")
return False
try:
with open(config_path) as f:
config = json.load(f)
print(f"β
Config file loaded: {config_path}")
# Check key fields
checks = [
("model_name", config.get("model_name")),
("openrouter_enabled", config.get("openrouter_enabled")),
("openrouter_model", config.get("openrouter_model")),
]
for key, value in checks:
if value:
print(f"β
{key}: {value}")
else:
print(f"β οΈ {key}: Not set")
# Verify OpenRouter is enabled
if config.get("openrouter_enabled"):
print("\nβ
OpenRouter is ENABLED in config")
else:
print("\nβ οΈ OpenRouter is NOT enabled in config")
return True
except json.JSONDecodeError as e:
print(f"β Invalid JSON in config file: {e}")
return False
except Exception as e:
print(f"β Error reading config: {e}")
return False
def check_agent_loop():
"""Check that agent_loop.py has the OpenRouter fixes."""
print("\n" + "=" * 60)
print("Checking Agent Loop (OpenRouter Integration)")
print("=" * 60)
agent_loop_path = Path(__file__).parent / "agent" / "core" / "agent_loop.py"
if not agent_loop_path.exists():
print(f"β agent_loop.py not found: {agent_loop_path}")
return False
with open(agent_loop_path) as f:
content = f.read()
checks = [
("api_base_to_use", "api_base parameter"),
("extra_headers", "extra_headers parameter"),
("https://openrouter.ai/api/v1", "OpenRouter API base URL"),
("HTTP-Referer", "HTTP-Referer header"),
("X-Title", "X-Title header"),
]
all_good = True
for keyword, description in checks:
if keyword in content:
print(f"β
{description}: Found")
else:
print(f"β {description}: NOT found")
all_good = False
return all_good
def check_routes():
"""Check that routes/agent.py has the OpenRouter fixes."""
print("\n" + "=" * 60)
print("Checking Routes (OpenRouter Integration)")
print("=" * 60)
routes_path = Path(__file__).parent / "routes" / "agent.py"
if not routes_path.exists():
print(f"β agent.py not found: {routes_path}")
return False
with open(routes_path) as f:
content = f.read()
checks = [
("llm_health_check", "Health check endpoint"),
("generate_title", "Title generation endpoint"),
("api_base", "api_base in routes"),
("openrouter_enabled", "openrouter_enabled check"),
]
all_good = True
for keyword, description in checks:
if keyword in content:
print(f"β
{description}: Found")
else:
print(f"β {description}: NOT found")
all_good = False
return all_good
def main():
"""Run all validation checks."""
print("=" * 60)
print("OpenRouter Setup Validation")
print("=" * 60)
results = []
results.append(("Environment Variables", check_env_variables()))
results.append(("Config File", check_config_file()))
results.append(("Agent Loop", check_agent_loop()))
results.append(("Routes", check_routes()))
print("\n" + "=" * 60)
print("Validation Summary")
print("=" * 60)
for name, passed in results:
status = "β
PASS" if passed else "β FAIL"
print(f"{name}: {status}")
all_passed = all(r[1] for r in results)
if all_passed:
print("\nπ All validation checks passed!")
print("The backend is configured correctly for OpenRouter.")
return 0
else:
print("\nβ οΈ Some validation checks failed.")
print("Please review the errors above and fix the issues.")
return 1
if __name__ == "__main__":
sys.exit(main())
|