@retrivora-ai/rag-engine 2.4.3 → 2.4.5
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/dist/{BatchProcessor-zbQMaZHm.d.mts → BatchProcessor-Csy_K1qQ.d.mts} +2 -0
- package/dist/{BatchProcessor-D42E706a.d.ts → BatchProcessor-Dg18zZGk.d.ts} +2 -0
- package/dist/handlers/index.js +27 -4
- package/dist/handlers/index.mjs +27 -4
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +12 -10
- package/dist/index.mjs +12 -10
- package/dist/server.d.mts +2 -2
- package/dist/server.d.ts +2 -2
- package/dist/server.js +28 -4
- package/dist/server.mjs +28 -4
- package/package.json +1 -3
- package/src/LicensedRetrivora.ts +0 -278
- package/src/app/api/chat/route.ts +0 -4
- package/src/app/api/health/route.ts +0 -5
- package/src/app/api/ingest/route.ts +0 -27
- package/src/app/api/suggestions/route.ts +0 -5
- package/src/app/api/upload/route.ts +0 -5
- package/src/app/constants.tsx +0 -270
- package/src/app/favicon.ico +0 -0
- package/src/app/globals.css +0 -449
- package/src/app/layout.tsx +0 -22
- package/src/app/page.tsx +0 -64
- package/src/app/types.ts +0 -30
- package/src/browser.tsx +0 -127
- package/src/components/ChatWidget.tsx +0 -393
- package/src/components/ChatWindow.tsx +0 -952
- package/src/components/CodeViewer.tsx +0 -56
- package/src/components/ConfigProvider.tsx +0 -141
- package/src/components/DocumentUpload.tsx +0 -354
- package/src/components/DynamicChart.tsx +0 -297
- package/src/components/HourglassLoader.tsx +0 -87
- package/src/components/MarkdownComponents.tsx +0 -186
- package/src/components/MessageBubble.tsx +0 -498
- package/src/components/ObservabilityPanel.tsx +0 -374
- package/src/components/ProductCard.tsx +0 -107
- package/src/components/ProductCarousel.tsx +0 -97
- package/src/components/SourceCard.tsx +0 -61
- package/src/components/ThemeProvider.tsx +0 -8
- package/src/components/ThemeToggle.tsx +0 -29
- package/src/components/ThinkingBlock.tsx +0 -75
- package/src/components/UIDispatcher.tsx +0 -342
- package/src/components/VisualizationRenderer.tsx +0 -621
- package/src/components/constants.tsx +0 -503
- package/src/config/ConfigBuilder.ts +0 -285
- package/src/config/EmbeddingStrategy.ts +0 -148
- package/src/config/LicenseConfigResolver.ts +0 -298
- package/src/config/RagConfig.ts +0 -328
- package/src/config/UniversalProfiles.ts +0 -82
- package/src/config/constants.ts +0 -64
- package/src/config/serverConfig.ts +0 -378
- package/src/config/uiConstants.ts +0 -47
- package/src/core/BatchProcessor.ts +0 -385
- package/src/core/CircuitBreaker.ts +0 -118
- package/src/core/ConfigFetcher.ts +0 -220
- package/src/core/ConfigResolver.ts +0 -165
- package/src/core/ConfigValidator.ts +0 -197
- package/src/core/DatabaseStorage.ts +0 -526
- package/src/core/FreeTierLimitsGuard.ts +0 -281
- package/src/core/LLMRouter.ts +0 -79
- package/src/core/LangChainAgent.ts +0 -132
- package/src/core/LicenseValidator.ts +0 -284
- package/src/core/LicenseVerifier.ts +0 -261
- package/src/core/MultiAgentCoordinator.ts +0 -239
- package/src/core/Pipeline.ts +0 -1279
- package/src/core/PluginManager.ts +0 -277
- package/src/core/ProjectSettings.ts +0 -247
- package/src/core/ProviderHealthCheck.ts +0 -133
- package/src/core/ProviderInterfaces.ts +0 -37
- package/src/core/ProviderRegistry.ts +0 -156
- package/src/core/QueryProcessor.ts +0 -399
- package/src/core/Retrivora.ts +0 -98
- package/src/core/VectorPlugin.ts +0 -193
- package/src/core/mcp.ts +0 -261
- package/src/exceptions/index.ts +0 -131
- package/src/handlers/index.ts +0 -1625
- package/src/hooks/useHydrated.ts +0 -13
- package/src/hooks/useProjectSettings.ts +0 -105
- package/src/hooks/useRagChat.ts +0 -436
- package/src/hooks/useStoredMessages.ts +0 -64
- package/src/index.css +0 -4269
- package/src/index.ts +0 -137
- package/src/lib/plugin.ts +0 -24
- package/src/llm/ILLMProvider.ts +0 -60
- package/src/llm/LLMFactory.ts +0 -141
- package/src/llm/providers/AnthropicProvider.ts +0 -215
- package/src/llm/providers/GeminiProvider.ts +0 -311
- package/src/llm/providers/GroqProvider.ts +0 -184
- package/src/llm/providers/OllamaProvider.ts +0 -232
- package/src/llm/providers/OpenAIProvider.ts +0 -188
- package/src/llm/providers/QwenProvider.ts +0 -199
- package/src/llm/providers/UniversalLLMAdapter.ts +0 -422
- package/src/llm/utils.ts +0 -46
- package/src/providers/graphdb/BaseGraphProvider.ts +0 -43
- package/src/providers/graphdb/SimpleGraphProvider.ts +0 -66
- package/src/providers/vectordb/BaseVectorProvider.ts +0 -98
- package/src/providers/vectordb/ChromaDBProvider.ts +0 -130
- package/src/providers/vectordb/MilvusProvider.ts +0 -134
- package/src/providers/vectordb/MongoDBProvider.ts +0 -250
- package/src/providers/vectordb/MultiTablePostgresProvider.ts +0 -461
- package/src/providers/vectordb/PineconeProvider.ts +0 -295
- package/src/providers/vectordb/PostgreSQLProvider.ts +0 -227
- package/src/providers/vectordb/QdrantProvider.ts +0 -294
- package/src/providers/vectordb/RedisProvider.ts +0 -105
- package/src/providers/vectordb/UniversalVectorProvider.ts +0 -221
- package/src/providers/vectordb/WeaviateProvider.ts +0 -172
- package/src/rag/DocumentChunker.ts +0 -144
- package/src/rag/EntityExtractor.ts +0 -90
- package/src/rag/LlamaIndexIngestor.ts +0 -60
- package/src/rag/Reranker.ts +0 -106
- package/src/rendering/IntentClassifier.ts +0 -167
- package/src/rendering/RendererRegistry.ts +0 -164
- package/src/rendering/RuleEngine.ts +0 -67
- package/src/rendering/VisualizationDecisionEngine.ts +0 -164
- package/src/rendering/__tests__/VisualizationDecisionEngine.test.ts +0 -237
- package/src/rendering/index.ts +0 -12
- package/src/rendering/rules/Rule1SpecificInfoRule.ts +0 -28
- package/src/rendering/rules/Rule2ComparisonRule.ts +0 -24
- package/src/rendering/rules/Rule3ProductDiscoveryRule.ts +0 -28
- package/src/rendering/rules/Rule4AnalyticalRule.ts +0 -38
- package/src/rendering/rules/Rule5MixedResponseRule.ts +0 -29
- package/src/rendering/rules/Rule6SmallResultSetRule.ts +0 -24
- package/src/rendering/rules/Rule7LargeTableRule.ts +0 -35
- package/src/rendering/types.ts +0 -96
- package/src/server.ts +0 -156
- package/src/tailwind.css +0 -194
- package/src/telemetry/TelemetryService.ts +0 -330
- package/src/types/chat.ts +0 -89
- package/src/types/index.ts +0 -278
- package/src/types/pdf-parse.d.ts +0 -13
- package/src/types/props.ts +0 -169
- package/src/utils/DocumentParser.ts +0 -111
- package/src/utils/ProductExtractor.ts +0 -350
- package/src/utils/SchemaMapper.ts +0 -176
- package/src/utils/UITransformer.ts +0 -1812
- package/src/utils/synonyms.ts +0 -80
- package/src/utils/templateUtils.ts +0 -78
- package/src/version.ts +0 -6
|
@@ -1,311 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Gemini LLM Provider (Google Gen AI)
|
|
3
|
-
*
|
|
4
|
-
* Handles both chat completion (gemini-2.5-flash, gemini-2.5-pro, etc.) and
|
|
5
|
-
* embedding (text-embedding-004).
|
|
6
|
-
* Uses the modern @google/genai SDK.
|
|
7
|
-
*
|
|
8
|
-
* Required LLMConfig fields:
|
|
9
|
-
* - apiKey: string – Gemini API key
|
|
10
|
-
* - model: string – e.g. "gemini-2.5-flash"
|
|
11
|
-
*
|
|
12
|
-
* Required EmbeddingConfig fields (when used for embedding):
|
|
13
|
-
* - apiKey: string – same or separate key
|
|
14
|
-
* - model: string – e.g. "text-embedding-004"
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
import { GoogleGenerativeAI } from '@google/generative-ai';
|
|
18
|
-
import { ILLMProvider, EmbedOptions } from '../ILLMProvider';
|
|
19
|
-
import { ChatMessage, ChatOptions } from '../../types';
|
|
20
|
-
import { LLMConfig, EmbeddingConfig } from '../../config/RagConfig';
|
|
21
|
-
import { IProviderValidator, IProviderHealthChecker, HealthCheckResult } from '../../core/ProviderInterfaces';
|
|
22
|
-
import { ValidationError } from '../../core/ConfigValidator';
|
|
23
|
-
import { buildSystemContent } from '../utils';
|
|
24
|
-
|
|
25
|
-
const DEFAULT_EMBEDDING_MODEL = 'text-embedding-004';
|
|
26
|
-
const DEFAULT_TEMPERATURE = 0.7;
|
|
27
|
-
const DEFAULT_MAX_TOKENS = 1024;
|
|
28
|
-
|
|
29
|
-
// ---------------------------------------------------------------------------
|
|
30
|
-
// Internal helpers
|
|
31
|
-
// ---------------------------------------------------------------------------
|
|
32
|
-
|
|
33
|
-
/** Strip suffixes like `:latest` that are valid in Ollama but not in Google SDK. */
|
|
34
|
-
function sanitizeModel(model: string): string {
|
|
35
|
-
return model ? model.split(':')[0] : model;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** Build a GoogleGenerativeAI client for the given API key. */
|
|
39
|
-
function buildClient(apiKey: string): GoogleGenerativeAI {
|
|
40
|
-
return new GoogleGenerativeAI(apiKey);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Apply a task-specific prefix to a text string when configured to do so.
|
|
45
|
-
* Returns the original string unchanged when no prefix applies.
|
|
46
|
-
*/
|
|
47
|
-
function applyPrefix(
|
|
48
|
-
text: string,
|
|
49
|
-
taskType: EmbedOptions['taskType'] | undefined,
|
|
50
|
-
queryPrefix: string | undefined,
|
|
51
|
-
docPrefix: string | undefined,
|
|
52
|
-
): string {
|
|
53
|
-
if (taskType === 'query' && queryPrefix && !text.startsWith(queryPrefix)) {
|
|
54
|
-
return `${queryPrefix}${text}`;
|
|
55
|
-
}
|
|
56
|
-
if (taskType === 'document' && docPrefix && !text.startsWith(docPrefix)) {
|
|
57
|
-
return `${docPrefix}${text}`;
|
|
58
|
-
}
|
|
59
|
-
return text;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// ---------------------------------------------------------------------------
|
|
63
|
-
// Provider
|
|
64
|
-
// ---------------------------------------------------------------------------
|
|
65
|
-
|
|
66
|
-
export class GeminiProvider implements ILLMProvider {
|
|
67
|
-
private readonly client: GoogleGenerativeAI;
|
|
68
|
-
private readonly llmConfig: LLMConfig;
|
|
69
|
-
private readonly embeddingConfig?: EmbeddingConfig;
|
|
70
|
-
|
|
71
|
-
constructor(llmConfig: LLMConfig, embeddingConfig?: EmbeddingConfig) {
|
|
72
|
-
if (!llmConfig.apiKey) {
|
|
73
|
-
throw new Error('[GeminiProvider] llmConfig.apiKey is required');
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
this.llmConfig = { ...llmConfig, model: sanitizeModel(llmConfig.model) };
|
|
77
|
-
this.client = buildClient(this.llmConfig.apiKey!);
|
|
78
|
-
|
|
79
|
-
if (embeddingConfig) {
|
|
80
|
-
this.embeddingConfig = {
|
|
81
|
-
...embeddingConfig,
|
|
82
|
-
model: sanitizeModel(embeddingConfig.model),
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// -------------------------------------------------------------------------
|
|
88
|
-
// Static factory helpers
|
|
89
|
-
// -------------------------------------------------------------------------
|
|
90
|
-
|
|
91
|
-
static getValidator(): IProviderValidator {
|
|
92
|
-
return {
|
|
93
|
-
validate(config: Record<string, unknown>): ValidationError[] {
|
|
94
|
-
const errors: ValidationError[] = [];
|
|
95
|
-
|
|
96
|
-
if (!config.apiKey && !process.env.GOOGLE_GENAI_API_KEY) {
|
|
97
|
-
errors.push({
|
|
98
|
-
field: 'llm.apiKey',
|
|
99
|
-
message: 'Gemini API key is required',
|
|
100
|
-
suggestion: 'Set GOOGLE_GENAI_API_KEY environment variable or provide in config',
|
|
101
|
-
severity: 'error',
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
if (!config.model) {
|
|
106
|
-
errors.push({
|
|
107
|
-
field: 'llm.model',
|
|
108
|
-
message: 'Gemini model name is required',
|
|
109
|
-
severity: 'error',
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return errors;
|
|
114
|
-
},
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
static getHealthChecker(): IProviderHealthChecker {
|
|
119
|
-
return {
|
|
120
|
-
async check(config: Record<string, unknown>): Promise<HealthCheckResult> {
|
|
121
|
-
const timestamp = Date.now();
|
|
122
|
-
const apiKey = (config.apiKey as string | undefined) ?? process.env.GOOGLE_GENAI_API_KEY ?? '';
|
|
123
|
-
const modelName = sanitizeModel(config.model as string);
|
|
124
|
-
|
|
125
|
-
try {
|
|
126
|
-
const { GoogleGenerativeAI } = await import('@google/generative-ai');
|
|
127
|
-
const ai = new GoogleGenerativeAI(apiKey);
|
|
128
|
-
|
|
129
|
-
// Lightweight verification: attempt a minimal embed call
|
|
130
|
-
const model = ai.getGenerativeModel({ model: DEFAULT_EMBEDDING_MODEL });
|
|
131
|
-
await model.embedContent('health-check');
|
|
132
|
-
|
|
133
|
-
return {
|
|
134
|
-
healthy: true,
|
|
135
|
-
provider: 'gemini',
|
|
136
|
-
capabilities: { model: modelName },
|
|
137
|
-
timestamp,
|
|
138
|
-
};
|
|
139
|
-
} catch (error) {
|
|
140
|
-
return {
|
|
141
|
-
healthy: false,
|
|
142
|
-
provider: 'gemini',
|
|
143
|
-
error: error instanceof Error ? error.message : String(error),
|
|
144
|
-
timestamp,
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
},
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// -------------------------------------------------------------------------
|
|
152
|
-
// Private utilities
|
|
153
|
-
// -------------------------------------------------------------------------
|
|
154
|
-
|
|
155
|
-
/** Resolve the embedding client — uses a separate client when the embedding
|
|
156
|
-
* API key differs from the LLM API key. */
|
|
157
|
-
private get embeddingClient(): GoogleGenerativeAI {
|
|
158
|
-
if (this.embeddingConfig?.apiKey && this.embeddingConfig.apiKey !== this.llmConfig.apiKey) {
|
|
159
|
-
return buildClient(this.embeddingConfig.apiKey);
|
|
160
|
-
}
|
|
161
|
-
return this.client;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/** Resolve the embedding model to use, in order of specificity. */
|
|
165
|
-
private resolveEmbeddingModel(optionsModel?: string): string {
|
|
166
|
-
return sanitizeModel(optionsModel ?? this.embeddingConfig?.model ?? DEFAULT_EMBEDDING_MODEL);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Convert ChatMessage[] to the Gemini contents format.
|
|
171
|
-
*
|
|
172
|
-
* Rules enforced by the Gemini API:
|
|
173
|
-
* - Only `user` and `model` roles are allowed in `contents`.
|
|
174
|
-
* - System messages must be passed via `systemInstruction`, not here.
|
|
175
|
-
* - Messages must strictly alternate between `user` and `model`.
|
|
176
|
-
*/
|
|
177
|
-
private buildGeminiContents(messages: ChatMessage[]): Array<{ role: 'user' | 'model'; parts: [{ text: string }] }> {
|
|
178
|
-
return (messages || [])
|
|
179
|
-
.filter((m): m is ChatMessage => Boolean(m && typeof m === 'object' && m.role !== 'system'))
|
|
180
|
-
.map((m) => ({
|
|
181
|
-
role: m.role === 'assistant' ? 'model' : 'user',
|
|
182
|
-
parts: [{ text: typeof m.content === 'string' ? m.content : (m.content != null ? String(m.content) : '') }],
|
|
183
|
-
}));
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// -------------------------------------------------------------------------
|
|
187
|
-
// ILLMProvider — chat
|
|
188
|
-
// -------------------------------------------------------------------------
|
|
189
|
-
|
|
190
|
-
async chat(messages: ChatMessage[], context: string, options?: ChatOptions): Promise<string> {
|
|
191
|
-
const basePrompt =
|
|
192
|
-
options?.systemPrompt ??
|
|
193
|
-
this.llmConfig.systemPrompt ??
|
|
194
|
-
'You are a helpful assistant. Answer questions based on the provided context.';
|
|
195
|
-
|
|
196
|
-
const model = this.client.getGenerativeModel({
|
|
197
|
-
model: this.llmConfig.model,
|
|
198
|
-
systemInstruction: buildSystemContent(basePrompt, context),
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
const result = await model.generateContent({
|
|
202
|
-
contents: this.buildGeminiContents(messages),
|
|
203
|
-
generationConfig: {
|
|
204
|
-
temperature: options?.temperature ?? this.llmConfig.temperature ?? DEFAULT_TEMPERATURE,
|
|
205
|
-
maxOutputTokens: options?.maxTokens ?? this.llmConfig.maxTokens ?? DEFAULT_MAX_TOKENS,
|
|
206
|
-
stopSequences: options?.stop,
|
|
207
|
-
},
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
return result.response.text();
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
async *chatStream(messages: ChatMessage[], context: string, options?: ChatOptions): AsyncIterable<string> {
|
|
214
|
-
const basePrompt =
|
|
215
|
-
options?.systemPrompt ??
|
|
216
|
-
this.llmConfig.systemPrompt ??
|
|
217
|
-
'You are a helpful assistant. Answer questions based on the provided context.';
|
|
218
|
-
|
|
219
|
-
const model = this.client.getGenerativeModel({
|
|
220
|
-
model: this.llmConfig.model,
|
|
221
|
-
systemInstruction: buildSystemContent(basePrompt, context),
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
const result = await model.generateContentStream({
|
|
225
|
-
contents: this.buildGeminiContents(messages),
|
|
226
|
-
generationConfig: {
|
|
227
|
-
temperature: options?.temperature ?? this.llmConfig.temperature ?? DEFAULT_TEMPERATURE,
|
|
228
|
-
maxOutputTokens: options?.maxTokens ?? this.llmConfig.maxTokens ?? DEFAULT_MAX_TOKENS,
|
|
229
|
-
stopSequences: options?.stop,
|
|
230
|
-
},
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
for await (const chunk of result.stream) {
|
|
234
|
-
const text = chunk.text();
|
|
235
|
-
if (text) yield text;
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
// -------------------------------------------------------------------------
|
|
240
|
-
// ILLMProvider — embeddings
|
|
241
|
-
// -------------------------------------------------------------------------
|
|
242
|
-
|
|
243
|
-
async embed(text: string, options?: EmbedOptions): Promise<number[]> {
|
|
244
|
-
const content = applyPrefix(
|
|
245
|
-
text,
|
|
246
|
-
options?.taskType,
|
|
247
|
-
this.embeddingConfig?.queryPrefix,
|
|
248
|
-
this.embeddingConfig?.documentPrefix,
|
|
249
|
-
);
|
|
250
|
-
|
|
251
|
-
const modelName = this.resolveEmbeddingModel(options?.model);
|
|
252
|
-
const model = this.embeddingClient.getGenerativeModel({ model: modelName });
|
|
253
|
-
|
|
254
|
-
const response = await model.embedContent({
|
|
255
|
-
content: { role: 'user', parts: [{ text: content }] },
|
|
256
|
-
}).catch((err: Error) => {
|
|
257
|
-
console.error(`[GeminiProvider] Embedding failed for model "${modelName}":`, err.message);
|
|
258
|
-
throw err;
|
|
259
|
-
});
|
|
260
|
-
|
|
261
|
-
return response.embedding.values;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
async batchEmbed(texts: string[], options?: EmbedOptions): Promise<number[][]> {
|
|
265
|
-
const modelName = this.resolveEmbeddingModel(options?.model);
|
|
266
|
-
const model = this.embeddingClient.getGenerativeModel({ model: modelName });
|
|
267
|
-
|
|
268
|
-
const requests = texts.map((text) => ({
|
|
269
|
-
content: {
|
|
270
|
-
role: 'user',
|
|
271
|
-
parts: [{
|
|
272
|
-
text: applyPrefix(
|
|
273
|
-
text,
|
|
274
|
-
options?.taskType,
|
|
275
|
-
this.embeddingConfig?.queryPrefix,
|
|
276
|
-
this.embeddingConfig?.documentPrefix,
|
|
277
|
-
)
|
|
278
|
-
}]
|
|
279
|
-
},
|
|
280
|
-
}));
|
|
281
|
-
|
|
282
|
-
const response = await model.batchEmbedContents({
|
|
283
|
-
requests: requests.map(r => ({
|
|
284
|
-
model: `models/${modelName}`,
|
|
285
|
-
content: r.content,
|
|
286
|
-
}))
|
|
287
|
-
}).catch((err: Error) => {
|
|
288
|
-
console.error(`[GeminiProvider] Batch embedding failed for model "${modelName}":`, err.message);
|
|
289
|
-
throw err;
|
|
290
|
-
});
|
|
291
|
-
|
|
292
|
-
return response.embeddings.map((e) => e.values ?? []);
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
// -------------------------------------------------------------------------
|
|
296
|
-
// ILLMProvider — health
|
|
297
|
-
// -------------------------------------------------------------------------
|
|
298
|
-
|
|
299
|
-
async ping(): Promise<boolean> {
|
|
300
|
-
try {
|
|
301
|
-
const model = this.embeddingClient.getGenerativeModel({
|
|
302
|
-
model: this.resolveEmbeddingModel(),
|
|
303
|
-
});
|
|
304
|
-
await model.embedContent('ping');
|
|
305
|
-
return true;
|
|
306
|
-
} catch (err) {
|
|
307
|
-
console.error('[GeminiProvider] Ping failed:', err);
|
|
308
|
-
return false;
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
}
|
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Groq LLM Provider
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import OpenAI from 'openai';
|
|
6
|
-
import { ILLMProvider, EmbedOptions } from '../ILLMProvider';
|
|
7
|
-
import { ChatMessage, ChatOptions } from '../../types';
|
|
8
|
-
import { LLMConfig, EmbeddingConfig } from '../../config/RagConfig';
|
|
9
|
-
import { IProviderValidator, IProviderHealthChecker, HealthCheckResult } from '../../core/ProviderInterfaces';
|
|
10
|
-
import { ValidationError } from '../../core/ConfigValidator';
|
|
11
|
-
import { buildSystemContent } from '../utils';
|
|
12
|
-
|
|
13
|
-
export class GroqProvider implements ILLMProvider {
|
|
14
|
-
private readonly client: OpenAI;
|
|
15
|
-
private readonly llmConfig: LLMConfig;
|
|
16
|
-
|
|
17
|
-
constructor(llmConfig: LLMConfig, embeddingConfig?: EmbeddingConfig) {
|
|
18
|
-
void embeddingConfig;
|
|
19
|
-
const apiKey = llmConfig.apiKey || process.env.GROQ_API_KEY;
|
|
20
|
-
if (!apiKey) throw new Error('[GroqProvider] llmConfig.apiKey or GROQ_API_KEY environment variable is required');
|
|
21
|
-
|
|
22
|
-
this.client = new OpenAI({
|
|
23
|
-
apiKey,
|
|
24
|
-
baseURL: llmConfig.baseUrl || 'https://api.groq.com/openai/v1',
|
|
25
|
-
});
|
|
26
|
-
this.llmConfig = llmConfig;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
static getValidator(): IProviderValidator {
|
|
30
|
-
return {
|
|
31
|
-
validate(config: Record<string, unknown>): ValidationError[] {
|
|
32
|
-
const errors: ValidationError[] = [];
|
|
33
|
-
if (!config.apiKey && !process.env.GROQ_API_KEY) {
|
|
34
|
-
errors.push({
|
|
35
|
-
field: 'llm.apiKey',
|
|
36
|
-
message: 'Groq API key is required',
|
|
37
|
-
suggestion: 'Set GROQ_API_KEY environment variable',
|
|
38
|
-
severity: 'error',
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
if (!config.model) {
|
|
42
|
-
errors.push({
|
|
43
|
-
field: 'llm.model',
|
|
44
|
-
message: 'Groq model name is required',
|
|
45
|
-
suggestion: 'e.g., "llama-3.3-70b-versatile"',
|
|
46
|
-
severity: 'error',
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
return errors;
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
static getHealthChecker(): IProviderHealthChecker {
|
|
55
|
-
return {
|
|
56
|
-
async check(config: Record<string, unknown>): Promise<HealthCheckResult> {
|
|
57
|
-
const timestamp = Date.now();
|
|
58
|
-
const apiKey = (config.apiKey as string) || process.env.GROQ_API_KEY || '';
|
|
59
|
-
const modelName = config.model as string;
|
|
60
|
-
|
|
61
|
-
try {
|
|
62
|
-
const OpenAI = await import('openai');
|
|
63
|
-
const client = new OpenAI.default({
|
|
64
|
-
apiKey,
|
|
65
|
-
baseURL: (config.baseUrl as string) || 'https://api.groq.com/openai/v1',
|
|
66
|
-
});
|
|
67
|
-
const models = await client.models.list();
|
|
68
|
-
const hasModel = models.data.some((m) => m.id === modelName);
|
|
69
|
-
return {
|
|
70
|
-
healthy: true,
|
|
71
|
-
provider: 'groq',
|
|
72
|
-
capabilities: { model: modelName, available: hasModel, totalModels: models.data.length },
|
|
73
|
-
timestamp,
|
|
74
|
-
};
|
|
75
|
-
} catch (error) {
|
|
76
|
-
return {
|
|
77
|
-
healthy: false,
|
|
78
|
-
provider: 'groq',
|
|
79
|
-
error: `Connection failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
80
|
-
timestamp,
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
async chat(messages: ChatMessage[], context: string, options?: ChatOptions): Promise<string> {
|
|
88
|
-
const basePrompt =
|
|
89
|
-
options?.systemPrompt ??
|
|
90
|
-
this.llmConfig.systemPrompt ??
|
|
91
|
-
'You are a helpful assistant. Answer questions based on the provided context.';
|
|
92
|
-
|
|
93
|
-
const systemMessage: OpenAI.Chat.ChatCompletionSystemMessageParam = {
|
|
94
|
-
role: 'system',
|
|
95
|
-
content: buildSystemContent(basePrompt, context),
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
const safeMessages = (Array.isArray(messages) ? messages : [])
|
|
99
|
-
.filter((m): m is ChatMessage => Boolean(m && typeof m === 'object'))
|
|
100
|
-
.map((m) => ({
|
|
101
|
-
role: (m.role || 'user') as 'user' | 'assistant',
|
|
102
|
-
content: typeof m.content === 'string' ? m.content : (m.content != null ? String(m.content) : ''),
|
|
103
|
-
}));
|
|
104
|
-
|
|
105
|
-
const formattedMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [
|
|
106
|
-
systemMessage,
|
|
107
|
-
...safeMessages,
|
|
108
|
-
];
|
|
109
|
-
|
|
110
|
-
const completion = await this.client.chat.completions.create({
|
|
111
|
-
model: this.llmConfig.model,
|
|
112
|
-
messages: formattedMessages,
|
|
113
|
-
max_tokens: options?.maxTokens ?? this.llmConfig.maxTokens ?? 1024,
|
|
114
|
-
temperature: options?.temperature ?? this.llmConfig.temperature ?? 0.7,
|
|
115
|
-
stop: options?.stop,
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
return completion.choices[0]?.message?.content ?? '';
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
async *chatStream(messages: ChatMessage[], context: string, options?: ChatOptions): AsyncIterable<string> {
|
|
122
|
-
const basePrompt =
|
|
123
|
-
options?.systemPrompt ??
|
|
124
|
-
this.llmConfig.systemPrompt ??
|
|
125
|
-
'You are a helpful assistant. Answer questions based on the provided context.';
|
|
126
|
-
|
|
127
|
-
const systemMessage: OpenAI.Chat.ChatCompletionSystemMessageParam = {
|
|
128
|
-
role: 'system',
|
|
129
|
-
content: buildSystemContent(basePrompt, context),
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
const safeStreamMessages = (Array.isArray(messages) ? messages : [])
|
|
133
|
-
.filter((m): m is ChatMessage => Boolean(m && typeof m === 'object'))
|
|
134
|
-
.map((m) => ({
|
|
135
|
-
role: (m.role || 'user') as 'user' | 'assistant',
|
|
136
|
-
content: typeof m.content === 'string' ? m.content : (m.content != null ? String(m.content) : ''),
|
|
137
|
-
}));
|
|
138
|
-
|
|
139
|
-
const formattedMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [
|
|
140
|
-
systemMessage,
|
|
141
|
-
...safeStreamMessages,
|
|
142
|
-
];
|
|
143
|
-
|
|
144
|
-
const stream = await this.client.chat.completions.create({
|
|
145
|
-
model: this.llmConfig.model,
|
|
146
|
-
messages: formattedMessages,
|
|
147
|
-
max_tokens: options?.maxTokens ?? this.llmConfig.maxTokens ?? 1024,
|
|
148
|
-
temperature: options?.temperature ?? this.llmConfig.temperature ?? 0.7,
|
|
149
|
-
stop: options?.stop,
|
|
150
|
-
stream: true,
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
if (!stream) {
|
|
154
|
-
throw new Error('[GroqProvider] completions.create stream is undefined');
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
for await (const chunk of stream) {
|
|
158
|
-
const content = chunk.choices[0]?.delta?.content || '';
|
|
159
|
-
if (content) yield content;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
async embed(text: string, options?: EmbedOptions): Promise<number[]> {
|
|
164
|
-
void text;
|
|
165
|
-
void options;
|
|
166
|
-
throw new Error('[GroqProvider] Groq does not provide a native embedding API. Please configure "openai" or "gemini" for embeddings in RagConfig.');
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
async batchEmbed(texts: string[], options?: EmbedOptions): Promise<number[][]> {
|
|
170
|
-
void texts;
|
|
171
|
-
void options;
|
|
172
|
-
throw new Error('[GroqProvider] Groq does not provide a native embedding API. Please configure "openai" or "gemini" for embeddings in RagConfig.');
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
async ping(): Promise<boolean> {
|
|
176
|
-
try {
|
|
177
|
-
await this.client.models.list();
|
|
178
|
-
return true;
|
|
179
|
-
} catch (err) {
|
|
180
|
-
console.error('[GroqProvider] Ping failed:', err);
|
|
181
|
-
return false;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
}
|