新增功能: - 在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>
106 lines
3.4 KiB
TypeScript
106 lines
3.4 KiB
TypeScript
import { LLMClient } from './src/utils/llm-client.js';
|
||
import type { PriceDataPoint } from './src/types/index.js';
|
||
|
||
/**
|
||
* 测试小幅波动场景(波动 < 0.2)
|
||
*/
|
||
async function testLowVolatility() {
|
||
console.log('========================================');
|
||
console.log('测试小幅波动场景(波动 < 0.2)');
|
||
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
|
||
};
|
||
|
||
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);
|
||
|
||
// 准备小幅波动的测试数据(波动幅度约 0.12)
|
||
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.12 之间波动
|
||
const price = basePrice + Math.sin(i * 0.5) * 0.06 + 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 maxPrice = Math.max(...prices);
|
||
const minPrice = Math.min(...prices);
|
||
const range = maxPrice - minPrice;
|
||
|
||
console.log(`测试数据特征:`);
|
||
console.log(` 最近 10 分钟最高价: ${maxPrice.toFixed(3)}`);
|
||
console.log(` 最近 10 分钟最低价: ${minPrice.toFixed(3)}`);
|
||
console.log(` 波动幅度: ${range.toFixed(3)} ${range < 0.2 ? '(< 0.2)' : '(≥ 0.2)'}`);
|
||
console.log(`\n预期:LLM 应判定为 ${range < 0.2 ? 'sideways/uptrend/downtrend(非 volatile)' : 'volatile'}\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 isCorrect = result.trend !== 'volatile' || range > 0.2;
|
||
if (isCorrect) {
|
||
console.log('✅ 测试通过!LLM 正确应用了 0.2 的波动阈值');
|
||
} else {
|
||
console.log('❌ 测试失败!LLM 误判为 volatile,但实际波动幅度 < 0.2');
|
||
}
|
||
|
||
} catch (error) {
|
||
console.error('\n❌ 测试失败:');
|
||
console.error(error);
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
testLowVolatility();
|