chore: initial commit
This commit is contained in:
39
test_claude_api.py
Normal file
39
test_claude_api.py
Normal file
@@ -0,0 +1,39 @@
|
||||
#!/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
|
||||
|
||||
print("环境变量检查:")
|
||||
print(f"ANTHROPIC_AUTH_TOKEN: {os.environ.get('ANTHROPIC_AUTH_TOKEN', '未设置')[:30]}...")
|
||||
print(f"ANTHROPIC_BASE_URL: {os.environ.get('ANTHROPIC_BASE_URL', '未设置')}")
|
||||
|
||||
# 初始化客户端
|
||||
try:
|
||||
client = anthropic.Anthropic(
|
||||
api_key=os.environ.get('ANTHROPIC_AUTH_TOKEN'),
|
||||
base_url=os.environ.get('ANTHROPIC_BASE_URL', 'https://api.anthropic.com')
|
||||
)
|
||||
print("\n✅ Claude客户端初始化成功")
|
||||
|
||||
# 测试API调用
|
||||
print("\n测试API调用...")
|
||||
response = client.messages.create(
|
||||
model="claude-3-5-sonnet-20241022",
|
||||
max_tokens=100,
|
||||
messages=[
|
||||
{"role": "user", "content": "你好,请用一句话介绍自己"}
|
||||
]
|
||||
)
|
||||
|
||||
print(f"\n✅ API调用成功!")
|
||||
print(f"响应: {response.content[0].text}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n❌ 错误: {e}")
|
||||
Reference in New Issue
Block a user