water3 / test_openrouter_simple.py
onewayto's picture
Upload 187 files
070daf8 verified
#!/usr/bin/env python3
"""Simple test script to verify OpenRouter integration."""
import asyncio
import os
import sys
# Load environment variables from .env file
from dotenv import load_dotenv
load_dotenv()
import httpx
async def test_openrouter_direct():
"""Test OpenRouter API directly using HTTP."""
api_key = os.environ.get("OPENROUTER_API_KEY")
if not api_key:
print("ERROR: OPENROUTER_API_KEY not set!")
return False
print(f"OPENROUTER_API_KEY: {api_key[:25]}...")
print(f"Testing direct API call to OpenRouter...")
async with httpx.AsyncClient(timeout=30.0) as client:
try:
response = await client.post(
"https://openrouter.ai/api/v1/chat/completions",
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
"HTTP-Referer": "https://localhost",
"X-Title": "HF Agent Test",
},
json={
"model": "meta-llama/llama-3.3-70b-instruct",
"messages": [
{"role": "user", "content": "Say 'OpenRouter works!' and nothing else."}
],
"max_tokens": 50,
}
)
print(f"Status code: {response.status_code}")
if response.status_code == 200:
data = response.json()
content = data["choices"][0]["message"]["content"]
print(f"โœ… SUCCESS! Response: {content}")
return True
else:
print(f"โŒ FAILED! Response: {response.text}")
return False
except Exception as e:
print(f"โŒ ERROR: {type(e).__name__}: {e}")
return False
async def main():
print("="*60)
print("OpenRouter Direct API Test")
print("="*60)
success = await test_openrouter_direct()
if success:
print("\n๐ŸŽ‰ OpenRouter API is working!")
return 0
else:
print("\nโš ๏ธ OpenRouter API test failed!")
return 1
if __name__ == "__main__":
exit_code = asyncio.run(main())
sys.exit(exit_code)