Repository navigation
Expand file tree
/
Copy pathvalidate_setup.py
More file actions
129 lines (104 loc) · 3.7 KB
/
Copy pathvalidate_setup.py
File metadata and controls
129 lines (104 loc) · 3.7 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Validation script to test OpenRouter connection
"""
import os
import sys
from pathlib import Path
from dotenv import load_dotenv
import requests
# Add project root to path
sys.path.insert(0, str(Path(__file__).resolve().parent))
from miniagent.logger import get_logger
logger = get_logger("validate")
def validate_api_key():
"""Validate OpenRouter API key"""
# Load environment
env_path = Path(__file__).parent / '.env'
if not env_path.exists():
logger.error("❌ No .env file found")
logger.info("💡 Create a .env file from .env.example")
return False
load_dotenv(env_path)
api_key = os.getenv("OPENROUTER_API_KEY")
if not api_key:
logger.error("❌ OPENROUTER_API_KEY not set in .env")
return False
if api_key == "your_openrouter_api_key_here":
logger.error("❌ Please replace the example API key with your actual key")
return False
logger.info("✓ API key found")
# Test the API key
try:
logger.info("Testing API connection...")
response = requests.post(
"https://openrouter.ai/api/v1/chat/completions",
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
},
json={
"model": "anthropic/claude-3-haiku", # Use cheapest model for testing
"messages": [
{"role": "user", "content": "Say 'Hello!' if you can hear me."}
],
"max_tokens": 10
},
timeout=10
)
if response.status_code == 200:
result = response.json()
message = result.get("choices", [{}])[0].get("message", {}).get("content", "")
logger.info(f"✓ API connection successful!")
logger.info(f"✓ Test response: {message}")
return True
elif response.status_code == 401:
logger.error("❌ Invalid API key")
logger.info("💡 Get your key from https://openrouter.ai/keys")
return False
elif response.status_code == 402:
logger.error("❌ Insufficient credits")
logger.info("💡 Add credits at https://openrouter.ai/credits")
return False
else:
logger.error(f"❌ API error: {response.status_code}")
logger.error(f"Response: {response.text}")
return False
except requests.exceptions.Timeout:
logger.error("❌ Connection timeout")
logger.info("💡 Check your internet connection")
return False
except requests.exceptions.RequestException as e:
logger.error(f"❌ Connection error: {e}")
return False
def validate_dependencies():
"""Check if all dependencies are installed"""
try:
import requests
import dotenv
logger.info("✓ All dependencies installed")
return True
except ImportError as e:
logger.error(f"❌ Missing dependency: {e}")
logger.info("💡 Run: pip install -r requirements.txt")
return False
def main():
"""Run all validations"""
print("\n" + "="*60)
print("MiniAgent-OpenRouter Validation")
print("="*60 + "\n")
# Check dependencies
if not validate_dependencies():
return
# Check API key
if not validate_api_key():
return
print("\n" + "="*60)
print("✓ All checks passed! You're ready to use MiniAgent")
print("="*60)
print("\nTry running an example:")
print(" python examples/simple_example.py")
print()
if __name__ == "__main__":
main()