lastin-ai-2/check_deepseek.py

77 lines
2.4 KiB
Python

import os
import asyncio
import aiohttp
import json
from dotenv import load_dotenv
import platform
async def check_deepseek_api():
"""Simple check if Deepseek API is responsive."""
# Load API key from environment
load_dotenv()
api_key = os.getenv('DEEPSEEK_API_KEY')
if not api_key:
print("Error: DEEPSEEK_API_KEY not found in environment")
return False
session = None
try:
# Create session with auth header
session = aiohttp.ClientSession(headers={"Authorization": f"Bearer {api_key}"})
# Simple test request
payload = {
"model": "deepseek-chat",
"messages": [{"role": "user", "content": "Say hi and introduce yourself briefly"}],
"max_tokens": 100 # Increased to get full response
}
print("Testing Deepseek API...")
print("\nRequest:")
print(json.dumps(payload, indent=2))
async with session.post("https://api.deepseek.com/v1/chat/completions", json=payload) as response:
if response.status != 200:
print(f"\nError: API returned status {response.status}")
error_text = await response.text()
print(f"Response: {error_text}")
return False
data = await response.json()
if not data or 'choices' not in data or not data['choices']:
print("\nError: Invalid response format")
print("Full Response:")
print(json.dumps(data, indent=2))
return False
print("\nSuccess! API is responding correctly")
print("\nFull Response:")
print(json.dumps(data, indent=2))
print("\nMessage Content:")
print(data['choices'][0]['message']['content'])
return True
except Exception as e:
print(f"\nError connecting to Deepseek API: {e}")
return False
finally:
if session:
await session.close()
def main():
# Set event loop policy for Windows
if platform.system() == 'Windows':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
# Create new event loop
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
loop.run_until_complete(check_deepseek_api())
finally:
loop.close()
if __name__ == "__main__":
main()