Repository navigation
Expand file tree
/
Copy pathfull-chatbot.ts
More file actions
558 lines (474 loc) · 16.8 KB
/
Copy pathfull-chatbot.ts
File metadata and controls
558 lines (474 loc) · 16.8 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
/**
* Full-featured chatbot example demonstrating all groq-rag capabilities:
* - RAG (Retrieval-Augmented Generation)
* - Web Search
* - URL Fetching
* - Agent with Tools
* - Multi-turn conversation
*
* Usage:
* GROQ_API_KEY=your_key npx tsx full-chatbot.ts
*
* Commands:
* /help - Show all commands
* /add - Add URL to knowledge base
* /search - Web search
* /fetch - Fetch and summarize URL
* /mode - Toggle agent/RAG mode
* /quit - Exit
*/
import 'dotenv/config';
import GroqRAG from 'groq-rag';
import * as readline from 'readline';
interface Document {
url?: string;
source?: string;
title?: string;
addedAt: string;
}
interface Message {
role: 'user' | 'assistant' | 'system';
content: string;
}
// Initialize GroqRAG client
const client = new GroqRAG({
apiKey: process.env.GROQ_API_KEY,
});
// Initialize RAG
await client.initRAG({
embedding: { provider: 'groq' },
vectorStore: { provider: 'memory' },
});
// Create agent with all built-in tools
const agent = await client.createAgentWithBuiltins({
name: 'SuperAssistant',
model: 'llama-3.3-70b-versatile',
systemPrompt: `You are a powerful AI assistant with full capabilities:
ABILITIES:
1. RAG Knowledge - Search and retrieve from your knowledge base
2. Web Search - Search the internet for current information
3. URL Fetching - Read and analyze any webpage
4. Calculator - Perform mathematical calculations
BEHAVIOR:
- Be helpful, accurate, and thorough
- Use your tools proactively when needed
- Cite sources when using web search or fetched URLs
- If you don't know something, search for it
Always provide the best possible answer using all available resources.`,
verbose: false,
});
// Store documents info
let documents: Document[] = [];
// Chat history
let chatHistory: Message[] = [];
// Mode
let useAgentMode = true;
// Custom system prompt
let customSystemPrompt = `You are a helpful AI assistant. Use the provided context to answer questions accurately.
If you don't know something, say so honestly. Always be helpful and concise.`;
// Context - additional context provided by user
let userContext = '';
// Readline interface
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
// Display banner
function showBanner(): void {
console.log('\n╔══════════════════════════════════════════════════════════╗');
console.log('║ GROQ-RAG FULL CHATBOT ║');
console.log('║ ║');
console.log('║ CAPABILITIES: ║');
console.log('║ • RAG Knowledge Base • Web Search (DuckDuckGo) ║');
console.log('║ • URL Fetching • Agent with Tools ║');
console.log('║ • Custom Prompts • Context Management ║');
console.log('║ • Multi-turn Memory • Calculator ║');
console.log('║ ║');
console.log('╠══════════════════════════════════════════════════════════╣');
console.log('║ COMMANDS: /help for full list ║');
console.log('╚══════════════════════════════════════════════════════════╝\n');
}
// Show help
function showHelp(): void {
console.log(`
╔═══════════════════════════════════════════════════════════════╗
║ COMMANDS ║
╠═══════════════════════════════════════════════════════════════╣
║ KNOWLEDGE BASE: ║
║ /add <url> Add webpage content to knowledge base ║
║ /addtext Add custom text (multi-line input) ║
║ /docs List all documents in knowledge base ║
║ ║
║ WEB TOOLS: ║
║ /search <query> Search the web for information ║
║ /fetch <url> Fetch URL and summarize content ║
║ ║
║ CUSTOMIZATION: ║
║ /prompt Set custom system prompt ║
║ /context Set additional context for responses ║
║ /showprompt Display current system prompt ║
║ /showcontext Display current context ║
║ /resetprompt Reset to default system prompt ║
║ ║
║ CHAT CONTROL: ║
║ /mode Toggle between Agent and RAG mode ║
║ /clear Clear conversation history ║
║ /history Show conversation history ║
║ ║
║ OTHER: ║
║ /help Show this help ║
║ /quit Exit the chatbot ║
╚═══════════════════════════════════════════════════════════════╝
`);
}
// Add URL to knowledge base
async function addURL(url: string): Promise<void> {
try {
console.log(`\n📥 Fetching: ${url}...`);
// Use chat.withUrl to get content and add to RAG
const response = await client.chat.withUrl({
messages: [{ role: 'user', content: 'Extract the main content from this page as plain text.' }],
url,
});
const content = response.content;
await client.rag.addDocument(content, {
source: url,
title: response.source?.title || url,
addedAt: new Date().toISOString(),
});
documents.push({ url, title: response.source?.title, addedAt: new Date().toISOString() });
console.log(`✅ Added to knowledge base! (${content.length} chars)\n`);
} catch (error) {
console.error(`❌ Failed: ${(error as Error).message}\n`);
}
}
// Add custom text
async function addCustomText(): Promise<void> {
console.log('\n📝 Enter your text (type "END" on a new line to finish):');
let text = '';
const collectText = (): Promise<string> => {
return new Promise((resolve) => {
const lineHandler = (line: string): void => {
if (line.trim() === 'END') {
rl.removeListener('line', lineHandler);
resolve(text);
} else {
text += line + '\n';
}
};
rl.on('line', lineHandler);
});
};
const content = await collectText();
if (content.trim()) {
await client.rag.addDocument(content, {
source: 'user-input',
addedAt: new Date().toISOString(),
});
documents.push({ source: 'Custom Text', addedAt: new Date().toISOString() });
console.log(`✅ Added custom text to knowledge base!\n`);
} else {
console.log('❌ No text provided.\n');
}
}
// Web search
async function webSearch(query: string): Promise<void> {
try {
console.log(`\n🔍 Searching: "${query}"...`);
const response = await client.chat.withWebSearch({
messages: [{ role: 'user', content: query }],
maxResults: 5,
});
console.log(`\n📋 Answer:\n${response.content}\n`);
if (response.sources && response.sources.length > 0) {
console.log('Sources:');
response.sources.forEach((s, i) => {
console.log(` ${i + 1}. ${s.title || 'No title'}`);
console.log(` ${s.url}\n`);
});
}
} catch (error) {
console.error(`❌ Search failed: ${(error as Error).message}\n`);
}
}
// Fetch URL and summarize
async function fetchURL(url: string): Promise<void> {
try {
console.log(`\n🌐 Fetching: ${url}...`);
const response = await client.chat.withUrl({
messages: [{ role: 'user', content: 'Summarize this page in detail.' }],
url,
});
console.log('\n--- Summary ---\n');
console.log(response.content);
console.log('\n--- End ---\n');
if (response.source?.title) {
console.log(`Page title: ${response.source.title}\n`);
}
} catch (error) {
console.error(`❌ Fetch failed: ${(error as Error).message}\n`);
}
}
// List documents
function listDocs(): void {
if (documents.length === 0) {
console.log('\n📚 Knowledge base is empty. Use /add <url> to add content.\n');
return;
}
console.log('\n📚 Knowledge Base Documents:\n');
documents.forEach((doc, i) => {
console.log(`${i + 1}. ${doc.url || doc.source}`);
if (doc.title) console.log(` Title: ${doc.title}`);
console.log(` Added: ${doc.addedAt}\n`);
});
}
// Show history
function showHistory(): void {
if (chatHistory.length === 0) {
console.log('\n📜 No conversation history yet.\n');
return;
}
console.log('\n📜 Conversation History:\n');
chatHistory.forEach((msg) => {
const role = msg.role === 'user' ? 'You' : 'Bot';
const preview = msg.content.length > 100 ? msg.content.substring(0, 100) + '...' : msg.content;
console.log(`${role}: ${preview}`);
});
console.log('');
}
// Set custom prompt
async function setCustomPrompt(): Promise<void> {
console.log('\n📝 Enter your custom system prompt (type "END" on a new line to finish):');
console.log(' This defines how the AI should behave.\n');
let text = '';
const collectText = (): Promise<string> => {
return new Promise((resolve) => {
const lineHandler = (line: string): void => {
if (line.trim() === 'END') {
rl.removeListener('line', lineHandler);
resolve(text);
} else {
text += line + '\n';
}
};
rl.on('line', lineHandler);
});
};
const content = await collectText();
if (content.trim()) {
customSystemPrompt = content.trim();
console.log(`✅ Custom prompt set!\n`);
} else {
console.log('❌ No prompt provided. Keeping current prompt.\n');
}
}
// Set context
async function setContext(): Promise<void> {
console.log('\n📝 Enter context information (type "END" on a new line to finish):');
console.log(' This will be included in every response.\n');
let text = '';
const collectText = (): Promise<string> => {
return new Promise((resolve) => {
const lineHandler = (line: string): void => {
if (line.trim() === 'END') {
rl.removeListener('line', lineHandler);
resolve(text);
} else {
text += line + '\n';
}
};
rl.on('line', lineHandler);
});
};
const content = await collectText();
if (content.trim()) {
userContext = content.trim();
// Add context to RAG knowledge base
await client.rag.addDocument(userContext, {
source: 'user-context',
type: 'context',
addedAt: new Date().toISOString(),
});
console.log(`✅ Context set and added to knowledge base!\n`);
} else {
console.log('❌ No context provided.\n');
}
}
// Show current prompt
function showCurrentPrompt(): void {
console.log('\n📋 Current System Prompt:\n');
console.log('─'.repeat(50));
console.log(customSystemPrompt);
console.log('─'.repeat(50));
console.log('');
}
// Show current context
function showCurrentContext(): void {
if (!userContext) {
console.log('\n📋 No custom context set.\n');
return;
}
console.log('\n📋 Current Context:\n');
console.log('─'.repeat(50));
console.log(userContext);
console.log('─'.repeat(50));
console.log('');
}
// Reset prompt to default
function resetPrompt(): void {
customSystemPrompt = `You are a helpful AI assistant. Use the provided context to answer questions accurately.
If you don't know something, say so honestly. Always be helpful and concise.`;
console.log('\n✅ System prompt reset to default.\n');
}
// Chat with Agent (uses tools automatically)
async function chatWithAgent(userInput: string): Promise<string> {
try {
console.log('\n🤖 Thinking...');
const result = await agent.run(userInput);
if (result.toolCalls && result.toolCalls.length > 0) {
console.log(' Tools used:', result.toolCalls.map(t => t.name).join(', '));
}
return result.output;
} catch (error) {
console.error('Error:', (error as Error).message);
return 'Sorry, I encountered an error. Please try again.';
}
}
// Chat with RAG
async function chatWithRAG(userInput: string): Promise<string> {
try {
chatHistory.push({ role: 'user', content: userInput });
console.log('\n🤖 Thinking...');
const response = await client.chat.withRAG({
messages: chatHistory,
topK: 5,
systemPrompt: customSystemPrompt,
});
const answer = response.content;
chatHistory.push({ role: 'assistant', content: answer });
if (response.sources && response.sources.length > 0) {
console.log(` (Used ${response.sources.length} knowledge sources)`);
}
return answer;
} catch (error) {
// Fallback to basic chat if RAG fails
try {
const response = await client.complete({
model: 'llama-3.3-70b-versatile',
messages: [
{ role: 'system', content: customSystemPrompt },
...chatHistory,
],
});
const answer = response.choices[0].message.content;
chatHistory.push({ role: 'assistant', content: answer });
return answer;
} catch (e) {
console.error('Error:', (e as Error).message);
return 'Sorry, I encountered an error. Please try again.';
}
}
}
// Main chat function
async function chat(userInput: string): Promise<string> {
if (useAgentMode) {
return await chatWithAgent(userInput);
} else {
return await chatWithRAG(userInput);
}
}
// Process commands
async function processCommand(input: string): Promise<boolean> {
const parts = input.trim().split(' ');
const cmd = parts[0].toLowerCase();
const args = parts.slice(1).join(' ');
switch (cmd) {
case '/help':
showHelp();
return true;
case '/add':
if (args) await addURL(args);
else console.log('Usage: /add <url>\n');
return true;
case '/addtext':
await addCustomText();
return true;
case '/search':
if (args) await webSearch(args);
else console.log('Usage: /search <query>\n');
return true;
case '/fetch':
if (args) await fetchURL(args);
else console.log('Usage: /fetch <url>\n');
return true;
case '/docs':
listDocs();
return true;
case '/prompt':
await setCustomPrompt();
return true;
case '/context':
await setContext();
return true;
case '/showprompt':
showCurrentPrompt();
return true;
case '/showcontext':
showCurrentContext();
return true;
case '/resetprompt':
resetPrompt();
return true;
case '/clear':
chatHistory = [];
console.log('\n🗑️ Chat history cleared!\n');
return true;
case '/history':
showHistory();
return true;
case '/mode':
useAgentMode = !useAgentMode;
console.log(`\n🔄 Switched to ${useAgentMode ? 'Agent' : 'RAG'} mode`);
if (useAgentMode) {
console.log(' Agent: Auto uses web search, URL fetch, calculator, RAG\n');
} else {
console.log(' RAG: Uses knowledge base for context\n');
}
return true;
case '/quit':
case '/exit':
console.log('\n👋 Goodbye!\n');
rl.close();
process.exit(0);
default:
return false;
}
}
// Main prompt loop
async function prompt(): Promise<void> {
const modeIcon = useAgentMode ? '🤖' : '💬';
rl.question(`${modeIcon} You: `, async (input) => {
const trimmed = input.trim();
if (!trimmed) {
prompt();
return;
}
// Check if it's a command
if (trimmed.startsWith('/')) {
const handled = await processCommand(trimmed);
if (handled) {
prompt();
return;
}
}
// Regular chat
const response = await chat(trimmed);
console.log(`\n🤖 Assistant: ${response}\n`);
prompt();
});
}
// Start
showBanner();
console.log(`Mode: ${useAgentMode ? 'Agent (all tools enabled)' : 'RAG Chat'}`);
console.log('Type /help for all commands\n');
prompt();