Files
rongtongjin/test-llm-history-context.ts
T
hughhuangandClaude Sonnet 4.5 ccf523543a 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>
2026-01-15 19:37:28 +08:00

91 lines
3.3 KiB
TypeScript

/**
* 测试 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('========================================');