@retrivora-ai/rag-engine 2.4.3 → 2.4.6

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.
Files changed (142) hide show
  1. package/README.md +22 -0
  2. package/dist/{BatchProcessor-zbQMaZHm.d.mts → BatchProcessor-BGslIR5l.d.mts} +65 -4
  3. package/dist/{BatchProcessor-D42E706a.d.ts → BatchProcessor-C3gY6kZ0.d.ts} +65 -4
  4. package/dist/handlers/index.js +668 -837
  5. package/dist/handlers/index.mjs +678 -837
  6. package/dist/index.css +5 -0
  7. package/dist/index.d.mts +4 -28
  8. package/dist/index.d.ts +4 -28
  9. package/dist/index.js +91 -43
  10. package/dist/index.min.css +1 -1
  11. package/dist/index.mjs +91 -43
  12. package/dist/server.d.mts +21 -7
  13. package/dist/server.d.ts +21 -7
  14. package/dist/server.js +731 -875
  15. package/dist/server.mjs +741 -875
  16. package/package.json +11 -4
  17. package/src/LicensedRetrivora.ts +0 -278
  18. package/src/app/api/chat/route.ts +0 -4
  19. package/src/app/api/health/route.ts +0 -5
  20. package/src/app/api/ingest/route.ts +0 -27
  21. package/src/app/api/suggestions/route.ts +0 -5
  22. package/src/app/api/upload/route.ts +0 -5
  23. package/src/app/constants.tsx +0 -270
  24. package/src/app/favicon.ico +0 -0
  25. package/src/app/globals.css +0 -449
  26. package/src/app/layout.tsx +0 -22
  27. package/src/app/page.tsx +0 -64
  28. package/src/app/types.ts +0 -30
  29. package/src/browser.tsx +0 -127
  30. package/src/components/ChatWidget.tsx +0 -393
  31. package/src/components/ChatWindow.tsx +0 -952
  32. package/src/components/CodeViewer.tsx +0 -56
  33. package/src/components/ConfigProvider.tsx +0 -141
  34. package/src/components/DocumentUpload.tsx +0 -354
  35. package/src/components/DynamicChart.tsx +0 -297
  36. package/src/components/HourglassLoader.tsx +0 -87
  37. package/src/components/MarkdownComponents.tsx +0 -186
  38. package/src/components/MessageBubble.tsx +0 -498
  39. package/src/components/ObservabilityPanel.tsx +0 -374
  40. package/src/components/ProductCard.tsx +0 -107
  41. package/src/components/ProductCarousel.tsx +0 -97
  42. package/src/components/SourceCard.tsx +0 -61
  43. package/src/components/ThemeProvider.tsx +0 -8
  44. package/src/components/ThemeToggle.tsx +0 -29
  45. package/src/components/ThinkingBlock.tsx +0 -75
  46. package/src/components/UIDispatcher.tsx +0 -342
  47. package/src/components/VisualizationRenderer.tsx +0 -621
  48. package/src/components/constants.tsx +0 -503
  49. package/src/config/ConfigBuilder.ts +0 -285
  50. package/src/config/EmbeddingStrategy.ts +0 -148
  51. package/src/config/LicenseConfigResolver.ts +0 -298
  52. package/src/config/RagConfig.ts +0 -328
  53. package/src/config/UniversalProfiles.ts +0 -82
  54. package/src/config/constants.ts +0 -64
  55. package/src/config/serverConfig.ts +0 -378
  56. package/src/config/uiConstants.ts +0 -47
  57. package/src/core/BatchProcessor.ts +0 -385
  58. package/src/core/CircuitBreaker.ts +0 -118
  59. package/src/core/ConfigFetcher.ts +0 -220
  60. package/src/core/ConfigResolver.ts +0 -165
  61. package/src/core/ConfigValidator.ts +0 -197
  62. package/src/core/DatabaseStorage.ts +0 -526
  63. package/src/core/FreeTierLimitsGuard.ts +0 -281
  64. package/src/core/LLMRouter.ts +0 -79
  65. package/src/core/LangChainAgent.ts +0 -132
  66. package/src/core/LicenseValidator.ts +0 -284
  67. package/src/core/LicenseVerifier.ts +0 -261
  68. package/src/core/MultiAgentCoordinator.ts +0 -239
  69. package/src/core/Pipeline.ts +0 -1279
  70. package/src/core/PluginManager.ts +0 -277
  71. package/src/core/ProjectSettings.ts +0 -247
  72. package/src/core/ProviderHealthCheck.ts +0 -133
  73. package/src/core/ProviderInterfaces.ts +0 -37
  74. package/src/core/ProviderRegistry.ts +0 -156
  75. package/src/core/QueryProcessor.ts +0 -399
  76. package/src/core/Retrivora.ts +0 -98
  77. package/src/core/VectorPlugin.ts +0 -193
  78. package/src/core/mcp.ts +0 -261
  79. package/src/exceptions/index.ts +0 -131
  80. package/src/handlers/index.ts +0 -1625
  81. package/src/hooks/useHydrated.ts +0 -13
  82. package/src/hooks/useProjectSettings.ts +0 -105
  83. package/src/hooks/useRagChat.ts +0 -436
  84. package/src/hooks/useStoredMessages.ts +0 -64
  85. package/src/index.css +0 -4269
  86. package/src/index.ts +0 -137
  87. package/src/lib/plugin.ts +0 -24
  88. package/src/llm/ILLMProvider.ts +0 -60
  89. package/src/llm/LLMFactory.ts +0 -141
  90. package/src/llm/providers/AnthropicProvider.ts +0 -215
  91. package/src/llm/providers/GeminiProvider.ts +0 -311
  92. package/src/llm/providers/GroqProvider.ts +0 -184
  93. package/src/llm/providers/OllamaProvider.ts +0 -232
  94. package/src/llm/providers/OpenAIProvider.ts +0 -188
  95. package/src/llm/providers/QwenProvider.ts +0 -199
  96. package/src/llm/providers/UniversalLLMAdapter.ts +0 -422
  97. package/src/llm/utils.ts +0 -46
  98. package/src/providers/graphdb/BaseGraphProvider.ts +0 -43
  99. package/src/providers/graphdb/SimpleGraphProvider.ts +0 -66
  100. package/src/providers/vectordb/BaseVectorProvider.ts +0 -98
  101. package/src/providers/vectordb/ChromaDBProvider.ts +0 -130
  102. package/src/providers/vectordb/MilvusProvider.ts +0 -134
  103. package/src/providers/vectordb/MongoDBProvider.ts +0 -250
  104. package/src/providers/vectordb/MultiTablePostgresProvider.ts +0 -461
  105. package/src/providers/vectordb/PineconeProvider.ts +0 -295
  106. package/src/providers/vectordb/PostgreSQLProvider.ts +0 -227
  107. package/src/providers/vectordb/QdrantProvider.ts +0 -294
  108. package/src/providers/vectordb/RedisProvider.ts +0 -105
  109. package/src/providers/vectordb/UniversalVectorProvider.ts +0 -221
  110. package/src/providers/vectordb/WeaviateProvider.ts +0 -172
  111. package/src/rag/DocumentChunker.ts +0 -144
  112. package/src/rag/EntityExtractor.ts +0 -90
  113. package/src/rag/LlamaIndexIngestor.ts +0 -60
  114. package/src/rag/Reranker.ts +0 -106
  115. package/src/rendering/IntentClassifier.ts +0 -167
  116. package/src/rendering/RendererRegistry.ts +0 -164
  117. package/src/rendering/RuleEngine.ts +0 -67
  118. package/src/rendering/VisualizationDecisionEngine.ts +0 -164
  119. package/src/rendering/__tests__/VisualizationDecisionEngine.test.ts +0 -237
  120. package/src/rendering/index.ts +0 -12
  121. package/src/rendering/rules/Rule1SpecificInfoRule.ts +0 -28
  122. package/src/rendering/rules/Rule2ComparisonRule.ts +0 -24
  123. package/src/rendering/rules/Rule3ProductDiscoveryRule.ts +0 -28
  124. package/src/rendering/rules/Rule4AnalyticalRule.ts +0 -38
  125. package/src/rendering/rules/Rule5MixedResponseRule.ts +0 -29
  126. package/src/rendering/rules/Rule6SmallResultSetRule.ts +0 -24
  127. package/src/rendering/rules/Rule7LargeTableRule.ts +0 -35
  128. package/src/rendering/types.ts +0 -96
  129. package/src/server.ts +0 -156
  130. package/src/tailwind.css +0 -194
  131. package/src/telemetry/TelemetryService.ts +0 -330
  132. package/src/types/chat.ts +0 -89
  133. package/src/types/index.ts +0 -278
  134. package/src/types/pdf-parse.d.ts +0 -13
  135. package/src/types/props.ts +0 -169
  136. package/src/utils/DocumentParser.ts +0 -111
  137. package/src/utils/ProductExtractor.ts +0 -350
  138. package/src/utils/SchemaMapper.ts +0 -176
  139. package/src/utils/UITransformer.ts +0 -1812
  140. package/src/utils/synonyms.ts +0 -80
  141. package/src/utils/templateUtils.ts +0 -78
  142. package/src/version.ts +0 -6
