feat: 实现LLM趋势告警历史上下文判断功能
新增功能: - 在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>
This commit is contained in:
commit
ccf523543a
52 files changed
+7763
No files matched your search
@@ -0,0 +1,115 @@
|
||||
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();
|
||||
Reference in new issue
Block a user