@retrivora-ai/rag-engine 2.4.2 → 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 +31 -5
- package/dist/handlers/index.mjs +31 -5
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +41 -21
- package/dist/index.min.css +2 -0
- package/dist/index.mjs +41 -21
- package/dist/server.d.mts +2 -2
- package/dist/server.d.ts +2 -2
- package/dist/server.js +32 -5
- package/dist/server.mjs +32 -5
- package/package.json +5 -4
- 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 -139
- 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 -123
- 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
package/src/index.ts
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Package entry point — public API exported to consumers of this npm package.
|
|
5
|
-
* This entry point is CLIENT-SAFE and can be imported in React Client Components.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import './index.css';
|
|
9
|
-
|
|
10
|
-
// ── React Components ─────────────────────────────────────────
|
|
11
|
-
export { ChatWidget } from './components/ChatWidget';
|
|
12
|
-
export { ChatWindow } from './components/ChatWindow';
|
|
13
|
-
export { DocumentUpload } from './components/DocumentUpload';
|
|
14
|
-
export { MessageBubble } from './components/MessageBubble';
|
|
15
|
-
export { SourceCard } from './components/SourceCard';
|
|
16
|
-
export { ObservabilityPanel } from './components/ObservabilityPanel';
|
|
17
|
-
export { ConfigProvider, useConfig } from './components/ConfigProvider';
|
|
18
|
-
export { SDK_VERSION } from './version';
|
|
19
|
-
export { CodeViewer } from './components/CodeViewer';
|
|
20
|
-
export { ProductCard } from './components/ProductCard';
|
|
21
|
-
export { ProductCarousel } from './components/ProductCarousel';
|
|
22
|
-
|
|
23
|
-
// ── Headless Hooks ────────────────────────────────────────────
|
|
24
|
-
export { useRagChat } from './hooks/useRagChat';
|
|
25
|
-
export { useProjectSettings } from './hooks/useProjectSettings';
|
|
26
|
-
|
|
27
|
-
// ── Utils ─────────────────────────────────────────────────────
|
|
28
|
-
export { addSynonyms } from './utils/synonyms';
|
|
29
|
-
|
|
30
|
-
// ── License Config Resolver (Server-only, types exported) ────
|
|
31
|
-
// Implementation available via server.ts import only
|
|
32
|
-
// export { LicenseConfigResolver } from './config/LicenseConfigResolver';
|
|
33
|
-
export type { LicenseTierConfig, ProviderCredentials } from './config/LicenseConfigResolver';
|
|
34
|
-
|
|
35
|
-
// ── Telemetry Service (Server-only) ────────────────────────────
|
|
36
|
-
// Only available via server.ts import
|
|
37
|
-
// export { TelemetryService, EventType, ... } from './telemetry/TelemetryService';
|
|
38
|
-
|
|
39
|
-
// ── Visualization Types ────────────────────────────────────────
|
|
40
|
-
// Runtime rendering utilities are exported from the server entrypoint to
|
|
41
|
-
// avoid pulling the rendering engine into client bundles unnecessarily.
|
|
42
|
-
export type {
|
|
43
|
-
RenderDecision,
|
|
44
|
-
RenderType,
|
|
45
|
-
ChartType,
|
|
46
|
-
IntentCategory,
|
|
47
|
-
DecisionContext,
|
|
48
|
-
IRenderRule,
|
|
49
|
-
IRendererStrategy,
|
|
50
|
-
} from './rendering/types';
|
|
51
|
-
|
|
52
|
-
// ── Types (Interfaces/Types only, no Node.js deps) ─────────────
|
|
53
|
-
export type {
|
|
54
|
-
RagConfig,
|
|
55
|
-
UniversalRagConfig,
|
|
56
|
-
RetrievalConfig,
|
|
57
|
-
WorkflowConfig,
|
|
58
|
-
VectorDBConfig,
|
|
59
|
-
VectorDBProvider,
|
|
60
|
-
LLMConfig,
|
|
61
|
-
LLMProvider,
|
|
62
|
-
EmbeddingConfig,
|
|
63
|
-
EmbeddingProvider,
|
|
64
|
-
UIConfig,
|
|
65
|
-
RAGConfig,
|
|
66
|
-
} from './config/RagConfig';
|
|
67
|
-
export type { ILLMProvider, EmbedOptions } from './llm/ILLMProvider';
|
|
68
|
-
export type { Chunk, ChunkOptions } from './rag/DocumentChunker';
|
|
69
|
-
export type { Retrivora } from './core/Retrivora';
|
|
70
|
-
export type { RetrivoraErrorCode } from './exceptions';
|
|
71
|
-
export { LicenseValidator } from './core/LicenseValidator';
|
|
72
|
-
export type { LicenseValidationRequest, LicenseValidationResponse } from './core/LicenseValidator';
|
|
73
|
-
export { ConfigFetcher } from './core/ConfigFetcher';
|
|
74
|
-
export {
|
|
75
|
-
createDefaultProjectSettings,
|
|
76
|
-
normalizeProjectSettings,
|
|
77
|
-
projectSettingsToUiConfig,
|
|
78
|
-
} from './core/ProjectSettings';
|
|
79
|
-
export { CircuitBreaker } from './core/CircuitBreaker';
|
|
80
|
-
export type { CircuitBreakerOptions } from './core/CircuitBreaker';
|
|
81
|
-
export { BatchProcessor, isTransientError } from './core/BatchProcessor';
|
|
82
|
-
export type { BatchOptions, BatchResult } from './core/BatchProcessor';
|
|
83
|
-
export {
|
|
84
|
-
RetrivoraError,
|
|
85
|
-
ProviderNotFoundException,
|
|
86
|
-
EmbeddingFailedException,
|
|
87
|
-
RetrievalException,
|
|
88
|
-
RateLimitException,
|
|
89
|
-
ConfigurationException,
|
|
90
|
-
AuthenticationException,
|
|
91
|
-
SDKVersionUnsupportedError,
|
|
92
|
-
LicenseValidationError,
|
|
93
|
-
} from './exceptions';
|
|
94
|
-
export type {
|
|
95
|
-
VectorMatch,
|
|
96
|
-
UpsertDocument,
|
|
97
|
-
IngestDocument,
|
|
98
|
-
ChatResponse,
|
|
99
|
-
ChatMessage,
|
|
100
|
-
ChatOptions,
|
|
101
|
-
RagMessage,
|
|
102
|
-
ClientConfig,
|
|
103
|
-
UseRagChatOptions,
|
|
104
|
-
UseRagChatReturn,
|
|
105
|
-
ChatWindowProps,
|
|
106
|
-
ChatWidgetProps,
|
|
107
|
-
MessageBubbleProps,
|
|
108
|
-
ProductCardProps,
|
|
109
|
-
ProductCarouselProps,
|
|
110
|
-
DocumentUploadProps,
|
|
111
|
-
SourceCardProps,
|
|
112
|
-
ObservabilityTrace,
|
|
113
|
-
LatencyBreakdown,
|
|
114
|
-
TokenUsage,
|
|
115
|
-
RetrievedChunk
|
|
116
|
-
} from './types';
|
|
117
|
-
export type {
|
|
118
|
-
RemoteConfig,
|
|
119
|
-
RemoteVectorConfig,
|
|
120
|
-
RemoteEmbeddingConfig,
|
|
121
|
-
RemoteLLMConfig,
|
|
122
|
-
} from './core/ConfigFetcher';
|
|
123
|
-
export type {
|
|
124
|
-
AuthMode,
|
|
125
|
-
ProjectAiSettings,
|
|
126
|
-
ProjectAuthSettings,
|
|
127
|
-
ProjectBrandingSettings,
|
|
128
|
-
ProjectChatSettings,
|
|
129
|
-
ProjectTelemetrySettings,
|
|
130
|
-
ProjectWidgetSettings,
|
|
131
|
-
ThemeMode,
|
|
132
|
-
WidgetPosition,
|
|
133
|
-
} from './core/ProjectSettings';
|
|
134
|
-
export type {
|
|
135
|
-
UseProjectSettingsOptions,
|
|
136
|
-
UseProjectSettingsReturn,
|
|
137
|
-
} from './hooks/useProjectSettings';
|
package/src/lib/plugin.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* lib/plugin.ts — Singleton VectorPlugin for this Next.js application.
|
|
3
|
-
*
|
|
4
|
-
* All API route handlers import this module to share ONE plugin instance
|
|
5
|
-
* (and therefore one pipeline + one DB connection pool) across the process.
|
|
6
|
-
*
|
|
7
|
-
* Why a singleton?
|
|
8
|
-
* - Prevents a new MongoDB/Postgres connection being opened on every request.
|
|
9
|
-
* - Ensures the LRU embedding cache is shared across all chat requests.
|
|
10
|
-
* - In serverless environments (Vercel) the module is cached per function
|
|
11
|
-
* instance, so cold-starts initialize once and stay warm.
|
|
12
|
-
*
|
|
13
|
-
* To swap provider at runtime, change .env.local and restart the dev server.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import { VectorPlugin } from '@/core/VectorPlugin';
|
|
17
|
-
import { getRagConfig } from '@/config/serverConfig';
|
|
18
|
-
|
|
19
|
-
// Build config once from environment variables at module load time.
|
|
20
|
-
const ragConfig = getRagConfig();
|
|
21
|
-
|
|
22
|
-
// Create and export the singleton.
|
|
23
|
-
// VectorPlugin is lazy — it doesn't open connections until the first request.
|
|
24
|
-
export const plugin = new VectorPlugin(ragConfig);
|
package/src/llm/ILLMProvider.ts
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generic LLM Provider interface.
|
|
3
|
-
* Covers both chat completion and embedding generation so a single
|
|
4
|
-
* provider can handle both responsibilities when appropriate.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { ChatMessage, ChatOptions } from '../types';
|
|
8
|
-
|
|
9
|
-
export interface EmbedOptions {
|
|
10
|
-
/** Override model for this specific embed call */
|
|
11
|
-
model?: string;
|
|
12
|
-
/** Specify the task type for models that require prefixes (e.g. nomic-embed-text) */
|
|
13
|
-
taskType?: 'query' | 'document';
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface ILLMProvider {
|
|
17
|
-
/**
|
|
18
|
-
* Send a chat completion request.
|
|
19
|
-
* @param messages – the full conversation history
|
|
20
|
-
* @param context – retrieved RAG context to inject
|
|
21
|
-
* @param options – optional per-call overrides
|
|
22
|
-
* @returns – the assistant's reply text
|
|
23
|
-
*/
|
|
24
|
-
chat(
|
|
25
|
-
messages: ChatMessage[],
|
|
26
|
-
context: string,
|
|
27
|
-
options?: ChatOptions
|
|
28
|
-
): Promise<string>;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Send a streaming chat completion request.
|
|
32
|
-
* @returns – an async iterable of text chunks
|
|
33
|
-
*/
|
|
34
|
-
chatStream?(
|
|
35
|
-
messages: ChatMessage[],
|
|
36
|
-
context: string,
|
|
37
|
-
options?: ChatOptions
|
|
38
|
-
): AsyncIterable<string>;
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Generate an embedding vector for the given text.
|
|
42
|
-
* @param text – text to embed
|
|
43
|
-
* @param options – optional overrides
|
|
44
|
-
* @returns – float array (the embedding)
|
|
45
|
-
*/
|
|
46
|
-
embed(text: string, options?: EmbedOptions): Promise<number[]>;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Generate embedding vectors for multiple texts in a single batch.
|
|
50
|
-
* @param texts – array of texts to embed
|
|
51
|
-
* @param options – optional overrides
|
|
52
|
-
* @returns – array of float arrays
|
|
53
|
-
*/
|
|
54
|
-
batchEmbed(texts: string[], options?: EmbedOptions): Promise<number[][]>;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Check if the provider endpoint is reachable.
|
|
58
|
-
*/
|
|
59
|
-
ping(): Promise<boolean>;
|
|
60
|
-
}
|
package/src/llm/LLMFactory.ts
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { ILLMProvider } from './ILLMProvider';
|
|
2
|
-
import { LLMConfig, EmbeddingConfig, LLMProvider } from '../config/RagConfig';
|
|
3
|
-
import { OpenAIProvider } from './providers/OpenAIProvider';
|
|
4
|
-
import { AnthropicProvider } from './providers/AnthropicProvider';
|
|
5
|
-
import { OllamaProvider } from './providers/OllamaProvider';
|
|
6
|
-
import { GeminiProvider } from './providers/GeminiProvider';
|
|
7
|
-
import { GroqProvider } from './providers/GroqProvider';
|
|
8
|
-
import { QwenProvider } from './providers/QwenProvider';
|
|
9
|
-
import { UniversalLLMAdapter } from './providers/UniversalLLMAdapter';
|
|
10
|
-
import { IProviderValidator, IProviderHealthChecker } from '../core/ProviderInterfaces';
|
|
11
|
-
import { ProviderNotFoundException } from '../exceptions';
|
|
12
|
-
|
|
13
|
-
interface LLMProviderStatic {
|
|
14
|
-
getValidator?: () => IProviderValidator;
|
|
15
|
-
getHealthChecker?: () => IProviderHealthChecker;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/** Registry for custom provider factories registered at runtime. */
|
|
19
|
-
const customProviders = new Map<string, (config: LLMConfig) => ILLMProvider>();
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* LLMFactory — instantiates the correct ILLMProvider based on llmConfig.provider.
|
|
23
|
-
*/
|
|
24
|
-
export class LLMFactory {
|
|
25
|
-
/**
|
|
26
|
-
* Register a custom LLM provider factory at runtime.
|
|
27
|
-
*
|
|
28
|
-
* Use this to add support for any LLM backend (Azure OpenAI, Cohere, Mistral,
|
|
29
|
-
* Bedrock, etc.) without modifying this library's source code.
|
|
30
|
-
*
|
|
31
|
-
* @example
|
|
32
|
-
* // In your Next.js app initialization:
|
|
33
|
-
* import { LLMFactory } from '@retrivora-ai/rag-engine/server';
|
|
34
|
-
* import { MyCustomProvider } from './providers/MyCustomProvider';
|
|
35
|
-
*
|
|
36
|
-
* LLMFactory.register('my-provider', (config) => new MyCustomProvider(config));
|
|
37
|
-
*
|
|
38
|
-
* // Then set in your .env.local:
|
|
39
|
-
* // LLM_PROVIDER=my-provider
|
|
40
|
-
*/
|
|
41
|
-
static register(name: string, factory: (config: LLMConfig) => ILLMProvider): void {
|
|
42
|
-
customProviders.set(name.toLowerCase(), factory);
|
|
43
|
-
//console.log(`[LLMFactory] Registered custom provider: "${name}"`);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Unregister a previously registered custom provider.
|
|
48
|
-
*/
|
|
49
|
-
static unregister(name: string): void {
|
|
50
|
-
customProviders.delete(name.toLowerCase());
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* List all registered provider names (built-in + custom).
|
|
55
|
-
*/
|
|
56
|
-
static listProviders(): string[] {
|
|
57
|
-
return [
|
|
58
|
-
'openai', 'anthropic', 'ollama', 'gemini', 'groq', 'qwen', 'rest', 'universal_rest', 'custom',
|
|
59
|
-
...Array.from(customProviders.keys()),
|
|
60
|
-
];
|
|
61
|
-
}
|
|
62
|
-
static create(llmConfig: LLMConfig, embeddingConfig?: EmbeddingConfig): ILLMProvider {
|
|
63
|
-
switch (llmConfig.provider) {
|
|
64
|
-
case 'openai':
|
|
65
|
-
return new OpenAIProvider(llmConfig, embeddingConfig);
|
|
66
|
-
case 'anthropic':
|
|
67
|
-
return new AnthropicProvider(llmConfig, embeddingConfig);
|
|
68
|
-
case 'ollama':
|
|
69
|
-
return new OllamaProvider(llmConfig, embeddingConfig);
|
|
70
|
-
case 'gemini':
|
|
71
|
-
return new GeminiProvider(llmConfig, embeddingConfig);
|
|
72
|
-
case 'groq':
|
|
73
|
-
return new GroqProvider(llmConfig, embeddingConfig);
|
|
74
|
-
case 'qwen':
|
|
75
|
-
return new QwenProvider(llmConfig, embeddingConfig);
|
|
76
|
-
case 'rest':
|
|
77
|
-
case 'universal_rest':
|
|
78
|
-
case 'custom':
|
|
79
|
-
return new UniversalLLMAdapter(llmConfig);
|
|
80
|
-
default: {
|
|
81
|
-
// Check the runtime custom provider registry first
|
|
82
|
-
const providerName = String(llmConfig.provider ?? '').toLowerCase();
|
|
83
|
-
const customFactory = customProviders.get(providerName);
|
|
84
|
-
if (customFactory) {
|
|
85
|
-
return customFactory(llmConfig);
|
|
86
|
-
}
|
|
87
|
-
// Fall back to UniversalLLMAdapter if a baseUrl is provided
|
|
88
|
-
if (llmConfig.baseUrl || (llmConfig.options as Record<string, unknown>)?.baseUrl) {
|
|
89
|
-
return new UniversalLLMAdapter(llmConfig);
|
|
90
|
-
}
|
|
91
|
-
throw new ProviderNotFoundException(
|
|
92
|
-
'llm',
|
|
93
|
-
llmConfig.provider ?? 'undefined',
|
|
94
|
-
{
|
|
95
|
-
message: `Unknown provider "${llmConfig.provider}". Register a custom provider with LLMFactory.register().`,
|
|
96
|
-
available: LLMFactory.listProviders()
|
|
97
|
-
}
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
static getValidator(provider: LLMProvider): IProviderValidator | null {
|
|
104
|
-
const providerClass = this.getProviderClass(provider);
|
|
105
|
-
return providerClass && providerClass.getValidator ? providerClass.getValidator() : null;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
static getHealthChecker(provider: LLMProvider): IProviderHealthChecker | null {
|
|
109
|
-
const providerClass = this.getProviderClass(provider);
|
|
110
|
-
return providerClass && providerClass.getHealthChecker ? providerClass.getHealthChecker() : null;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
private static getProviderClass(provider: string): LLMProviderStatic | null {
|
|
114
|
-
switch (provider) {
|
|
115
|
-
case 'openai': return OpenAIProvider;
|
|
116
|
-
case 'anthropic': return AnthropicProvider;
|
|
117
|
-
case 'ollama': return OllamaProvider;
|
|
118
|
-
case 'gemini': return GeminiProvider;
|
|
119
|
-
case 'groq': return GroqProvider;
|
|
120
|
-
case 'qwen': return QwenProvider;
|
|
121
|
-
case 'rest':
|
|
122
|
-
case 'universal_rest':
|
|
123
|
-
case 'custom': return UniversalLLMAdapter as unknown as LLMProviderStatic;
|
|
124
|
-
default: return null;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Creates a dedicated embedding-only provider.
|
|
130
|
-
*/
|
|
131
|
-
static createEmbeddingProvider(embeddingConfig: EmbeddingConfig): ILLMProvider {
|
|
132
|
-
const fakeLLMConfig: LLMConfig = {
|
|
133
|
-
provider: embeddingConfig.provider as LLMProvider,
|
|
134
|
-
model: embeddingConfig.model,
|
|
135
|
-
apiKey: embeddingConfig.apiKey,
|
|
136
|
-
baseUrl: embeddingConfig.baseUrl,
|
|
137
|
-
options: embeddingConfig.options,
|
|
138
|
-
};
|
|
139
|
-
return LLMFactory.create(fakeLLMConfig, embeddingConfig);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
@@ -1,215 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Anthropic (Claude) LLM Provider
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import Anthropic from '@anthropic-ai/sdk';
|
|
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 AnthropicProvider implements ILLMProvider {
|
|
14
|
-
private readonly client: Anthropic;
|
|
15
|
-
private readonly llmConfig: LLMConfig;
|
|
16
|
-
private readonly embeddingConfig?: EmbeddingConfig;
|
|
17
|
-
|
|
18
|
-
constructor(llmConfig: LLMConfig, embeddingConfig?: EmbeddingConfig) {
|
|
19
|
-
if (!llmConfig.apiKey) throw new Error('[AnthropicProvider] llmConfig.apiKey is required');
|
|
20
|
-
this.client = new Anthropic({ 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
|
-
if (!config.apiKey) {
|
|
30
|
-
errors.push({
|
|
31
|
-
field: 'llm.apiKey',
|
|
32
|
-
message: 'Anthropic API key is required',
|
|
33
|
-
suggestion: 'Set ANTHROPIC_API_KEY environment variable',
|
|
34
|
-
severity: 'error',
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
if (!config.model) {
|
|
38
|
-
errors.push({
|
|
39
|
-
field: 'llm.model',
|
|
40
|
-
message: 'Anthropic model name is required',
|
|
41
|
-
suggestion: 'e.g., "claude-3-5-sonnet-20241022"',
|
|
42
|
-
severity: 'error',
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
return errors;
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
static getHealthChecker(): IProviderHealthChecker {
|
|
51
|
-
return {
|
|
52
|
-
async check(config: Record<string, unknown>): Promise<HealthCheckResult> {
|
|
53
|
-
const timestamp = Date.now();
|
|
54
|
-
const apiKey = config.apiKey as string;
|
|
55
|
-
const modelName = config.model as string;
|
|
56
|
-
|
|
57
|
-
try {
|
|
58
|
-
const { default: Anthropic } = await import('@anthropic-ai/sdk');
|
|
59
|
-
const client = new Anthropic({ apiKey });
|
|
60
|
-
await client.messages.create({
|
|
61
|
-
model: modelName,
|
|
62
|
-
max_tokens: 10,
|
|
63
|
-
messages: [{ role: 'user', content: 'ping' }],
|
|
64
|
-
});
|
|
65
|
-
return { healthy: true, provider: 'anthropic', capabilities: { model: modelName }, timestamp };
|
|
66
|
-
} catch (error) {
|
|
67
|
-
return {
|
|
68
|
-
healthy: false,
|
|
69
|
-
provider: 'anthropic',
|
|
70
|
-
error: `Connection failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
71
|
-
timestamp,
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
async chat(messages: ChatMessage[], context: string, options?: ChatOptions): Promise<string> {
|
|
79
|
-
const basePrompt =
|
|
80
|
-
options?.systemPrompt ??
|
|
81
|
-
this.llmConfig.systemPrompt ??
|
|
82
|
-
'You are a helpful assistant. Use the context below to answer the user\'s question accurately.';
|
|
83
|
-
|
|
84
|
-
const system = buildSystemContent(basePrompt, context);
|
|
85
|
-
|
|
86
|
-
const anthropicMessages: Anthropic.MessageParam[] = messages.map((m) => ({
|
|
87
|
-
role: m.role === 'assistant' ? 'assistant' : 'user',
|
|
88
|
-
content: m.content,
|
|
89
|
-
}));
|
|
90
|
-
|
|
91
|
-
const isClaude37 = this.llmConfig.model.includes('claude-3-7-sonnet');
|
|
92
|
-
const isThinkingEnabled = this.llmConfig.options?.thinking === true ||
|
|
93
|
-
this.llmConfig.options?.thinking === 'enabled' ||
|
|
94
|
-
(isClaude37 && this.llmConfig.options?.thinking !== false);
|
|
95
|
-
|
|
96
|
-
const extraParams: any = {};
|
|
97
|
-
if (isThinkingEnabled && isClaude37) {
|
|
98
|
-
extraParams.betas = ['interleaved-thinking-2025-05-14'];
|
|
99
|
-
const maxTokens = options?.maxTokens ?? this.llmConfig.maxTokens ?? 4096;
|
|
100
|
-
const budget = Math.min(
|
|
101
|
-
typeof this.llmConfig.options?.thinkingBudget === 'number'
|
|
102
|
-
? this.llmConfig.options?.thinkingBudget
|
|
103
|
-
: 2048,
|
|
104
|
-
maxTokens - 1
|
|
105
|
-
);
|
|
106
|
-
extraParams.thinking = {
|
|
107
|
-
type: 'enabled',
|
|
108
|
-
budget_tokens: budget,
|
|
109
|
-
};
|
|
110
|
-
extraParams.max_tokens = Math.max(maxTokens, budget + 1024);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const response = await this.client.messages.create({
|
|
114
|
-
model: this.llmConfig.model,
|
|
115
|
-
max_tokens: options?.maxTokens ?? this.llmConfig.maxTokens ?? 1024,
|
|
116
|
-
system,
|
|
117
|
-
messages: anthropicMessages,
|
|
118
|
-
...extraParams,
|
|
119
|
-
} as any);
|
|
120
|
-
|
|
121
|
-
let reply = '';
|
|
122
|
-
for (const block of response.content) {
|
|
123
|
-
if (block.type === 'text') {
|
|
124
|
-
reply += block.text;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
return reply;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
async *chatStream(messages: ChatMessage[], context: string, options?: ChatOptions): AsyncIterable<string> {
|
|
131
|
-
const basePrompt =
|
|
132
|
-
options?.systemPrompt ??
|
|
133
|
-
this.llmConfig.systemPrompt ??
|
|
134
|
-
'You are a helpful assistant. Use the context below to answer the user\'s question accurately.';
|
|
135
|
-
|
|
136
|
-
const system = buildSystemContent(basePrompt, context);
|
|
137
|
-
|
|
138
|
-
const anthropicMessages: Anthropic.MessageParam[] = messages.map((m) => ({
|
|
139
|
-
role: m.role === 'assistant' ? 'assistant' : 'user',
|
|
140
|
-
content: m.content,
|
|
141
|
-
}));
|
|
142
|
-
|
|
143
|
-
const isClaude37 = this.llmConfig.model.includes('claude-3-7-sonnet');
|
|
144
|
-
const isThinkingEnabled = this.llmConfig.options?.thinking === true ||
|
|
145
|
-
this.llmConfig.options?.thinking === 'enabled' ||
|
|
146
|
-
(isClaude37 && this.llmConfig.options?.thinking !== false);
|
|
147
|
-
|
|
148
|
-
const extraParams: any = {};
|
|
149
|
-
if (isThinkingEnabled && isClaude37) {
|
|
150
|
-
extraParams.betas = ['interleaved-thinking-2025-05-14'];
|
|
151
|
-
const maxTokens = options?.maxTokens ?? this.llmConfig.maxTokens ?? 4096;
|
|
152
|
-
const budget = Math.min(
|
|
153
|
-
typeof this.llmConfig.options?.thinkingBudget === 'number'
|
|
154
|
-
? this.llmConfig.options?.thinkingBudget
|
|
155
|
-
: 2048,
|
|
156
|
-
maxTokens - 1
|
|
157
|
-
);
|
|
158
|
-
extraParams.thinking = {
|
|
159
|
-
type: 'enabled',
|
|
160
|
-
budget_tokens: budget,
|
|
161
|
-
};
|
|
162
|
-
extraParams.max_tokens = Math.max(maxTokens, budget + 1024);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
const stream = await this.client.messages.create({
|
|
166
|
-
model: this.llmConfig.model,
|
|
167
|
-
max_tokens: options?.maxTokens ?? this.llmConfig.maxTokens ?? 1024,
|
|
168
|
-
system,
|
|
169
|
-
messages: anthropicMessages,
|
|
170
|
-
stream: true,
|
|
171
|
-
...extraParams,
|
|
172
|
-
} as any) as any;
|
|
173
|
-
|
|
174
|
-
if (!stream) {
|
|
175
|
-
throw new Error('[AnthropicProvider] messages.create stream is undefined');
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
for await (const chunk of stream) {
|
|
179
|
-
if (chunk.type === 'content_block_delta') {
|
|
180
|
-
if (chunk.delta.type === 'text_delta') {
|
|
181
|
-
yield chunk.delta.text;
|
|
182
|
-
} else if ((chunk.delta as any).type === 'thinking_delta') {
|
|
183
|
-
yield `\x00THINKING\x00${(chunk.delta as any).thinking}`;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
async embed(text: string, options?: EmbedOptions): Promise<number[]> {
|
|
190
|
-
void text;
|
|
191
|
-
void options;
|
|
192
|
-
throw new Error(
|
|
193
|
-
'[AnthropicProvider] Anthropic does not provide an embedding API. ' +
|
|
194
|
-
'Set embedding.provider to "openai" or "ollama" in your RagConfig.'
|
|
195
|
-
);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
async batchEmbed(texts: string[], options?: EmbedOptions): Promise<number[][]> {
|
|
199
|
-
void texts;
|
|
200
|
-
void options;
|
|
201
|
-
throw new Error(
|
|
202
|
-
'[AnthropicProvider] Anthropic does not provide an embedding API.'
|
|
203
|
-
);
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
async ping(): Promise<boolean> {
|
|
207
|
-
try {
|
|
208
|
-
await this.client.models.list();
|
|
209
|
-
return true;
|
|
210
|
-
} catch (err) {
|
|
211
|
-
console.error('[AnthropicProvider] Ping failed:', err);
|
|
212
|
-
return false;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
}
|