File size: 2,378 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 | #!/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)
|