115 lines
3.9 KiB
TypeScript
115 lines
3.9 KiB
TypeScript
import { LLMClient } from './src/utils/llm-client.js';
|
|||
|
|
import type { PriceDataPoint } from './src/types/index.js';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 测试上涨趋势场景
|
||
|
|
*/
|
||
|
|
async function testUptrend() {
|
||
|
|
console.log('========================================');
|
||
|
|
console.log('测试上涨趋势场景');
|
||
|
|
console.log('========================================\n');
|
||
|
|
|
||
|
|
// 检查环境变量
|
||
|
|
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);
|
||
|
|
|
||
|
|
// 准备上涨趋势的测试数据
|
||
|
|
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 + (30 - i) * 0.015 + 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 allPrices = testData.map(p => p.price);
|
||
|
|
const maxPrice = Math.max(...allPrices);
|
||
|
|
const minPrice = Math.min(...allPrices);
|
||
|
|
const totalRange = maxPrice - minPrice;
|
||
|
|
|
||
|
|
const recent10Prices = testData.slice(-10).map(p => p.price);
|
||
|
|
const currentPrice = recent10Prices[recent10Prices.length - 1];
|
||
|
|
const first10Price = recent10Prices[0];
|
||
|
|
const change10 = currentPrice - first10Price;
|
||
|
|
|
||
|
|
console.log(`测试数据特征:`);
|
||
|
|
console.log(` 当前价格: ${currentPrice.toFixed(3)} 元`);
|
||
|
|
console.log(` 30分钟总价差: ${totalRange.toFixed(3)} 元 (${minPrice.toFixed(3)} → ${maxPrice.toFixed(3)})`);
|
||
|
|
console.log(` 近10分钟变化: ${change10 > 0 ? '+' : ''}${change10.toFixed(3)} 元`);
|
||
|
|
console.log(`\n预期:LLM 应判定为 uptrend 并说明上涨幅度\n`);
|
||
|
|
|
||
|
|
console.log('开始 LLM 分析...\n');
|
||
|
|
|
||
|
|
try {
|
||
|
|
const result = await llmClient.analyzeTrend(testData);
|
||
|
|
|
||
|
|
console.log('========================================');
|
||
|
|
console.log('分析结果');
|
||
|
|
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');
|
||
|
|
|
||
|
|
// 验证是否包含了所需信息
|
||
|
|
const hasCurrentPrice = result.summary.includes('元');
|
||
|
|
const hasTotalRange = result.summary.match(/总价差.*?\d+\.?\d*\s*元/);
|
||
|
|
const hasChangeAmount = result.trend === 'uptrend' &&
|
||
|
|
(result.summary.includes('上涨') || result.summary.includes('涨') ||
|
||
|
|
result.summary.includes('增加') || result.summary.includes('升'));
|
||
|
|
|
||
|
|
console.log('验证结果:');
|
||
|
|
console.log(` ✅ 包含单位"元": ${hasCurrentPrice}`);
|
||
|
|
console.log(` ✅ 包含总价差: ${hasTotalRange ? '是' : '否'}`);
|
||
|
|
console.log(` ✅ 说明上涨幅度: ${hasChangeAmount}`);
|
||
|
|
|
||
|
|
if (hasCurrentPrice && hasTotalRange) {
|
||
|
|
console.log('\n✅ 测试通过!LLM 正确包含了价格信息');
|
||
|
|
} else {
|
||
|
|
console.log('\n⚠️ 部分信息缺失,请检查summary');
|
||
|
|
}
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.error('\n❌ 测试失败:');
|
||
|
|
console.error(error);
|
||
|
|
process.exit(1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
testUptrend();
|