groq-rag 0.1.2 → 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 +527 -157
- package/package.json +2 -6
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -3,31 +3,55 @@
|
|
|
3
3
|
[](https://www.npmjs.com/package/groq-rag)
|
|
4
4
|
[](https://opensource.org/licenses/MIT)
|
|
5
5
|
[](https://www.typescriptlang.org/)
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
This package works with all Groq-supported models. Recommended models:
|
|
22
|
-
|
|
23
|
-
| Model | Description | Best For |
|
|
24
|
-
|-------|-------------|----------|
|
|
25
|
-
| `llama-3.3-70b-versatile` | Latest Llama 3.3 70B | General purpose, best quality |
|
|
26
|
-
| `llama-3.1-8b-instant` | Fast Llama 3.1 8B | Quick responses, lower cost |
|
|
27
|
-
| `qwen/qwen3-32b` | Qwen 3 32B | Alternative, good reasoning |
|
|
28
|
-
| `meta-llama/llama-4-scout-17b-16e-instruct` | Llama 4 Scout | Vision tasks, newest |
|
|
29
|
-
|
|
30
|
-
See [Groq Models](https://console.groq.com/docs/models) for the full list.
|
|
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 |
|
|
31
55
|
|
|
32
56
|
## Installation
|
|
33
57
|
|
|
@@ -35,6 +59,10 @@ See [Groq Models](https://console.groq.com/docs/models) for the full list.
|
|
|
35
59
|
npm install groq-rag
|
|
36
60
|
```
|
|
37
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
|
+
|
|
38
66
|
## Quick Start
|
|
39
67
|
|
|
40
68
|
### Basic Chat
|
|
@@ -48,9 +76,7 @@ const client = new GroqRAG({
|
|
|
48
76
|
|
|
49
77
|
const response = await client.complete({
|
|
50
78
|
model: 'llama-3.3-70b-versatile',
|
|
51
|
-
messages: [
|
|
52
|
-
{ role: 'user', content: 'Hello!' },
|
|
53
|
-
],
|
|
79
|
+
messages: [{ role: 'user', content: 'Hello!' }],
|
|
54
80
|
});
|
|
55
81
|
|
|
56
82
|
console.log(response.choices[0].message.content);
|
|
@@ -59,116 +85,181 @@ console.log(response.choices[0].message.content);
|
|
|
59
85
|
### RAG-Augmented Chat
|
|
60
86
|
|
|
61
87
|
```typescript
|
|
62
|
-
import GroqRAG from 'groq-rag';
|
|
63
|
-
|
|
64
88
|
const client = new GroqRAG();
|
|
65
89
|
|
|
66
|
-
// Initialize RAG
|
|
90
|
+
// Initialize RAG with in-memory vector store
|
|
67
91
|
await client.initRAG();
|
|
68
92
|
|
|
69
93
|
// Add documents to the knowledge base
|
|
70
94
|
await client.rag.addDocument('Your document content here...');
|
|
71
95
|
await client.rag.addDocument('Another document...', { source: 'manual.pdf' });
|
|
72
96
|
|
|
73
|
-
// Chat with context retrieval
|
|
97
|
+
// Chat with automatic context retrieval
|
|
74
98
|
const response = await client.chat.withRAG({
|
|
75
99
|
messages: [{ role: 'user', content: 'What does the document say about X?' }],
|
|
76
|
-
topK: 5,
|
|
77
|
-
minScore: 0.5,
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
console.log(response.content);
|
|
81
|
-
console.log('Sources:', response.sources);
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
### Web Search Chat
|
|
85
|
-
|
|
86
|
-
```typescript
|
|
87
|
-
const response = await client.chat.withWebSearch({
|
|
88
|
-
messages: [{ role: 'user', content: 'Latest AI news?' }],
|
|
89
|
-
maxResults: 5,
|
|
100
|
+
topK: 5,
|
|
101
|
+
minScore: 0.5,
|
|
90
102
|
});
|
|
91
103
|
|
|
92
104
|
console.log(response.content);
|
|
93
105
|
console.log('Sources:', response.sources);
|
|
94
106
|
```
|
|
95
107
|
|
|
96
|
-
###
|
|
108
|
+
### Autonomous Agent
|
|
97
109
|
|
|
98
110
|
```typescript
|
|
99
|
-
// Fetch and parse a URL
|
|
100
|
-
const result = await client.web.fetch('https://example.com');
|
|
101
|
-
console.log(result.title);
|
|
102
|
-
console.log(result.markdown);
|
|
103
|
-
|
|
104
|
-
// Chat about a URL's content
|
|
105
|
-
const response = await client.chat.withUrl({
|
|
106
|
-
messages: [{ role: 'user', content: 'Summarize this page' }],
|
|
107
|
-
url: 'https://example.com/article',
|
|
108
|
-
});
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
### Agents with Tools
|
|
112
|
-
|
|
113
|
-
```typescript
|
|
114
|
-
// Create agent with built-in tools
|
|
115
111
|
const agent = await client.createAgentWithBuiltins({
|
|
116
112
|
model: 'llama-3.3-70b-versatile',
|
|
117
113
|
verbose: true,
|
|
118
114
|
});
|
|
119
115
|
|
|
120
116
|
const result = await agent.run('Search for recent AI news and summarize the top 3 stories');
|
|
117
|
+
|
|
121
118
|
console.log(result.output);
|
|
122
119
|
console.log('Tools used:', result.toolCalls.map(t => t.name));
|
|
123
120
|
```
|
|
124
121
|
|
|
125
|
-
|
|
122
|
+
## Supported Models
|
|
126
123
|
|
|
127
|
-
|
|
128
|
-
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.
|
|
129
125
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
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:
|
|
144
155
|
|
|
145
|
-
|
|
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 |
|
|
171
|
+
|
|
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
|
|
146
211
|
|
|
147
212
|
### GroqRAG Client
|
|
148
213
|
|
|
214
|
+
The main entry point providing unified access to all functionality.
|
|
215
|
+
|
|
149
216
|
```typescript
|
|
217
|
+
import GroqRAG from 'groq-rag';
|
|
218
|
+
|
|
150
219
|
const client = new GroqRAG({
|
|
151
|
-
apiKey
|
|
152
|
-
baseURL?: string,
|
|
153
|
-
timeout?: number,
|
|
154
|
-
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)
|
|
155
224
|
});
|
|
156
225
|
```
|
|
157
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
|
+
|
|
158
245
|
### RAG Module
|
|
159
246
|
|
|
247
|
+
Manage your knowledge base with document ingestion, chunking, and semantic retrieval.
|
|
248
|
+
|
|
249
|
+
#### Initialization
|
|
250
|
+
|
|
160
251
|
```typescript
|
|
161
|
-
// Initialize with custom configuration
|
|
162
252
|
await client.initRAG({
|
|
163
253
|
embedding: {
|
|
164
254
|
provider: 'groq' | 'openai',
|
|
165
|
-
apiKey
|
|
166
|
-
model
|
|
255
|
+
apiKey?: string,
|
|
256
|
+
model?: string,
|
|
257
|
+
dimensions?: number,
|
|
167
258
|
},
|
|
168
259
|
vectorStore: {
|
|
169
260
|
provider: 'memory' | 'chroma',
|
|
170
|
-
connectionString
|
|
171
|
-
indexName
|
|
261
|
+
connectionString?: string,
|
|
262
|
+
indexName?: string,
|
|
172
263
|
},
|
|
173
264
|
chunking: {
|
|
174
265
|
strategy: 'recursive' | 'fixed' | 'sentence' | 'paragraph',
|
|
@@ -176,111 +267,241 @@ await client.initRAG({
|
|
|
176
267
|
chunkOverlap: 200,
|
|
177
268
|
},
|
|
178
269
|
});
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
#### Document Operations
|
|
273
|
+
|
|
274
|
+
```typescript
|
|
275
|
+
// Add single document
|
|
276
|
+
await client.rag.addDocument(content: string, metadata?: Record<string, unknown>);
|
|
179
277
|
|
|
180
|
-
//
|
|
181
|
-
await client.rag.
|
|
182
|
-
|
|
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
|
|
183
285
|
await client.rag.addUrl('https://example.com');
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
#### Querying
|
|
184
289
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
const
|
|
290
|
+
```typescript
|
|
291
|
+
// Semantic search
|
|
292
|
+
const results = await client.rag.query('search query', {
|
|
293
|
+
topK: 5,
|
|
294
|
+
minScore: 0.5,
|
|
295
|
+
});
|
|
188
296
|
|
|
189
|
-
//
|
|
190
|
-
await client.rag.
|
|
191
|
-
|
|
297
|
+
// Get formatted context for LLM
|
|
298
|
+
const context = await client.rag.getContext('query', {
|
|
299
|
+
includeMetadata: true,
|
|
300
|
+
maxTokens: 4000,
|
|
301
|
+
});
|
|
192
302
|
```
|
|
193
303
|
|
|
304
|
+
#### Management
|
|
305
|
+
|
|
306
|
+
```typescript
|
|
307
|
+
await client.rag.clear(); // Clear all documents
|
|
308
|
+
const count = await client.rag.count(); // Get document count
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
---
|
|
312
|
+
|
|
194
313
|
### Web Module
|
|
195
314
|
|
|
315
|
+
Fetch, parse, and search the web.
|
|
316
|
+
|
|
317
|
+
#### Fetching URLs
|
|
318
|
+
|
|
196
319
|
```typescript
|
|
197
|
-
// Fetch
|
|
320
|
+
// Fetch single URL
|
|
198
321
|
const result = await client.web.fetch(url, {
|
|
199
|
-
headers
|
|
200
|
-
timeout:
|
|
201
|
-
|
|
202
|
-
|
|
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
|
|
203
327
|
});
|
|
204
328
|
|
|
205
|
-
|
|
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
|
|
206
345
|
const markdown = await client.web.fetchMarkdown(url);
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
#### Web Search
|
|
207
349
|
|
|
208
|
-
|
|
350
|
+
```typescript
|
|
209
351
|
const results = await client.web.search('query', {
|
|
210
|
-
maxResults: 10
|
|
211
|
-
safeSearch: true
|
|
352
|
+
maxResults?: number, // Default: 10
|
|
353
|
+
safeSearch?: boolean, // Default: true
|
|
354
|
+
language?: string,
|
|
355
|
+
region?: string,
|
|
212
356
|
});
|
|
357
|
+
|
|
358
|
+
// Returns:
|
|
359
|
+
// Array<{
|
|
360
|
+
// title: string,
|
|
361
|
+
// url: string,
|
|
362
|
+
// snippet: string,
|
|
363
|
+
// position: number,
|
|
364
|
+
// }>
|
|
213
365
|
```
|
|
214
366
|
|
|
367
|
+
---
|
|
368
|
+
|
|
215
369
|
### Chat Module
|
|
216
370
|
|
|
371
|
+
Enhanced chat methods with built-in RAG and web integration.
|
|
372
|
+
|
|
373
|
+
#### RAG-Augmented Chat
|
|
374
|
+
|
|
217
375
|
```typescript
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
messages,
|
|
376
|
+
const response = await client.chat.withRAG({
|
|
377
|
+
messages: Message[],
|
|
221
378
|
model?: string,
|
|
222
|
-
topK?: number,
|
|
223
|
-
minScore?: number,
|
|
379
|
+
topK?: number, // Documents to retrieve (default: 5)
|
|
380
|
+
minScore?: number, // Minimum similarity (default: 0.5)
|
|
224
381
|
includeMetadata?: boolean,
|
|
225
382
|
systemPrompt?: string,
|
|
383
|
+
temperature?: number,
|
|
384
|
+
maxTokens?: number,
|
|
226
385
|
});
|
|
227
386
|
|
|
228
|
-
//
|
|
229
|
-
|
|
230
|
-
|
|
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[],
|
|
231
400
|
model?: string,
|
|
232
|
-
searchQuery?: string,
|
|
233
|
-
maxResults?: number,
|
|
401
|
+
searchQuery?: string, // Custom search query
|
|
402
|
+
maxResults?: number, // Search results to include
|
|
234
403
|
});
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
#### URL Content Chat
|
|
235
407
|
|
|
236
|
-
|
|
237
|
-
await client.chat.withUrl({
|
|
238
|
-
messages,
|
|
408
|
+
```typescript
|
|
409
|
+
const response = await client.chat.withUrl({
|
|
410
|
+
messages: Message[],
|
|
239
411
|
url: string,
|
|
240
412
|
model?: string,
|
|
241
413
|
});
|
|
242
414
|
```
|
|
243
415
|
|
|
244
|
-
|
|
416
|
+
---
|
|
417
|
+
|
|
418
|
+
### Agent System
|
|
419
|
+
|
|
420
|
+
Create autonomous agents that reason and use tools to accomplish tasks.
|
|
421
|
+
|
|
422
|
+
#### Creating Agents
|
|
245
423
|
|
|
246
424
|
```typescript
|
|
247
|
-
//
|
|
425
|
+
// Basic agent with custom tools
|
|
248
426
|
const agent = client.createAgent({
|
|
249
427
|
name?: string,
|
|
250
428
|
model?: string,
|
|
251
429
|
systemPrompt?: string,
|
|
252
430
|
tools?: ToolDefinition[],
|
|
253
|
-
maxIterations?: number,
|
|
254
|
-
verbose?: boolean,
|
|
431
|
+
maxIterations?: number, // Default: 10
|
|
432
|
+
verbose?: boolean, // Log agent reasoning
|
|
255
433
|
});
|
|
256
434
|
|
|
257
|
-
//
|
|
258
|
-
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
|
+
```
|
|
441
|
+
|
|
442
|
+
#### Running Agents
|
|
443
|
+
|
|
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
|
+
```
|
|
259
456
|
|
|
260
|
-
|
|
261
|
-
const result = await agent.run('task description');
|
|
457
|
+
#### Streaming Execution
|
|
262
458
|
|
|
263
|
-
|
|
264
|
-
for await (const event of agent.runStream('
|
|
265
|
-
|
|
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
|
+
}
|
|
266
478
|
}
|
|
479
|
+
```
|
|
480
|
+
|
|
481
|
+
#### Memory Management
|
|
267
482
|
|
|
268
|
-
|
|
269
|
-
agent.clearHistory();
|
|
270
|
-
const history = agent.getHistory();
|
|
483
|
+
```typescript
|
|
484
|
+
agent.clearHistory(); // Reset conversation
|
|
485
|
+
const history = agent.getHistory(); // Get conversation history
|
|
271
486
|
```
|
|
272
487
|
|
|
273
|
-
|
|
488
|
+
---
|
|
489
|
+
|
|
490
|
+
### Tool System
|
|
491
|
+
|
|
492
|
+
Define custom tools for agents to use.
|
|
493
|
+
|
|
494
|
+
#### Built-in Tools
|
|
274
495
|
|
|
275
496
|
| Tool | Description |
|
|
276
497
|
|------|-------------|
|
|
277
498
|
| `web_search` | Search the web using DuckDuckGo |
|
|
278
499
|
| `fetch_url` | Fetch and parse web pages |
|
|
279
|
-
| `rag_query` | Query the knowledge base |
|
|
280
500
|
| `calculator` | Mathematical calculations |
|
|
281
501
|
| `get_datetime` | Get current date/time |
|
|
502
|
+
| `rag_query` | Query knowledge base (requires RAG initialization) |
|
|
282
503
|
|
|
283
|
-
|
|
504
|
+
#### Custom Tools
|
|
284
505
|
|
|
285
506
|
```typescript
|
|
286
507
|
import { ToolDefinition } from 'groq-rag';
|
|
@@ -305,22 +526,36 @@ const myTool: ToolDefinition = {
|
|
|
305
526
|
const agent = client.createAgent({ tools: [myTool] });
|
|
306
527
|
```
|
|
307
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
|
+
|
|
308
541
|
## Configuration
|
|
309
542
|
|
|
310
|
-
### Vector
|
|
543
|
+
### Vector Stores
|
|
311
544
|
|
|
312
545
|
#### In-Memory (Default)
|
|
313
546
|
|
|
547
|
+
Best for development, testing, and small datasets. No persistence.
|
|
548
|
+
|
|
314
549
|
```typescript
|
|
315
550
|
await client.initRAG({
|
|
316
551
|
vectorStore: { provider: 'memory' },
|
|
317
552
|
});
|
|
318
553
|
```
|
|
319
554
|
|
|
320
|
-
Best for: Development, testing, small datasets.
|
|
321
|
-
|
|
322
555
|
#### ChromaDB
|
|
323
556
|
|
|
557
|
+
Best for production, large datasets, and persistence.
|
|
558
|
+
|
|
324
559
|
```typescript
|
|
325
560
|
await client.initRAG({
|
|
326
561
|
vectorStore: {
|
|
@@ -331,13 +566,13 @@ await client.initRAG({
|
|
|
331
566
|
});
|
|
332
567
|
```
|
|
333
568
|
|
|
334
|
-
|
|
569
|
+
---
|
|
335
570
|
|
|
336
571
|
### Embedding Providers
|
|
337
572
|
|
|
338
|
-
#### Groq (Default)
|
|
573
|
+
#### Groq Embeddings (Default)
|
|
339
574
|
|
|
340
|
-
|
|
575
|
+
Deterministic pseudo-embeddings for testing. No API cost.
|
|
341
576
|
|
|
342
577
|
```typescript
|
|
343
578
|
await client.initRAG({
|
|
@@ -345,9 +580,9 @@ await client.initRAG({
|
|
|
345
580
|
});
|
|
346
581
|
```
|
|
347
582
|
|
|
348
|
-
#### OpenAI
|
|
583
|
+
#### OpenAI Embeddings
|
|
349
584
|
|
|
350
|
-
|
|
585
|
+
High-quality embeddings for production use.
|
|
351
586
|
|
|
352
587
|
```typescript
|
|
353
588
|
await client.initRAG({
|
|
@@ -360,9 +595,13 @@ await client.initRAG({
|
|
|
360
595
|
});
|
|
361
596
|
```
|
|
362
597
|
|
|
598
|
+
---
|
|
599
|
+
|
|
363
600
|
### Search Providers
|
|
364
601
|
|
|
365
|
-
#### DuckDuckGo (Default
|
|
602
|
+
#### DuckDuckGo (Default)
|
|
603
|
+
|
|
604
|
+
Free, no API key required.
|
|
366
605
|
|
|
367
606
|
```typescript
|
|
368
607
|
import { createSearchProvider } from 'groq-rag';
|
|
@@ -371,6 +610,8 @@ const search = createSearchProvider({ provider: 'duckduckgo' });
|
|
|
371
610
|
|
|
372
611
|
#### Brave Search
|
|
373
612
|
|
|
613
|
+
High-quality results, requires API key.
|
|
614
|
+
|
|
374
615
|
```typescript
|
|
375
616
|
const search = createSearchProvider({
|
|
376
617
|
provider: 'brave',
|
|
@@ -380,6 +621,8 @@ const search = createSearchProvider({
|
|
|
380
621
|
|
|
381
622
|
#### Serper (Google)
|
|
382
623
|
|
|
624
|
+
Google search via Serper API.
|
|
625
|
+
|
|
383
626
|
```typescript
|
|
384
627
|
const search = createSearchProvider({
|
|
385
628
|
provider: 'serper',
|
|
@@ -387,14 +630,17 @@ const search = createSearchProvider({
|
|
|
387
630
|
});
|
|
388
631
|
```
|
|
389
632
|
|
|
390
|
-
|
|
633
|
+
---
|
|
634
|
+
|
|
635
|
+
### Chunking Strategies
|
|
391
636
|
|
|
392
637
|
| Strategy | Description | Best For |
|
|
393
638
|
|----------|-------------|----------|
|
|
394
|
-
| `recursive` | Splits by separators
|
|
639
|
+
| `recursive` | Splits by separators with fallback | General purpose (default) |
|
|
395
640
|
| `fixed` | Fixed character size with overlap | Uniform chunk sizes |
|
|
396
641
|
| `sentence` | Splits by sentence boundaries | Preserving sentence context |
|
|
397
|
-
| `paragraph` | Splits by paragraphs | Document structure
|
|
642
|
+
| `paragraph` | Splits by paragraphs | Document structure |
|
|
643
|
+
| `semantic` | Context-aware boundaries | Preserving meaning |
|
|
398
644
|
|
|
399
645
|
```typescript
|
|
400
646
|
await client.initRAG({
|
|
@@ -408,39 +654,146 @@ await client.initRAG({
|
|
|
408
654
|
|
|
409
655
|
## Utilities
|
|
410
656
|
|
|
657
|
+
Standalone utility functions exported for direct use.
|
|
658
|
+
|
|
411
659
|
```typescript
|
|
412
660
|
import {
|
|
413
661
|
chunkText,
|
|
414
662
|
cosineSimilarity,
|
|
415
663
|
estimateTokens,
|
|
664
|
+
truncateToTokens,
|
|
416
665
|
formatContext,
|
|
417
666
|
extractUrls,
|
|
667
|
+
cleanText,
|
|
668
|
+
generateId,
|
|
669
|
+
sleep,
|
|
670
|
+
retry,
|
|
671
|
+
batch,
|
|
672
|
+
safeJsonParse,
|
|
418
673
|
} from 'groq-rag';
|
|
419
674
|
|
|
420
675
|
// Chunk text manually
|
|
421
|
-
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
|
+
});
|
|
422
681
|
|
|
423
|
-
// Calculate similarity
|
|
682
|
+
// Calculate vector similarity
|
|
424
683
|
const similarity = cosineSimilarity(embedding1, embedding2);
|
|
425
684
|
|
|
426
685
|
// Estimate tokens
|
|
427
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
|
+
}
|
|
428
705
|
```
|
|
429
706
|
|
|
430
707
|
## Examples
|
|
431
708
|
|
|
432
|
-
|
|
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:
|
|
433
724
|
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
-
|
|
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:**
|
|
778
|
+
|
|
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
|
+
```
|
|
440
789
|
|
|
441
790
|
## Development
|
|
442
791
|
|
|
443
792
|
```bash
|
|
793
|
+
# Clone repository
|
|
794
|
+
git clone https://github.com/mithun50/groq-rag.git
|
|
795
|
+
cd groq-rag
|
|
796
|
+
|
|
444
797
|
# Install dependencies
|
|
445
798
|
npm install
|
|
446
799
|
|
|
@@ -450,6 +803,9 @@ npm test
|
|
|
450
803
|
# Run tests in watch mode
|
|
451
804
|
npm run test:watch
|
|
452
805
|
|
|
806
|
+
# Run tests with coverage
|
|
807
|
+
npm run test:coverage
|
|
808
|
+
|
|
453
809
|
# Build
|
|
454
810
|
npm run build
|
|
455
811
|
|
|
@@ -462,8 +818,22 @@ npm run typecheck
|
|
|
462
818
|
|
|
463
819
|
## Contributing
|
|
464
820
|
|
|
465
|
-
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)
|
|
466
828
|
|
|
467
829
|
## License
|
|
468
830
|
|
|
469
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"
|