@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
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration Builder
|
|
3
|
+
* Builder pattern for Groq configuration
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { GroqConfig, GroqGenerationConfig } from "../domain/entities";
|
|
7
|
+
import { DEFAULT_MODELS } from "../domain/entities";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Builder for Groq configuration
|
|
11
|
+
*/
|
|
12
|
+
export class ConfigBuilder {
|
|
13
|
+
private config: Partial<GroqConfig> = {};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Set API key
|
|
17
|
+
*/
|
|
18
|
+
withApiKey(apiKey: string): ConfigBuilder {
|
|
19
|
+
this.config.apiKey = apiKey;
|
|
20
|
+
return this;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Set base URL
|
|
25
|
+
*/
|
|
26
|
+
withBaseUrl(baseUrl: string): ConfigBuilder {
|
|
27
|
+
this.config.baseUrl = baseUrl;
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Set timeout
|
|
33
|
+
*/
|
|
34
|
+
withTimeout(timeoutMs: number): ConfigBuilder {
|
|
35
|
+
this.config.timeoutMs = timeoutMs;
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Set default text model
|
|
41
|
+
*/
|
|
42
|
+
withTextModel(model: string): ConfigBuilder {
|
|
43
|
+
this.config.textModel = model;
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Build configuration
|
|
49
|
+
*/
|
|
50
|
+
build(): GroqConfig {
|
|
51
|
+
if (!this.config.apiKey) {
|
|
52
|
+
throw new Error("API key is required. Use withApiKey() to set it.");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
apiKey: this.config.apiKey,
|
|
57
|
+
baseUrl: this.config.baseUrl,
|
|
58
|
+
timeoutMs: this.config.timeoutMs,
|
|
59
|
+
textModel: this.config.textModel || DEFAULT_MODELS.TEXT,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Create a new builder instance
|
|
65
|
+
*/
|
|
66
|
+
static create(): ConfigBuilder {
|
|
67
|
+
return new ConfigBuilder();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Builder for generation configuration
|
|
73
|
+
*/
|
|
74
|
+
export class GenerationConfigBuilder {
|
|
75
|
+
private config: GroqGenerationConfig = {};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Set temperature
|
|
79
|
+
*/
|
|
80
|
+
withTemperature(temperature: number): GenerationConfigBuilder {
|
|
81
|
+
this.config.temperature = temperature;
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Set max tokens
|
|
87
|
+
*/
|
|
88
|
+
withMaxTokens(maxTokens: number): GenerationConfigBuilder {
|
|
89
|
+
this.config.maxTokens = maxTokens;
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Set top P
|
|
95
|
+
*/
|
|
96
|
+
withTopP(topP: number): GenerationConfigBuilder {
|
|
97
|
+
this.config.topP = topP;
|
|
98
|
+
return this;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Set frequency penalty
|
|
103
|
+
*/
|
|
104
|
+
withFrequencyPenalty(penalty: number): GenerationConfigBuilder {
|
|
105
|
+
this.config.frequencyPenalty = penalty;
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Set presence penalty
|
|
111
|
+
*/
|
|
112
|
+
withPresencePenalty(penalty: number): GenerationConfigBuilder {
|
|
113
|
+
this.config.presencePenalty = penalty;
|
|
114
|
+
return this;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Set stop sequences
|
|
119
|
+
*/
|
|
120
|
+
withStop(stop: string[]): GenerationConfigBuilder {
|
|
121
|
+
this.config.stop = stop;
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Build configuration
|
|
127
|
+
*/
|
|
128
|
+
build(): GroqGenerationConfig {
|
|
129
|
+
return { ...this.config };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Create a new builder instance
|
|
134
|
+
*/
|
|
135
|
+
static create(): GenerationConfigBuilder {
|
|
136
|
+
return new GenerationConfigBuilder();
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Create a balanced configuration
|
|
141
|
+
*/
|
|
142
|
+
static balanced(): GenerationConfigBuilder {
|
|
143
|
+
return new GenerationConfigBuilder().withTemperature(0.7);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Create a creative configuration
|
|
148
|
+
*/
|
|
149
|
+
static creative(): GenerationConfigBuilder {
|
|
150
|
+
return new GenerationConfigBuilder().withTemperature(1.0);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Create a precise configuration
|
|
155
|
+
*/
|
|
156
|
+
static precise(): GenerationConfigBuilder {
|
|
157
|
+
return new GenerationConfigBuilder().withTemperature(0.3);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider Factory
|
|
3
|
+
* Factory for creating configured Groq provider instances
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { GroqConfig, GroqGenerationConfig } from "../domain/entities";
|
|
7
|
+
import { groqHttpClient } from "../infrastructure/services/GroqClient";
|
|
8
|
+
import { ConfigBuilder, GenerationConfigBuilder } from "./ConfigBuilder";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Provider configuration options
|
|
12
|
+
*/
|
|
13
|
+
export interface ProviderConfig {
|
|
14
|
+
apiKey: string;
|
|
15
|
+
baseUrl?: string;
|
|
16
|
+
timeoutMs?: number;
|
|
17
|
+
defaultModel?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Provider factory options
|
|
22
|
+
*/
|
|
23
|
+
export interface ProviderFactoryOptions {
|
|
24
|
+
enableTelemetry?: boolean;
|
|
25
|
+
onError?: (error: Error) => void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Initialize Groq provider with configuration
|
|
30
|
+
*/
|
|
31
|
+
export function initializeProvider(config: ProviderConfig): void {
|
|
32
|
+
groqHttpClient.initialize({
|
|
33
|
+
apiKey: config.apiKey,
|
|
34
|
+
baseUrl: config.baseUrl,
|
|
35
|
+
timeoutMs: config.timeoutMs,
|
|
36
|
+
textModel: config.defaultModel,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Provider factory - creates configured provider instances
|
|
42
|
+
*/
|
|
43
|
+
export const providerFactory = {
|
|
44
|
+
/**
|
|
45
|
+
* Create a new provider instance
|
|
46
|
+
*/
|
|
47
|
+
create(config: ProviderConfig): void {
|
|
48
|
+
initializeProvider(config);
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Create provider from environment variables
|
|
53
|
+
*/
|
|
54
|
+
fromEnv(): void {
|
|
55
|
+
const apiKey = process.env.GROQ_API_KEY;
|
|
56
|
+
|
|
57
|
+
if (!apiKey) {
|
|
58
|
+
throw new Error("GROQ_API_KEY environment variable is not set");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
initializeProvider({
|
|
62
|
+
apiKey,
|
|
63
|
+
baseUrl: process.env.GROQ_BASE_URL,
|
|
64
|
+
timeoutMs: process.env.GROQ_TIMEOUT_MS ? parseInt(process.env.GROQ_TIMEOUT_MS) : undefined,
|
|
65
|
+
});
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Reset provider (clear configuration)
|
|
70
|
+
*/
|
|
71
|
+
reset(): void {
|
|
72
|
+
groqHttpClient.reset();
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Check if provider is initialized
|
|
77
|
+
*/
|
|
78
|
+
isInitialized(): boolean {
|
|
79
|
+
return groqHttpClient.isInitialized();
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Convenience function to initialize provider
|
|
85
|
+
*/
|
|
86
|
+
export function configureProvider(config: ProviderConfig): void {
|
|
87
|
+
providerFactory.create(config);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Convenience function to reset provider
|
|
92
|
+
*/
|
|
93
|
+
export function resetProvider(): void {
|
|
94
|
+
providerFactory.reset();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Re-export builders
|
|
98
|
+
export { ConfigBuilder, GenerationConfigBuilder };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Providers
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export { ConfigBuilder, GenerationConfigBuilder } from "./ConfigBuilder";
|
|
6
|
+
export {
|
|
7
|
+
providerFactory,
|
|
8
|
+
initializeProvider,
|
|
9
|
+
configureProvider,
|
|
10
|
+
resetProvider,
|
|
11
|
+
} from "./ProviderFactory";
|
|
12
|
+
|
|
13
|
+
export type {
|
|
14
|
+
ProviderConfig,
|
|
15
|
+
ProviderFactoryOptions,
|
|
16
|
+
} from "./ProviderFactory";
|