groq-rag 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 mithun50
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,456 @@
1
+ # groq-rag
2
+
3
+ [![CI](https://github.com/mithun50/groq-rag/actions/workflows/ci.yml/badge.svg)](https://github.com/mithun50/groq-rag/actions/workflows/ci.yml)
4
+ [![npm version](https://badge.fury.io/js/groq-rag.svg)](https://www.npmjs.com/package/groq-rag)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
+
7
+ Extended Groq SDK with RAG (Retrieval-Augmented Generation), web browsing, and agent capabilities.
8
+
9
+ ## Features
10
+
11
+ - **RAG Support**: Built-in vector store and document retrieval with chunking strategies
12
+ - **Web Fetching**: Fetch and parse web pages to clean markdown
13
+ - **Web Search**: DuckDuckGo (free), Brave Search, and Serper (Google) integration
14
+ - **Tool System**: Extensible tool framework with built-in tools
15
+ - **Agents**: ReAct-style agents with tool use, memory, and streaming
16
+ - **TypeScript**: Full type safety and IntelliSense support
17
+ - **Zero Config**: Works out of the box with sensible defaults
18
+
19
+ ## Installation
20
+
21
+ ```bash
22
+ npm install groq-rag
23
+ ```
24
+
25
+ ## Quick Start
26
+
27
+ ### Basic Chat
28
+
29
+ ```typescript
30
+ import GroqRAG from 'groq-rag';
31
+
32
+ const client = new GroqRAG({
33
+ apiKey: process.env.GROQ_API_KEY,
34
+ });
35
+
36
+ const response = await client.complete({
37
+ model: 'llama-3.3-70b-versatile',
38
+ messages: [
39
+ { role: 'user', content: 'Hello!' },
40
+ ],
41
+ });
42
+
43
+ console.log(response.choices[0].message.content);
44
+ ```
45
+
46
+ ### RAG-Augmented Chat
47
+
48
+ ```typescript
49
+ import GroqRAG from 'groq-rag';
50
+
51
+ const client = new GroqRAG();
52
+
53
+ // Initialize RAG (uses in-memory vector store by default)
54
+ await client.initRAG();
55
+
56
+ // Add documents to the knowledge base
57
+ await client.rag.addDocument('Your document content here...');
58
+ await client.rag.addDocument('Another document...', { source: 'manual.pdf' });
59
+
60
+ // Chat with context retrieval
61
+ const response = await client.chat.withRAG({
62
+ messages: [{ role: 'user', content: 'What does the document say about X?' }],
63
+ topK: 5, // Number of chunks to retrieve
64
+ minScore: 0.5, // Minimum similarity score
65
+ });
66
+
67
+ console.log(response.content);
68
+ console.log('Sources:', response.sources);
69
+ ```
70
+
71
+ ### Web Search Chat
72
+
73
+ ```typescript
74
+ const response = await client.chat.withWebSearch({
75
+ messages: [{ role: 'user', content: 'Latest AI news?' }],
76
+ maxResults: 5,
77
+ });
78
+
79
+ console.log(response.content);
80
+ console.log('Sources:', response.sources);
81
+ ```
82
+
83
+ ### URL Fetching
84
+
85
+ ```typescript
86
+ // Fetch and parse a URL
87
+ const result = await client.web.fetch('https://example.com');
88
+ console.log(result.title);
89
+ console.log(result.markdown);
90
+
91
+ // Chat about a URL's content
92
+ const response = await client.chat.withUrl({
93
+ messages: [{ role: 'user', content: 'Summarize this page' }],
94
+ url: 'https://example.com/article',
95
+ });
96
+ ```
97
+
98
+ ### Agents with Tools
99
+
100
+ ```typescript
101
+ // Create agent with built-in tools
102
+ const agent = await client.createAgentWithBuiltins({
103
+ model: 'llama-3.3-70b-versatile',
104
+ verbose: true,
105
+ });
106
+
107
+ const result = await agent.run('Search for recent AI news and summarize the top 3 stories');
108
+ console.log(result.output);
109
+ console.log('Tools used:', result.toolCalls.map(t => t.name));
110
+ ```
111
+
112
+ ### Streaming Agent
113
+
114
+ ```typescript
115
+ const agent = await client.createAgentWithBuiltins();
116
+
117
+ for await (const event of agent.runStream('Research topic X')) {
118
+ switch (event.type) {
119
+ case 'content':
120
+ process.stdout.write(event.data as string);
121
+ break;
122
+ case 'tool_call':
123
+ console.log('\n[Calling tool...]');
124
+ break;
125
+ case 'tool_result':
126
+ console.log('[Tool completed]');
127
+ break;
128
+ }
129
+ }
130
+ ```
131
+
132
+ ## API Reference
133
+
134
+ ### GroqRAG Client
135
+
136
+ ```typescript
137
+ const client = new GroqRAG({
138
+ apiKey?: string, // Groq API key (defaults to GROQ_API_KEY env var)
139
+ baseURL?: string, // Custom API base URL
140
+ timeout?: number, // Request timeout in milliseconds
141
+ maxRetries?: number, // Max retry attempts (default: 2)
142
+ });
143
+ ```
144
+
145
+ ### RAG Module
146
+
147
+ ```typescript
148
+ // Initialize with custom configuration
149
+ await client.initRAG({
150
+ embedding: {
151
+ provider: 'groq' | 'openai',
152
+ apiKey: 'optional-key',
153
+ model: 'text-embedding-3-small',
154
+ },
155
+ vectorStore: {
156
+ provider: 'memory' | 'chroma',
157
+ connectionString: 'http://localhost:8000',
158
+ indexName: 'my-collection',
159
+ },
160
+ chunking: {
161
+ strategy: 'recursive' | 'fixed' | 'sentence' | 'paragraph',
162
+ chunkSize: 1000,
163
+ chunkOverlap: 200,
164
+ },
165
+ });
166
+
167
+ // Document operations
168
+ await client.rag.addDocument(content, metadata?);
169
+ await client.rag.addDocuments([{ content, metadata }]);
170
+ await client.rag.addUrl('https://example.com');
171
+
172
+ // Querying
173
+ const results = await client.rag.query('search query', { topK: 5, minScore: 0.5 });
174
+ const context = await client.rag.getContext('query', { includeMetadata: true });
175
+
176
+ // Management
177
+ await client.rag.clear();
178
+ const count = await client.rag.count();
179
+ ```
180
+
181
+ ### Web Module
182
+
183
+ ```typescript
184
+ // Fetch URLs
185
+ const result = await client.web.fetch(url, {
186
+ headers: {},
187
+ timeout: 30000,
188
+ includeLinks: true,
189
+ includeImages: false,
190
+ });
191
+
192
+ const results = await client.web.fetchMany(urls);
193
+ const markdown = await client.web.fetchMarkdown(url);
194
+
195
+ // Search the web
196
+ const results = await client.web.search('query', {
197
+ maxResults: 10,
198
+ safeSearch: true,
199
+ });
200
+ ```
201
+
202
+ ### Chat Module
203
+
204
+ ```typescript
205
+ // RAG-augmented chat
206
+ await client.chat.withRAG({
207
+ messages,
208
+ model?: string,
209
+ topK?: number,
210
+ minScore?: number,
211
+ includeMetadata?: boolean,
212
+ systemPrompt?: string,
213
+ });
214
+
215
+ // Web search chat
216
+ await client.chat.withWebSearch({
217
+ messages,
218
+ model?: string,
219
+ searchQuery?: string,
220
+ maxResults?: number,
221
+ });
222
+
223
+ // URL content chat
224
+ await client.chat.withUrl({
225
+ messages,
226
+ url: string,
227
+ model?: string,
228
+ });
229
+ ```
230
+
231
+ ### Agents
232
+
233
+ ```typescript
234
+ // Create basic agent
235
+ const agent = client.createAgent({
236
+ name?: string,
237
+ model?: string,
238
+ systemPrompt?: string,
239
+ tools?: ToolDefinition[],
240
+ maxIterations?: number,
241
+ verbose?: boolean,
242
+ });
243
+
244
+ // Create with built-in tools
245
+ const agent = await client.createAgentWithBuiltins(config);
246
+
247
+ // Execute
248
+ const result = await agent.run('task description');
249
+
250
+ // Stream execution
251
+ for await (const event of agent.runStream('task')) {
252
+ // Handle events: 'content', 'tool_call', 'tool_result', 'done'
253
+ }
254
+
255
+ // Memory management
256
+ agent.clearHistory();
257
+ const history = agent.getHistory();
258
+ ```
259
+
260
+ ### Built-in Tools
261
+
262
+ | Tool | Description |
263
+ |------|-------------|
264
+ | `web_search` | Search the web using DuckDuckGo |
265
+ | `fetch_url` | Fetch and parse web pages |
266
+ | `rag_query` | Query the knowledge base |
267
+ | `calculator` | Mathematical calculations |
268
+ | `get_datetime` | Get current date/time |
269
+
270
+ ### Custom Tools
271
+
272
+ ```typescript
273
+ import { ToolDefinition } from 'groq-rag';
274
+
275
+ const myTool: ToolDefinition = {
276
+ name: 'my_tool',
277
+ description: 'Does something useful',
278
+ parameters: {
279
+ type: 'object',
280
+ properties: {
281
+ input: { type: 'string', description: 'The input value' },
282
+ count: { type: 'number', description: 'How many times' },
283
+ },
284
+ required: ['input'],
285
+ },
286
+ execute: async (params) => {
287
+ const { input, count = 1 } = params as { input: string; count?: number };
288
+ return { result: input.repeat(count) };
289
+ },
290
+ };
291
+
292
+ const agent = client.createAgent({ tools: [myTool] });
293
+ ```
294
+
295
+ ## Configuration
296
+
297
+ ### Vector Store Providers
298
+
299
+ #### In-Memory (Default)
300
+
301
+ ```typescript
302
+ await client.initRAG({
303
+ vectorStore: { provider: 'memory' },
304
+ });
305
+ ```
306
+
307
+ Best for: Development, testing, small datasets.
308
+
309
+ #### ChromaDB
310
+
311
+ ```typescript
312
+ await client.initRAG({
313
+ vectorStore: {
314
+ provider: 'chroma',
315
+ connectionString: 'http://localhost:8000',
316
+ indexName: 'my-collection',
317
+ },
318
+ });
319
+ ```
320
+
321
+ Best for: Production, large datasets, persistence.
322
+
323
+ ### Embedding Providers
324
+
325
+ #### Groq (Default)
326
+
327
+ Uses a deterministic pseudo-embedding for demos. Suitable for testing.
328
+
329
+ ```typescript
330
+ await client.initRAG({
331
+ embedding: { provider: 'groq' },
332
+ });
333
+ ```
334
+
335
+ #### OpenAI
336
+
337
+ For production use with high-quality embeddings:
338
+
339
+ ```typescript
340
+ await client.initRAG({
341
+ embedding: {
342
+ provider: 'openai',
343
+ apiKey: process.env.OPENAI_API_KEY,
344
+ model: 'text-embedding-3-small',
345
+ dimensions: 1536,
346
+ },
347
+ });
348
+ ```
349
+
350
+ ### Search Providers
351
+
352
+ #### DuckDuckGo (Default, No API Key)
353
+
354
+ ```typescript
355
+ import { createSearchProvider } from 'groq-rag';
356
+ const search = createSearchProvider({ provider: 'duckduckgo' });
357
+ ```
358
+
359
+ #### Brave Search
360
+
361
+ ```typescript
362
+ const search = createSearchProvider({
363
+ provider: 'brave',
364
+ apiKey: process.env.BRAVE_API_KEY,
365
+ });
366
+ ```
367
+
368
+ #### Serper (Google)
369
+
370
+ ```typescript
371
+ const search = createSearchProvider({
372
+ provider: 'serper',
373
+ apiKey: process.env.SERPER_API_KEY,
374
+ });
375
+ ```
376
+
377
+ ## Text Chunking Strategies
378
+
379
+ | Strategy | Description | Best For |
380
+ |----------|-------------|----------|
381
+ | `recursive` | Splits by separators, falls back to smaller separators | General purpose (default) |
382
+ | `fixed` | Fixed character size with overlap | Uniform chunk sizes |
383
+ | `sentence` | Splits by sentence boundaries | Preserving sentence context |
384
+ | `paragraph` | Splits by paragraphs | Document structure preservation |
385
+
386
+ ```typescript
387
+ await client.initRAG({
388
+ chunking: {
389
+ strategy: 'recursive',
390
+ chunkSize: 1000,
391
+ chunkOverlap: 200,
392
+ },
393
+ });
394
+ ```
395
+
396
+ ## Utilities
397
+
398
+ ```typescript
399
+ import {
400
+ chunkText,
401
+ cosineSimilarity,
402
+ estimateTokens,
403
+ formatContext,
404
+ extractUrls,
405
+ } from 'groq-rag';
406
+
407
+ // Chunk text manually
408
+ const chunks = chunkText('Long text...', 'doc-id', { chunkSize: 500 });
409
+
410
+ // Calculate similarity
411
+ const similarity = cosineSimilarity(embedding1, embedding2);
412
+
413
+ // Estimate tokens
414
+ const tokenCount = estimateTokens('Some text');
415
+ ```
416
+
417
+ ## Examples
418
+
419
+ See the [examples](./examples) directory for complete usage examples:
420
+
421
+ - `basic-chat.ts` - Simple chat completion
422
+ - `rag-chat.ts` - RAG-augmented conversation
423
+ - `web-search.ts` - Web search integration
424
+ - `url-fetch.ts` - URL fetching and summarization
425
+ - `agent.ts` - Agent with tools
426
+ - `streaming-agent.ts` - Streaming agent execution
427
+
428
+ ## Development
429
+
430
+ ```bash
431
+ # Install dependencies
432
+ npm install
433
+
434
+ # Run tests
435
+ npm test
436
+
437
+ # Run tests in watch mode
438
+ npm run test:watch
439
+
440
+ # Build
441
+ npm run build
442
+
443
+ # Lint
444
+ npm run lint
445
+
446
+ # Type check
447
+ npm run typecheck
448
+ ```
449
+
450
+ ## Contributing
451
+
452
+ Contributions are welcome! Please read our [Contributing Guide](CONTRIBUTING.md) for details.
453
+
454
+ ## License
455
+
456
+ MIT - see [LICENSE](LICENSE) for details.