@umituz/react-native-ai-groq-provider 1.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/LICENSE +21 -0
- package/README.md +266 -0
- package/package.json +96 -0
- package/src/domain/entities/error.types.ts +54 -0
- package/src/domain/entities/groq.types.ts +235 -0
- package/src/domain/entities/index.ts +7 -0
- package/src/domain/entities/models.ts +212 -0
- package/src/index.ts +113 -0
- package/src/infrastructure/services/ChatSession.ts +265 -0
- package/src/infrastructure/services/GroqClient.ts +280 -0
- package/src/infrastructure/services/Streaming.ts +114 -0
- package/src/infrastructure/services/StructuredText.ts +171 -0
- package/src/infrastructure/services/TextGeneration.ts +94 -0
- package/src/infrastructure/services/index.ts +19 -0
- package/src/infrastructure/telemetry/TelemetryHooks.ts +79 -0
- package/src/infrastructure/telemetry/index.ts +5 -0
- package/src/infrastructure/utils/async/execute-state.util.ts +76 -0
- package/src/infrastructure/utils/async/index.ts +6 -0
- package/src/infrastructure/utils/content-mapper.util.ts +86 -0
- package/src/infrastructure/utils/error-mapper.util.ts +77 -0
- package/src/infrastructure/utils/index.ts +7 -0
- package/src/presentation/hooks/useGroq.ts +224 -0
- package/src/presentation/hooks/useOperationManager.ts +110 -0
- package/src/providers/ConfigBuilder.ts +159 -0
- package/src/providers/ProviderFactory.ts +98 -0
- package/src/providers/index.ts +16 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 umituz
|
|
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,266 @@
|
|
|
1
|
+
# @umituz/react-native-ai-groq-provider
|
|
2
|
+
|
|
3
|
+
Groq text generation provider for React Native applications. This package provides a clean, type-safe interface to Groq's ultra-fast LLM API.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🚀 **Ultra-fast inference** - Groq delivers up to 1000 tokens/second
|
|
8
|
+
- 💰 **Affordable pricing** - Starting from $0.05 per 1M tokens
|
|
9
|
+
- 🎯 **Multiple models** - Llama 3.1 8B, Llama 3.3 70B, GPT-OSS, and more
|
|
10
|
+
- 🔄 **Streaming support** - Real-time streaming responses
|
|
11
|
+
- 📦 **Structured output** - Generate JSON with schema validation
|
|
12
|
+
- 💬 **Chat sessions** - Multi-turn conversation management
|
|
13
|
+
- 🔒 **Type-safe** - Full TypeScript support
|
|
14
|
+
- 🪝 **React Hooks** - Easy integration with React/React Native
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @umituz/react-native-ai-groq-provider
|
|
20
|
+
# or
|
|
21
|
+
yarn add @umituz/react-native-ai-groq-provider
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Getting Started
|
|
25
|
+
|
|
26
|
+
### 1. Get a Groq API Key
|
|
27
|
+
|
|
28
|
+
Sign up at [console.groq.com](https://console.groq.com) and get your API key.
|
|
29
|
+
|
|
30
|
+
### 2. Initialize the Provider
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import { configureProvider } from "@umituz/react-native-ai-groq-provider";
|
|
34
|
+
|
|
35
|
+
// Initialize with your API key
|
|
36
|
+
configureProvider({
|
|
37
|
+
apiKey: "your-groq-api-key",
|
|
38
|
+
defaultModel: "llama-3.3-70b-versatile", // Optional
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### 3. Use the useGroq Hook
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { useGroq } from "@umituz/react-native-ai-groq-provider";
|
|
46
|
+
|
|
47
|
+
function MyComponent() {
|
|
48
|
+
const { generate, isLoading, error, result } = useGroq();
|
|
49
|
+
|
|
50
|
+
const handleGenerate = async () => {
|
|
51
|
+
try {
|
|
52
|
+
const response = await generate("Write a short poem about coding");
|
|
53
|
+
console.log(response);
|
|
54
|
+
} catch (err) {
|
|
55
|
+
console.error(err);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
return (
|
|
60
|
+
<View>
|
|
61
|
+
<Button onPress={handleGenerate} disabled={isLoading} />
|
|
62
|
+
{isLoading && <Text>Loading...</Text>}
|
|
63
|
+
{error && <Text>Error: {error}</Text>}
|
|
64
|
+
{result && <Text>{result}</Text>}
|
|
65
|
+
</View>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Usage Examples
|
|
71
|
+
|
|
72
|
+
### Basic Text Generation
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
import { textGeneration } from "@umituz/react-native-ai-groq-provider";
|
|
76
|
+
|
|
77
|
+
const result = await textGeneration("Explain quantum computing in simple terms");
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Chat Conversation
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
import { chatGeneration } from "@umituz/react-native-ai-groq-provider";
|
|
84
|
+
|
|
85
|
+
const messages = [
|
|
86
|
+
{ role: "user", content: "What is React Native?" },
|
|
87
|
+
{ role: "assistant", content: "React Native is..." },
|
|
88
|
+
{ role: "user", content: "How does it differ from React?" },
|
|
89
|
+
];
|
|
90
|
+
|
|
91
|
+
const response = await chatGeneration(messages);
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Structured JSON Output
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
import { structuredText } from "@umituz/react-native-ai-groq-provider";
|
|
98
|
+
|
|
99
|
+
interface TodoItem {
|
|
100
|
+
title: string;
|
|
101
|
+
priority: "high" | "medium" | "low";
|
|
102
|
+
completed: boolean;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const todos = await structuredText<TodoItem>(
|
|
106
|
+
"Create a todo item for learning Groq API",
|
|
107
|
+
{
|
|
108
|
+
schema: {
|
|
109
|
+
type: "object",
|
|
110
|
+
properties: {
|
|
111
|
+
title: { type: "string" },
|
|
112
|
+
priority: { type: "string", enum: ["high", "medium", "low"] },
|
|
113
|
+
completed: { type: "boolean" },
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
}
|
|
117
|
+
);
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Streaming Responses
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
import { useGroq } from "@umituz/react-native-ai-groq-provider";
|
|
124
|
+
|
|
125
|
+
function StreamingComponent() {
|
|
126
|
+
const { stream } = useGroq();
|
|
127
|
+
|
|
128
|
+
const handleStream = async () => {
|
|
129
|
+
let fullText = "";
|
|
130
|
+
await stream(
|
|
131
|
+
"Tell me a story",
|
|
132
|
+
(chunk) => {
|
|
133
|
+
fullText += chunk;
|
|
134
|
+
console.log("Received chunk:", chunk);
|
|
135
|
+
// Update UI with chunk
|
|
136
|
+
}
|
|
137
|
+
);
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
return <Button onPress={handleStream} />;
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Chat Sessions
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
import {
|
|
148
|
+
createChatSession,
|
|
149
|
+
sendChatMessage,
|
|
150
|
+
} from "@umituz/react-native-ai-groq-provider";
|
|
151
|
+
|
|
152
|
+
// Create a chat session
|
|
153
|
+
const session = createChatSession({
|
|
154
|
+
model: "llama-3.3-70b-versatile",
|
|
155
|
+
systemInstruction: "You are a helpful assistant.",
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// Send messages
|
|
159
|
+
const result1 = await sendChatMessage(session.id, "Hello!");
|
|
160
|
+
console.log(result1.response);
|
|
161
|
+
|
|
162
|
+
const result2 = await sendChatMessage(session.id, "How are you?");
|
|
163
|
+
console.log(result2.response);
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## Available Models
|
|
167
|
+
|
|
168
|
+
| Model | Speed | Context | Best For |
|
|
169
|
+
|-------|-------|---------|----------|
|
|
170
|
+
| `llama-3.1-8b-instant` | 560 T/s | 128K | Fast responses, simple tasks |
|
|
171
|
+
| `llama-3.3-70b-versatile` | 280 T/s | 128K | General purpose, complex tasks |
|
|
172
|
+
| `llama-3.1-70b-versatile` | 280 T/s | 128K | General purpose |
|
|
173
|
+
| `openai/gpt-oss-20b` | 1000 T/s | 128K | Experimental, fastest |
|
|
174
|
+
| `openai/gpt-oss-120b` | 400 T/s | 128K | Large tasks |
|
|
175
|
+
| `mixtral-8x7b-32768` | 250 T/s | 32K | MoE model |
|
|
176
|
+
| `gemma2-9b-it` | 450 T/s | 128K | Google's model |
|
|
177
|
+
|
|
178
|
+
## Configuration
|
|
179
|
+
|
|
180
|
+
### Provider Configuration
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
import { configureProvider } from "@umituz/react-native-ai-groq-provider";
|
|
184
|
+
|
|
185
|
+
configureProvider({
|
|
186
|
+
apiKey: "your-api-key",
|
|
187
|
+
baseUrl: "https://api.groq.com/openai/v1", // Optional, default
|
|
188
|
+
timeoutMs: 60000, // Optional, default 60s
|
|
189
|
+
defaultModel: "llama-3.3-70b-versatile", // Optional
|
|
190
|
+
});
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Generation Configuration
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
import { GenerationConfigBuilder } from "@umituz/react-native-ai-groq-provider";
|
|
197
|
+
|
|
198
|
+
const config = GenerationConfigBuilder.create()
|
|
199
|
+
.withTemperature(0.7)
|
|
200
|
+
.withMaxTokens(1024)
|
|
201
|
+
.withTopP(0.9)
|
|
202
|
+
.build();
|
|
203
|
+
|
|
204
|
+
await textGeneration("Your prompt", { generationConfig: config });
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## Error Handling
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
import {
|
|
211
|
+
getUserFriendlyError,
|
|
212
|
+
isRetryableError,
|
|
213
|
+
isAuthError,
|
|
214
|
+
} from "@umituz/react-native-ai-groq-provider";
|
|
215
|
+
|
|
216
|
+
try {
|
|
217
|
+
const result = await textGeneration("Your prompt");
|
|
218
|
+
} catch (error) {
|
|
219
|
+
const friendlyMessage = getUserFriendlyError(error);
|
|
220
|
+
console.log(friendlyMessage);
|
|
221
|
+
|
|
222
|
+
if (isRetryableError(error)) {
|
|
223
|
+
// Retry the request
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (isAuthError(error)) {
|
|
227
|
+
// Check API key
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## API Reference
|
|
233
|
+
|
|
234
|
+
### Hooks
|
|
235
|
+
|
|
236
|
+
- `useGroq(options?)` - Main hook for text generation
|
|
237
|
+
- `useOperationManager()` - Manage async operations
|
|
238
|
+
|
|
239
|
+
### Services
|
|
240
|
+
|
|
241
|
+
- `textGeneration(prompt, options?)` - Generate text from prompt
|
|
242
|
+
- `chatGeneration(messages, options?)` - Generate from chat history
|
|
243
|
+
- `structuredText<T>(prompt, options?)` - Generate structured JSON
|
|
244
|
+
- `streaming(prompt, options?)` - Stream text generation
|
|
245
|
+
- `createChatSession(config?)` - Create chat session
|
|
246
|
+
- `sendChatMessage(sessionId, content, options?)` - Send message in session
|
|
247
|
+
|
|
248
|
+
### Utilities
|
|
249
|
+
|
|
250
|
+
- `ConfigBuilder` - Build provider configuration
|
|
251
|
+
- `GenerationConfigBuilder` - Build generation configuration
|
|
252
|
+
- `providerFactory` - Factory for provider instances
|
|
253
|
+
|
|
254
|
+
## License
|
|
255
|
+
|
|
256
|
+
MIT
|
|
257
|
+
|
|
258
|
+
## Links
|
|
259
|
+
|
|
260
|
+
- [Groq Documentation](https://console.groq.com/docs)
|
|
261
|
+
- [Groq Models](https://console.groq.com/docs/models)
|
|
262
|
+
- [GitHub](https://github.com/umituz/react-native-ai-groq-provider)
|
|
263
|
+
|
|
264
|
+
## Author
|
|
265
|
+
|
|
266
|
+
umituz
|
package/package.json
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@umituz/react-native-ai-groq-provider",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Groq text generation provider for React Native applications",
|
|
5
|
+
"main": "./src/index.ts",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"react-native": "./src/index.ts",
|
|
12
|
+
"default": "./src/index.ts"
|
|
13
|
+
},
|
|
14
|
+
"./package.json": "./package.json"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "tsc --project tsconfig.build.json",
|
|
18
|
+
"typecheck": "tsc --noEmit",
|
|
19
|
+
"lint": "eslint src --ext .ts,.tsx --max-warnings 0",
|
|
20
|
+
"lint:fix": "eslint src --ext .ts,.tsx --fix",
|
|
21
|
+
"version:patch": "npm version patch -m 'chore: release v%s'",
|
|
22
|
+
"version:minor": "npm version minor -m 'chore: release v%s'",
|
|
23
|
+
"version:major": "npm version major -m 'chore: release v%s'"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"react-native",
|
|
27
|
+
"ai",
|
|
28
|
+
"groq",
|
|
29
|
+
"llama",
|
|
30
|
+
"text-generation",
|
|
31
|
+
"llm",
|
|
32
|
+
"openai-compatible"
|
|
33
|
+
],
|
|
34
|
+
"author": "umituz",
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "https://github.com/umituz/react-native-ai-groq-provider"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"react": ">=19.0.0",
|
|
42
|
+
"react-native": ">=0.74.0"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@expo/vector-icons": "^15.0.3",
|
|
47
|
+
"@react-native-async-storage/async-storage": "^2.2.0",
|
|
48
|
+
"@react-native-community/slider": "^5.1.1",
|
|
49
|
+
"@react-navigation/bottom-tabs": "^7.9.0",
|
|
50
|
+
"@react-navigation/native": "^7.1.26",
|
|
51
|
+
"@react-navigation/stack": "^7.6.13",
|
|
52
|
+
"@tanstack/react-query": "^5.90.16",
|
|
53
|
+
"@types/react": "~19.1.10",
|
|
54
|
+
"@typescript-eslint/eslint-plugin": "^7.0.0",
|
|
55
|
+
"@typescript-eslint/parser": "^7.0.0",
|
|
56
|
+
"@umituz/react-native-design-system": "*",
|
|
57
|
+
"eslint": "^8.57.0",
|
|
58
|
+
"expo-apple-authentication": "^8.0.8",
|
|
59
|
+
"expo-application": "^7.0.8",
|
|
60
|
+
"expo-auth-session": "^7.0.10",
|
|
61
|
+
"expo-clipboard": "^8.0.8",
|
|
62
|
+
"expo-crypto": "^15.0.8",
|
|
63
|
+
"expo-device": "^8.0.10",
|
|
64
|
+
"expo-file-system": "^19.0.21",
|
|
65
|
+
"expo-font": "^14.0.10",
|
|
66
|
+
"expo-haptics": "^15.0.8",
|
|
67
|
+
"expo-image": "^3.0.11",
|
|
68
|
+
"expo-image-manipulator": "^14.0.8",
|
|
69
|
+
"expo-localization": "^17.0.8",
|
|
70
|
+
"expo-media-library": "^18.2.1",
|
|
71
|
+
"expo-modules-core": "^3.0.29",
|
|
72
|
+
"expo-network": "^8.0.8",
|
|
73
|
+
"expo-secure-store": "^15.0.8",
|
|
74
|
+
"expo-sharing": "^14.0.8",
|
|
75
|
+
"expo-video": "^3.0.15",
|
|
76
|
+
"expo-web-browser": "^15.0.10",
|
|
77
|
+
"firebase": "^12.7.0",
|
|
78
|
+
"i18next": "^25.7.4",
|
|
79
|
+
"react": "19.1.0",
|
|
80
|
+
"react-i18next": "^16.5.1",
|
|
81
|
+
"react-native": "0.81.5",
|
|
82
|
+
"react-native-gesture-handler": "^2.30.0",
|
|
83
|
+
"react-native-safe-area-context": "^5.6.2",
|
|
84
|
+
"react-native-svg": "^15.15.1",
|
|
85
|
+
"typescript": "^5.3.0"
|
|
86
|
+
},
|
|
87
|
+
"publishConfig": {
|
|
88
|
+
"access": "public"
|
|
89
|
+
},
|
|
90
|
+
"files": [
|
|
91
|
+
"src",
|
|
92
|
+
"dist",
|
|
93
|
+
"README.md",
|
|
94
|
+
"LICENSE"
|
|
95
|
+
]
|
|
96
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom error class for Groq operations
|
|
3
|
+
*/
|
|
4
|
+
export class GroqError extends Error {
|
|
5
|
+
constructor(
|
|
6
|
+
public type: GroqErrorType,
|
|
7
|
+
message: string,
|
|
8
|
+
public cause?: unknown
|
|
9
|
+
) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.name = "GroqError";
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Error types for Groq operations
|
|
17
|
+
*/
|
|
18
|
+
export enum GroqErrorType {
|
|
19
|
+
/** API key is missing or invalid */
|
|
20
|
+
INVALID_API_KEY = "INVALID_API_KEY",
|
|
21
|
+
/** Configuration is missing or invalid */
|
|
22
|
+
MISSING_CONFIG = "MISSING_CONFIG",
|
|
23
|
+
/** Network error occurred */
|
|
24
|
+
NETWORK_ERROR = "NETWORK_ERROR",
|
|
25
|
+
/** Request was aborted */
|
|
26
|
+
ABORT_ERROR = "ABORT_ERROR",
|
|
27
|
+
/** Rate limit exceeded */
|
|
28
|
+
RATE_LIMIT_ERROR = "RATE_LIMIT_ERROR",
|
|
29
|
+
/** Insufficient quota */
|
|
30
|
+
QUOTA_EXCEEDED = "QUOTA_EXCEEDED",
|
|
31
|
+
/** Server error */
|
|
32
|
+
SERVER_ERROR = "SERVER_ERROR",
|
|
33
|
+
/** Unknown error occurred */
|
|
34
|
+
UNKNOWN_ERROR = "UNKNOWN_ERROR",
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Map HTTP status codes to error types
|
|
39
|
+
*/
|
|
40
|
+
export function mapHttpStatusToErrorType(status: number): GroqErrorType {
|
|
41
|
+
if (status === 401 || status === 403) {
|
|
42
|
+
return GroqErrorType.INVALID_API_KEY;
|
|
43
|
+
}
|
|
44
|
+
if (status === 429) {
|
|
45
|
+
return GroqErrorType.RATE_LIMIT_ERROR;
|
|
46
|
+
}
|
|
47
|
+
if (status >= 500 && status < 600) {
|
|
48
|
+
return GroqErrorType.SERVER_ERROR;
|
|
49
|
+
}
|
|
50
|
+
if (status >= 400 && status < 500) {
|
|
51
|
+
return GroqErrorType.INVALID_API_KEY;
|
|
52
|
+
}
|
|
53
|
+
return GroqErrorType.UNKNOWN_ERROR;
|
|
54
|
+
}
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration for Groq client initialization
|
|
3
|
+
*/
|
|
4
|
+
export interface GroqConfig {
|
|
5
|
+
/** API key for authentication */
|
|
6
|
+
apiKey: string;
|
|
7
|
+
/** Base URL for API requests (default: https://api.groq.com/openai/v1) */
|
|
8
|
+
baseUrl?: string;
|
|
9
|
+
/** Default timeout in milliseconds */
|
|
10
|
+
timeoutMs?: number;
|
|
11
|
+
/** Default model to use for text generation */
|
|
12
|
+
textModel?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Generation configuration for AI requests
|
|
17
|
+
*/
|
|
18
|
+
export interface GroqGenerationConfig {
|
|
19
|
+
/** Controls randomness (0.0 - 2.0, default: 0.7) */
|
|
20
|
+
temperature?: number;
|
|
21
|
+
/** Maximum number of tokens to generate */
|
|
22
|
+
maxTokens?: number;
|
|
23
|
+
/** Nucleus sampling threshold (0.0 - 1.0) */
|
|
24
|
+
topP?: number;
|
|
25
|
+
/** Number of completions to generate */
|
|
26
|
+
n?: number;
|
|
27
|
+
/** Stop sequences */
|
|
28
|
+
stop?: string[];
|
|
29
|
+
/** Frequency penalty (-2.0 to 2.0) */
|
|
30
|
+
frequencyPenalty?: number;
|
|
31
|
+
/** Presence penalty (-2.0 to 2.0) */
|
|
32
|
+
presencePenalty?: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Message role in chat conversation
|
|
37
|
+
*/
|
|
38
|
+
export type GroqMessageRole = "system" | "user" | "assistant";
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Chat message structure
|
|
42
|
+
*/
|
|
43
|
+
export interface GroqMessage {
|
|
44
|
+
/** Role of the message sender */
|
|
45
|
+
role: GroqMessageRole;
|
|
46
|
+
/** Content of the message */
|
|
47
|
+
content: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Chat completion request
|
|
52
|
+
*/
|
|
53
|
+
export interface GroqChatRequest {
|
|
54
|
+
/** Model to use for generation */
|
|
55
|
+
model: string;
|
|
56
|
+
/** Array of messages in the conversation */
|
|
57
|
+
messages: GroqMessage[];
|
|
58
|
+
/** Generation configuration */
|
|
59
|
+
temperature?: number;
|
|
60
|
+
max_tokens?: number;
|
|
61
|
+
top_p?: number;
|
|
62
|
+
n?: number;
|
|
63
|
+
stop?: string[];
|
|
64
|
+
frequency_penalty?: number;
|
|
65
|
+
presence_penalty?: number;
|
|
66
|
+
/** Enable streaming response */
|
|
67
|
+
stream?: boolean;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Chat completion response
|
|
72
|
+
*/
|
|
73
|
+
export interface GroqChatResponse {
|
|
74
|
+
/** Unique identifier for the response */
|
|
75
|
+
id: string;
|
|
76
|
+
/** Object type (chat.completion) */
|
|
77
|
+
object: string;
|
|
78
|
+
/** Timestamp of creation */
|
|
79
|
+
created: number;
|
|
80
|
+
/** Model used for generation */
|
|
81
|
+
model: string;
|
|
82
|
+
/** Array of completion choices */
|
|
83
|
+
choices: GroqChoice[];
|
|
84
|
+
/** Token usage information */
|
|
85
|
+
usage: GroqUsage;
|
|
86
|
+
/** System fingerprint (Groq specific) */
|
|
87
|
+
system_fingerprint?: string;
|
|
88
|
+
/** X Groq (Groq specific) */
|
|
89
|
+
x_groq?: {
|
|
90
|
+
id?: string;
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Individual completion choice
|
|
96
|
+
*/
|
|
97
|
+
export interface GroqChoice {
|
|
98
|
+
/** Index of the choice */
|
|
99
|
+
index: number;
|
|
100
|
+
/** Generated message */
|
|
101
|
+
message: {
|
|
102
|
+
role: "assistant";
|
|
103
|
+
content: string;
|
|
104
|
+
};
|
|
105
|
+
/** Reason for finish (stop, length, etc.) */
|
|
106
|
+
finish_reason: GroqFinishReason;
|
|
107
|
+
/** Logprobs (optional) */
|
|
108
|
+
logprobs: null | object;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Finish reason types
|
|
113
|
+
*/
|
|
114
|
+
export type GroqFinishReason = "stop" | "length" | "content_filter";
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Token usage information
|
|
118
|
+
*/
|
|
119
|
+
export interface GroqUsage {
|
|
120
|
+
/** Number of tokens in the prompt */
|
|
121
|
+
prompt_tokens: number;
|
|
122
|
+
/** Number of tokens in the completion */
|
|
123
|
+
completion_tokens: number;
|
|
124
|
+
/** Total number of tokens used */
|
|
125
|
+
total_tokens: number;
|
|
126
|
+
/** Prompt time (Groq specific) */
|
|
127
|
+
prompt_time?: number;
|
|
128
|
+
/** Completion time (Groq specific) */
|
|
129
|
+
completion_time?: number;
|
|
130
|
+
/** Total time (Groq specific) */
|
|
131
|
+
total_time?: number;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Streaming chunk response
|
|
136
|
+
*/
|
|
137
|
+
export interface GroqChatChunk {
|
|
138
|
+
/** Unique identifier for the response */
|
|
139
|
+
id: string;
|
|
140
|
+
/** Object type (chat.completion.chunk) */
|
|
141
|
+
object: string;
|
|
142
|
+
/** Timestamp of creation */
|
|
143
|
+
created: number;
|
|
144
|
+
/** Model used for generation */
|
|
145
|
+
model: string;
|
|
146
|
+
/** Array of completion choices */
|
|
147
|
+
choices: GroqChunkChoice[];
|
|
148
|
+
/** System fingerprint (Groq specific) */
|
|
149
|
+
system_fingerprint?: string;
|
|
150
|
+
/** X Groq (Groq specific) */
|
|
151
|
+
x_groq?: {
|
|
152
|
+
id?: string;
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Individual chunk choice for streaming
|
|
158
|
+
*/
|
|
159
|
+
export interface GroqChunkChoice {
|
|
160
|
+
/** Index of the choice */
|
|
161
|
+
index: number;
|
|
162
|
+
/** Delta message (partial content) */
|
|
163
|
+
delta: {
|
|
164
|
+
role?: "assistant";
|
|
165
|
+
content?: string;
|
|
166
|
+
};
|
|
167
|
+
/** Reason for finish (null if not finished) */
|
|
168
|
+
finish_reason: GroqFinishReason | null;
|
|
169
|
+
/** Logprobs (optional) */
|
|
170
|
+
logprobs: null | object;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* API error response
|
|
175
|
+
*/
|
|
176
|
+
export interface GroqErrorResponse {
|
|
177
|
+
/** Error type */
|
|
178
|
+
error: {
|
|
179
|
+
/** Error message */
|
|
180
|
+
message: string;
|
|
181
|
+
/** Error type */
|
|
182
|
+
type: string;
|
|
183
|
+
/** Error code */
|
|
184
|
+
code?: string;
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Configuration for a chat session
|
|
190
|
+
*/
|
|
191
|
+
export interface GroqChatConfig {
|
|
192
|
+
/** Model name override */
|
|
193
|
+
model?: string;
|
|
194
|
+
/** System instruction for the model */
|
|
195
|
+
systemInstruction?: string;
|
|
196
|
+
/** Generation config (temperature, maxTokens, etc.) */
|
|
197
|
+
generationConfig?: GroqGenerationConfig;
|
|
198
|
+
/** Initial conversation history */
|
|
199
|
+
history?: GroqMessage[];
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Available Groq models
|
|
204
|
+
*/
|
|
205
|
+
export const GROQ_MODELS = {
|
|
206
|
+
/** Llama 3.1 8B - Fast and efficient (560 T/s) */
|
|
207
|
+
LLAMA_3_1_8B_INSTANT: "llama-3.1-8b-instant",
|
|
208
|
+
/** Llama 3.3 70B - Versatile and powerful (280 T/s) */
|
|
209
|
+
LLAMA_3_3_70B_VERSATILE: "llama-3.3-70b-versatile",
|
|
210
|
+
/** Llama 3.1 70B - Versatile (280 T/s) */
|
|
211
|
+
LLAMA_3_1_70B_VERSATILE: "llama-3.1-70b-versatile",
|
|
212
|
+
/** GPT-OSS 20B - Experimental (1000 T/s) */
|
|
213
|
+
GPT_OSS_20B: "openai/gpt-oss-20b",
|
|
214
|
+
/** GPT-OSS 120B - Large experimental model */
|
|
215
|
+
GPT_OSS_120B: "openai/gpt-oss-120b",
|
|
216
|
+
/** Mixtral 8x7b - MoE model */
|
|
217
|
+
MIXTRAL_8X7B: "mixtral-8x7b-32768",
|
|
218
|
+
/** Gemma 2 9B - Google's model */
|
|
219
|
+
GEMMA_2_9B: "gemma2-9b-it",
|
|
220
|
+
/** Llama 4 Scout 17B - New model (30K T/s) */
|
|
221
|
+
LLAMA_4_SCOUT_17B: "meta-llama/llama-4-scout-17b-16e-instruct",
|
|
222
|
+
/** Kimi K2 - Moonshot AI model */
|
|
223
|
+
KIMI_K2_INSTRUCT: "moonshotai/kimi-k2-instruct",
|
|
224
|
+
/** Qwen 3 32B - Alibaba's model */
|
|
225
|
+
QWEN3_32B: "qwen/qwen3-32b",
|
|
226
|
+
} as const;
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Default models for different use cases
|
|
230
|
+
*/
|
|
231
|
+
export const DEFAULT_MODELS = {
|
|
232
|
+
TEXT: GROQ_MODELS.LLAMA_3_1_8B_INSTANT,
|
|
233
|
+
FAST: GROQ_MODELS.LLAMA_3_1_8B_INSTANT,
|
|
234
|
+
EXPERIMENTAL: GROQ_MODELS.GPT_OSS_20B,
|
|
235
|
+
} as const;
|