import { LLMClient } from './src/utils/llm-client.js'; import type { PriceDataPoint } from './src/types/index.js'; /** * 测试不同风险等级的判断标准 */ // 检查环境变量 const apiKey = process.env.LLM_API_KEY; if (!apiKey) { console.error('❌ 错误:未配置 LLM_API_KEY 环境变量'); process.exit(1); } const config = { apiBase: process.env.LLM_API_BASE || 'https://api.openai.com/v1', apiKey: apiKey, model: process.env.LLM_MODEL || 'gpt-4o-mini', timeout: 120000 }; const llmClient = new LLMClient(config); /** * 场景1:高风险(波动 > 0.3元) */ async function testHighRisk() { console.log('\n========================================'); console.log('场景 1: 高风险(波动 > 0.3 元)'); console.log('========================================\n'); 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(); // 创建剧烈波动:22.0 → 22.5 → 22.1 → 22.6,波动幅度 > 0.3 let price = basePrice; if (i > 20) price = 22.0 + Math.random() * 0.1; else if (i > 10) price = 22.5 + Math.random() * 0.1; else price = 22.1 + Math.random() * 0.5; const changeFromPrevious = i < 30 ? price - testData[testData.length - 1].price : undefined; const volatility = i > 0 && testData.length > 0 ? Math.abs(changeFromPrevious!) / price : undefined; testData.push({ price, timestamp, changeFromPrevious, volatility }); } const prices = testData.slice(-10).map(p => p.price); const range = Math.max(...prices) - Math.min(...prices); console.log(`数据特征:`); console.log(` 最近10分钟波动幅度: ${range.toFixed(3)} 元`); console.log(` 预期风险: high ${range > 0.3 ? '✅' : '❌'}\n`); try { const result = await llmClient.analyzeTrend(testData); console.log(`LLM 判断结果:`); console.log(` 趋势: ${result.trend}`); console.log(` 风险: ${result.risk} ${result.risk === 'high' ? '✅' : '⚠️'}`); console.log(` 置信度: ${(result.confidence * 100).toFixed(0)}%`); console.log(` 总结: ${result.summary}`); return result.risk === 'high'; } catch (error) { console.error('测试失败:', error); return false; } } /** * 场景2:中等风险(波动 0.15-0.3元) */ async function testMediumRisk() { console.log('\n========================================'); console.log('场景 2: 中等风险(波动 0.15-0.3 元)'); console.log('========================================\n'); 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(); // 创建中等波动:22.0 → 22.25,波动幅度约 0.25 let price = basePrice + (i % 2) * 0.12 + Math.random() * 0.05; const changeFromPrevious = i < 30 ? price - testData[testData.length - 1].price : undefined; const volatility = i > 0 && testData.length > 0 ? Math.abs(changeFromPrevious!) / price : undefined; testData.push({ price, timestamp, changeFromPrevious, volatility }); } const prices = testData.slice(-10).map(p => p.price); const range = Math.max(...prices) - Math.min(...prices); console.log(`数据特征:`); console.log(` 最近10分钟波动幅度: ${range.toFixed(3)} 元`); console.log(` 预期风险: medium ${range >= 0.15 && range <= 0.3 ? '✅' : '❌'}\n`); try { const result = await llmClient.analyzeTrend(testData); console.log(`LLM 判断结果:`); console.log(` 趋势: ${result.trend}`); console.log(` 风险: ${result.risk} ${result.risk === 'medium' ? '✅' : '⚠️'}`); console.log(` 置信度: ${(result.confidence * 100).toFixed(0)}%`); console.log(` 总结: ${result.summary}`); return result.risk === 'medium'; } catch (error) { console.error('测试失败:', error); return false; } } /** * 场景3:低风险(波动 < 0.15元) */ async function testLowRisk() { console.log('\n========================================'); console.log('场景 3: 低风险(波动 < 0.15 元)'); console.log('========================================\n'); 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(); // 创建平稳上涨:波动幅度 < 0.15 const price = basePrice + (30 - i) * 0.01 + Math.random() * 0.02; const changeFromPrevious = i < 30 ? price - testData[testData.length - 1].price : undefined; const volatility = i > 0 && testData.length > 0 ? Math.abs(changeFromPrevious!) / price : undefined; testData.push({ price, timestamp, changeFromPrevious, volatility }); } const prices = testData.slice(-10).map(p => p.price); const range = Math.max(...prices) - Math.min(...prices); console.log(`数据特征:`); console.log(` 最近10分钟波动幅度: ${range.toFixed(3)} 元`); console.log(` 预期风险: low ${range < 0.15 ? '✅' : '❌'}\n`); try { const result = await llmClient.analyzeTrend(testData); console.log(`LLM 判断结果:`); console.log(` 趋势: ${result.trend}`); console.log(` 风险: ${result.risk} ${result.risk === 'low' ? '✅' : '⚠️'}`); console.log(` 置信度: ${(result.confidence * 100).toFixed(0)}%`); console.log(` 总结: ${result.summary}`); return result.risk === 'low'; } catch (error) { console.error('测试失败:', error); return false; } } /** * 运行所有测试 */ async function runAllTests() { console.log('========================================'); console.log('测试风险评估标准'); console.log('========================================'); console.log(`配置信息:`); console.log(` API Base: ${config.apiBase}`); console.log(` Model: ${config.model}`); const results = { high: await testHighRisk(), medium: await testMediumRisk(), low: await testLowRisk() }; console.log('\n========================================'); console.log('测试结果汇总'); console.log('========================================'); console.log(`高风险场景: ${results.high ? '✅ 通过' : '❌ 失败'}`); console.log(`中等风险场景: ${results.medium ? '✅ 通过' : '❌ 失败'}`); console.log(`低风险场景: ${results.low ? '✅ 通过' : '❌ 失败'}`); const allPassed = results.high && results.medium && results.low; console.log(`\n${allPassed ? '✅ 所有测试通过!' : '⚠️ 部分测试未通过'}`); console.log('========================================\n'); process.exit(allPassed ? 0 : 1); } runAllTests();