@umituz/web-ai-groq-provider 1.1.13 → 2.0.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/README.md +154 -72
- package/dist/domains/chat/index.cjs +539 -0
- package/dist/domains/chat/index.cjs.map +1 -0
- package/dist/domains/chat/index.d.cts +322 -0
- package/dist/domains/chat/index.d.ts +322 -0
- package/dist/domains/chat/index.js +531 -0
- package/dist/domains/chat/index.js.map +1 -0
- package/dist/domains/groq/index.cjs +1499 -0
- package/dist/domains/groq/index.cjs.map +1 -0
- package/dist/domains/groq/index.d.cts +561 -0
- package/dist/domains/groq/index.d.ts +561 -0
- package/dist/domains/groq/index.js +1453 -0
- package/dist/domains/groq/index.js.map +1 -0
- package/dist/index.cjs +1993 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +53 -0
- package/dist/index.d.ts +53 -0
- package/dist/index.js +1937 -0
- package/dist/index.js.map +1 -0
- package/dist/text-generation.service-BP4Q_SH0.d.cts +252 -0
- package/dist/text-generation.service-BP4Q_SH0.d.ts +252 -0
- package/package.json +25 -11
- package/src/domains/chat/entities/chat.entities.ts +0 -113
- package/src/domains/chat/entities/index.ts +0 -14
- package/src/domains/chat/hooks/index.ts +0 -5
- package/src/domains/chat/hooks/use-chat.hook.ts +0 -259
- package/src/domains/chat/index.ts +0 -32
- package/src/domains/chat/interfaces/chat.interface.ts +0 -80
- package/src/domains/chat/interfaces/index.ts +0 -10
- package/src/domains/chat/services/chat.service.ts +0 -195
- package/src/domains/chat/services/index.ts +0 -5
- package/src/domains/chat/utils/message-formatter.ts +0 -49
- package/src/domains/groq/constants/error.constants.ts +0 -46
- package/src/domains/groq/constants/groq.constants.ts +0 -68
- package/src/domains/groq/constants/index.ts +0 -20
- package/src/domains/groq/entities/groq.entities.ts +0 -196
- package/src/domains/groq/entities/index.ts +0 -20
- package/src/domains/groq/hooks/index.ts +0 -7
- package/src/domains/groq/hooks/use-groq.hook.ts +0 -232
- package/src/domains/groq/index.ts +0 -72
- package/src/domains/groq/interfaces/groq.interface.ts +0 -77
- package/src/domains/groq/interfaces/index.ts +0 -22
- package/src/domains/groq/services/http-client.service.ts +0 -265
- package/src/domains/groq/services/index.ts +0 -9
- package/src/domains/groq/services/text-generation.service.ts +0 -238
- package/src/domains/groq/utils/cache-manager.util.ts +0 -215
- package/src/domains/groq/utils/debounce.util.ts +0 -268
- package/src/domains/groq/utils/error.util.ts +0 -57
- package/src/domains/groq/utils/groq-error.util.ts +0 -18
- package/src/domains/groq/utils/index.ts +0 -37
- package/src/domains/groq/utils/message.util.ts +0 -57
- package/src/domains/groq/utils/request-deduplicator.util.ts +0 -123
- package/src/domains/groq/utils/request-queue.util.ts +0 -178
- package/src/domains/groq/utils/retry.util.ts +0 -207
- package/src/index.ts +0 -15
package/README.md
CHANGED
|
@@ -1,50 +1,52 @@
|
|
|
1
|
-
# @umituz/
|
|
1
|
+
# @umituz/web-ai-groq-provider
|
|
2
2
|
|
|
3
|
-
Groq AI text generation provider for React web applications.
|
|
3
|
+
Groq AI text generation and chat provider for React web applications.
|
|
4
|
+
Provides a clean, type-safe interface to Groq's ultra-fast LLM API with
|
|
5
|
+
dependency-injected services, strategy-based request execution, and
|
|
6
|
+
zero hardcoded language content.
|
|
4
7
|
|
|
5
8
|
## Features
|
|
6
9
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
10
|
+
- Ultra-fast inference (up to 1000 tokens/second)
|
|
11
|
+
- Multiple models (Llama 3.x, GPT-OSS, Mixtral, Gemma 2, Kimi K2, Qwen 3)
|
|
12
|
+
- Streaming responses via SSE
|
|
13
|
+
- Structured JSON output with schema-aware prompts
|
|
14
|
+
- Multi-turn chat sessions with pluggable prompt strategy
|
|
15
|
+
- Type-safe, fully tree-shakable subpath imports
|
|
16
|
+
- Dependency injection ready (no hidden singletons inside services)
|
|
17
|
+
- React Hooks with isolated, component-scoped abort signals
|
|
15
18
|
|
|
16
19
|
## Installation
|
|
17
20
|
|
|
18
21
|
```bash
|
|
19
|
-
npm install @umituz/
|
|
22
|
+
npm install @umituz/web-ai-groq-provider
|
|
20
23
|
# or
|
|
21
|
-
yarn add @umituz/
|
|
24
|
+
yarn add @umituz/web-ai-groq-provider
|
|
22
25
|
# or
|
|
23
|
-
pnpm add @umituz/
|
|
26
|
+
pnpm add @umituz/web-ai-groq-provider
|
|
24
27
|
```
|
|
25
28
|
|
|
26
29
|
## Getting Started
|
|
27
30
|
|
|
28
31
|
### 1. Get a Groq API Key
|
|
29
32
|
|
|
30
|
-
Sign up at [console.groq.com](https://console.groq.com) and
|
|
33
|
+
Sign up at [console.groq.com](https://console.groq.com) and obtain an API key.
|
|
31
34
|
|
|
32
|
-
### 2. Initialize the Provider
|
|
35
|
+
### 2. Initialize the Provider (once at app startup)
|
|
33
36
|
|
|
34
37
|
```typescript
|
|
35
|
-
import { configureProvider } from "@umituz/
|
|
38
|
+
import { configureProvider } from "@umituz/web-ai-groq-provider";
|
|
36
39
|
|
|
37
|
-
// Initialize with your API key
|
|
38
40
|
configureProvider({
|
|
39
41
|
apiKey: "your-groq-api-key",
|
|
40
|
-
defaultModel: "llama-3.3-70b-versatile", //
|
|
42
|
+
defaultModel: "llama-3.3-70b-versatile", // optional
|
|
41
43
|
});
|
|
42
44
|
```
|
|
43
45
|
|
|
44
|
-
### 3. Use the useGroq Hook
|
|
46
|
+
### 3. Use the `useGroq` Hook
|
|
45
47
|
|
|
46
48
|
```typescript
|
|
47
|
-
import { useGroq } from "@umituz/
|
|
49
|
+
import { useGroq } from "@umituz/web-ai-groq-provider";
|
|
48
50
|
|
|
49
51
|
function MyComponent() {
|
|
50
52
|
const { generate, isLoading, error, result } = useGroq();
|
|
@@ -73,32 +75,56 @@ function MyComponent() {
|
|
|
73
75
|
|
|
74
76
|
## Usage Examples
|
|
75
77
|
|
|
76
|
-
###
|
|
78
|
+
### Dependency Injection (advanced)
|
|
79
|
+
|
|
80
|
+
The hooks accept a second argument for explicit DI. Use this when you
|
|
81
|
+
need full control over the container, e.g., for tests or per-tenant keys.
|
|
77
82
|
|
|
78
83
|
```typescript
|
|
79
|
-
import {
|
|
84
|
+
import { useGroq, GroqHttpClient, TextGenerationService } from "@umituz/web-ai-groq-provider";
|
|
85
|
+
|
|
86
|
+
const httpClient = new GroqHttpClient();
|
|
87
|
+
httpClient.initialize({ apiKey: "your-groq-api-key" });
|
|
88
|
+
const textGeneration = new TextGenerationService(httpClient);
|
|
80
89
|
|
|
81
|
-
|
|
90
|
+
function MyComponent() {
|
|
91
|
+
const { generate } = useGroq({}, { textGeneration });
|
|
92
|
+
// ...
|
|
93
|
+
}
|
|
82
94
|
```
|
|
83
95
|
|
|
84
|
-
###
|
|
96
|
+
### Streaming Responses
|
|
85
97
|
|
|
86
98
|
```typescript
|
|
87
|
-
import {
|
|
99
|
+
import { useGroq } from "@umituz/web-ai-groq-provider";
|
|
100
|
+
|
|
101
|
+
function StreamingComponent() {
|
|
102
|
+
const { stream, result } = useGroq();
|
|
88
103
|
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
104
|
+
const handleStream = async () => {
|
|
105
|
+
await stream(
|
|
106
|
+
"Tell me a story",
|
|
107
|
+
(chunk) => {
|
|
108
|
+
console.log("Received chunk:", chunk);
|
|
109
|
+
},
|
|
110
|
+
);
|
|
111
|
+
};
|
|
94
112
|
|
|
95
|
-
|
|
113
|
+
return (
|
|
114
|
+
<div>
|
|
115
|
+
<button onClick={handleStream}>Stream Story</button>
|
|
116
|
+
{result && <p>{result}</p>}
|
|
117
|
+
</div>
|
|
118
|
+
);
|
|
119
|
+
}
|
|
96
120
|
```
|
|
97
121
|
|
|
98
122
|
### Structured JSON Output
|
|
99
123
|
|
|
124
|
+
Use the lower-level service when you need a structured payload.
|
|
125
|
+
|
|
100
126
|
```typescript
|
|
101
|
-
import {
|
|
127
|
+
import { getProvider } from "@umituz/web-ai-groq-provider";
|
|
102
128
|
|
|
103
129
|
interface TodoItem {
|
|
104
130
|
title: string;
|
|
@@ -106,8 +132,9 @@ interface TodoItem {
|
|
|
106
132
|
completed: boolean;
|
|
107
133
|
}
|
|
108
134
|
|
|
109
|
-
const
|
|
110
|
-
|
|
135
|
+
const { textGeneration } = getProvider()!;
|
|
136
|
+
const todo = await textGeneration.generateStructured<TodoItem>(
|
|
137
|
+
"Create a todo item for learning the Groq API",
|
|
111
138
|
{
|
|
112
139
|
schema: {
|
|
113
140
|
type: "object",
|
|
@@ -117,74 +144,129 @@ const todos = await structuredText<TodoItem>(
|
|
|
117
144
|
completed: { type: "boolean" },
|
|
118
145
|
},
|
|
119
146
|
},
|
|
120
|
-
}
|
|
147
|
+
},
|
|
121
148
|
);
|
|
122
149
|
```
|
|
123
150
|
|
|
124
|
-
###
|
|
151
|
+
### Chat Conversations
|
|
125
152
|
|
|
126
153
|
```typescript
|
|
127
|
-
import {
|
|
154
|
+
import { getProvider, useChat } from "@umituz/web-ai-groq-provider";
|
|
128
155
|
|
|
129
|
-
function
|
|
130
|
-
const {
|
|
156
|
+
function Chat() {
|
|
157
|
+
const { messages, sendMessage, isLoading, error } = useChat({
|
|
158
|
+
conversationId: "demo-1",
|
|
159
|
+
config: { systemPrompt: "You are a helpful assistant." },
|
|
160
|
+
});
|
|
131
161
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
162
|
+
return (
|
|
163
|
+
<div>
|
|
164
|
+
{messages.map((m) => (
|
|
165
|
+
<p key={m.id}>
|
|
166
|
+
<strong>{m.sender}:</strong> {m.content}
|
|
167
|
+
</p>
|
|
168
|
+
))}
|
|
169
|
+
<button
|
|
170
|
+
disabled={isLoading}
|
|
171
|
+
onClick={() => sendMessage("What is React?")}
|
|
172
|
+
>
|
|
173
|
+
Ask
|
|
174
|
+
</button>
|
|
175
|
+
{error && <p>Error: {error}</p>}
|
|
176
|
+
</div>
|
|
177
|
+
);
|
|
145
178
|
}
|
|
146
179
|
```
|
|
147
180
|
|
|
181
|
+
#### Pluggable Prompt Strategy
|
|
182
|
+
|
|
183
|
+
`ChatService` does not hardcode any language or fallback content. Plug
|
|
184
|
+
in your own prompt resolver:
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
import { getProvider } from "@umituz/web-ai-groq-provider";
|
|
188
|
+
|
|
189
|
+
const { chat } = getProvider()!;
|
|
190
|
+
chat.setPromptStrategy({
|
|
191
|
+
resolve: (config) => config.systemPrompt ?? "Default prompt here.",
|
|
192
|
+
});
|
|
193
|
+
```
|
|
194
|
+
|
|
148
195
|
## Available Models
|
|
149
196
|
|
|
150
|
-
| Model
|
|
151
|
-
|
|
152
|
-
| `llama-3.1-8b-instant`
|
|
153
|
-
| `llama-3.3-70b-versatile`
|
|
154
|
-
| `llama-3.1-70b-versatile`
|
|
155
|
-
| `openai/gpt-oss-20b`
|
|
156
|
-
| `openai/gpt-oss-120b`
|
|
157
|
-
| `mixtral-8x7b-32768`
|
|
158
|
-
| `gemma2-9b-it`
|
|
197
|
+
| Model | Speed | Context | Notes |
|
|
198
|
+
| ---------------------------------- | -------- | ------- | ------------------------------ |
|
|
199
|
+
| `llama-3.1-8b-instant` | 560 T/s | 128K | Fast responses, simple tasks |
|
|
200
|
+
| `llama-3.3-70b-versatile` | 280 T/s | 128K | General purpose, complex tasks |
|
|
201
|
+
| `llama-3.1-70b-versatile` | 280 T/s | 128K | General purpose |
|
|
202
|
+
| `openai/gpt-oss-20b` | 1000 T/s | 128K | Experimental, fastest |
|
|
203
|
+
| `openai/gpt-oss-120b` | 400 T/s | 128K | Large tasks |
|
|
204
|
+
| `mixtral-8x7b-32768` | 250 T/s | 32K | MoE model |
|
|
205
|
+
| `gemma2-9b-it` | 450 T/s | 128K | Google |
|
|
206
|
+
| `meta-llama/llama-4-scout-17b-16e-instruct` | ~30K T/s | large | Llama 4 Scout |
|
|
207
|
+
| `moonshotai/kimi-k2-instruct` | - | - | Moonshot AI |
|
|
208
|
+
| `qwen/qwen3-32b` | - | - | Alibaba Qwen 3 |
|
|
159
209
|
|
|
160
210
|
## Configuration
|
|
161
211
|
|
|
162
212
|
### Provider Configuration
|
|
163
213
|
|
|
164
214
|
```typescript
|
|
165
|
-
import { configureProvider } from "@umituz/
|
|
215
|
+
import { configureProvider } from "@umituz/web-ai-groq-provider";
|
|
166
216
|
|
|
167
217
|
configureProvider({
|
|
168
218
|
apiKey: "your-api-key",
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
219
|
+
// All fields below are optional.
|
|
220
|
+
baseUrl: "https://api.groq.com/openai/v1",
|
|
221
|
+
timeoutMs: 60_000,
|
|
222
|
+
textModel: "llama-3.3-70b-versatile",
|
|
172
223
|
});
|
|
173
224
|
```
|
|
174
225
|
|
|
175
226
|
### Generation Configuration
|
|
176
227
|
|
|
228
|
+
Pass a `GroqGenerationConfig` to the hook methods.
|
|
229
|
+
|
|
177
230
|
```typescript
|
|
178
|
-
|
|
231
|
+
const { generate } = useGroq();
|
|
232
|
+
|
|
233
|
+
await generate("Your prompt", {
|
|
234
|
+
temperature: 0.7,
|
|
235
|
+
maxTokens: 1024,
|
|
236
|
+
topP: 0.9,
|
|
237
|
+
});
|
|
238
|
+
```
|
|
179
239
|
|
|
180
|
-
|
|
181
|
-
.withTemperature(0.7)
|
|
182
|
-
.withMaxTokens(1024)
|
|
183
|
-
.withTopP(0.9)
|
|
184
|
-
.build();
|
|
240
|
+
## Architecture
|
|
185
241
|
|
|
186
|
-
await textGeneration("Your prompt", { generationConfig: config });
|
|
187
242
|
```
|
|
243
|
+
src/
|
|
244
|
+
provider.ts # App-level composition root
|
|
245
|
+
domains/
|
|
246
|
+
groq/
|
|
247
|
+
clients/ # Transport + config + facade
|
|
248
|
+
services/ # High-level text generation orchestrator
|
|
249
|
+
strategies/ # Retry / Queue / Dedupe / Composite
|
|
250
|
+
builders/ # Request + structured prompt builders
|
|
251
|
+
parsers/ # Streaming + JSON response parsers
|
|
252
|
+
hooks/ # useGroq
|
|
253
|
+
utils/ # Cache, retry, debounce, errors, etc.
|
|
254
|
+
constants/ # Models, error types, protocols, IDs
|
|
255
|
+
entities/ # Domain types
|
|
256
|
+
interfaces/ # Contracts
|
|
257
|
+
chat/
|
|
258
|
+
services/ # ChatService
|
|
259
|
+
hooks/ # useChat
|
|
260
|
+
utils/ # Formatter, ID generator, prompt strategy
|
|
261
|
+
entities/ # Domain types
|
|
262
|
+
interfaces/ # Contracts
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
The package follows SOLID throughout. Request execution is composed via
|
|
266
|
+
the Strategy pattern (`CompositeExecutor` of `RetryExecutor`,
|
|
267
|
+
`QueueExecutor`, and `DedupeExecutor`), so adding a new behavior is a
|
|
268
|
+
matter of providing a new `IRequestExecutor` and inserting it into the
|
|
269
|
+
chain.
|
|
188
270
|
|
|
189
271
|
## License
|
|
190
272
|
|