Files

90 lines
3.3 KiB
TypeScript
Raw Permalink Normal View History

/**
* 测试 LLM 历史上下文功能
*/
import { DatabaseService } from './src/services/database.js';
import { LLMClient } from './src/utils/llm-client.js';
import type { PriceDataPoint, TrendAnalysisRecord } from './src/types/index.js';
// 初始化服务
const db = new DatabaseService();
await db.init();
// 创建 LLM 客户端
const llmClient = new LLMClient({
apiBase: process.env.LLM_API_BASE || 'https://api.openai.com/v1',
apiKey: process.env.LLM_API_KEY || '',
model: process.env.LLM_MODEL || 'gpt-4o-mini'
});
console.log('========================================');
console.log('测试 LLM 历史上下文功能');
console.log('========================================\n');
// 测试场景 1: 无历史告警记录
console.log('\n### 测试场景 1: 无历史告警记录 ###');
{
const dataPoints: PriceDataPoint[] = db.getRecentPricePoints(30);
console.log(`获取到 ${dataPoints.length} 个价格数据点`);
const historyAlerts: TrendAnalysisRecord[] = [];
console.log('历史告警记录: 0 条');
const result = await llmClient.analyzeTrendWithHistory(dataPoints, historyAlerts);
console.log('\n分析结果:');
console.log(` 趋势: ${result.trend}`);
console.log(` 置信度: ${(result.confidence * 100).toFixed(0)}%`);
console.log(` 风险: ${result.risk}`);
console.log(` 通知决策: ${result.shouldNotify ? '发送' : '跳过'}`);
console.log(` 决策原因: ${result.notifyReason}`);
console.log(` 总结: ${result.summary}`);
console.log(` 信号: ${result.signals.join('; ')}`);
}
// 测试场景 2: 有历史告警记录
console.log('\n\n### 测试场景 2: 有历史告警记录 ###');
{
const dataPoints: PriceDataPoint[] = db.getRecentPricePoints(30);
console.log(`获取到 ${dataPoints.length} 个价格数据点`);
const historyAlerts = db.getRecentTriggeredAnalyses(3);
console.log(`历史告警记录: ${historyAlerts.length} 条`);
if (historyAlerts.length > 0) {
console.log('\n历史告警详情:');
historyAlerts.forEach((alert, index) => {
console.log(` [${index + 1}] ${alert.trend} - ${alert.summary}`);
});
}
const result = await llmClient.analyzeTrendWithHistory(dataPoints, historyAlerts);
console.log('\n分析结果:');
console.log(` 趋势: ${result.trend}`);
console.log(` 置信度: ${(result.confidence * 100).toFixed(0)}%`);
console.log(` 风险: ${result.risk}`);
console.log(` 通知决策: ${result.shouldNotify ? '发送' : '跳过'}`);
console.log(` 决策原因: ${result.notifyReason}`);
console.log(` 总结: ${result.summary}`);
console.log(` 信号: ${result.signals.join('; ')}`);
}
// 测试场景 3: 测试数据库方法
console.log('\n\n### 测试场景 3: 测试数据库方法 ###');
{
const recentAlerts = db.getRecentTriggeredAnalyses(3);
console.log(`获取最近 3 次已触发告警的记录:`);
console.log(` 总数: ${recentAlerts.length} 条`);
if (recentAlerts.length > 0) {
recentAlerts.forEach((alert, index) => {
const time = new Date(alert.timestamp).toLocaleString('zh-CN');
console.log(` [${index + 1}] ${time} - ${alert.trend} (${alert.risk})`);
});
} else {
console.log(' 没有已触发告警的记录');
}
}
console.log('\n========================================');
console.log('测试完成');
console.log('========================================');