Files
Ubuntu 32b43f85bd a
2026-07-03 16:58:28 +08:00

94 lines
3.2 KiB
Python

#!/usr/bin/env python3
"""测试 DeepSeek API 是否正常工作"""
import asyncio
import aiohttp
import json
# API配置
API_KEY = "sk-8cb88f614b5b4def882d0ac46025cdcc"
API_URL = "https://api.deepseek.com/v1/chat/completions"
async def test_deepseek():
print("=" * 60)
print(" 🧠 DeepSeek API 测试")
print("=" * 60)
headers = {
'Authorization': f'Bearer {API_KEY}',
'Content-Type': 'application/json'
}
payload = {
'model': 'deepseek-chat',
'messages': [
{'role': 'system', 'content': '你是一个斗地主游戏AI助手。请根据提示做出最佳决策,只输出JSON格式。'},
{'role': 'user', 'content': '''你是一个斗地主高手。请根据你的手牌决定是否叫地主。
你的手牌(共17张):
♠3, ♥4, ♦5, ♣6, ♠7, ♥8, ♦9, ♣10, ♠J, ♥Q, ♦K, ♣A, ♠2, ♥小王, ♦大王, ♣3, ♠4
手牌强度评分: 15/30
请决定是否叫地主,回答格式:
{"call": true/false, "reason": "你的理由"}
只输出JSON,不要其他内容。'''} ],
'temperature': 0.3,
'max_tokens': 500
}
print(f"\n📡 调用 DeepSeek API...")
print(f" URL: {API_URL}")
print(f" Key: {API_KEY[:10]}...{API_KEY[-4:]}")
try:
async with aiohttp.ClientSession() as session:
async with session.post(
API_URL,
headers=headers,
json=payload,
timeout=aiohttp.ClientTimeout(total=30)
) as response:
print(f"\n📥 响应状态: {response.status}")
if response.status != 200:
error_text = await response.text()
print(f"❌ API Error: {error_text}")
return False
data = await response.json()
content = data['choices'][0]['message']['content']
print(f"\n📤 AI 响应:")
print(f" {content}")
# 解析 JSON
try:
result = json.loads(content)
print(f"\n✅ JSON 解析成功:")
print(f" call: {result.get('call')}")
print(f" reason: {result.get('reason')}")
return True
except json.JSONDecodeError:
print(f"\n⚠️ 不是标准JSON,尝试提取...")
import re
json_match = re.search(r'\{[^}]+\}', content)
if json_match:
result = json.loads(json_match.group())
print(f" 提取成功: {result}")
return True
else:
print(f"❌ 无法解析")
return False
except Exception as e:
print(f"\n❌ 调用失败: {e}")
import traceback
traceback.print_exc()
return False
if __name__ == '__main__':
result = asyncio.run(test_deepseek())
print(f"\n{'='*60}")
print(f"测试结果: {'✅ 通过' if result else '❌ 失败'}")