Files
telegram-customer-bot/test_claude_api2.py
2025-11-01 21:58:31 +08:00

44 lines
1.2 KiB
Python

#!/usr/bin/env python3
import os
import anthropic
# 加载环境变量
with open("/home/atai/telegram-bot/.env") as f:
for line in f:
line = line.strip()
if line and not line.startswith("#") and "=" in line:
key, value = line.split("=", 1)
os.environ[key] = value
# 初始化客户端
try:
client = anthropic.Anthropic(
api_key=os.environ.get('ANTHROPIC_AUTH_TOKEN'),
base_url=os.environ.get('ANTHROPIC_BASE_URL', 'https://api.anthropic.com')
)
# 尝试不同的模型名称
models = [
"claude-3-5-sonnet-latest",
"claude-3-5-sonnet-20240620",
"claude-3-sonnet-20240229",
"claude-3-opus-20240229"
]
for model in models:
try:
print(f"\n测试模型: {model}")
response = client.messages.create(
model=model,
max_tokens=50,
messages=[{"role": "user", "content": "Hi"}]
)
print(f"{model} 成功!")
print(f"响应: {response.content[0].text[:100]}")
break
except Exception as e:
print(f"{model} 失败: {str(e)[:100]}")
except Exception as e:
print(f"\n❌ 错误: {e}")