新增功能: - 在LLM趋势分析中集成历史告警上下文,提供最近3次已触发告警的记录 - LLM自动判断本次分析是否与历史告警相似,避免重复通知 - 新增shouldNotify和notifyReason字段,支持智能通知决策 技术改进: - 扩展TrendAnalysisResponse类型,增加通知决策字段 - 数据库服务新增getRecentTriggeredAnalyses()方法 - LLM客户端新增analyzeTrendWithHistory()和相关方法 - 趋势分析服务集成历史上下文获取和通知决策逻辑 测试验证: - 创建test-db-history-method.ts验证数据库方法 - 创建test-llm-history-context.ts测试历史上下文功能 - 通过TypeScript类型检查和构建验证 这个功能可以有效减少重复告警,提升告警质量,避免通知疲劳。 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
99 lines
3.1 KiB
TypeScript
99 lines
3.1 KiB
TypeScript
import { LLMClient } from './src/utils/llm-client.js';
|
|
import type { PriceDataPoint } from './src/types/index.js';
|
|
|
|
/**
|
|
* 测试 LLM 趋势分析功能
|
|
*/
|
|
async function testLLMTrend() {
|
|
console.log('========================================');
|
|
console.log('LLM 趋势分析测试');
|
|
console.log('========================================\n');
|
|
|
|
// 检查环境变量
|
|
const apiKey = process.env.LLM_API_KEY;
|
|
if (!apiKey) {
|
|
console.error('❌ 错误:未配置 LLM_API_KEY 环境变量');
|
|
console.log('请在 .env 文件中设置 LLM_API_KEY');
|
|
process.exit(1);
|
|
}
|
|
|
|
// 创建 LLM 客户端
|
|
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 // 智谱AI推理模型需要更长时间
|
|
};
|
|
|
|
console.log('配置信息:');
|
|
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 {
|
|
// 调用 LLM 分析
|
|
console.log('开始 LLM 分析...\n');
|
|
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');
|
|
|
|
// 显示完整的 JSON 结果
|
|
console.log('完整的 JSON 结果:');
|
|
console.log(JSON.stringify(result, null, 2));
|
|
console.log('');
|
|
|
|
console.log('✅ 测试成功!');
|
|
} catch (error) {
|
|
console.error('\n❌ 测试失败:');
|
|
console.error(error);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
// 运行测试
|
|
testLLMTrend();
|