61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import os
|
|
import asyncio
|
|
from claude_agent_sdk import ClaudeSDKClient
|
|
|
|
# 设置 API key
|
|
os.environ['ANTHROPIC_API_KEY'] = 'cr_9792f20a98f055e204248a41f280780ca2fb8f08f35e60c785e5245653937e06'
|
|
|
|
async def test_agent_sdk():
|
|
print('🔧 测试 claude-agent-sdk 配置...')
|
|
|
|
try:
|
|
# 初始化客户端
|
|
client = ClaudeSDKClient()
|
|
print('✅ Claude SDK 客户端初始化成功')
|
|
|
|
# 连接
|
|
await client.connect()
|
|
print('✅ 连接成功')
|
|
|
|
# 获取服务器信息
|
|
info = await client.get_server_info()
|
|
print(f'✅ 服务器信息: {info}')
|
|
|
|
# 测试对话
|
|
await client.query('你好,请简短回复测试成功')
|
|
print(f'✅ 查询已发送')
|
|
|
|
# 接收响应
|
|
full_response = ''
|
|
async for chunk in client.receive_response():
|
|
if hasattr(chunk, 'text'):
|
|
full_response += chunk.text
|
|
elif isinstance(chunk, dict) and 'text' in chunk:
|
|
full_response += chunk['text']
|
|
else:
|
|
full_response += str(chunk)
|
|
|
|
print(f'✅ 对话测试成功')
|
|
print(f'📝 AI回复: {full_response}')
|
|
|
|
await client.disconnect()
|
|
print('✅ 断开连接成功')
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f'❌ 测试失败: {e}')
|
|
import traceback
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
if __name__ == '__main__':
|
|
result = asyncio.run(test_agent_sdk())
|
|
if result:
|
|
print('\n🎉 claude-agent-sdk 配置成功!')
|
|
else:
|
|
print('\n❌ 配置失败')
|