groq-rag 0.1.1 → 0.1.3
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 +1 -1
- package/README.md +528 -145
- package/package.json +2 -6
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,20 +1,57 @@
|
|
|
1
1
|
# groq-rag
|
|
2
2
|
|
|
3
|
-
[](https://github.com/mithun50/groq-rag/actions/workflows/ci.yml)
|
|
4
3
|
[](https://www.npmjs.com/package/groq-rag)
|
|
5
4
|
[](https://opensource.org/licenses/MIT)
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
[](https://www.typescriptlang.org/)
|
|
6
|
+
[](https://nodejs.org/)
|
|
7
|
+
[](https://groq-rag.onrender.com)
|
|
8
|
+
|
|
9
|
+
Extended Groq SDK with RAG (Retrieval-Augmented Generation), web browsing, and autonomous agent capabilities. Build intelligent AI applications that can search the web, fetch URLs, query knowledge bases, and reason through complex tasks.
|
|
10
|
+
|
|
11
|
+
## Table of Contents
|
|
12
|
+
|
|
13
|
+
- [Features](#features)
|
|
14
|
+
- [Installation](#installation)
|
|
15
|
+
- [Quick Start](#quick-start)
|
|
16
|
+
- [Supported Models](#supported-models)
|
|
17
|
+
- [Production Models](#production-models)
|
|
18
|
+
- [Compound AI Systems](#compound-ai-systems)
|
|
19
|
+
- [Preview Models](#preview-models)
|
|
20
|
+
- [Reasoning Models](#reasoning-models)
|
|
21
|
+
- [Vision Models](#vision-models)
|
|
22
|
+
- [Safety & Moderation Models](#safety--moderation-models)
|
|
23
|
+
- [Feature Compatibility](#feature-compatibility)
|
|
24
|
+
- [Core Modules](#core-modules)
|
|
25
|
+
- [GroqRAG Client](#groqrag-client)
|
|
26
|
+
- [RAG Module](#rag-module)
|
|
27
|
+
- [Web Module](#web-module)
|
|
28
|
+
- [Chat Module](#chat-module)
|
|
29
|
+
- [Agent System](#agent-system)
|
|
30
|
+
- [Tool System](#tool-system)
|
|
31
|
+
- [Configuration](#configuration)
|
|
32
|
+
- [Vector Stores](#vector-stores)
|
|
33
|
+
- [Embedding Providers](#embedding-providers)
|
|
34
|
+
- [Search Providers](#search-providers)
|
|
35
|
+
- [Chunking Strategies](#chunking-strategies)
|
|
36
|
+
- [Utilities](#utilities)
|
|
37
|
+
- [Examples](#examples)
|
|
38
|
+
- [Architecture](#architecture)
|
|
39
|
+
- [Development](#development)
|
|
40
|
+
- [Contributing](#contributing)
|
|
41
|
+
- [License](#license)
|
|
8
42
|
|
|
9
43
|
## Features
|
|
10
44
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
45
|
+
| Feature | Description |
|
|
46
|
+
|---------|-------------|
|
|
47
|
+
| **RAG Support** | Built-in vector store with document chunking, embedding, and semantic retrieval |
|
|
48
|
+
| **Web Fetching** | Fetch and parse web pages to clean markdown with metadata extraction |
|
|
49
|
+
| **Web Search** | DuckDuckGo (free), Brave Search, and Serper (Google) integration |
|
|
50
|
+
| **Agent System** | ReAct-style autonomous agents with tool use, memory, and streaming |
|
|
51
|
+
| **Tool Framework** | Extensible tool system with built-in and custom tools |
|
|
52
|
+
| **TypeScript** | Full type safety with comprehensive IntelliSense support |
|
|
53
|
+
| **Zero Config** | Works out of the box with sensible defaults |
|
|
54
|
+
| **Streaming** | Real-time streaming for both chat and agent execution |
|
|
18
55
|
|
|
19
56
|
## Installation
|
|
20
57
|
|
|
@@ -22,6 +59,10 @@ Extended Groq SDK with RAG (Retrieval-Augmented Generation), web browsing, and a
|
|
|
22
59
|
npm install groq-rag
|
|
23
60
|
```
|
|
24
61
|
|
|
62
|
+
**Requirements:**
|
|
63
|
+
- Node.js 18.0.0 or higher
|
|
64
|
+
- Groq API key (get one at [console.groq.com](https://console.groq.com))
|
|
65
|
+
|
|
25
66
|
## Quick Start
|
|
26
67
|
|
|
27
68
|
### Basic Chat
|
|
@@ -35,9 +76,7 @@ const client = new GroqRAG({
|
|
|
35
76
|
|
|
36
77
|
const response = await client.complete({
|
|
37
78
|
model: 'llama-3.3-70b-versatile',
|
|
38
|
-
messages: [
|
|
39
|
-
{ role: 'user', content: 'Hello!' },
|
|
40
|
-
],
|
|
79
|
+
messages: [{ role: 'user', content: 'Hello!' }],
|
|
41
80
|
});
|
|
42
81
|
|
|
43
82
|
console.log(response.choices[0].message.content);
|
|
@@ -46,116 +85,181 @@ console.log(response.choices[0].message.content);
|
|
|
46
85
|
### RAG-Augmented Chat
|
|
47
86
|
|
|
48
87
|
```typescript
|
|
49
|
-
import GroqRAG from 'groq-rag';
|
|
50
|
-
|
|
51
88
|
const client = new GroqRAG();
|
|
52
89
|
|
|
53
|
-
// Initialize RAG
|
|
90
|
+
// Initialize RAG with in-memory vector store
|
|
54
91
|
await client.initRAG();
|
|
55
92
|
|
|
56
93
|
// Add documents to the knowledge base
|
|
57
94
|
await client.rag.addDocument('Your document content here...');
|
|
58
95
|
await client.rag.addDocument('Another document...', { source: 'manual.pdf' });
|
|
59
96
|
|
|
60
|
-
// Chat with context retrieval
|
|
97
|
+
// Chat with automatic context retrieval
|
|
61
98
|
const response = await client.chat.withRAG({
|
|
62
99
|
messages: [{ role: 'user', content: 'What does the document say about X?' }],
|
|
63
|
-
topK: 5,
|
|
64
|
-
minScore: 0.5,
|
|
100
|
+
topK: 5,
|
|
101
|
+
minScore: 0.5,
|
|
65
102
|
});
|
|
66
103
|
|
|
67
104
|
console.log(response.content);
|
|
68
105
|
console.log('Sources:', response.sources);
|
|
69
106
|
```
|
|
70
107
|
|
|
71
|
-
###
|
|
108
|
+
### Autonomous Agent
|
|
72
109
|
|
|
73
110
|
```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
111
|
const agent = await client.createAgentWithBuiltins({
|
|
103
112
|
model: 'llama-3.3-70b-versatile',
|
|
104
113
|
verbose: true,
|
|
105
114
|
});
|
|
106
115
|
|
|
107
116
|
const result = await agent.run('Search for recent AI news and summarize the top 3 stories');
|
|
117
|
+
|
|
108
118
|
console.log(result.output);
|
|
109
119
|
console.log('Tools used:', result.toolCalls.map(t => t.name));
|
|
110
120
|
```
|
|
111
121
|
|
|
112
|
-
|
|
122
|
+
## Supported Models
|
|
113
123
|
|
|
114
|
-
|
|
115
|
-
const agent = await client.createAgentWithBuiltins();
|
|
124
|
+
This package supports **all Groq models** through direct API passthrough. Any model available on Groq works with groq-rag.
|
|
116
125
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
126
|
+
### Production Models
|
|
127
|
+
|
|
128
|
+
| Model ID | Developer | Speed | Context | Best For |
|
|
129
|
+
|----------|-----------|-------|---------|----------|
|
|
130
|
+
| `llama-3.3-70b-versatile` | Meta | 280 T/s | 131K | General purpose, highest quality |
|
|
131
|
+
| `llama-3.1-8b-instant` | Meta | 560 T/s | 131K | Fast responses, cost-effective |
|
|
132
|
+
| `openai/gpt-oss-120b` | OpenAI | 500 T/s | 131K | Complex reasoning, flagship open model |
|
|
133
|
+
| `openai/gpt-oss-20b` | OpenAI | 1000 T/s | 131K | Fast reasoning tasks |
|
|
134
|
+
|
|
135
|
+
### Compound AI Systems
|
|
136
|
+
|
|
137
|
+
| Model ID | Description |
|
|
138
|
+
|----------|-------------|
|
|
139
|
+
| `groq/compound` | AI system with built-in web search & code execution |
|
|
140
|
+
| `groq/compound-mini` | Lightweight compound system |
|
|
141
|
+
|
|
142
|
+
### Preview Models
|
|
143
|
+
|
|
144
|
+
| Model ID | Developer | Features |
|
|
145
|
+
|----------|-----------|----------|
|
|
146
|
+
| `meta-llama/llama-4-scout-17b-16e-instruct` | Meta | 🖼️ Vision, 128K context |
|
|
147
|
+
| `meta-llama/llama-4-maverick-17b-128e-instruct` | Meta | 🖼️ Vision, 128K context |
|
|
148
|
+
| `qwen/qwen3-32b` | Alibaba | Strong reasoning |
|
|
149
|
+
| `moonshotai/kimi-k2-instruct-0905` | Moonshot AI | Extended context |
|
|
150
|
+
| `deepseek-r1-distill-qwen-32b` | DeepSeek | Math & code reasoning, 128K context |
|
|
151
|
+
|
|
152
|
+
### Reasoning Models
|
|
153
|
+
|
|
154
|
+
Best for math, logic, and complex problem-solving:
|
|
155
|
+
|
|
156
|
+
| Model ID | Strengths |
|
|
157
|
+
|----------|-----------|
|
|
158
|
+
| `openai/gpt-oss-120b` | Complex reasoning with tools |
|
|
159
|
+
| `openai/gpt-oss-20b` | Fast reasoning |
|
|
160
|
+
| `qwen/qwen3-32b` | Math, structured thinking |
|
|
161
|
+
| `deepseek-r1-distill-qwen-32b` | Math (94.3% MATH-500), code (1691 CodeForces) |
|
|
162
|
+
|
|
163
|
+
### Vision Models
|
|
164
|
+
|
|
165
|
+
Support image inputs alongside text:
|
|
166
|
+
|
|
167
|
+
| Model ID | Max Images | Max Resolution |
|
|
168
|
+
|----------|------------|----------------|
|
|
169
|
+
| `meta-llama/llama-4-scout-17b-16e-instruct` | 5/request | 33 megapixels |
|
|
170
|
+
| `meta-llama/llama-4-maverick-17b-128e-instruct` | 5/request | 33 megapixels |
|
|
131
171
|
|
|
132
|
-
|
|
172
|
+
### Safety & Moderation Models
|
|
173
|
+
|
|
174
|
+
| Model ID | Purpose |
|
|
175
|
+
|----------|---------|
|
|
176
|
+
| `meta-llama/llama-guard-4-12b` | Content safety classification (text & images) |
|
|
177
|
+
| `openai/gpt-oss-safeguard-20b` | Custom policy enforcement |
|
|
178
|
+
| `meta-llama/llama-prompt-guard-2-86m` | Prompt injection detection |
|
|
179
|
+
| `meta-llama/llama-prompt-guard-2-22m` | Lightweight injection detection |
|
|
180
|
+
|
|
181
|
+
### Audio Models
|
|
182
|
+
|
|
183
|
+
| Model ID | Purpose |
|
|
184
|
+
|----------|---------|
|
|
185
|
+
| `whisper-large-v3` | Speech-to-text transcription |
|
|
186
|
+
| `whisper-large-v3-turbo` | Fast transcription |
|
|
187
|
+
|
|
188
|
+
### Feature Compatibility
|
|
189
|
+
|
|
190
|
+
| Feature | Compatible Models |
|
|
191
|
+
|---------|-------------------|
|
|
192
|
+
| **RAG** | All chat models (11+) |
|
|
193
|
+
| **Web Search** | All chat models (11+) |
|
|
194
|
+
| **URL Fetch** | All chat models (11+) |
|
|
195
|
+
| **Agents (Tool Use)** | All chat models with function calling |
|
|
196
|
+
| **Streaming** | All chat models |
|
|
197
|
+
| **Vision + RAG** | llama-4-scout, llama-4-maverick |
|
|
198
|
+
|
|
199
|
+
### References
|
|
200
|
+
|
|
201
|
+
- 📚 [Groq Models Documentation](https://console.groq.com/docs/models) - Complete model list & specs
|
|
202
|
+
- 🧠 [Reasoning Models Guide](https://console.groq.com/docs/reasoning) - Using reasoning models
|
|
203
|
+
- 👁️ [Vision Models Guide](https://console.groq.com/docs/vision) - Image input support
|
|
204
|
+
- 🛡️ [Content Moderation](https://console.groq.com/docs/content-moderation) - Safety models
|
|
205
|
+
- 📖 [Groq API Reference](https://console.groq.com/docs/api-reference) - Full API documentation
|
|
206
|
+
- 💰 [Pricing](https://groq.com/pricing) - Model pricing information
|
|
207
|
+
|
|
208
|
+
> **Note:** Model availability may change. Use the [Groq Models API](https://api.groq.com/openai/v1/models) to get the current list programmatically.
|
|
209
|
+
|
|
210
|
+
## Core Modules
|
|
133
211
|
|
|
134
212
|
### GroqRAG Client
|
|
135
213
|
|
|
214
|
+
The main entry point providing unified access to all functionality.
|
|
215
|
+
|
|
136
216
|
```typescript
|
|
217
|
+
import GroqRAG from 'groq-rag';
|
|
218
|
+
|
|
137
219
|
const client = new GroqRAG({
|
|
138
|
-
apiKey
|
|
139
|
-
baseURL?: string,
|
|
140
|
-
timeout?: number,
|
|
141
|
-
maxRetries?: number,
|
|
220
|
+
apiKey: string, // Groq API key (defaults to GROQ_API_KEY env var)
|
|
221
|
+
baseURL?: string, // Custom API base URL
|
|
222
|
+
timeout?: number, // Request timeout in milliseconds
|
|
223
|
+
maxRetries?: number, // Max retry attempts (default: 2)
|
|
142
224
|
});
|
|
143
225
|
```
|
|
144
226
|
|
|
227
|
+
**Methods:**
|
|
228
|
+
|
|
229
|
+
| Method | Description |
|
|
230
|
+
|--------|-------------|
|
|
231
|
+
| `initRAG(options)` | Initialize RAG with vector store and embeddings |
|
|
232
|
+
| `complete(params)` | Standard chat completion (passthrough to Groq) |
|
|
233
|
+
| `stream(params)` | Streaming chat completion |
|
|
234
|
+
| `createAgent(config)` | Create a basic agent |
|
|
235
|
+
| `createAgentWithBuiltins(config)` | Create agent with all built-in tools |
|
|
236
|
+
| `getRetriever()` | Get the RAG retriever instance |
|
|
237
|
+
|
|
238
|
+
**Sub-modules:**
|
|
239
|
+
- `client.chat` - Enhanced chat methods (withRAG, withWebSearch, withUrl)
|
|
240
|
+
- `client.web` - Web operations (fetch, search, fetchMany)
|
|
241
|
+
- `client.rag` - Knowledge base management (addDocument, query, getContext)
|
|
242
|
+
|
|
243
|
+
---
|
|
244
|
+
|
|
145
245
|
### RAG Module
|
|
146
246
|
|
|
247
|
+
Manage your knowledge base with document ingestion, chunking, and semantic retrieval.
|
|
248
|
+
|
|
249
|
+
#### Initialization
|
|
250
|
+
|
|
147
251
|
```typescript
|
|
148
|
-
// Initialize with custom configuration
|
|
149
252
|
await client.initRAG({
|
|
150
253
|
embedding: {
|
|
151
254
|
provider: 'groq' | 'openai',
|
|
152
|
-
apiKey
|
|
153
|
-
model
|
|
255
|
+
apiKey?: string,
|
|
256
|
+
model?: string,
|
|
257
|
+
dimensions?: number,
|
|
154
258
|
},
|
|
155
259
|
vectorStore: {
|
|
156
260
|
provider: 'memory' | 'chroma',
|
|
157
|
-
connectionString
|
|
158
|
-
indexName
|
|
261
|
+
connectionString?: string,
|
|
262
|
+
indexName?: string,
|
|
159
263
|
},
|
|
160
264
|
chunking: {
|
|
161
265
|
strategy: 'recursive' | 'fixed' | 'sentence' | 'paragraph',
|
|
@@ -163,111 +267,241 @@ await client.initRAG({
|
|
|
163
267
|
chunkOverlap: 200,
|
|
164
268
|
},
|
|
165
269
|
});
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
#### Document Operations
|
|
166
273
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
await client.rag.
|
|
274
|
+
```typescript
|
|
275
|
+
// Add single document
|
|
276
|
+
await client.rag.addDocument(content: string, metadata?: Record<string, unknown>);
|
|
277
|
+
|
|
278
|
+
// Add multiple documents
|
|
279
|
+
await client.rag.addDocuments([
|
|
280
|
+
{ content: 'Document 1...', metadata: { source: 'file1.txt' } },
|
|
281
|
+
{ content: 'Document 2...', metadata: { source: 'file2.txt' } },
|
|
282
|
+
]);
|
|
283
|
+
|
|
284
|
+
// Add URL content directly
|
|
170
285
|
await client.rag.addUrl('https://example.com');
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
#### Querying
|
|
289
|
+
|
|
290
|
+
```typescript
|
|
291
|
+
// Semantic search
|
|
292
|
+
const results = await client.rag.query('search query', {
|
|
293
|
+
topK: 5,
|
|
294
|
+
minScore: 0.5,
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
// Get formatted context for LLM
|
|
298
|
+
const context = await client.rag.getContext('query', {
|
|
299
|
+
includeMetadata: true,
|
|
300
|
+
maxTokens: 4000,
|
|
301
|
+
});
|
|
302
|
+
```
|
|
171
303
|
|
|
172
|
-
|
|
173
|
-
const results = await client.rag.query('search query', { topK: 5, minScore: 0.5 });
|
|
174
|
-
const context = await client.rag.getContext('query', { includeMetadata: true });
|
|
304
|
+
#### Management
|
|
175
305
|
|
|
176
|
-
|
|
177
|
-
await client.rag.clear();
|
|
178
|
-
const count = await client.rag.count();
|
|
306
|
+
```typescript
|
|
307
|
+
await client.rag.clear(); // Clear all documents
|
|
308
|
+
const count = await client.rag.count(); // Get document count
|
|
179
309
|
```
|
|
180
310
|
|
|
311
|
+
---
|
|
312
|
+
|
|
181
313
|
### Web Module
|
|
182
314
|
|
|
315
|
+
Fetch, parse, and search the web.
|
|
316
|
+
|
|
317
|
+
#### Fetching URLs
|
|
318
|
+
|
|
183
319
|
```typescript
|
|
184
|
-
// Fetch
|
|
320
|
+
// Fetch single URL
|
|
185
321
|
const result = await client.web.fetch(url, {
|
|
186
|
-
headers
|
|
187
|
-
timeout:
|
|
188
|
-
|
|
189
|
-
|
|
322
|
+
headers?: Record<string, string>,
|
|
323
|
+
timeout?: number, // Default: 30000ms
|
|
324
|
+
maxLength?: number, // Max content length
|
|
325
|
+
includeLinks?: boolean, // Extract links
|
|
326
|
+
includeImages?: boolean, // Extract images
|
|
190
327
|
});
|
|
191
328
|
|
|
192
|
-
|
|
329
|
+
// Returns:
|
|
330
|
+
// {
|
|
331
|
+
// url: string,
|
|
332
|
+
// title?: string,
|
|
333
|
+
// content: string,
|
|
334
|
+
// markdown?: string,
|
|
335
|
+
// links?: Array<{ text: string, href: string }>,
|
|
336
|
+
// images?: Array<{ alt: string, src: string }>,
|
|
337
|
+
// metadata?: { description?, author?, publishedDate? },
|
|
338
|
+
// fetchedAt: Date,
|
|
339
|
+
// }
|
|
340
|
+
|
|
341
|
+
// Fetch multiple URLs
|
|
342
|
+
const results = await client.web.fetchMany(['url1', 'url2', 'url3']);
|
|
343
|
+
|
|
344
|
+
// Get markdown only
|
|
193
345
|
const markdown = await client.web.fetchMarkdown(url);
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
#### Web Search
|
|
194
349
|
|
|
195
|
-
|
|
350
|
+
```typescript
|
|
196
351
|
const results = await client.web.search('query', {
|
|
197
|
-
maxResults: 10
|
|
198
|
-
safeSearch: true
|
|
352
|
+
maxResults?: number, // Default: 10
|
|
353
|
+
safeSearch?: boolean, // Default: true
|
|
354
|
+
language?: string,
|
|
355
|
+
region?: string,
|
|
199
356
|
});
|
|
357
|
+
|
|
358
|
+
// Returns:
|
|
359
|
+
// Array<{
|
|
360
|
+
// title: string,
|
|
361
|
+
// url: string,
|
|
362
|
+
// snippet: string,
|
|
363
|
+
// position: number,
|
|
364
|
+
// }>
|
|
200
365
|
```
|
|
201
366
|
|
|
367
|
+
---
|
|
368
|
+
|
|
202
369
|
### Chat Module
|
|
203
370
|
|
|
371
|
+
Enhanced chat methods with built-in RAG and web integration.
|
|
372
|
+
|
|
373
|
+
#### RAG-Augmented Chat
|
|
374
|
+
|
|
204
375
|
```typescript
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
messages,
|
|
376
|
+
const response = await client.chat.withRAG({
|
|
377
|
+
messages: Message[],
|
|
208
378
|
model?: string,
|
|
209
|
-
topK?: number,
|
|
210
|
-
minScore?: number,
|
|
379
|
+
topK?: number, // Documents to retrieve (default: 5)
|
|
380
|
+
minScore?: number, // Minimum similarity (default: 0.5)
|
|
211
381
|
includeMetadata?: boolean,
|
|
212
382
|
systemPrompt?: string,
|
|
383
|
+
temperature?: number,
|
|
384
|
+
maxTokens?: number,
|
|
213
385
|
});
|
|
214
386
|
|
|
215
|
-
//
|
|
216
|
-
|
|
217
|
-
|
|
387
|
+
// Returns:
|
|
388
|
+
// {
|
|
389
|
+
// content: string,
|
|
390
|
+
// sources: SearchResult[],
|
|
391
|
+
// usage?: { promptTokens, completionTokens, totalTokens },
|
|
392
|
+
// }
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
#### Web Search Chat
|
|
396
|
+
|
|
397
|
+
```typescript
|
|
398
|
+
const response = await client.chat.withWebSearch({
|
|
399
|
+
messages: Message[],
|
|
218
400
|
model?: string,
|
|
219
|
-
searchQuery?: string,
|
|
220
|
-
maxResults?: number,
|
|
401
|
+
searchQuery?: string, // Custom search query
|
|
402
|
+
maxResults?: number, // Search results to include
|
|
221
403
|
});
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
#### URL Content Chat
|
|
222
407
|
|
|
223
|
-
|
|
224
|
-
await client.chat.withUrl({
|
|
225
|
-
messages,
|
|
408
|
+
```typescript
|
|
409
|
+
const response = await client.chat.withUrl({
|
|
410
|
+
messages: Message[],
|
|
226
411
|
url: string,
|
|
227
412
|
model?: string,
|
|
228
413
|
});
|
|
229
414
|
```
|
|
230
415
|
|
|
231
|
-
|
|
416
|
+
---
|
|
417
|
+
|
|
418
|
+
### Agent System
|
|
419
|
+
|
|
420
|
+
Create autonomous agents that reason and use tools to accomplish tasks.
|
|
421
|
+
|
|
422
|
+
#### Creating Agents
|
|
232
423
|
|
|
233
424
|
```typescript
|
|
234
|
-
//
|
|
425
|
+
// Basic agent with custom tools
|
|
235
426
|
const agent = client.createAgent({
|
|
236
427
|
name?: string,
|
|
237
428
|
model?: string,
|
|
238
429
|
systemPrompt?: string,
|
|
239
430
|
tools?: ToolDefinition[],
|
|
240
|
-
maxIterations?: number,
|
|
241
|
-
verbose?: boolean,
|
|
431
|
+
maxIterations?: number, // Default: 10
|
|
432
|
+
verbose?: boolean, // Log agent reasoning
|
|
242
433
|
});
|
|
243
434
|
|
|
244
|
-
//
|
|
245
|
-
const agent = await client.createAgentWithBuiltins(
|
|
435
|
+
// Agent with all built-in tools
|
|
436
|
+
const agent = await client.createAgentWithBuiltins({
|
|
437
|
+
model: 'llama-3.3-70b-versatile',
|
|
438
|
+
verbose: true,
|
|
439
|
+
});
|
|
440
|
+
```
|
|
246
441
|
|
|
247
|
-
|
|
248
|
-
const result = await agent.run('task description');
|
|
442
|
+
#### Running Agents
|
|
249
443
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
444
|
+
```typescript
|
|
445
|
+
// Synchronous execution
|
|
446
|
+
const result = await agent.run('Your task description');
|
|
447
|
+
|
|
448
|
+
// Returns:
|
|
449
|
+
// {
|
|
450
|
+
// output: string, // Final answer
|
|
451
|
+
// steps: AgentStep[], // Reasoning steps
|
|
452
|
+
// toolCalls: ToolResult[], // Tools used
|
|
453
|
+
// totalTokens?: number,
|
|
454
|
+
// }
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
#### Streaming Execution
|
|
458
|
+
|
|
459
|
+
```typescript
|
|
460
|
+
for await (const event of agent.runStream('Research topic X')) {
|
|
461
|
+
switch (event.type) {
|
|
462
|
+
case 'thought':
|
|
463
|
+
console.log('Thinking:', event.data);
|
|
464
|
+
break;
|
|
465
|
+
case 'content':
|
|
466
|
+
process.stdout.write(event.data as string);
|
|
467
|
+
break;
|
|
468
|
+
case 'tool_call':
|
|
469
|
+
console.log('Calling tool:', event.data);
|
|
470
|
+
break;
|
|
471
|
+
case 'tool_result':
|
|
472
|
+
console.log('Tool result received');
|
|
473
|
+
break;
|
|
474
|
+
case 'done':
|
|
475
|
+
console.log('Agent finished');
|
|
476
|
+
break;
|
|
477
|
+
}
|
|
253
478
|
}
|
|
479
|
+
```
|
|
480
|
+
|
|
481
|
+
#### Memory Management
|
|
254
482
|
|
|
255
|
-
|
|
256
|
-
agent.clearHistory();
|
|
257
|
-
const history = agent.getHistory();
|
|
483
|
+
```typescript
|
|
484
|
+
agent.clearHistory(); // Reset conversation
|
|
485
|
+
const history = agent.getHistory(); // Get conversation history
|
|
258
486
|
```
|
|
259
487
|
|
|
260
|
-
|
|
488
|
+
---
|
|
489
|
+
|
|
490
|
+
### Tool System
|
|
491
|
+
|
|
492
|
+
Define custom tools for agents to use.
|
|
493
|
+
|
|
494
|
+
#### Built-in Tools
|
|
261
495
|
|
|
262
496
|
| Tool | Description |
|
|
263
497
|
|------|-------------|
|
|
264
498
|
| `web_search` | Search the web using DuckDuckGo |
|
|
265
499
|
| `fetch_url` | Fetch and parse web pages |
|
|
266
|
-
| `rag_query` | Query the knowledge base |
|
|
267
500
|
| `calculator` | Mathematical calculations |
|
|
268
501
|
| `get_datetime` | Get current date/time |
|
|
502
|
+
| `rag_query` | Query knowledge base (requires RAG initialization) |
|
|
269
503
|
|
|
270
|
-
|
|
504
|
+
#### Custom Tools
|
|
271
505
|
|
|
272
506
|
```typescript
|
|
273
507
|
import { ToolDefinition } from 'groq-rag';
|
|
@@ -292,22 +526,36 @@ const myTool: ToolDefinition = {
|
|
|
292
526
|
const agent = client.createAgent({ tools: [myTool] });
|
|
293
527
|
```
|
|
294
528
|
|
|
529
|
+
#### Tool Executor
|
|
530
|
+
|
|
531
|
+
```typescript
|
|
532
|
+
import { ToolExecutor, createToolExecutor } from 'groq-rag';
|
|
533
|
+
|
|
534
|
+
const executor = createToolExecutor();
|
|
535
|
+
executor.register(myTool);
|
|
536
|
+
executor.register(anotherTool);
|
|
537
|
+
|
|
538
|
+
const result = await executor.execute('my_tool', { input: 'hello' });
|
|
539
|
+
```
|
|
540
|
+
|
|
295
541
|
## Configuration
|
|
296
542
|
|
|
297
|
-
### Vector
|
|
543
|
+
### Vector Stores
|
|
298
544
|
|
|
299
545
|
#### In-Memory (Default)
|
|
300
546
|
|
|
547
|
+
Best for development, testing, and small datasets. No persistence.
|
|
548
|
+
|
|
301
549
|
```typescript
|
|
302
550
|
await client.initRAG({
|
|
303
551
|
vectorStore: { provider: 'memory' },
|
|
304
552
|
});
|
|
305
553
|
```
|
|
306
554
|
|
|
307
|
-
Best for: Development, testing, small datasets.
|
|
308
|
-
|
|
309
555
|
#### ChromaDB
|
|
310
556
|
|
|
557
|
+
Best for production, large datasets, and persistence.
|
|
558
|
+
|
|
311
559
|
```typescript
|
|
312
560
|
await client.initRAG({
|
|
313
561
|
vectorStore: {
|
|
@@ -318,13 +566,13 @@ await client.initRAG({
|
|
|
318
566
|
});
|
|
319
567
|
```
|
|
320
568
|
|
|
321
|
-
|
|
569
|
+
---
|
|
322
570
|
|
|
323
571
|
### Embedding Providers
|
|
324
572
|
|
|
325
|
-
#### Groq (Default)
|
|
573
|
+
#### Groq Embeddings (Default)
|
|
326
574
|
|
|
327
|
-
|
|
575
|
+
Deterministic pseudo-embeddings for testing. No API cost.
|
|
328
576
|
|
|
329
577
|
```typescript
|
|
330
578
|
await client.initRAG({
|
|
@@ -332,9 +580,9 @@ await client.initRAG({
|
|
|
332
580
|
});
|
|
333
581
|
```
|
|
334
582
|
|
|
335
|
-
#### OpenAI
|
|
583
|
+
#### OpenAI Embeddings
|
|
336
584
|
|
|
337
|
-
|
|
585
|
+
High-quality embeddings for production use.
|
|
338
586
|
|
|
339
587
|
```typescript
|
|
340
588
|
await client.initRAG({
|
|
@@ -347,9 +595,13 @@ await client.initRAG({
|
|
|
347
595
|
});
|
|
348
596
|
```
|
|
349
597
|
|
|
598
|
+
---
|
|
599
|
+
|
|
350
600
|
### Search Providers
|
|
351
601
|
|
|
352
|
-
#### DuckDuckGo (Default
|
|
602
|
+
#### DuckDuckGo (Default)
|
|
603
|
+
|
|
604
|
+
Free, no API key required.
|
|
353
605
|
|
|
354
606
|
```typescript
|
|
355
607
|
import { createSearchProvider } from 'groq-rag';
|
|
@@ -358,6 +610,8 @@ const search = createSearchProvider({ provider: 'duckduckgo' });
|
|
|
358
610
|
|
|
359
611
|
#### Brave Search
|
|
360
612
|
|
|
613
|
+
High-quality results, requires API key.
|
|
614
|
+
|
|
361
615
|
```typescript
|
|
362
616
|
const search = createSearchProvider({
|
|
363
617
|
provider: 'brave',
|
|
@@ -367,6 +621,8 @@ const search = createSearchProvider({
|
|
|
367
621
|
|
|
368
622
|
#### Serper (Google)
|
|
369
623
|
|
|
624
|
+
Google search via Serper API.
|
|
625
|
+
|
|
370
626
|
```typescript
|
|
371
627
|
const search = createSearchProvider({
|
|
372
628
|
provider: 'serper',
|
|
@@ -374,14 +630,17 @@ const search = createSearchProvider({
|
|
|
374
630
|
});
|
|
375
631
|
```
|
|
376
632
|
|
|
377
|
-
|
|
633
|
+
---
|
|
634
|
+
|
|
635
|
+
### Chunking Strategies
|
|
378
636
|
|
|
379
637
|
| Strategy | Description | Best For |
|
|
380
638
|
|----------|-------------|----------|
|
|
381
|
-
| `recursive` | Splits by separators
|
|
639
|
+
| `recursive` | Splits by separators with fallback | General purpose (default) |
|
|
382
640
|
| `fixed` | Fixed character size with overlap | Uniform chunk sizes |
|
|
383
641
|
| `sentence` | Splits by sentence boundaries | Preserving sentence context |
|
|
384
|
-
| `paragraph` | Splits by paragraphs | Document structure
|
|
642
|
+
| `paragraph` | Splits by paragraphs | Document structure |
|
|
643
|
+
| `semantic` | Context-aware boundaries | Preserving meaning |
|
|
385
644
|
|
|
386
645
|
```typescript
|
|
387
646
|
await client.initRAG({
|
|
@@ -395,39 +654,146 @@ await client.initRAG({
|
|
|
395
654
|
|
|
396
655
|
## Utilities
|
|
397
656
|
|
|
657
|
+
Standalone utility functions exported for direct use.
|
|
658
|
+
|
|
398
659
|
```typescript
|
|
399
660
|
import {
|
|
400
661
|
chunkText,
|
|
401
662
|
cosineSimilarity,
|
|
402
663
|
estimateTokens,
|
|
664
|
+
truncateToTokens,
|
|
403
665
|
formatContext,
|
|
404
666
|
extractUrls,
|
|
667
|
+
cleanText,
|
|
668
|
+
generateId,
|
|
669
|
+
sleep,
|
|
670
|
+
retry,
|
|
671
|
+
batch,
|
|
672
|
+
safeJsonParse,
|
|
405
673
|
} from 'groq-rag';
|
|
406
674
|
|
|
407
675
|
// Chunk text manually
|
|
408
|
-
const chunks = chunkText('Long text...', 'doc-id', {
|
|
676
|
+
const chunks = chunkText('Long text...', 'doc-id', {
|
|
677
|
+
strategy: 'recursive',
|
|
678
|
+
chunkSize: 500,
|
|
679
|
+
chunkOverlap: 100,
|
|
680
|
+
});
|
|
409
681
|
|
|
410
|
-
// Calculate similarity
|
|
682
|
+
// Calculate vector similarity
|
|
411
683
|
const similarity = cosineSimilarity(embedding1, embedding2);
|
|
412
684
|
|
|
413
685
|
// Estimate tokens
|
|
414
686
|
const tokenCount = estimateTokens('Some text');
|
|
687
|
+
|
|
688
|
+
// Truncate to token limit
|
|
689
|
+
const truncated = truncateToTokens('Long text...', 1000);
|
|
690
|
+
|
|
691
|
+
// Format retrieved docs for LLM
|
|
692
|
+
const context = formatContext(searchResults, { includeMetadata: true });
|
|
693
|
+
|
|
694
|
+
// Extract URLs from text
|
|
695
|
+
const urls = extractUrls('Check out https://example.com for more');
|
|
696
|
+
|
|
697
|
+
// Retry with exponential backoff
|
|
698
|
+
const result = await retry(() => fetchData(), { maxRetries: 3 });
|
|
699
|
+
|
|
700
|
+
// Split array into batches
|
|
701
|
+
const batches = batch(items, 10); // Returns T[][]
|
|
702
|
+
for (const group of batches) {
|
|
703
|
+
await processBatch(group);
|
|
704
|
+
}
|
|
415
705
|
```
|
|
416
706
|
|
|
417
707
|
## Examples
|
|
418
708
|
|
|
419
|
-
|
|
709
|
+
Complete examples in the [examples/](./examples) directory:
|
|
710
|
+
|
|
711
|
+
| Example | Description |
|
|
712
|
+
|---------|-------------|
|
|
713
|
+
| `basic-chat.ts` | Simple chat completion |
|
|
714
|
+
| `rag-chat.ts` | RAG-augmented conversation |
|
|
715
|
+
| `web-search.ts` | Web search integration |
|
|
716
|
+
| `url-fetch.ts` | URL fetching and summarization |
|
|
717
|
+
| `agent.ts` | Agent with tools |
|
|
718
|
+
| `streaming-agent.ts` | Streaming agent execution |
|
|
719
|
+
| `full-chatbot.ts` | **Full-featured interactive CLI chatbot** |
|
|
720
|
+
|
|
721
|
+
### Running the Full Chatbot
|
|
722
|
+
|
|
723
|
+
The `full-chatbot.ts` example demonstrates all groq-rag capabilities:
|
|
724
|
+
|
|
725
|
+
```bash
|
|
726
|
+
GROQ_API_KEY=your_key npx tsx examples/full-chatbot.ts
|
|
727
|
+
```
|
|
728
|
+
|
|
729
|
+
**Capabilities:**
|
|
730
|
+
- Agent Mode: Automatically uses web search, URL fetch, calculator, and RAG
|
|
731
|
+
- RAG Mode: Uses knowledge base for context-aware responses
|
|
732
|
+
- Custom system prompts and context management
|
|
733
|
+
- Knowledge base management (add URLs, custom text)
|
|
734
|
+
- Web search and URL fetching
|
|
735
|
+
|
|
736
|
+
**Commands:**
|
|
737
|
+
```
|
|
738
|
+
/help - Show all commands
|
|
739
|
+
/add <url> - Add URL to knowledge base
|
|
740
|
+
/addtext - Add custom text to knowledge
|
|
741
|
+
/search <q> - Web search
|
|
742
|
+
/fetch <url> - Fetch and summarize URL
|
|
743
|
+
/prompt - Set custom system prompt
|
|
744
|
+
/context - Set additional context
|
|
745
|
+
/mode - Toggle agent/RAG mode
|
|
746
|
+
/clear - Clear chat history
|
|
747
|
+
/quit - Exit
|
|
748
|
+
```
|
|
749
|
+
|
|
750
|
+
## Architecture
|
|
751
|
+
|
|
752
|
+
```
|
|
753
|
+
groq-rag/
|
|
754
|
+
├── src/
|
|
755
|
+
│ ├── index.ts # Public API exports
|
|
756
|
+
│ ├── client.ts # GroqRAG client class
|
|
757
|
+
│ ├── types.ts # TypeScript interfaces
|
|
758
|
+
│ ├── rag/
|
|
759
|
+
│ │ ├── retriever.ts # Document retrieval orchestrator
|
|
760
|
+
│ │ ├── vectorStore.ts # Vector store implementations
|
|
761
|
+
│ │ └── embeddings.ts # Embedding providers
|
|
762
|
+
│ ├── web/
|
|
763
|
+
│ │ ├── fetcher.ts # Web page fetching
|
|
764
|
+
│ │ └── search.ts # Search providers
|
|
765
|
+
│ ├── tools/
|
|
766
|
+
│ │ ├── executor.ts # Tool execution engine
|
|
767
|
+
│ │ └── builtins.ts # Built-in tools
|
|
768
|
+
│ ├── agents/
|
|
769
|
+
│ │ └── agent.ts # ReAct agent implementation
|
|
770
|
+
│ └── utils/
|
|
771
|
+
│ ├── chunker.ts # Text chunking
|
|
772
|
+
│ └── helpers.ts # Utility functions
|
|
773
|
+
├── tests/ # Test files
|
|
774
|
+
└── examples/ # Usage examples
|
|
775
|
+
```
|
|
776
|
+
|
|
777
|
+
**Data Flow:**
|
|
420
778
|
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
779
|
+
```
|
|
780
|
+
Document Ingestion:
|
|
781
|
+
Document → Chunker → Embeddings → Vector Store
|
|
782
|
+
|
|
783
|
+
Query Flow:
|
|
784
|
+
Query → Embedding → Vector Search → Top-K Results → LLM Context
|
|
785
|
+
|
|
786
|
+
Agent Flow:
|
|
787
|
+
User Input → Agent Loop → Tool Selection → Tool Execution → Response
|
|
788
|
+
```
|
|
427
789
|
|
|
428
790
|
## Development
|
|
429
791
|
|
|
430
792
|
```bash
|
|
793
|
+
# Clone repository
|
|
794
|
+
git clone https://github.com/mithun50/groq-rag.git
|
|
795
|
+
cd groq-rag
|
|
796
|
+
|
|
431
797
|
# Install dependencies
|
|
432
798
|
npm install
|
|
433
799
|
|
|
@@ -437,6 +803,9 @@ npm test
|
|
|
437
803
|
# Run tests in watch mode
|
|
438
804
|
npm run test:watch
|
|
439
805
|
|
|
806
|
+
# Run tests with coverage
|
|
807
|
+
npm run test:coverage
|
|
808
|
+
|
|
440
809
|
# Build
|
|
441
810
|
npm run build
|
|
442
811
|
|
|
@@ -449,8 +818,22 @@ npm run typecheck
|
|
|
449
818
|
|
|
450
819
|
## Contributing
|
|
451
820
|
|
|
452
|
-
Contributions are welcome! Please read our [Contributing Guide](CONTRIBUTING.md) for details
|
|
821
|
+
Contributions are welcome! Please read our [Contributing Guide](CONTRIBUTING.md) for details on:
|
|
822
|
+
|
|
823
|
+
- Development setup
|
|
824
|
+
- Code style guidelines
|
|
825
|
+
- Testing requirements
|
|
826
|
+
- Pull request process
|
|
827
|
+
- Adding new features (vector stores, search providers, tools)
|
|
453
828
|
|
|
454
829
|
## License
|
|
455
830
|
|
|
456
831
|
MIT - see [LICENSE](LICENSE) for details.
|
|
832
|
+
|
|
833
|
+
---
|
|
834
|
+
|
|
835
|
+
**Author:** [mithun50](https://github.com/mithun50)
|
|
836
|
+
|
|
837
|
+
**Repository:** [github.com/mithun50/groq-rag](https://github.com/mithun50/groq-rag)
|
|
838
|
+
|
|
839
|
+
**npm:** [npmjs.com/package/groq-rag](https://www.npmjs.com/package/groq-rag)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "groq-rag",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Extended Groq SDK with RAG, web browsing, and agent capabilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -24,8 +24,6 @@
|
|
|
24
24
|
"test:watch": "vitest",
|
|
25
25
|
"test:coverage": "vitest run --coverage",
|
|
26
26
|
"typecheck": "tsc --noEmit",
|
|
27
|
-
"docs": "typedoc",
|
|
28
|
-
"docs:md": "typedoc --plugin typedoc-plugin-markdown --out docs/api",
|
|
29
27
|
"prepublishOnly": "npm run build"
|
|
30
28
|
},
|
|
31
29
|
"keywords": [
|
|
@@ -62,11 +60,9 @@
|
|
|
62
60
|
"@vitest/coverage-v8": "^4.0.18",
|
|
63
61
|
"eslint": "^9.0.0",
|
|
64
62
|
"tsup": "^8.0.1",
|
|
65
|
-
"typedoc": "^0.28.16",
|
|
66
|
-
"typedoc-plugin-markdown": "^4.9.0",
|
|
67
63
|
"typescript": "^5.3.3",
|
|
68
64
|
"typescript-eslint": "^8.0.0",
|
|
69
|
-
"vitest": "^
|
|
65
|
+
"vitest": "^4.0.18"
|
|
70
66
|
},
|
|
71
67
|
"engines": {
|
|
72
68
|
"node": ">=18.0.0"
|