@mate-academy/llm-gateway 3.0.0 → 3.0.3
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/README.md +163 -11
- package/dist/LLMService.constants.d.ts +553 -5
- package/dist/LLMService.constants.js.map +1 -1
- package/dist/LLMService.factory.js +1 -1
- package/dist/LLMService.factory.js.map +1 -1
- package/dist/providers/GoogleGenerativeAI/GoogleGenerativeAI.constants.d.ts +176 -3
- package/dist/providers/GoogleGenerativeAI/GoogleGenerativeAI.constants.js.map +1 -1
- package/dist/providers/GoogleGenerativeAI/GoogleGenerativeAIService.factory.js.map +1 -1
- package/dist/providers/GoogleGenerativeAI/schemas/GoogleSchemaAdapter.d.ts +12 -0
- package/dist/providers/GoogleGenerativeAI/schemas/GoogleSchemaAdapter.js +72 -0
- package/dist/providers/GoogleGenerativeAI/schemas/GoogleSchemaAdapter.js.map +1 -0
- package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAIAssistance.service.js +1 -1
- package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAIAssistance.service.js.map +1 -1
- package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAICompletion.service.js +1 -1
- package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAICompletion.service.js.map +1 -1
- package/dist/providers/OpenAI/OpenAI.constants.d.ts +384 -3
- package/dist/providers/OpenAI/OpenAI.constants.js.map +1 -1
- package/dist/providers/OpenAI/OpenAIService.factory.js.map +1 -1
- package/dist/providers/OpenAI/schemas/OpenAISchemaAdapter.d.ts +14 -0
- package/dist/providers/OpenAI/schemas/OpenAISchemaAdapter.js +87 -0
- package/dist/providers/OpenAI/schemas/OpenAISchemaAdapter.js.map +1 -0
- package/dist/providers/OpenAI/services/OpenAIAssistance.service.js +1 -1
- package/dist/providers/OpenAI/services/OpenAIAssistance.service.js.map +1 -1
- package/dist/providers/OpenAI/services/OpenAICompletion.service.js +1 -1
- package/dist/providers/OpenAI/services/OpenAICompletion.service.js.map +1 -1
- package/dist/services/LLMAssistanceService.abstract.js.map +1 -1
- package/dist/services/LLMBaseService.abstract.d.ts +2 -2
- package/dist/services/LLMBaseService.abstract.js.map +1 -1
- package/dist/utilities/promptBuilder.d.ts +7 -2
- package/dist/utilities/promptBuilder.js +18 -2
- package/dist/utilities/promptBuilder.js.map +1 -1
- package/dist/utilities/schema/LLMSchema.d.ts +4 -20
- package/dist/utilities/schema/LLMSchema.js +8 -147
- package/dist/utilities/schema/LLMSchema.js.map +1 -1
- package/dist/utilities/schema/LLMSchemaInterface.d.ts +3 -7
- package/dist/utilities/schema/adapters/SchemaAdapterInterface.d.ts +12 -0
- package/dist/utilities/schema/adapters/SchemaAdapterInterface.js +3 -0
- package/dist/utilities/schema/adapters/SchemaAdapterInterface.js.map +1 -0
- package/dist/utilities/schema/adapters/SchemaAdapterRegistry.d.ts +24 -0
- package/dist/utilities/schema/adapters/SchemaAdapterRegistry.js +30 -0
- package/dist/utilities/schema/adapters/SchemaAdapterRegistry.js.map +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -16,6 +16,7 @@ npm install @mate-academy/llm-gateway
|
|
|
16
16
|
|
|
17
17
|
- Support for multiple LLM providers (OpenAI, Google Generative AI)
|
|
18
18
|
- **Structured Output**: Type-safe JSON responses with schema validation
|
|
19
|
+
- **Extensible Schema Architecture**: Driver pattern with provider-specific adapters
|
|
19
20
|
- Standardized completion service interface
|
|
20
21
|
- Standardized assistance service interface with file handling
|
|
21
22
|
- Speech-to-text transcription capabilities
|
|
@@ -398,6 +399,12 @@ if (result.data) {
|
|
|
398
399
|
- Use `.min()` and `.max()` instead of `.positive()`, `.negative()`
|
|
399
400
|
- Test schemas with both providers if cross-compatibility is important
|
|
400
401
|
|
|
402
|
+
#### Schema Architecture
|
|
403
|
+
|
|
404
|
+
The package uses a **driver pattern** for schema conversion, automatically adapting schemas to each provider's specific format while maintaining a unified API.
|
|
405
|
+
|
|
406
|
+
Schema adapters are automatically registered when providers are imported, so no manual configuration is needed. For detailed architecture information and extending with new providers, see the [Developer Guide](#developer-guide).
|
|
407
|
+
|
|
401
408
|
### Model Configuration
|
|
402
409
|
|
|
403
410
|
Each model comes with default configuration values that can be customized for your specific needs.
|
|
@@ -764,6 +771,47 @@ const basicLesson = coursePrompt({
|
|
|
764
771
|
});
|
|
765
772
|
```
|
|
766
773
|
|
|
774
|
+
#### Negative Conditional Sections
|
|
775
|
+
|
|
776
|
+
Negative conditions allow you to show content when a value is falsy:
|
|
777
|
+
|
|
778
|
+
```typescript
|
|
779
|
+
const feedbackPrompt = createPromptTemplate(`
|
|
780
|
+
Analyze the {{language}} code submission.
|
|
781
|
+
{{#passed}}
|
|
782
|
+
Great job! The tests passed successfully.
|
|
783
|
+
{{/passed}}
|
|
784
|
+
{{#!passed}}
|
|
785
|
+
The tests did not pass. Please review the following issues:
|
|
786
|
+
{{errors}}
|
|
787
|
+
{{/passed}}
|
|
788
|
+
|
|
789
|
+
{{#!skipSuggestions}}
|
|
790
|
+
Here are some suggestions for improvement:
|
|
791
|
+
- Consider refactoring for better readability
|
|
792
|
+
- Add more comprehensive error handling
|
|
793
|
+
{{/skipSuggestions}}
|
|
794
|
+
`);
|
|
795
|
+
|
|
796
|
+
// When tests pass
|
|
797
|
+
const successResult = feedbackPrompt({
|
|
798
|
+
language: 'JavaScript',
|
|
799
|
+
passed: true,
|
|
800
|
+
errors: '',
|
|
801
|
+
skipSuggestions: false
|
|
802
|
+
});
|
|
803
|
+
// Result: Shows success message and suggestions
|
|
804
|
+
|
|
805
|
+
// When tests fail
|
|
806
|
+
const failureResult = feedbackPrompt({
|
|
807
|
+
language: 'Python',
|
|
808
|
+
passed: false,
|
|
809
|
+
errors: 'TypeError on line 15',
|
|
810
|
+
skipSuggestions: false
|
|
811
|
+
});
|
|
812
|
+
// Result: Shows failure message with errors and suggestions
|
|
813
|
+
```
|
|
814
|
+
|
|
767
815
|
#### Nested Conditional Sections
|
|
768
816
|
|
|
769
817
|
You can nest conditional sections for more complex logic:
|
|
@@ -962,8 +1010,10 @@ Creates a prompt template function from a template string.
|
|
|
962
1010
|
- Replacement values can be strings, numbers, or booleans (automatically converted to strings)
|
|
963
1011
|
|
|
964
1012
|
**Conditional Sections:**
|
|
965
|
-
-
|
|
966
|
-
- The section content is included only if the condition variable is truthy
|
|
1013
|
+
- Positive conditional sections use the syntax: `{{#conditionName}}content{{/conditionName}}`
|
|
1014
|
+
- The section content is included only if the condition variable is truthy
|
|
1015
|
+
- Negative conditional sections use the syntax: `{{#!conditionName}}content{{/conditionName}}`
|
|
1016
|
+
- The section content is included only if the condition variable is falsy
|
|
967
1017
|
- Truthy values: `true`, non-empty strings, non-zero numbers
|
|
968
1018
|
- Falsy values: `false`, empty strings, `0`, `null`, `undefined`
|
|
969
1019
|
- Conditional variables are optional in the type system when used only as conditions
|
|
@@ -1111,7 +1161,7 @@ const testConfig = resolveTestConfig(LLMPurposes.Completion);
|
|
|
1111
1161
|
// Use in tests to iterate over all providers
|
|
1112
1162
|
Object.values(testConfig).forEach((config) => {
|
|
1113
1163
|
const { provider, clientOptions, availableModels, requireCredentials } = config;
|
|
1114
|
-
|
|
1164
|
+
|
|
1115
1165
|
describe(`${provider} Provider`, () => {
|
|
1116
1166
|
beforeAll(() => {
|
|
1117
1167
|
requireCredentials(); // Ensures API keys are present
|
|
@@ -1194,7 +1244,7 @@ describe('Custom LLM Integration Test', () => {
|
|
|
1194
1244
|
|
|
1195
1245
|
beforeAll(() => {
|
|
1196
1246
|
requireCredentials(); // Validates API keys are present
|
|
1197
|
-
|
|
1247
|
+
|
|
1198
1248
|
service = LLMServiceFactory.getCompletionService({
|
|
1199
1249
|
provider,
|
|
1200
1250
|
options: clientOptions,
|
|
@@ -1225,7 +1275,54 @@ describe('Custom LLM Integration Test', () => {
|
|
|
1225
1275
|
});
|
|
1226
1276
|
```
|
|
1227
1277
|
|
|
1228
|
-
## Developer Guide
|
|
1278
|
+
## Developer Guide
|
|
1279
|
+
|
|
1280
|
+
### Schema Architecture Overview
|
|
1281
|
+
|
|
1282
|
+
The LLM Gateway uses a **driver pattern** for schema conversion, providing clean separation between core schema logic and provider-specific implementations.
|
|
1283
|
+
|
|
1284
|
+
**Core Components:**
|
|
1285
|
+
|
|
1286
|
+
- **`LLMSchema`**: Core schema builder with unified API
|
|
1287
|
+
- **`SchemaAdapterInterface`**: Simple contract for provider-specific schema converters
|
|
1288
|
+
- **`SCHEMA_ADAPTER_REGISTRY`**: Type-safe registry ensuring all providers are handled
|
|
1289
|
+
- **Provider Adapters**: Convert generic JSON Schema to provider-specific formats
|
|
1290
|
+
|
|
1291
|
+
**How It Works:**
|
|
1292
|
+
|
|
1293
|
+
```typescript
|
|
1294
|
+
// The schema uses a unified API regardless of provider
|
|
1295
|
+
const schema = LLMSchema.object({
|
|
1296
|
+
name: LLMSchema.string(),
|
|
1297
|
+
age: LLMSchema.number().min(1),
|
|
1298
|
+
});
|
|
1299
|
+
|
|
1300
|
+
// Internally, services call _toProviderSchema() which automatically
|
|
1301
|
+
// converts to the correct provider-specific format:
|
|
1302
|
+
schema._toProviderSchema(LLMProviders.OpenAI); // → OpenAI JSON Schema format
|
|
1303
|
+
schema._toProviderSchema(LLMProviders.GoogleGenerativeAI); // → Google Type-based format
|
|
1304
|
+
```
|
|
1305
|
+
|
|
1306
|
+
**Provider Schema Adapters:**
|
|
1307
|
+
|
|
1308
|
+
Each provider has its own schema adapter located in `src/providers/{Provider}/schemas/`:
|
|
1309
|
+
|
|
1310
|
+
- **`OpenAISchemaAdapter`**: Converts to OpenAI's JSON Schema format, handles strict mode requirements
|
|
1311
|
+
- **`GoogleSchemaAdapter`**: Converts to Google's Type-based schema format using their `Type` enum
|
|
1312
|
+
|
|
1313
|
+
**Type-Safe Registry:**
|
|
1314
|
+
|
|
1315
|
+
Schema adapters are managed through a centralized, type-safe registry that ensures compile-time safety:
|
|
1316
|
+
|
|
1317
|
+
```typescript
|
|
1318
|
+
// src/utilities/schema/adapters/SchemaAdapterRegistry.ts
|
|
1319
|
+
export const SCHEMA_ADAPTER_REGISTRY = {
|
|
1320
|
+
OpenAI: new OpenAISchemaAdapter(),
|
|
1321
|
+
GoogleGenerativeAI: new GoogleSchemaAdapter(),
|
|
1322
|
+
} as const satisfies Record<LLMProviders, SchemaAdapterInterface | null>;
|
|
1323
|
+
```
|
|
1324
|
+
|
|
1325
|
+
### Adding a New Provider
|
|
1229
1326
|
|
|
1230
1327
|
To add support for a new LLM provider, follow these steps:
|
|
1231
1328
|
|
|
@@ -1240,6 +1337,8 @@ src/providers/YourProvider/
|
|
|
1240
1337
|
├── YourProvider.entity.ts # Provider-specific entity
|
|
1241
1338
|
├── YourProvider.typedefs.ts # TypeScript definitions
|
|
1242
1339
|
├── YourProviderService.factory.ts # Factory for your provider's services
|
|
1340
|
+
├── schemas/ # Schema conversion adapters
|
|
1341
|
+
│ └── YourProviderSchemaAdapter.ts
|
|
1243
1342
|
└── services/ # Provider service implementations
|
|
1244
1343
|
├── index.ts
|
|
1245
1344
|
├── YourProviderCompletionService.ts
|
|
@@ -1314,7 +1413,60 @@ export type LLMModelName = {
|
|
|
1314
1413
|
};
|
|
1315
1414
|
```
|
|
1316
1415
|
|
|
1317
|
-
### 4.
|
|
1416
|
+
### 4. Create Schema Adapter
|
|
1417
|
+
|
|
1418
|
+
Implement a schema adapter in `src/providers/YourProvider/schemas/YourProviderSchemaAdapter.ts`:
|
|
1419
|
+
|
|
1420
|
+
```typescript
|
|
1421
|
+
import { LLMProviders } from '@/LLMService.typedefs';
|
|
1422
|
+
import type { SchemaAdapterInterface } from '@/utilities/schema/adapters/SchemaAdapterInterface';
|
|
1423
|
+
|
|
1424
|
+
export class YourProviderSchemaAdapter implements SchemaAdapterInterface {
|
|
1425
|
+
convertSchema(jsonSchema: any): any {
|
|
1426
|
+
// Convert JSON Schema to your provider's specific format
|
|
1427
|
+
// Example: transform to provider-specific schema structure
|
|
1428
|
+
return this.transformToYourProviderFormat(jsonSchema);
|
|
1429
|
+
}
|
|
1430
|
+
|
|
1431
|
+
private transformToYourProviderFormat(jsonSchema: any): any {
|
|
1432
|
+
// Implement provider-specific schema transformation logic
|
|
1433
|
+
// Handle objects, arrays, strings, numbers, etc.
|
|
1434
|
+
// Return the schema in your provider's expected format
|
|
1435
|
+
|
|
1436
|
+
if (jsonSchema.type === 'object') {
|
|
1437
|
+
// Handle object schemas
|
|
1438
|
+
return {
|
|
1439
|
+
// Your provider's object schema format
|
|
1440
|
+
};
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
// Handle other schema types...
|
|
1444
|
+
return jsonSchema;
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
```
|
|
1448
|
+
|
|
1449
|
+
### 5. Add Adapter to Schema Registry
|
|
1450
|
+
|
|
1451
|
+
Update the schema adapter registry in `src/utilities/schema/adapters/SchemaAdapterRegistry.ts`:
|
|
1452
|
+
|
|
1453
|
+
```typescript
|
|
1454
|
+
import { YourProviderSchemaAdapter } from '@/providers/YourProvider/schemas/YourProviderSchemaAdapter';
|
|
1455
|
+
|
|
1456
|
+
export const SCHEMA_ADAPTER_REGISTRY = {
|
|
1457
|
+
OpenAI: new OpenAISchemaAdapter(),
|
|
1458
|
+
GoogleGenerativeAI: new GoogleSchemaAdapter(),
|
|
1459
|
+
YourProvider: new YourProviderSchemaAdapter(), // Add your adapter here
|
|
1460
|
+
// TypeScript will enforce that ALL providers have adapters
|
|
1461
|
+
} as const satisfies Record<LLMProviders, SchemaAdapterInterface | null>;
|
|
1462
|
+
```
|
|
1463
|
+
|
|
1464
|
+
If your provider doesn't support structured output, set it to `null`:
|
|
1465
|
+
```typescript
|
|
1466
|
+
YourProvider: null, // Provider doesn't support structured output
|
|
1467
|
+
```
|
|
1468
|
+
|
|
1469
|
+
### 6. Implement Provider Constants
|
|
1318
1470
|
|
|
1319
1471
|
Define constants in `src/providers/YourProvider/YourProvider.constants.ts`:
|
|
1320
1472
|
|
|
@@ -1412,7 +1564,7 @@ export const YOUR_PROVIDER_SERVICE_BUILDERS: {
|
|
|
1412
1564
|
};
|
|
1413
1565
|
```
|
|
1414
1566
|
|
|
1415
|
-
###
|
|
1567
|
+
### 6. Implement Provider Entity (if needed)
|
|
1416
1568
|
|
|
1417
1569
|
Create the entity class in `src/providers/YourProvider/YourProvider.entity.ts`:
|
|
1418
1570
|
|
|
@@ -1422,7 +1574,7 @@ export class YourProviderEntity {
|
|
|
1422
1574
|
}
|
|
1423
1575
|
```
|
|
1424
1576
|
|
|
1425
|
-
###
|
|
1577
|
+
### 7. Implement Service Classes
|
|
1426
1578
|
|
|
1427
1579
|
Create service implementations in the `services` directory:
|
|
1428
1580
|
|
|
@@ -1572,7 +1724,7 @@ export class YourProviderTextToSpeechService extends LLMTextToSpeechService<LLMP
|
|
|
1572
1724
|
}
|
|
1573
1725
|
```
|
|
1574
1726
|
|
|
1575
|
-
###
|
|
1727
|
+
### 8. Create Service Factory
|
|
1576
1728
|
|
|
1577
1729
|
First, define service builders in `src/providers/YourProvider/YourProvider.constants.ts`:
|
|
1578
1730
|
|
|
@@ -1632,7 +1784,7 @@ export class YourProviderServiceFactory extends LLMServicePurposeFactory<
|
|
|
1632
1784
|
}
|
|
1633
1785
|
```
|
|
1634
1786
|
|
|
1635
|
-
###
|
|
1787
|
+
### 9. Update Entry Point Files
|
|
1636
1788
|
|
|
1637
1789
|
Update the provider's `index.ts`:
|
|
1638
1790
|
|
|
@@ -1651,7 +1803,7 @@ Update the main providers `index.ts` at `src/providers/index.ts`:
|
|
|
1651
1803
|
export * from './YourProvider';
|
|
1652
1804
|
```
|
|
1653
1805
|
|
|
1654
|
-
###
|
|
1806
|
+
### 10. Update LLM Service Factory
|
|
1655
1807
|
|
|
1656
1808
|
Modify `src/LLMService.factory.ts` to include your new provider:
|
|
1657
1809
|
|