@@ -1,232 +0,0 @@
1
- /**
2
- * Ollama LLM Provider (local / self-hosted)
3
- */
4
-
5
- import axios, { AxiosInstance } from 'axios';
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
- interface OllamaChatResponse {
14
- message: { content: string };
15
- }
16
-
17
- interface OllamaEmbedResponse {
18
- embedding: number[];
19
- }
20
-
21
- export class OllamaProvider implements ILLMProvider {
22
- private readonly http: AxiosInstance;
23
- private readonly llmConfig: LLMConfig;
24
- private readonly embeddingConfig?: EmbeddingConfig;
25
-
26
- constructor(llmConfig: LLMConfig, embeddingConfig?: EmbeddingConfig) {
27
- const rawBaseURL = (llmConfig.baseUrl ?? 'http://localhost:11434').replace(/\/+$/, '');
28
- const baseURL = rawBaseURL.replace(/\/(?:api\/v1|v1|api)$/i, '');
29
- const timeout = Number(llmConfig.options?.timeout) || 300_000;
30
- const headers = (llmConfig.options?.headers as Record<string, string>) || {};
31
- this.http = axios.create({ baseURL, timeout, headers });
32
- this.llmConfig = llmConfig;
33
- this.embeddingConfig = embeddingConfig;
34
- }
35
-
36
- static getValidator(): IProviderValidator {
37
- return {
38
- validate(config: Record<string, unknown>): ValidationError[] {
39
- const errors: ValidationError[] = [];
40
- if (!config.model) {
41
- const isEmbedding = config.provider === 'ollama' && 'dimensions' in config;
42
- const prefix = isEmbedding ? 'embedding' : 'llm';
43
- errors.push({
44
- field: `${prefix}.model`,
45
- message: 'Ollama model name is required',
46
- suggestion: 'e.g., "llama3" or "mistral"',
47
- severity: 'error',
48
- });
49
- }
50
- return errors;
51
- }
52
- };
53
- }
54
-
55
- static getHealthChecker(): IProviderHealthChecker {
56
- return {
57
- async check(config: Record<string, unknown>): Promise<HealthCheckResult> {
58
- const timestamp = Date.now();
59
- const baseUrl = (config.baseUrl as string) || 'http://localhost:11434';
60
- const modelName = config.model as string;
61
-
62
- try {
63
- const axios = (await import('axios')).default;
64
- const { data } = await axios.get(`${baseUrl}/api/tags`);
65
- const models = (data.models as { name: string }[]) || [];
66
- const hasModel = models.some((m) => m.name === modelName || m.name.startsWith(`${modelName}:`));
67
-
68
- return {
69
- healthy: true,
70
- provider: 'ollama',
71
- capabilities: {
72
- baseUrl,
73
- model: modelName,
74
- available: hasModel,
75
- totalModels: models.length
76
- },
77
- timestamp,
78
- };
79
- } catch {
80
- return {
81
- healthy: false,
82
- provider: 'ollama',
83
- error: `Ollama server not reachable at ${baseUrl}. Is it running?`,
84
- timestamp,
85
- };
86
- }
87
- }
88
- };
89
- }
90
-
91
- async chat(messages: ChatMessage[], context: string, options?: ChatOptions): Promise<string> {
92
- const basePrompt =
93
- options?.systemPrompt ??
94
- this.llmConfig.systemPrompt ??
95
- 'You are a helpful assistant. Use the provided context to answer the user\'s question.';
96
- const system = buildSystemContent(basePrompt, context);
97
-
98
- const { data } = await this.http.post<OllamaChatResponse>('/api/chat', {
99
- model: this.llmConfig.model,
100
- stream: false,
101
- options: {
102
- temperature: options?.temperature ?? this.llmConfig.temperature ?? 0.7,
103
- num_predict: options?.maxTokens ?? this.llmConfig.maxTokens ?? 1024,
104
- },
105
- messages: [
106
- { role: 'system', content: system },
107
- ...messages.map((m) => ({ role: m.role, content: m.content })),
108
- ],
109
- });
110
-
111
- return data.message.content;
112
- }
113
-
114
- async *chatStream(messages: ChatMessage[], context: string, options?: ChatOptions): AsyncIterable<string> {
115
- const basePrompt =
116
- options?.systemPrompt ??
117
- this.llmConfig.systemPrompt ??
118
- 'You are a helpful assistant. Use the provided context to answer the user\'s question.';
119
- const system = buildSystemContent(basePrompt, context);
120
-
121
- const response = await this.http.post('/api/chat', {
122
- model: this.llmConfig.model,
123
- stream: true,
124
- options: {
125
- temperature: options?.temperature ?? this.llmConfig.temperature ?? 0.7,
126
- num_predict: options?.maxTokens ?? this.llmConfig.maxTokens ?? 1024,
127
- },
128
- messages: [
129
- { role: 'system', content: system },
130
- ...messages.map((m) => ({ role: m.role, content: m.content })),
131
- ],
132
- }, { responseType: 'stream' });
133
-
134
- let lineBuffer = '';
135
- const stream = response.data;
136
- if (!stream) {
137
- throw new Error('[OllamaProvider] response.data is undefined for stream request. Axios might not support streams in this environment.');
138
- }
139
-
140
- for await (const chunk of stream) {
141
- lineBuffer += chunk.toString();
142
- const lines = lineBuffer.split('\n');
143
-
144
- // Keep the last partial line in the buffer
145
- lineBuffer = lines.pop() || '';
146
-
147
- for (const line of lines) {
148
- if (!line.trim()) continue;
149
- try {
150
- const json = JSON.parse(line);
151
- if (json.message?.content) {
152
- yield json.message.content;
153
- }
154
- if (json.done) return;
155
- } catch {
156
- // If parsing fails, it might be a malformed line or something else
157
- // But with line buffering, this should be rare
158
- console.warn('[OllamaProvider] Failed to parse streaming line:', line);
159
- }
160
- }
161
- }
162
-
163
- // Process anything left in the buffer at the end
164
- if (lineBuffer.trim()) {
165
- try {
166
- const json = JSON.parse(lineBuffer);
167
- if (json.message?.content) yield json.message.content;
168
- } catch {
169
- // Final cleanup
170
- }
171
- }
172
- }
173
-
174
-
175
- async embed(text: string, options?: EmbedOptions): Promise<number[]> {
176
- const model =
177
- options?.model ??
178
- this.embeddingConfig?.model ??
179
- 'nomic-embed-text';
180
-
181
- const baseURL =
182
- this.embeddingConfig?.baseUrl ?? this.llmConfig.baseUrl ?? 'http://localhost:11434';
183
-
184
- const client =
185
- baseURL !== (this.llmConfig.baseUrl ?? 'http://localhost:11434')
186
- ? axios.create({ baseURL, timeout: 60_000 })
187
- : this.http;
188
-
189
- let prompt = text;
190
-
191
- // Dynamically apply prefixes from configuration if they exist
192
- // Fallback to standard nomic-embed-text prefixes if not provided
193
- const queryPrefix = this.embeddingConfig?.queryPrefix ?? (model.includes('nomic') ? 'search_query: ' : '');
194
- const docPrefix = this.embeddingConfig?.documentPrefix ?? (model.includes('nomic') ? 'search_document: ' : '');
195
-
196
- if (options?.taskType === 'query' && queryPrefix) {
197
- if (!prompt.startsWith(queryPrefix)) {
198
- prompt = `${queryPrefix}${text}`;
199
- }
200
- } else if (options?.taskType === 'document' && docPrefix) {
201
- if (!prompt.startsWith(docPrefix)) {
202
- prompt = `${docPrefix}${text}`;
203
- }
204
- }
205
-
206
- const { data } = await client.post<OllamaEmbedResponse>('/api/embeddings', {
207
- model,
208
- prompt,
209
- });
210
-
211
- return data.embedding;
212
- }
213
-
214
- async batchEmbed(texts: string[], options?: EmbedOptions): Promise<number[][]> {
215
- const vectors: number[][] = [];
216
- // Sequential fallback for Ollama (batch support varies by version)
217
- for (const text of texts) {
218
- vectors.push(await this.embed(text, options));
219
- }
220
- return vectors;
221
- }
222
-
223
- async ping(): Promise<boolean> {
224
- try {
225
- await this.http.get('/api/tags');
226
- return true;
227
- } catch (err) {
228
- console.error('[OllamaProvider] Ping failed:', err);
229
- return false;
230
- }
231
- }
232
- }
@@ -1,188 +0,0 @@
1
- /**
2
- * OpenAI 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 OpenAIProvider implements ILLMProvider {
14
- private readonly client: OpenAI;
15
- private readonly llmConfig: LLMConfig;
16
- private readonly embeddingConfig?: EmbeddingConfig;
17
-
18
- constructor(llmConfig: LLMConfig, embeddingConfig?: EmbeddingConfig) {
19
- if (!llmConfig.apiKey) throw new Error('[OpenAIProvider] llmConfig.apiKey is required');
20
- this.client = new OpenAI({ apiKey: llmConfig.apiKey });
21
- this.llmConfig = llmConfig;
22
- this.embeddingConfig = embeddingConfig;
23
- }
24
-
25
- static getValidator(): IProviderValidator {
26
- return {
27
- validate(config: Record<string, unknown>): ValidationError[] {
28
- const errors: ValidationError[] = [];
29
- const isEmbedding = config.provider === 'openai' && 'dimensions' in config;
30
- const prefix = isEmbedding ? 'embedding' : 'llm';
31
-
32
- if (!config.apiKey) {
33
- errors.push({
34
- field: `${prefix}.apiKey`,
35
- message: 'OpenAI API key is required',
36
- suggestion: 'Set OPENAI_API_KEY environment variable',
37
- severity: 'error',
38
- });
39
- }
40
- if (!config.model) {
41
- errors.push({
42
- field: `${prefix}.model`,
43
- message: 'OpenAI model name is required',
44
- suggestion: isEmbedding ? 'e.g., "text-embedding-3-small"' : 'e.g., "gpt-4o"',
45
- severity: 'error',
46
- });
47
- }
48
- return errors;
49
- }
50
- };
51
- }
52
-
53
- static getHealthChecker(): IProviderHealthChecker {
54
- return {
55
- async check(config: Record<string, unknown>): Promise<HealthCheckResult> {
56
- const timestamp = Date.now();
57
- const apiKey = config.apiKey as string;
58
- const modelName = config.model as string;
59
-
60
- try {
61
- const OpenAI = await import('openai');
62
- const client = new OpenAI.default({ apiKey });
63
- const models = await client.models.list();
64
- const hasModel = models.data.some((m) => m.id === modelName);
65
- return {
66
- healthy: true,
67
- provider: 'openai',
68
- capabilities: { model: modelName, available: hasModel, totalModels: models.data.length },
69
- timestamp,
70
- };
71
- } catch (error) {
72
- return {
73
- healthy: false,
74
- provider: 'openai',
75
- error: `Connection failed: ${error instanceof Error ? error.message : String(error)}`,
76
- timestamp,
77
- };
78
- }
79
- }
80
- };
81
- }
82
-
83
- async chat(messages: ChatMessage[], context: string, options?: ChatOptions): Promise<string> {
84
- const basePrompt =
85
- options?.systemPrompt ??
86
- this.llmConfig.systemPrompt ??
87
- 'You are a helpful assistant. Answer questions based on the provided context.';
88
-
89
- const systemMessage: OpenAI.Chat.ChatCompletionSystemMessageParam = {
90
- role: 'system',
91
- content: buildSystemContent(basePrompt, context),
92
- };
93
-
94
- const safeMessages = (Array.isArray(messages) ? messages : [])
95
- .filter((m): m is ChatMessage => Boolean(m && typeof m === 'object'))
96
- .map((m) => ({
97
- role: (m.role || 'user') as 'user' | 'assistant',
98
- content: typeof m.content === 'string' ? m.content : (m.content != null ? String(m.content) : ''),
99
- }));
100
-
101
- const formattedMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [
102
- systemMessage,
103
- ...safeMessages,
104
- ];
105
-
106
- const completion = await this.client.chat.completions.create({
107
- model: this.llmConfig.model,
108
- messages: formattedMessages,
109
- max_tokens: options?.maxTokens ?? this.llmConfig.maxTokens ?? 1024,
110
- temperature: options?.temperature ?? this.llmConfig.temperature ?? 0.7,
111
- stop: options?.stop,
112
- });
113
-
114
- return completion.choices[0]?.message?.content ?? '';
115
- }
116
-
117
- async *chatStream(messages: ChatMessage[], context: string, options?: ChatOptions): AsyncIterable<string> {
118
- const basePrompt =
119
- options?.systemPrompt ??
120
- this.llmConfig.systemPrompt ??
121
- 'You are a helpful assistant. Answer questions based on the provided context.';
122
-
123
- const systemMessage: OpenAI.Chat.ChatCompletionSystemMessageParam = {
124
- role: 'system',
125
- content: buildSystemContent(basePrompt, context),
126
- };
127
-
128
- const safeStreamMessages = (Array.isArray(messages) ? messages : [])
129
- .filter((m): m is ChatMessage => Boolean(m && typeof m === 'object'))
130
- .map((m) => ({
131
- role: (m.role || 'user') as 'user' | 'assistant',
132
- content: typeof m.content === 'string' ? m.content : (m.content != null ? String(m.content) : ''),
133
- }));
134
-
135
- const formattedMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [
136
- systemMessage,
137
- ...safeStreamMessages,
138
- ];
139
-
140
- const stream = await this.client.chat.completions.create({
141
- model: this.llmConfig.model,
142
- messages: formattedMessages,
143
- max_tokens: options?.maxTokens ?? this.llmConfig.maxTokens ?? 1024,
144
- temperature: options?.temperature ?? this.llmConfig.temperature ?? 0.7,
145
- stop: options?.stop,
146
- stream: true,
147
- });
148
-
149
- if (!stream) {
150
- throw new Error('[OpenAIProvider] completions.create stream is undefined');
151
- }
152
-
153
- for await (const chunk of stream) {
154
- const content = chunk.choices[0]?.delta?.content || '';
155
- if (content) yield content;
156
- }
157
- }
158
-
159
- async embed(text: string, options?: EmbedOptions): Promise<number[]> {
160
- const results = await this.batchEmbed([text], options);
161
- return results[0];
162
- }
163
-
164
- async batchEmbed(texts: string[], options?: EmbedOptions): Promise<number[][]> {
165
- const model =
166
- options?.model ??
167
- this.embeddingConfig?.model ??
168
- 'text-embedding-3-small';
169
-
170
- const apiKey = this.embeddingConfig?.apiKey ?? this.llmConfig.apiKey;
171
- const client = apiKey !== this.llmConfig.apiKey
172
- ? new OpenAI({ apiKey })
173
- : this.client;
174
-
175
- const response = await client.embeddings.create({ model, input: texts });
176
- return response.data.map((d) => d.embedding);
177
- }
178
-
179
- async ping(): Promise<boolean> {
180
- try {
181
- await this.client.models.list();
182
- return true;
183
- } catch (err) {
184
- console.error('[OpenAIProvider] Ping failed:', err);
185
- return false;
186
- }
187
- }
188
- }
@@ -1,199 +0,0 @@
1
- /**
2
- * Qwen LLM Provider (via Alibaba DashScope / ModelStudio compatible mode)
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 QwenProvider implements ILLMProvider {
14
- private readonly client: OpenAI;
15
- private readonly llmConfig: LLMConfig;
16
- private readonly embeddingConfig?: EmbeddingConfig;
17
-
18
- constructor(llmConfig: LLMConfig, embeddingConfig?: EmbeddingConfig) {
19
- const apiKey = llmConfig.apiKey || process.env.DASHSCOPE_API_KEY || process.env.QWEN_API_KEY;
20
- if (!apiKey) throw new Error('[QwenProvider] llmConfig.apiKey, DASHSCOPE_API_KEY, or QWEN_API_KEY environment variable is required');
21
-
22
- this.client = new OpenAI({
23
- apiKey,
24
- baseURL: llmConfig.baseUrl || 'https://dashscope.aliyuncs.com/compatible-mode/v1',
25
- });
26
- this.llmConfig = llmConfig;
27
- this.embeddingConfig = embeddingConfig;
28
- }
29
-
30
- static getValidator(): IProviderValidator {
31
- return {
32
- validate(config: Record<string, unknown>): ValidationError[] {
33
- const errors: ValidationError[] = [];
34
- const isEmbedding = config.provider === 'qwen' && 'dimensions' in config;
35
- const prefix = isEmbedding ? 'embedding' : 'llm';
36
-
37
- if (!config.apiKey && !process.env.DASHSCOPE_API_KEY && !process.env.QWEN_API_KEY) {
38
- errors.push({
39
- field: `${prefix}.apiKey`,
40
- message: 'Qwen API key is required',
41
- suggestion: 'Set DASHSCOPE_API_KEY or QWEN_API_KEY environment variable',
42
- severity: 'error',
43
- });
44
- }
45
- if (!config.model) {
46
- errors.push({
47
- field: `${prefix}.model`,
48
- message: 'Qwen model name is required',
49
- suggestion: isEmbedding ? 'e.g., "text-embedding-v3"' : 'e.g., "qwen-plus" or "qwen-max"',
50
- severity: 'error',
51
- });
52
- }
53
- return errors;
54
- }
55
- };
56
- }
57
-
58
- static getHealthChecker(): IProviderHealthChecker {
59
- return {
60
- async check(config: Record<string, unknown>): Promise<HealthCheckResult> {
61
- const timestamp = Date.now();
62
- const apiKey = (config.apiKey as string) || process.env.DASHSCOPE_API_KEY || process.env.QWEN_API_KEY || '';
63
- const modelName = config.model as string;
64
-
65
- try {
66
- const OpenAI = await import('openai');
67
- const client = new OpenAI.default({
68
- apiKey,
69
- baseURL: (config.baseUrl as string) || 'https://dashscope.aliyuncs.com/compatible-mode/v1',
70
- });
71
- const models = await client.models.list();
72
- const hasModel = models.data.some((m) => m.id === modelName);
73
- return {
74
- healthy: true,
75
- provider: 'qwen',
76
- capabilities: { model: modelName, available: hasModel, totalModels: models.data.length },
77
- timestamp,
78
- };
79
- } catch (error) {
80
- return {
81
- healthy: false,
82
- provider: 'qwen',
83
- error: `Connection failed: ${error instanceof Error ? error.message : String(error)}`,
84
- timestamp,
85
- };
86
- }
87
- }
88
- };
89
- }
90
-
91
- async chat(messages: ChatMessage[], context: string, options?: ChatOptions): Promise<string> {
92
- const basePrompt =
93
- options?.systemPrompt ??
94
- this.llmConfig.systemPrompt ??
95
- 'You are a helpful assistant. Answer questions based on the provided context.';
96
-
97
- const systemMessage: OpenAI.Chat.ChatCompletionSystemMessageParam = {
98
- role: 'system',
99
- content: buildSystemContent(basePrompt, context),
100
- };
101
-
102
- const safeMessages = (Array.isArray(messages) ? messages : [])
103
- .filter((m): m is ChatMessage => Boolean(m && typeof m === 'object'))
104
- .map((m) => ({
105
- role: (m.role || 'user') as 'user' | 'assistant',
106
- content: typeof m.content === 'string' ? m.content : (m.content != null ? String(m.content) : ''),
107
- }));
108
-
109
- const formattedMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [
110
- systemMessage,
111
- ...safeMessages,
112
- ];
113
-
114
- const completion = await this.client.chat.completions.create({
115
- model: this.llmConfig.model,
116
- messages: formattedMessages,
117
- max_tokens: options?.maxTokens ?? this.llmConfig.maxTokens ?? 1024,
118
- temperature: options?.temperature ?? this.llmConfig.temperature ?? 0.7,
119
- stop: options?.stop,
120
- });
121
-
122
- return completion.choices[0]?.message?.content ?? '';
123
- }
124
-
125
- async *chatStream(messages: ChatMessage[], context: string, options?: ChatOptions): AsyncIterable<string> {
126
- const basePrompt =
127
- options?.systemPrompt ??
128
- this.llmConfig.systemPrompt ??
129
- 'You are a helpful assistant. Answer questions based on the provided context.';
130
-
131
- const systemMessage: OpenAI.Chat.ChatCompletionSystemMessageParam = {
132
- role: 'system',
133
- content: buildSystemContent(basePrompt, context),
134
- };
135
-
136
- const safeStreamMessages = (Array.isArray(messages) ? messages : [])
137
- .filter((m): m is ChatMessage => Boolean(m && typeof m === 'object'))
138
- .map((m) => ({
139
- role: (m.role || 'user') as 'user' | 'assistant',
140
- content: typeof m.content === 'string' ? m.content : (m.content != null ? String(m.content) : ''),
141
- }));
142
-
143
- const formattedMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [
144
- systemMessage,
145
- ...safeStreamMessages,
146
- ];
147
-
148
- const stream = await this.client.chat.completions.create({
149
- model: this.llmConfig.model,
150
- messages: formattedMessages,
151
- max_tokens: options?.maxTokens ?? this.llmConfig.maxTokens ?? 1024,
152
- temperature: options?.temperature ?? this.llmConfig.temperature ?? 0.7,
153
- stop: options?.stop,
154
- stream: true,
155
- });
156
-
157
- if (!stream) {
158
- throw new Error('[QwenProvider] completions.create stream is undefined');
159
- }
160
-
161
- for await (const chunk of stream) {
162
- const content = chunk.choices[0]?.delta?.content || '';
163
- if (content) yield content;
164
- }
165
- }
166
-
167
- async embed(text: string, options?: EmbedOptions): Promise<number[]> {
168
- const results = await this.batchEmbed([text], options);
169
- return results[0];
170
- }
171
-
172
- async batchEmbed(texts: string[], options?: EmbedOptions): Promise<number[][]> {
173
- const model =
174
- options?.model ??
175
- this.embeddingConfig?.model ??
176
- 'text-embedding-v1';
177
-
178
- const apiKey = this.embeddingConfig?.apiKey ?? this.llmConfig.apiKey;
179
- const client = apiKey !== this.llmConfig.apiKey
180
- ? new OpenAI({
181
- apiKey,
182
- baseURL: this.embeddingConfig?.baseUrl || 'https://dashscope.aliyuncs.com/compatible-mode/v1',
183
- })
184
- : this.client;
185
-
186
- const response = await client.embeddings.create({ model, input: texts });
187
- return response.data.map((d) => d.embedding);
188
- }
189
-
190
- async ping(): Promise<boolean> {
191
- try {
192
- await this.client.models.list();
193
- return true;
194
- } catch (err) {
195
- console.error('[QwenProvider] Ping failed:', err);
196
- return false;
197
- }
198
- }
199
- }