@mate-academy/llm-gateway 1.0.3 → 1.0.4

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 (2) hide show
  1. package/README.md +377 -0
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -173,6 +173,383 @@ Supports both completion and assistance APIs. For more information, see [OpenAI
173
173
 
174
174
  Supports completion API through Google's Generative AI models. For more information, see [Google Generative AI documentation](https://ai.google.dev/docs).
175
175
 
176
+ ## Developer Guide: Adding a New Provider
177
+
178
+ To add support for a new LLM provider, follow these steps:
179
+
180
+ ### 1. Create Provider Directory Structure
181
+
182
+ Create a new directory in `src/providers` with your provider name, following the established pattern:
183
+
184
+ ```
185
+ src/providers/YourProvider/
186
+ ├── index.ts # Entry point for provider exports
187
+ ├── YourProvider.constants.ts # Provider-specific constants
188
+ ├── YourProvider.entity.ts # Provider-specific entity
189
+ ├── YourProvider.typedefs.ts # TypeScript definitions
190
+ ├── YourProviderService.factory.ts # Factory for your provider's services
191
+ └── services/ # Provider service implementations
192
+ ├── index.ts
193
+ ├── YourProviderCompletionService.ts
194
+ └── YourProviderAssistanceService.ts
195
+ ```
196
+
197
+ ### 2. Add Provider to LLM Providers Enum
198
+
199
+ Update the LLM providers enum in `src/LLMService.typedefs.ts`:
200
+
201
+ ```typescript
202
+ export enum LLMProviders {
203
+ OpenAI = 'openai',
204
+ GoogleGenerativeAI = 'google',
205
+ YourProvider = 'your-provider-id',
206
+ }
207
+ ```
208
+
209
+ ### 3. Define Provider-Specific Types
210
+
211
+ Create type definitions in `src/providers/YourProvider/YourProvider.typedefs.ts`:
212
+
213
+ ```typescript
214
+ // Define model names as an enum for type safety
215
+ export enum YourProviderModelNames {
216
+ MODEL_ONE = 'model-one',
217
+ MODEL_TWO = 'model-extended',
218
+ }
219
+
220
+ // Define message roles if applicable
221
+ export enum YourProviderRoles {
222
+ User = 'user',
223
+ Assistant = 'assistant',
224
+ System = 'system',
225
+ }
226
+
227
+ // Add any other provider-specific enums or interfaces
228
+ ```
229
+
230
+ Then ensure your provider is properly integrated in the main type system by updating the necessary type mappings in `src/LLMService.typedefs.ts`:
231
+
232
+ ```typescript
233
+ // Add import for your provider's types
234
+ import { type YourProviderModelNames } from './providers/YourProvider/YourProvider.typedefs';
235
+
236
+ // Update the LLMProviders enum
237
+ export enum LLMProviders {
238
+ OpenAI = 'OpenAI',
239
+ GoogleGenerativeAI = 'GoogleGenerativeAI',
240
+ YourProvider = 'YourProvider',
241
+ }
242
+
243
+ // Update LLMInstances type mapping
244
+ export type LLMInstances = {
245
+ // ...existing code...
246
+ [LLMProviders.YourProvider]: YourProviderClient; // Your provider's client type
247
+ };
248
+
249
+ // Update LLMInstanceOptions type mapping
250
+ export type LLMInstanceOptions = {
251
+ // ...existing code...
252
+ [LLMProviders.YourProvider]: {
253
+ apiKey: string;
254
+ // Add other provider-specific options
255
+ };
256
+ };
257
+
258
+ // Update LLMModelName type mapping
259
+ export type LLMModelName = {
260
+ // ...existing code...
261
+ [LLMProviders.YourProvider]: YourProviderModelNames;
262
+ };
263
+ ```
264
+
265
+ ### 4. Implement Provider Constants
266
+
267
+ Define constants in `src/providers/YourProvider/YourProvider.constants.ts`:
268
+
269
+ ```typescript
270
+ import {
271
+ type LLMProviderAvailableModels,
272
+ type LLMProviderModelsByPurpose,
273
+ type LLMProviders,
274
+ LLMPurposes,
275
+ type LLMServiceBuilder,
276
+ } from '../../LLMService.typedefs';
277
+ import { YourProviderModelNames } from './YourProvider.typedefs';
278
+ import {
279
+ YourProviderAssistanceService,
280
+ YourProviderCompletionService,
281
+ } from './services';
282
+ import { pick } from '../../functional.utils';
283
+
284
+ // Define available models with their capabilities and configurations
285
+ const YOUR_PROVIDER_AVAILABLE_MODELS: LLMProviderAvailableModels<
286
+ LLMProviders.YourProvider
287
+ > = {
288
+ [YourProviderModelNames.MODEL_ONE]: {
289
+ name: YourProviderModelNames.MODEL_ONE,
290
+ limits: {
291
+ maxInputTokens: 8_000,
292
+ maxOutputTokens: 2_000,
293
+ },
294
+ config: {
295
+ temperature: 0.2,
296
+ },
297
+ },
298
+ [YourProviderModelNames.MODEL_TWO]: {
299
+ name: YourProviderModelNames.MODEL_TWO,
300
+ limits: {
301
+ maxInputTokens: 16_000,
302
+ maxOutputTokens: 4_000,
303
+ },
304
+ config: {
305
+ temperature: 0.2,
306
+ },
307
+ },
308
+ };
309
+
310
+ // Specify which models are available for each purpose
311
+ export const YOUR_PROVIDER_MODELS: LLMProviderModelsByPurpose<
312
+ LLMPurposes,
313
+ LLMProviders.YourProvider
314
+ > = {
315
+ [LLMPurposes.Completion]: pick(
316
+ YOUR_PROVIDER_AVAILABLE_MODELS,
317
+ [
318
+ YourProviderModelNames.MODEL_ONE,
319
+ YourProviderModelNames.MODEL_TWO,
320
+ ],
321
+ ),
322
+ [LLMPurposes.Assistance]: pick(
323
+ YOUR_PROVIDER_AVAILABLE_MODELS,
324
+ [
325
+ YourProviderModelNames.MODEL_TWO, // Only MODEL_TWO supports assistance
326
+ ],
327
+ ),
328
+ };
329
+
330
+ // Define service builders for each LLM purpose
331
+ export const YOUR_PROVIDER_SERVICE_BUILDERS: {
332
+ [purpose in LLMPurposes]: (
333
+ LLMServiceBuilder<LLMProviders.YourProvider, purpose> | null
334
+ )
335
+ } = {
336
+ [LLMPurposes.Completion]: (logger, options) => (
337
+ new YourProviderCompletionService(logger, options)
338
+ ),
339
+ [LLMPurposes.Assistance]: (logger, options) => (
340
+ new YourProviderAssistanceService(logger, options)
341
+ ),
342
+ };
343
+ ```
344
+
345
+ ### 5. Implement Provider Entity (if needed)
346
+
347
+ Create the entity class in `src/providers/YourProvider/YourProvider.entity.ts`:
348
+
349
+ ```typescript
350
+ export class YourProviderEntity {
351
+ // Implement provider-specific methods
352
+ }
353
+ ```
354
+
355
+ ### 6. Implement Service Classes
356
+
357
+ Create service implementations in the `services` directory:
358
+
359
+ **CompletionService (src/providers/YourProvider/services/YourProviderCompletionService.ts)**:
360
+
361
+ ```typescript
362
+ import { Logger } from '../../../LLMService.typedefs';
363
+ import { LLMCompletionService } from '../../../services/LLMCompletionService.abstract';
364
+ import { CompletionParams, CompletionResult, LLMProviders } from '../../../LLMService.typedefs';
365
+ import { YourProviderEntity } from '../YourProvider.entity';
366
+
367
+ export class YourProviderCompletionService extends LLMCompletionService<LLMProviders.YourProvider> {
368
+ constructor(
369
+ logger: Logger,
370
+ private readonly providerEntity: YourProviderEntity,
371
+ ) {
372
+ super(logger);
373
+ }
374
+
375
+ async complete(params: CompletionParams): Promise<CompletionResult> {
376
+ this.logger.info('Starting completion with YourProvider', { params });
377
+
378
+ try {
379
+ // Implement provider-specific completion logic
380
+
381
+ return {
382
+ text: 'Completed text',
383
+ // Include other required fields
384
+ };
385
+ } catch (error) {
386
+ this.logger.error('Error in YourProvider completion', { error });
387
+ throw error;
388
+ }
389
+ }
390
+ }
391
+ ```
392
+
393
+ **AssistanceService (if applicable)**:
394
+
395
+ ```typescript
396
+ import { Logger } from '../../../LLMService.typedefs';
397
+ import { LLMAssistanceService } from '../../../services/LLMAssistanceService.abstract';
398
+ import {
399
+ AssistantParams,
400
+ ThreadParams,
401
+ MessageParams,
402
+ LLMProviders
403
+ } from '../../../LLMService.typedefs';
404
+ import { YourProviderEntity } from '../YourProvider.entity';
405
+
406
+ export class YourProviderAssistanceService extends LLMAssistanceService<LLMProviders.YourProvider> {
407
+ constructor(
408
+ logger: Logger,
409
+ private readonly providerEntity: YourProviderEntity,
410
+ ) {
411
+ super(logger);
412
+ }
413
+
414
+ // Implement required assistance methods
415
+ }
416
+ ```
417
+
418
+ ### 7. Create Service Factory
419
+
420
+ First, define service builders in `src/providers/YourProvider/YourProvider.constants.ts`:
421
+
422
+ ```typescript
423
+ import {
424
+ LLMPurposes,
425
+ type LLMServiceBuilder,
426
+ } from '../../LLMService.typedefs';
427
+ import {
428
+ YourProviderAssistanceService,
429
+ YourProviderCompletionService,
430
+ } from './services';
431
+
432
+ export const YOUR_PROVIDER_SERVICE_BUILDERS: {
433
+ [purpose in LLMPurposes]: (
434
+ LLMServiceBuilder<LLMProviders.YourProvider, purpose> | null
435
+ )
436
+ } = {
437
+ [LLMPurposes.Completion]: (logger, options) => (
438
+ new YourProviderCompletionService(logger, options)
439
+ ),
440
+ [LLMPurposes.Assistance]: (logger, options) => (
441
+ new YourProviderAssistanceService(logger, options)
442
+ ),
443
+ };
444
+ ```
445
+
446
+ Then, implement the service factory in `src/providers/YourProvider/YourProviderService.factory.ts`:
447
+
448
+ ```typescript
449
+ import { type Logger } from '@mate-academy/core';
450
+ import {
451
+ type LLMInstanceOptions,
452
+ LLMProviders,
453
+ type LLMPurposes,
454
+ type LLMServiceByPurpose,
455
+ } from '../../LLMService.typedefs';
456
+ import { LLMServicePurposeFactory } from '../../services';
457
+ import { YOUR_PROVIDER_SERVICE_BUILDERS } from './YourProvider.constants';
458
+
459
+ export class YourProviderServiceFactory extends LLMServicePurposeFactory<
460
+ LLMProviders.YourProvider
461
+ > {
462
+ createService<
463
+ Purpose extends LLMPurposes
464
+ >(
465
+ purpose: Purpose,
466
+ logger: Logger,
467
+ options: LLMInstanceOptions[LLMProviders.YourProvider],
468
+ ): LLMServiceByPurpose<LLMProviders.YourProvider>[Purpose] {
469
+ const serviceBuilder = YOUR_PROVIDER_SERVICE_BUILDERS[purpose];
470
+
471
+ if (!serviceBuilder) {
472
+ throw new Error(`Purpose [${purpose}] is not supported for [${LLMProviders.YourProvider}] service`);
473
+ }
474
+
475
+ return serviceBuilder(logger, options);
476
+ }
477
+ }
478
+ ```
479
+
480
+ ### 8. Update Entry Point Files
481
+
482
+ Update the provider's `index.ts`:
483
+
484
+ ```typescript
485
+ export * from './YourProvider.constants';
486
+ export * from './YourProvider.entity';
487
+ export * from './YourProvider.typedefs';
488
+ export * from './YourProviderService.factory';
489
+ export * from './services';
490
+ ```
491
+
492
+ Update the main providers `index.ts` at `src/providers/index.ts`:
493
+
494
+ ```typescript
495
+ // ... other providers
496
+ export * from './YourProvider';
497
+ ```
498
+
499
+ ### 9. Update LLM Service Factory
500
+
501
+ Modify `src/LLMService.factory.ts` to include your new provider:
502
+
503
+ ```typescript
504
+ import {
505
+ LLMProviders,
506
+ Logger,
507
+ YourProviderOptions,
508
+ } from './LLMService.typedefs';
509
+ import { YourProviderServiceFactory } from './providers/YourProvider';
510
+ import { YourProviderOptions } from './providers/YourProvider/YourProvider.typedefs';
511
+
512
+ export class LLMServiceFactory {
513
+ static resolveProviderOptions<T extends LLMProviders>(
514
+ provider: T,
515
+ optionsMap: {
516
+ // ... other providers
517
+ [LLMProviders.YourProvider]?: YourProviderOptions;
518
+ },
519
+ ) {
520
+ return optionsMap[provider];
521
+ }
522
+
523
+ static getCompletionService<T extends LLMProviders>(
524
+ provider: T,
525
+ logger: Logger,
526
+ options: any,
527
+ ) {
528
+ switch (provider) {
529
+ // ... other providers
530
+ case LLMProviders.YourProvider:
531
+ return YourProviderServiceFactory.createCompletionService(logger, options);
532
+ default:
533
+ throw new Error(`Unsupported provider: ${provider}`);
534
+ }
535
+ }
536
+
537
+ static getAssistanceService<T extends LLMProviders>(
538
+ provider: T,
539
+ logger: Logger,
540
+ options: any,
541
+ ) {
542
+ switch (provider) {
543
+ // ... other providers
544
+ case LLMProviders.YourProvider:
545
+ return YourProviderServiceFactory.createAssistanceService(logger, options);
546
+ default:
547
+ throw new Error(`Unsupported provider: ${provider}`);
548
+ }
549
+ }
550
+ }
551
+ ```
552
+
176
553
  ## License
177
554
 
178
555
  [MIT](LICENSE)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mate-academy/llm-gateway",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "A gateway package for LLM services.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",