通过 LLM_PROVIDER 环境变量区分 openai 和 anthropic 两种调用方式, LLMClient 根据 provider 分发到不同的 API 调用路径。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
101 lines
3.4 KiB
TypeScript
101 lines
3.4 KiB
TypeScript
import 'dotenv/config';
|
||
import { LLMClient } from './src/utils/llm-client.js';
|
||
import type { PriceDataPoint } from './src/types/index.js';
|
||
|
||
/**
|
||
* 测试 Anthropic API 调用
|
||
*/
|
||
async function testAnthropicLLM() {
|
||
console.log('========================================');
|
||
console.log('Anthropic LLM 趋势分析测试');
|
||
console.log('========================================\n');
|
||
|
||
// 检查环境变量
|
||
const apiKey = process.env.ANTHROPIC_API_KEY;
|
||
if (!apiKey) {
|
||
console.error('错误:未配置 ANTHROPIC_API_KEY 环境变量');
|
||
console.log('请在 .env 文件中设置:');
|
||
console.log(' ANTHROPIC_API_KEY=sk-ant-xxxxxxxxxxxx');
|
||
console.log(' ANTHROPIC_MODEL=claude-sonnet-4-20250514 (可选)');
|
||
console.log(' ANTHROPIC_API_BASE=https://api.anthropic.com (可选)');
|
||
process.exit(1);
|
||
}
|
||
|
||
const config = {
|
||
provider: 'anthropic' as const,
|
||
apiBase: process.env.ANTHROPIC_API_BASE || 'https://api.anthropic.com',
|
||
apiKey,
|
||
model: process.env.ANTHROPIC_MODEL || 'claude-sonnet-4-20250514',
|
||
timeout: 60000,
|
||
maxRetries: 1
|
||
};
|
||
|
||
console.log('配置信息:');
|
||
console.log(` Provider: ${config.provider}`);
|
||
console.log(` API Base: ${config.apiBase}`);
|
||
console.log(` Model: ${config.model}`);
|
||
console.log(` Timeout: ${config.timeout}ms\n`);
|
||
|
||
const llmClient = new LLMClient(config);
|
||
|
||
// 准备测试数据(模拟 30 分钟价格数据)
|
||
const testData: PriceDataPoint[] = [];
|
||
const now = Date.now();
|
||
const basePrice = 22.0;
|
||
|
||
for (let i = 30; i >= 0; i--) {
|
||
const timestamp = new Date(now - i * 60000).toISOString();
|
||
const price = basePrice + (Math.random() * 0.5 - 0.1) + (30 - i) * 0.01;
|
||
const changeFromPrevious = i < 30
|
||
? price - (basePrice + (Math.random() * 0.5 - 0.1) + (29 - i) * 0.01)
|
||
: undefined;
|
||
|
||
testData.push({
|
||
price: parseFloat(price.toFixed(3)),
|
||
timestamp,
|
||
changeFromPrevious: changeFromPrevious ? parseFloat(changeFromPrevious.toFixed(3)) : undefined,
|
||
volatility: Math.random() * 0.01
|
||
});
|
||
}
|
||
|
||
console.log(`准备了 ${testData.length} 个测试数据点`);
|
||
console.log('最后 5 个数据点:');
|
||
testData.slice(-5).forEach(point => {
|
||
console.log(` ${new Date(point.timestamp).toLocaleTimeString('zh-CN')} - ${point.price.toFixed(3)}`);
|
||
});
|
||
console.log();
|
||
|
||
try {
|
||
console.log('开始 Anthropic LLM 分析...\n');
|
||
const startTime = Date.now();
|
||
const result = await llmClient.analyzeTrend(testData);
|
||
const elapsed = Date.now() - startTime;
|
||
|
||
console.log('========================================');
|
||
console.log(`分析结果(耗时 ${elapsed}ms)`);
|
||
console.log('========================================');
|
||
console.log(`趋势类型: ${result.trend}`);
|
||
console.log(`置信度: ${(result.confidence * 100).toFixed(0)}%`);
|
||
console.log(`风险评估: ${result.risk}`);
|
||
console.log(`\n趋势总结:`);
|
||
console.log(` ${result.summary}`);
|
||
console.log(`\n关键信号:`);
|
||
result.signals.forEach((signal, index) => {
|
||
console.log(` ${index + 1}. ${signal}`);
|
||
});
|
||
console.log('========================================\n');
|
||
|
||
console.log('完整 JSON 结果:');
|
||
console.log(JSON.stringify(result, null, 2));
|
||
console.log();
|
||
|
||
console.log('测试成功!Anthropic API 调用正常。');
|
||
} catch (error) {
|
||
console.error('\n测试失败:');
|
||
console.error(error);
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
testAnthropicLLM();
|