-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrag-agent.js
More file actions
611 lines (520 loc) · 18.9 KB
/
rag-agent.js
File metadata and controls
611 lines (520 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
const OpenAI = require('openai');
const fs = require('fs');
// 检查环境变量
const isTestMode = process.env.OPENAI_API_KEY === 'test_key_for_local_development';
if (!process.env.OPENAI_API_KEY && !isTestMode) {
console.error('Error: OPENAI_API_KEY environment variable is required');
process.exit(1);
}
// 初始化OpenAI客户端
const openai = isTestMode ? null : new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});
// 加载产品数据
let productsData;
try {
// 优先加载扩展数据,然后是增强数据,最后是原始数据
if (fs.existsSync('./expanded-products.json')) {
productsData = JSON.parse(fs.readFileSync('./expanded-products.json', 'utf8'));
console.log('Loaded expanded product data from expanded-products.json');
} else if (fs.existsSync('./sample-products.json')) {
productsData = JSON.parse(fs.readFileSync('./sample-products.json', 'utf8'));
console.log('Loaded enhanced product data from sample-products.json');
} else {
productsData = JSON.parse(fs.readFileSync('./products.json', 'utf8'));
console.log('Loaded original product data from products.json');
}
} catch (error) {
console.error('Error loading product data:', error.message);
process.exit(1);
}
// 创建产品知识库索引
function createProductIndex() {
const index = [];
productsData.forEach(category => {
category.products.forEach(product => {
// 为每个产品创建可搜索的文本内容
const searchableContent = [
product.name,
product.fullName || product.name,
product.description || '',
...(product.keyFeatures || []),
JSON.stringify(product.technicalSpecs || product.specs || {}),
JSON.stringify(product.useCase || {}),
category.category
].join(' ').toLowerCase();
index.push({
id: product.id || product.name,
product: product,
category: category.category,
content: searchableContent,
keywords: extractKeywords(searchableContent)
});
});
});
return index;
}
// 提取关键词(支持中英文)
function extractKeywords(text) {
const keywords = new Set();
const words = text.toLowerCase().match(/\b\w+\b/g) || [];
// 网络相关关键词(中英文)
const networkTerms = ['wifi', 'router', 'mesh', 'deco', 'archer', 'extender', 'access', 'point',
'gigabit', 'ethernet', 'wireless', 'range', 'coverage', 'speed', 'bandwidth',
'路由', '路由器', '网络', '无线', '网状', '覆盖', '速度', '带宽', '以太网'];
// 技术规格关键词
const techTerms = ['ghz', 'mbps', 'gbps', 'ax', 'be', 'ac', '802.11', 'dual', 'tri', 'quad', 'band',
'双频', '三频', '四频', '频段', '千兆', '万兆'];
// 使用场景关键词
const usageTerms = ['gaming', 'streaming', 'home', 'office', 'smart', 'iot', 'video', 'large', 'small',
'游戏', '直播', '家庭', '办公', '智能', '大户型', '小户型', '别墅', '公寓'];
// 产品名称关键词
const productTerms = ['deco', 'archer', 'be65', 'be95', 'ax73', 're705x'];
words.forEach(word => {
if (word.length > 1) {
keywords.add(word);
// 添加相关术语
if (networkTerms.some(term => word.includes(term) || term.includes(word))) {
keywords.add('networking');
keywords.add('router');
keywords.add('wifi');
}
if (techTerms.some(term => word.includes(term) || term.includes(word))) {
keywords.add('technical');
keywords.add('speed');
}
if (usageTerms.some(term => word.includes(term) || term.includes(word))) {
keywords.add('usage');
keywords.add('home');
}
if (productTerms.some(term => word.includes(term) || term.includes(word))) {
keywords.add('product');
}
}
});
// 处理中文短语
const chineseText = text;
if (chineseText.includes('路由') || chineseText.includes('网络')) {
keywords.add('router');
keywords.add('networking');
keywords.add('wifi');
}
if (chineseText.includes('游戏')) {
keywords.add('gaming');
keywords.add('performance');
}
if (chineseText.includes('大户型') || chineseText.includes('别墅')) {
keywords.add('large');
keywords.add('coverage');
keywords.add('home');
}
if (chineseText.includes('智能家居') || chineseText.includes('智能')) {
keywords.add('smart');
keywords.add('iot');
}
return Array.from(keywords);
}
// 创建索引
const productIndex = createProductIndex();
// 意图识别(改进版)
async function classifyIntent(userInput) {
if (isTestMode) {
return classifyIntentLocal(userInput);
}
const prompt = `You are an expert AI assistant for TP-Link networking products.
Analyze the user's query and classify it into ONE of these categories:
- product_recommendation: User wants product recommendations or asking "what should I buy"
- product_comparison: User wants to compare specific products (mentions 2+ product names/models)
- spec_query: User asks about technical specifications, speeds, ports, coverage, etc.
- feature_query: User asks about features, capabilities, or "what can it do"
- troubleshooting: User has technical problems or setup issues
- unknown: Cannot determine clear intent
User query: "${userInput}"
Respond with ONLY the category name, nothing else.`;
try {
const response = await openai.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: prompt }],
max_tokens: 20,
temperature: 0.1
});
return response.choices[0].message.content.trim();
} catch (error) {
console.error('Intent classification error:', error);
return classifyIntentLocal(userInput);
}
}
// 本地意图识别
function classifyIntentLocal(userInput) {
const input = userInput.toLowerCase();
// 检查产品对比
const productMentions = input.match(/\b(be\d+|ax\d+|deco|archer|re\d+)\b/g) || [];
if ((input.includes('compare') || input.includes('vs') || input.includes('versus') || input.includes('difference'))
&& productMentions.length >= 2) {
return 'product_comparison';
}
// 检查规格查询
if (input.match(/\b(speed|port|coverage|range|ghz|mbps|gbps|specification|spec|technical|ethernet|wifi|band)\b/)) {
return 'spec_query';
}
// 检查功能查询
if (input.match(/\b(feature|function|capability|can it|does it|support|compatible)\b/)) {
return 'feature_query';
}
// 检查推荐查询
if (input.match(/\b(recommend|need|best|good|suggest|looking for|want|buy|choose)\b/)) {
return 'product_recommendation';
}
// 检查问题排查
if (input.match(/\b(problem|issue|not working|setup|install|connect|troubleshoot|help)\b/)) {
return 'troubleshooting';
}
return 'unknown';
}
// RAG检索相关产品(支持中英文)
function retrieveRelevantProducts(query, limit = 5) {
const queryWords = query.toLowerCase().match(/\b\w+\b/g) || [];
console.log('Query words:', queryWords);
const scores = productIndex.map(item => {
let score = 0;
// 基础关键词匹配
queryWords.forEach(word => {
if (item.content.includes(word)) {
score += 1;
}
if (item.keywords.includes(word)) {
score += 2;
}
});
// 产品名称匹配加权
queryWords.forEach(word => {
if (item.product.name.toLowerCase().includes(word)) {
score += 5;
}
if (item.product.fullName?.toLowerCase().includes(word)) {
score += 3;
}
});
// 分类匹配
if (queryWords.some(word => item.category.toLowerCase().includes(word))) {
score += 2;
}
// 中文关键词匹配
const queryText = query.toLowerCase();
if (queryText.includes('路由') || queryText.includes('网络')) {
if (item.product.name.toLowerCase().includes('deco') ||
item.product.name.toLowerCase().includes('archer') ||
item.category.toLowerCase().includes('router') ||
item.category.toLowerCase().includes('mesh')) {
score += 3;
}
}
if (queryText.includes('大户型') || queryText.includes('别墅') || queryText.includes('large')) {
if (item.product.useCase?.homeSize?.includes('large') ||
item.product.technicalSpecs?.coverage?.includes('sq ft') ||
(item.product.technicalSpecs?.coverage && parseInt(item.product.technicalSpecs.coverage) > 5000)) {
score += 4;
}
}
if (queryText.includes('游戏') || queryText.includes('gaming')) {
if (item.product.useCase?.activities?.includes('Gaming') ||
item.product.keyFeatures?.some(f => f.toLowerCase().includes('gaming'))) {
score += 3;
}
}
if (queryText.includes('智能') || queryText.includes('smart')) {
if (item.product.useCase?.activities?.includes('Smart home') ||
item.product.keyFeatures?.some(f => f.toLowerCase().includes('smart'))) {
score += 3;
}
}
// 通用路由器查询
if ((queryText.includes('路由') || queryText.includes('router') || queryText.includes('wifi')) && score === 0) {
score += 1; // 给所有产品一个基础分数
}
return { ...item, score };
});
const results = scores
.filter(item => item.score > 0)
.sort((a, b) => b.score - a.score)
.slice(0, limit);
console.log('Retrieval results:', results.map(r => ({ name: r.product.name, score: r.score })));
return results;
}
// RAG增强的产品推荐
async function ragProductRecommendation(query) {
const relevantProducts = retrieveRelevantProducts(query, 3);
if (relevantProducts.length === 0) {
return {
error: "I couldn't find any products matching your requirements. Please try rephrasing your query."
};
}
if (isTestMode) {
return relevantProducts.map(item => ({
name: item.product.name,
category: item.category,
description: item.product.description,
url: item.product.url,
score: item.score
}));
}
// 使用GPT生成个性化推荐
const productContext = relevantProducts.map(item =>
`${item.product.name}: ${item.product.description}
Key Features: ${(item.product.keyFeatures || []).join(', ')}
Use Case: ${JSON.stringify(item.product.useCase || {})}
Price: ${item.product.price || 'Contact for pricing'}`
).join('\n\n');
const prompt = `You are a TP-Link product expert. Based on the user's query and the following product information, provide personalized recommendations.
User Query: "${query}"
Available Products:
${productContext}
Provide a helpful recommendation in JSON format with an array of products, each containing:
- name: product name
- reason: why this product fits their needs (2-3 sentences)
- highlights: key features that match their requirements
- category: product category
Focus on matching the user's specific needs and use case.`;
try {
const response = await openai.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: prompt }],
max_tokens: 800,
temperature: 0.3
});
const aiResponse = response.choices[0].message.content;
// 尝试解析JSON,如果失败则返回原始推荐
try {
const parsedResponse = JSON.parse(aiResponse);
return parsedResponse;
} catch {
return relevantProducts.map(item => ({
name: item.product.name,
category: item.category,
description: item.product.description,
url: item.product.url,
reason: aiResponse.slice(0, 200) + '...',
score: item.score
}));
}
} catch (error) {
console.error('RAG recommendation error:', error);
return relevantProducts.map(item => ({
name: item.product.name,
category: item.category,
description: item.product.description,
url: item.product.url,
score: item.score
}));
}
}
// RAG增强的产品对比
async function ragProductComparison(query) {
const productNames = query.match(/\b(be\d+|ax\d+|deco|archer|re\d+)\b/gi) || [];
if (productNames.length < 2) {
return { error: 'Please specify at least 2 products to compare' };
}
// 查找相关产品
const foundProducts = [];
productNames.forEach(name => {
const product = productIndex.find(item =>
item.product.name.toLowerCase().includes(name.toLowerCase()) ||
item.product.fullName?.toLowerCase().includes(name.toLowerCase())
);
if (product) {
foundProducts.push(product.product);
}
});
if (foundProducts.length < 2) {
return { error: 'Could not find enough products to compare' };
}
// 创建对比表
const comparison = {
products: foundProducts.map(p => ({
name: p.name,
fullName: p.fullName,
category: p.category,
url: p.url,
price: p.price
})),
specifications: {}
};
// 对比关键规格
const keySpecs = ['wifiSpeeds', 'coverage', 'capacity', 'ethernetPorts', 'processor'];
keySpecs.forEach(spec => {
comparison.specifications[spec] = {};
foundProducts.forEach(product => {
const specValue = product.technicalSpecs?.[spec] || product.specs?.[spec] || 'N/A';
comparison.specifications[spec][product.name] = specValue;
});
});
return comparison;
}
// RAG增强的规格查询
async function ragSpecQuery(query) {
const relevantProducts = retrieveRelevantProducts(query, 1);
if (relevantProducts.length === 0) {
return { error: 'Product not found' };
}
const product = relevantProducts[0].product;
const specs = product.technicalSpecs || product.specs || {};
// 检测用户询问的具体规格
const specKeywords = {
'speed': 'wifiSpeeds',
'port': 'ethernetPorts',
'coverage': 'coverage',
'range': 'coverage',
'capacity': 'capacity',
'processor': 'processor',
'antenna': 'antennas'
};
const queryLower = query.toLowerCase();
let requestedSpec = null;
for (const [keyword, specKey] of Object.entries(specKeywords)) {
if (queryLower.includes(keyword)) {
requestedSpec = specKey;
break;
}
}
if (requestedSpec && specs[requestedSpec]) {
return {
product: product.name,
specification: requestedSpec,
value: specs[requestedSpec],
fullSpecs: false
};
}
return {
product: product.name,
specifications: specs,
fullSpecs: true
};
}
// 生成智能下一个问题建议
function generateNextQuestions(intent, result, userInput) {
const suggestions = [];
switch (intent) {
case 'product_recommendation':
if (Array.isArray(result) || (result.recommendations && result.recommendations.length > 0)) {
const products = result.recommendations || result;
if (products.length > 0) {
const topProduct = products[0];
suggestions.push(
`What are the technical specs of ${topProduct.name}?`,
`How does ${topProduct.name} compare to other models?`,
`What's the coverage area of ${topProduct.name}?`,
`What's the price of ${topProduct.name}?`
);
}
}
// 通用推荐后续问题
suggestions.push(
"What's the difference between mesh and traditional routers?",
"Which model is best for gaming?",
"What WiFi speed do I need?"
);
break;
case 'product_comparison':
if (result.products && result.products.length > 0) {
suggestions.push(
`Which one offers better value for money?`,
`What are the key differences in features?`,
`Which is better for large homes?`,
`What about gaming performance?`
);
}
break;
case 'spec_query':
if (result.product) {
suggestions.push(
`What other features does ${result.product} have?`,
`How does ${result.product} compare to similar models?`,
`What's the best use case for ${result.product}?`,
`What accessories work with ${result.product}?`
);
}
break;
case 'feature_query':
suggestions.push(
"What's the setup process like?",
"How reliable is the performance?",
"What about security features?",
"Does it support mesh networking?"
);
break;
default:
// 默认建议
suggestions.push(
"I need a router for gaming",
"What's the best mesh system for large homes?",
"Compare WiFi 6 vs WiFi 7",
"What's the difference between Deco and Archer?"
);
}
// 根据查询历史优化建议(简单实现)
const queryLower = userInput.toLowerCase();
if (!queryLower.includes('gaming') && !suggestions.some(s => s.toLowerCase().includes('gaming'))) {
suggestions.splice(1, 0, "What's the best router for gaming?");
}
if (!queryLower.includes('mesh') && !suggestions.some(s => s.toLowerCase().includes('mesh'))) {
suggestions.splice(2, 0, "Should I choose mesh or traditional router?");
}
// 返回前4个建议
return suggestions.slice(0, 4);
}
// 主要处理函数
async function processUserQuery(userInput) {
try {
console.log('Processing query with RAG:', userInput);
// 1. 意图识别
const intent = await classifyIntent(userInput);
console.log('Detected intent:', intent);
// 2. 根据意图使用RAG处理
let result;
switch (intent) {
case 'product_recommendation':
result = await ragProductRecommendation(userInput);
break;
case 'product_comparison':
result = await ragProductComparison(userInput);
break;
case 'spec_query':
result = await ragSpecQuery(userInput);
break;
case 'feature_query':
// 使用推荐系统但专注于功能
const featureQuery = userInput + ' features capabilities';
result = await ragProductRecommendation(featureQuery);
break;
case 'troubleshooting':
result = {
message: "For technical support, please visit TP-Link support at https://www.tp-link.com/support/ or contact customer service.",
supportUrl: "https://www.tp-link.com/support/"
};
break;
default:
result = { error: 'I could not understand your query. Please try asking about TP-Link products, comparisons, or specifications.' };
}
// 3. 生成下一个问题建议
const nextQuestions = generateNextQuestions(intent, result, userInput);
return {
intent: intent,
result: result,
nextQuestions: nextQuestions,
enhanced: true // 标记这是RAG增强的结果
};
} catch (error) {
console.error('RAG processing error:', error);
return {
intent: 'error',
result: { error: 'An error occurred while processing your request.' }
};
}
}
module.exports = {
processUserQuery,
ragProductRecommendation,
ragProductComparison,
ragSpecQuery,
classifyIntent
};