| |
|
| | """Simple test script to verify OpenRouter integration."""
|
| |
|
| | import asyncio
|
| | import os
|
| | import sys
|
| |
|
| |
|
| | 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)
|
| |
|