@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
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Groq Constants
|
|
3
|
-
* @description Available models and default configurations
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Available Groq models
|
|
8
|
-
*/
|
|
9
|
-
export const GROQ_MODELS = {
|
|
10
|
-
/** Llama 3.1 8B - Fast and efficient (560 T/s) */
|
|
11
|
-
LLAMA_3_1_8B_INSTANT: "llama-3.1-8b-instant",
|
|
12
|
-
/** Llama 3.3 70B - Versatile and powerful (280 T/s) */
|
|
13
|
-
LLAMA_3_3_70B_VERSATILE: "llama-3.3-70b-versatile",
|
|
14
|
-
/** Llama 3.1 70B - Versatile (280 T/s) */
|
|
15
|
-
LLAMA_3_1_70B_VERSATILE: "llama-3.1-70b-versatile",
|
|
16
|
-
/** GPT-OSS 20B - Experimental (1000 T/s) */
|
|
17
|
-
GPT_OSS_20B: "openai/gpt-oss-20b",
|
|
18
|
-
/** GPT-OSS 120B - Large experimental model */
|
|
19
|
-
GPT_OSS_120B: "openai/gpt-oss-120b",
|
|
20
|
-
/** Mixtral 8x7b - MoE model */
|
|
21
|
-
MIXTRAL_8X7B: "mixtral-8x7b-32768",
|
|
22
|
-
/** Gemma 2 9B - Google's model */
|
|
23
|
-
GEMMA_2_9B: "gemma2-9b-it",
|
|
24
|
-
/** Llama 4 Scout 17B - New model (30K T/s) */
|
|
25
|
-
LLAMA_4_SCOUT_17B: "meta-llama/llama-4-scout-17b-16e-instruct",
|
|
26
|
-
/** Kimi K2 - Moonshot AI model */
|
|
27
|
-
KIMI_K2_INSTRUCT: "moonshotai/kimi-k2-instruct",
|
|
28
|
-
/** Qwen 3 32B - Alibaba's model */
|
|
29
|
-
QWEN3_32B: "qwen/qwen3-32b",
|
|
30
|
-
} as const;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Default models for different use cases
|
|
34
|
-
*/
|
|
35
|
-
export const DEFAULT_MODELS = {
|
|
36
|
-
TEXT: GROQ_MODELS.LLAMA_3_1_8B_INSTANT,
|
|
37
|
-
FAST: GROQ_MODELS.LLAMA_3_1_8B_INSTANT,
|
|
38
|
-
EXPERIMENTAL: GROQ_MODELS.GPT_OSS_20B,
|
|
39
|
-
} as const;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Default generation configuration
|
|
43
|
-
*/
|
|
44
|
-
export const DEFAULT_GENERATION_CONFIG = {
|
|
45
|
-
temperature: 0.7,
|
|
46
|
-
maxTokens: 1024,
|
|
47
|
-
topP: 1.0,
|
|
48
|
-
} as const;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* API endpoints
|
|
52
|
-
*/
|
|
53
|
-
export const API_ENDPOINTS = {
|
|
54
|
-
CHAT: "/chat/completions",
|
|
55
|
-
} as const;
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Default base URL
|
|
59
|
-
*/
|
|
60
|
-
export const DEFAULT_BASE_URL = "https://api.groq.com/openai/v1" as const;
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Default timeouts
|
|
64
|
-
*/
|
|
65
|
-
export const TIMEOUTS = {
|
|
66
|
-
DEFAULT: 30000, // 30 seconds
|
|
67
|
-
STREAMING: 60000, // 60 seconds
|
|
68
|
-
} as const;
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Infrastructure Constants Index
|
|
3
|
-
* Subpath: @umituz/web-ai-groq-provider/services
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
export {
|
|
7
|
-
GROQ_MODELS,
|
|
8
|
-
DEFAULT_MODELS,
|
|
9
|
-
DEFAULT_GENERATION_CONFIG,
|
|
10
|
-
API_ENDPOINTS,
|
|
11
|
-
DEFAULT_BASE_URL,
|
|
12
|
-
TIMEOUTS,
|
|
13
|
-
} from "./groq.constants";
|
|
14
|
-
|
|
15
|
-
export {
|
|
16
|
-
GroqErrorType,
|
|
17
|
-
mapHttpStatusToErrorType,
|
|
18
|
-
isRetryableError,
|
|
19
|
-
isAuthError,
|
|
20
|
-
} from "./error.constants";
|
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Groq Domain Entities
|
|
3
|
-
* @description Core type definitions for Groq AI provider
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Configuration for Groq client initialization
|
|
8
|
-
*/
|
|
9
|
-
export interface GroqConfig {
|
|
10
|
-
/** API key for authentication */
|
|
11
|
-
readonly apiKey: string;
|
|
12
|
-
/** Base URL for API requests (default: https://api.groq.com/openai/v1) */
|
|
13
|
-
readonly baseUrl?: string;
|
|
14
|
-
/** Default timeout in milliseconds */
|
|
15
|
-
readonly timeoutMs?: number;
|
|
16
|
-
/** Default model to use for text generation */
|
|
17
|
-
readonly textModel?: string;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Generation configuration for AI requests
|
|
22
|
-
*/
|
|
23
|
-
export interface GroqGenerationConfig {
|
|
24
|
-
/** Controls randomness (0.0 - 2.0, default: 0.7) */
|
|
25
|
-
readonly temperature?: number;
|
|
26
|
-
/** Maximum number of tokens to generate */
|
|
27
|
-
readonly maxTokens?: number;
|
|
28
|
-
/** Nucleus sampling threshold (0.0 - 1.0) */
|
|
29
|
-
readonly topP?: number;
|
|
30
|
-
/** Number of completions to generate */
|
|
31
|
-
readonly n?: number;
|
|
32
|
-
/** Stop sequences */
|
|
33
|
-
readonly stop?: string[];
|
|
34
|
-
/** Frequency penalty (-2.0 to 2.0) */
|
|
35
|
-
readonly frequencyPenalty?: number;
|
|
36
|
-
/** Presence penalty (-2.0 to 2.0) */
|
|
37
|
-
readonly presencePenalty?: number;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Message role in chat conversation
|
|
42
|
-
*/
|
|
43
|
-
export type GroqMessageRole = "system" | "user" | "assistant";
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Chat message structure
|
|
47
|
-
*/
|
|
48
|
-
export interface GroqMessage {
|
|
49
|
-
/** Role of the message sender */
|
|
50
|
-
readonly role: GroqMessageRole;
|
|
51
|
-
/** Content of the message */
|
|
52
|
-
readonly content: string;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Chat completion request
|
|
57
|
-
*/
|
|
58
|
-
export interface GroqChatRequest {
|
|
59
|
-
/** Model to use for generation */
|
|
60
|
-
readonly model: string;
|
|
61
|
-
/** Array of messages in the conversation */
|
|
62
|
-
readonly messages: GroqMessage[];
|
|
63
|
-
/** Generation configuration */
|
|
64
|
-
readonly temperature?: number;
|
|
65
|
-
readonly max_tokens?: number;
|
|
66
|
-
readonly top_p?: number;
|
|
67
|
-
readonly n?: number;
|
|
68
|
-
readonly stop?: string[];
|
|
69
|
-
readonly frequency_penalty?: number;
|
|
70
|
-
readonly presence_penalty?: number;
|
|
71
|
-
/** Enable streaming response */
|
|
72
|
-
readonly stream?: boolean;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Individual completion choice
|
|
77
|
-
*/
|
|
78
|
-
export interface GroqChoice {
|
|
79
|
-
/** Index of the choice */
|
|
80
|
-
readonly index: number;
|
|
81
|
-
/** Generated message */
|
|
82
|
-
readonly message: {
|
|
83
|
-
readonly role: "assistant";
|
|
84
|
-
readonly content: string;
|
|
85
|
-
};
|
|
86
|
-
/** Reason for finish (stop, length, etc.) */
|
|
87
|
-
readonly finish_reason: GroqFinishReason;
|
|
88
|
-
/** Logprobs (optional) */
|
|
89
|
-
readonly logprobs: null | object;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Finish reason types
|
|
94
|
-
*/
|
|
95
|
-
export type GroqFinishReason = "stop" | "length" | "content_filter";
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Token usage information
|
|
99
|
-
*/
|
|
100
|
-
export interface GroqUsage {
|
|
101
|
-
/** Number of tokens in the prompt */
|
|
102
|
-
readonly prompt_tokens: number;
|
|
103
|
-
/** Number of tokens in the completion */
|
|
104
|
-
readonly completion_tokens: number;
|
|
105
|
-
/** Total number of tokens used */
|
|
106
|
-
readonly total_tokens: number;
|
|
107
|
-
/** Prompt time (Groq specific) */
|
|
108
|
-
readonly prompt_time?: number;
|
|
109
|
-
/** Completion time (Groq specific) */
|
|
110
|
-
readonly completion_time?: number;
|
|
111
|
-
/** Total time (Groq specific) */
|
|
112
|
-
readonly total_time?: number;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Chat completion response
|
|
117
|
-
*/
|
|
118
|
-
export interface GroqChatResponse {
|
|
119
|
-
/** Unique identifier for the response */
|
|
120
|
-
readonly id: string;
|
|
121
|
-
/** Object type (chat.completion) */
|
|
122
|
-
readonly object: string;
|
|
123
|
-
/** Timestamp of creation */
|
|
124
|
-
readonly created: number;
|
|
125
|
-
/** Model used for generation */
|
|
126
|
-
readonly model: string;
|
|
127
|
-
/** Array of completion choices */
|
|
128
|
-
readonly choices: GroqChoice[];
|
|
129
|
-
/** Token usage information */
|
|
130
|
-
readonly usage: GroqUsage;
|
|
131
|
-
/** System fingerprint (Groq specific) */
|
|
132
|
-
readonly system_fingerprint?: string;
|
|
133
|
-
/** X Groq (Groq specific) */
|
|
134
|
-
readonly x_groq?: {
|
|
135
|
-
readonly id?: string;
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Individual chunk choice for streaming
|
|
141
|
-
*/
|
|
142
|
-
export interface GroqChunkChoice {
|
|
143
|
-
/** Index of the choice */
|
|
144
|
-
readonly index: number;
|
|
145
|
-
/** Delta message (partial content) */
|
|
146
|
-
readonly delta: {
|
|
147
|
-
readonly role?: "assistant";
|
|
148
|
-
readonly content?: string;
|
|
149
|
-
};
|
|
150
|
-
/** Reason for finish (null if not finished) */
|
|
151
|
-
readonly finish_reason: GroqFinishReason | null;
|
|
152
|
-
/** Logprobs (optional) */
|
|
153
|
-
readonly logprobs: null | object;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Streaming chunk response
|
|
158
|
-
*/
|
|
159
|
-
export interface GroqChatChunk {
|
|
160
|
-
/** Unique identifier for the response */
|
|
161
|
-
readonly id: string;
|
|
162
|
-
/** Object type (chat.completion.chunk) */
|
|
163
|
-
readonly object: string;
|
|
164
|
-
/** Timestamp of creation */
|
|
165
|
-
readonly created: number;
|
|
166
|
-
/** Model used for generation */
|
|
167
|
-
readonly model: string;
|
|
168
|
-
/** Array of completion choices */
|
|
169
|
-
readonly choices: GroqChunkChoice[];
|
|
170
|
-
/** System fingerprint (Groq specific) */
|
|
171
|
-
readonly system_fingerprint?: string;
|
|
172
|
-
/** X Groq (Groq specific) */
|
|
173
|
-
readonly x_groq?: {
|
|
174
|
-
readonly id?: string;
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* API error response
|
|
180
|
-
*/
|
|
181
|
-
export interface GroqErrorResponse {
|
|
182
|
-
/** Error type */
|
|
183
|
-
readonly error: {
|
|
184
|
-
/** Error message */
|
|
185
|
-
readonly message: string;
|
|
186
|
-
/** Error type */
|
|
187
|
-
readonly type: string;
|
|
188
|
-
/** Error code */
|
|
189
|
-
readonly code?: string;
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* Input types for mutations
|
|
195
|
-
*/
|
|
196
|
-
export type GroqMessageCreateInput = Omit<GroqMessage, 'content'> & { content: string };
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Domain Entities Index
|
|
3
|
-
* Subpath: @umituz/web-ai-groq-provider/domain
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
export type {
|
|
7
|
-
GroqConfig,
|
|
8
|
-
GroqGenerationConfig,
|
|
9
|
-
GroqMessageRole,
|
|
10
|
-
GroqMessage,
|
|
11
|
-
GroqChatRequest,
|
|
12
|
-
GroqChoice,
|
|
13
|
-
GroqFinishReason,
|
|
14
|
-
GroqUsage,
|
|
15
|
-
GroqChatResponse,
|
|
16
|
-
GroqChunkChoice,
|
|
17
|
-
GroqChatChunk,
|
|
18
|
-
GroqErrorResponse,
|
|
19
|
-
GroqMessageCreateInput,
|
|
20
|
-
} from "./groq.entities";
|
|
@@ -1,232 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* useGroq Hook
|
|
3
|
-
* @description Main React hook for Groq text generation with performance optimizations
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { useState, useCallback, useMemo, useRef, useEffect } from "react";
|
|
7
|
-
import type { GroqGenerationConfig } from "../interfaces";
|
|
8
|
-
import type { StreamingCallbacks } from "../interfaces";
|
|
9
|
-
import { textGenerationService } from "../services";
|
|
10
|
-
import { getUserFriendlyError } from "../utils/error.util";
|
|
11
|
-
import { groqHttpClient } from "../services/http-client.service";
|
|
12
|
-
|
|
13
|
-
export interface UseGroqOptions {
|
|
14
|
-
/** Initial model to use */
|
|
15
|
-
readonly model?: string;
|
|
16
|
-
/** Default generation config */
|
|
17
|
-
readonly generationConfig?: GroqGenerationConfig;
|
|
18
|
-
/** Callback when generation starts */
|
|
19
|
-
readonly onStart?: () => void;
|
|
20
|
-
/** Callback when generation completes */
|
|
21
|
-
readonly onSuccess?: (result: string) => void;
|
|
22
|
-
/** Callback when generation fails */
|
|
23
|
-
readonly onError?: (error: string) => void;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export interface UseGroqReturn {
|
|
27
|
-
/** Loading state */
|
|
28
|
-
readonly isLoading: boolean;
|
|
29
|
-
/** Error message */
|
|
30
|
-
readonly error: string | null;
|
|
31
|
-
/** Generated result */
|
|
32
|
-
readonly result: string | null;
|
|
33
|
-
/** Generate text from a prompt */
|
|
34
|
-
generate: (prompt: string, options?: GroqGenerationConfig) => Promise<string>;
|
|
35
|
-
/** Generate structured JSON output */
|
|
36
|
-
generateJSON: <T = Record<string, unknown>>(
|
|
37
|
-
prompt: string,
|
|
38
|
-
options?: GroqGenerationConfig & { schema?: Record<string, unknown> }
|
|
39
|
-
) => Promise<T>;
|
|
40
|
-
/** Stream text generation */
|
|
41
|
-
stream: (
|
|
42
|
-
prompt: string,
|
|
43
|
-
onChunk: (chunk: string) => void,
|
|
44
|
-
options?: GroqGenerationConfig
|
|
45
|
-
) => Promise<void>;
|
|
46
|
-
/** Reset state */
|
|
47
|
-
reset: () => void;
|
|
48
|
-
/** Clear error */
|
|
49
|
-
clearError: () => void;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Hook for Groq text generation with performance optimizations
|
|
54
|
-
*/
|
|
55
|
-
export function useGroq(options: UseGroqOptions = {}): UseGroqReturn {
|
|
56
|
-
const [isLoading, setIsLoading] = useState(false);
|
|
57
|
-
const [error, setError] = useState<string | null>(null);
|
|
58
|
-
const [result, setResult] = useState<string | null>(null);
|
|
59
|
-
|
|
60
|
-
// Use ref for callbacks to avoid stale closures and unstable references
|
|
61
|
-
const optionsRef = useRef(options);
|
|
62
|
-
optionsRef.current = options;
|
|
63
|
-
|
|
64
|
-
// Memoize only the stable configuration values (not callbacks)
|
|
65
|
-
const stableConfig = useMemo(
|
|
66
|
-
() => ({
|
|
67
|
-
model: options.model,
|
|
68
|
-
generationConfig: options.generationConfig,
|
|
69
|
-
}),
|
|
70
|
-
[options.model, options.generationConfig?.temperature, options.generationConfig?.maxTokens, options.generationConfig?.topP]
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
// Cleanup pending requests on unmount
|
|
74
|
-
useEffect(() => {
|
|
75
|
-
return () => {
|
|
76
|
-
groqHttpClient.cancelPendingRequests();
|
|
77
|
-
};
|
|
78
|
-
}, []);
|
|
79
|
-
|
|
80
|
-
const generate = useCallback(
|
|
81
|
-
async (prompt: string, config?: GroqGenerationConfig): Promise<string> => {
|
|
82
|
-
setIsLoading(true);
|
|
83
|
-
setError(null);
|
|
84
|
-
setResult(null);
|
|
85
|
-
|
|
86
|
-
// Use ref to get latest callbacks without adding to dependencies
|
|
87
|
-
const currentOptions = optionsRef.current;
|
|
88
|
-
currentOptions.onStart?.();
|
|
89
|
-
|
|
90
|
-
try {
|
|
91
|
-
const response = await textGenerationService.generateCompletion(prompt, {
|
|
92
|
-
model: stableConfig.model,
|
|
93
|
-
generationConfig: {
|
|
94
|
-
...stableConfig.generationConfig,
|
|
95
|
-
...config,
|
|
96
|
-
},
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
setResult(response);
|
|
100
|
-
currentOptions.onSuccess?.(response);
|
|
101
|
-
|
|
102
|
-
return response;
|
|
103
|
-
} catch (err) {
|
|
104
|
-
const errorMessage = getUserFriendlyError(
|
|
105
|
-
err instanceof Error ? err : new Error("Unknown error")
|
|
106
|
-
);
|
|
107
|
-
setError(errorMessage);
|
|
108
|
-
currentOptions.onError?.(errorMessage);
|
|
109
|
-
|
|
110
|
-
throw err;
|
|
111
|
-
} finally {
|
|
112
|
-
setIsLoading(false);
|
|
113
|
-
}
|
|
114
|
-
},
|
|
115
|
-
[stableConfig]
|
|
116
|
-
);
|
|
117
|
-
|
|
118
|
-
const generateJSON = useCallback(
|
|
119
|
-
async <T = Record<string, unknown>,>(
|
|
120
|
-
prompt: string,
|
|
121
|
-
config?: GroqGenerationConfig & { schema?: Record<string, unknown> }
|
|
122
|
-
): Promise<T> => {
|
|
123
|
-
setIsLoading(true);
|
|
124
|
-
setError(null);
|
|
125
|
-
setResult(null);
|
|
126
|
-
|
|
127
|
-
const currentOptions = optionsRef.current;
|
|
128
|
-
currentOptions.onStart?.();
|
|
129
|
-
|
|
130
|
-
try {
|
|
131
|
-
const response = await textGenerationService.generateStructured<T>(prompt, {
|
|
132
|
-
model: stableConfig.model,
|
|
133
|
-
generationConfig: {
|
|
134
|
-
...stableConfig.generationConfig,
|
|
135
|
-
...config,
|
|
136
|
-
},
|
|
137
|
-
schema: config?.schema,
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
const jsonString = JSON.stringify(response, null, 2);
|
|
141
|
-
setResult(jsonString);
|
|
142
|
-
currentOptions.onSuccess?.(jsonString);
|
|
143
|
-
|
|
144
|
-
return response;
|
|
145
|
-
} catch (err) {
|
|
146
|
-
const errorMessage = getUserFriendlyError(
|
|
147
|
-
err instanceof Error ? err : new Error("Unknown error")
|
|
148
|
-
);
|
|
149
|
-
setError(errorMessage);
|
|
150
|
-
currentOptions.onError?.(errorMessage);
|
|
151
|
-
|
|
152
|
-
throw err;
|
|
153
|
-
} finally {
|
|
154
|
-
setIsLoading(false);
|
|
155
|
-
}
|
|
156
|
-
},
|
|
157
|
-
[stableConfig]
|
|
158
|
-
);
|
|
159
|
-
|
|
160
|
-
const stream = useCallback(
|
|
161
|
-
async (
|
|
162
|
-
prompt: string,
|
|
163
|
-
onChunk: (chunk: string) => void,
|
|
164
|
-
config?: GroqGenerationConfig
|
|
165
|
-
): Promise<void> => {
|
|
166
|
-
setIsLoading(true);
|
|
167
|
-
setError(null);
|
|
168
|
-
setResult(null);
|
|
169
|
-
|
|
170
|
-
const contentChunks: string[] = [];
|
|
171
|
-
const currentOptions = optionsRef.current;
|
|
172
|
-
currentOptions.onStart?.();
|
|
173
|
-
|
|
174
|
-
try {
|
|
175
|
-
const callbacks: StreamingCallbacks = {
|
|
176
|
-
onChunk: (c) => {
|
|
177
|
-
contentChunks.push(c); // Use array accumulation
|
|
178
|
-
onChunk(c);
|
|
179
|
-
},
|
|
180
|
-
onComplete: (text) => {
|
|
181
|
-
setResult(text);
|
|
182
|
-
currentOptions.onSuccess?.(text);
|
|
183
|
-
},
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
await textGenerationService.streamCompletion(
|
|
187
|
-
prompt,
|
|
188
|
-
callbacks,
|
|
189
|
-
{
|
|
190
|
-
model: stableConfig.model,
|
|
191
|
-
generationConfig: {
|
|
192
|
-
...stableConfig.generationConfig,
|
|
193
|
-
...config,
|
|
194
|
-
},
|
|
195
|
-
}
|
|
196
|
-
);
|
|
197
|
-
} catch (err) {
|
|
198
|
-
const errorMessage = getUserFriendlyError(
|
|
199
|
-
err instanceof Error ? err : new Error("Unknown error")
|
|
200
|
-
);
|
|
201
|
-
setError(errorMessage);
|
|
202
|
-
currentOptions.onError?.(errorMessage);
|
|
203
|
-
|
|
204
|
-
throw err;
|
|
205
|
-
} finally {
|
|
206
|
-
setIsLoading(false);
|
|
207
|
-
}
|
|
208
|
-
},
|
|
209
|
-
[stableConfig]
|
|
210
|
-
);
|
|
211
|
-
|
|
212
|
-
const reset = useCallback(() => {
|
|
213
|
-
setIsLoading(false);
|
|
214
|
-
setError(null);
|
|
215
|
-
setResult(null);
|
|
216
|
-
}, []);
|
|
217
|
-
|
|
218
|
-
const clearError = useCallback(() => {
|
|
219
|
-
setError(null);
|
|
220
|
-
}, []);
|
|
221
|
-
|
|
222
|
-
return {
|
|
223
|
-
isLoading,
|
|
224
|
-
error,
|
|
225
|
-
result,
|
|
226
|
-
generate,
|
|
227
|
-
generateJSON,
|
|
228
|
-
stream,
|
|
229
|
-
reset,
|
|
230
|
-
clearError,
|
|
231
|
-
};
|
|
232
|
-
}
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Groq Domain
|
|
3
|
-
* @description Groq API client and text generation
|
|
4
|
-
* Subpath: @umituz/web-ai-groq-provider/groq
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
// Entities
|
|
8
|
-
export type {
|
|
9
|
-
GroqMessageRole,
|
|
10
|
-
GroqMessage,
|
|
11
|
-
GroqChatRequest,
|
|
12
|
-
GroqChoice,
|
|
13
|
-
GroqFinishReason,
|
|
14
|
-
GroqUsage,
|
|
15
|
-
GroqChatResponse,
|
|
16
|
-
GroqChunkChoice,
|
|
17
|
-
GroqChatChunk,
|
|
18
|
-
GroqErrorResponse,
|
|
19
|
-
} from "./entities";
|
|
20
|
-
|
|
21
|
-
export type {
|
|
22
|
-
GroqConfig,
|
|
23
|
-
GroqGenerationConfig,
|
|
24
|
-
} from "./entities";
|
|
25
|
-
|
|
26
|
-
// Interfaces
|
|
27
|
-
export type {
|
|
28
|
-
TextGenerationOptions,
|
|
29
|
-
StreamingCallbacks,
|
|
30
|
-
StructuredGenerationOptions,
|
|
31
|
-
} from "./interfaces";
|
|
32
|
-
|
|
33
|
-
export type { IGroqChatService, IGroqHttpClient } from "./interfaces";
|
|
34
|
-
|
|
35
|
-
// Services
|
|
36
|
-
export { textGenerationService, groqHttpClient } from "./services";
|
|
37
|
-
|
|
38
|
-
// Hooks
|
|
39
|
-
export { useGroq } from "./hooks";
|
|
40
|
-
export type { UseGroqOptions, UseGroqReturn } from "./hooks";
|
|
41
|
-
|
|
42
|
-
// Constants
|
|
43
|
-
export {
|
|
44
|
-
GROQ_MODELS,
|
|
45
|
-
DEFAULT_MODELS,
|
|
46
|
-
DEFAULT_GENERATION_CONFIG,
|
|
47
|
-
API_ENDPOINTS,
|
|
48
|
-
DEFAULT_BASE_URL,
|
|
49
|
-
TIMEOUTS,
|
|
50
|
-
} from "./constants";
|
|
51
|
-
|
|
52
|
-
export {
|
|
53
|
-
GroqErrorType,
|
|
54
|
-
mapHttpStatusToErrorType,
|
|
55
|
-
isRetryableError,
|
|
56
|
-
isAuthError,
|
|
57
|
-
} from "./constants";
|
|
58
|
-
|
|
59
|
-
// Utils
|
|
60
|
-
export {
|
|
61
|
-
createUserMessage,
|
|
62
|
-
createAssistantMessage,
|
|
63
|
-
createSystemMessage,
|
|
64
|
-
createTextMessage,
|
|
65
|
-
promptToMessages,
|
|
66
|
-
extractTextFromMessages,
|
|
67
|
-
formatMessagesForDisplay,
|
|
68
|
-
} from "./utils";
|
|
69
|
-
|
|
70
|
-
export { getUserFriendlyError, formatErrorForLogging } from "./utils";
|
|
71
|
-
|
|
72
|
-
export { GroqError } from "./utils";
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Groq Service Interfaces
|
|
3
|
-
* @description Contracts for Groq AI services
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type {
|
|
7
|
-
GroqMessage,
|
|
8
|
-
GroqChatResponse,
|
|
9
|
-
GroqChatRequest,
|
|
10
|
-
GroqGenerationConfig,
|
|
11
|
-
} from "../entities";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Text generation options
|
|
15
|
-
*/
|
|
16
|
-
export interface TextGenerationOptions {
|
|
17
|
-
/** Model to use */
|
|
18
|
-
readonly model?: string;
|
|
19
|
-
/** Generation configuration */
|
|
20
|
-
readonly generationConfig?: GroqGenerationConfig;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Streaming callbacks
|
|
25
|
-
*/
|
|
26
|
-
export interface StreamingCallbacks {
|
|
27
|
-
/** Called for each chunk */
|
|
28
|
-
readonly onChunk?: (chunk: string) => void;
|
|
29
|
-
/** Called on completion */
|
|
30
|
-
readonly onComplete?: (fullText: string) => void;
|
|
31
|
-
/** Called on error */
|
|
32
|
-
readonly onError?: (error: Error) => void;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Structured text generation options
|
|
37
|
-
*/
|
|
38
|
-
export interface StructuredGenerationOptions {
|
|
39
|
-
/** Model to use */
|
|
40
|
-
readonly model?: string;
|
|
41
|
-
/** Generation configuration */
|
|
42
|
-
readonly generationConfig?: GroqGenerationConfig;
|
|
43
|
-
/** JSON schema for validation */
|
|
44
|
-
readonly schema?: Record<string, unknown>;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Chat completion service interface
|
|
49
|
-
*/
|
|
50
|
-
export interface IGroqChatService {
|
|
51
|
-
/** Generate completion from prompt */
|
|
52
|
-
generateCompletion(prompt: string, options?: TextGenerationOptions): Promise<string>;
|
|
53
|
-
/** Generate completion from messages */
|
|
54
|
-
generateChatCompletion(messages: GroqMessage[], options?: TextGenerationOptions): Promise<string>;
|
|
55
|
-
/** Generate structured JSON output */
|
|
56
|
-
generateStructured<T>(prompt: string, options?: StructuredGenerationOptions): Promise<T>;
|
|
57
|
-
/** Stream completion */
|
|
58
|
-
streamCompletion(
|
|
59
|
-
prompt: string,
|
|
60
|
-
callbacks: StreamingCallbacks,
|
|
61
|
-
options?: TextGenerationOptions
|
|
62
|
-
): Promise<void>;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* HTTP client interface
|
|
67
|
-
*/
|
|
68
|
-
export interface IGroqHttpClient {
|
|
69
|
-
/** Send chat completion request */
|
|
70
|
-
postChatCompletion(request: GroqChatRequest): Promise<GroqChatResponse>;
|
|
71
|
-
/** Send streaming request */
|
|
72
|
-
streamChatCompletion(request: GroqChatRequest): Promise<ReadableStream>;
|
|
73
|
-
/** Get API key */
|
|
74
|
-
getApiKey(): string;
|
|
75
|
-
/** Set API key */
|
|
76
|
-
setApiKey(apiKey: string): void;
|
|
77
|
-
}
|