@mate-academy/llm-gateway 1.3.0 → 1.3.2
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 +501 -22
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -16,9 +16,12 @@ npm install @mate-academy/llm-gateway
|
|
|
16
16
|
|
|
17
17
|
- Support for multiple LLM providers (OpenAI, Google Generative AI)
|
|
18
18
|
- Standardized completion service interface
|
|
19
|
-
- Standardized assistance service interface
|
|
19
|
+
- Standardized assistance service interface with file handling
|
|
20
|
+
- Speech-to-text transcription capabilities
|
|
21
|
+
- Text-to-speech generation capabilities
|
|
20
22
|
- Factory pattern for easy provider selection
|
|
21
23
|
- Consistent logging across all providers
|
|
24
|
+
- Comprehensive testing suite with integration tests
|
|
22
25
|
|
|
23
26
|
## Usage
|
|
24
27
|
|
|
@@ -29,7 +32,12 @@ import {
|
|
|
29
32
|
LLMProviders,
|
|
30
33
|
LLMServiceFactory,
|
|
31
34
|
LLMCompletionService,
|
|
32
|
-
LLMAssistanceService
|
|
35
|
+
LLMAssistanceService,
|
|
36
|
+
LLMSpeechToTextService,
|
|
37
|
+
LLMTextToSpeechService,
|
|
38
|
+
LLMRoles,
|
|
39
|
+
LLMMessageContentType,
|
|
40
|
+
LLMUploadFileMimeTypes,
|
|
33
41
|
} from '@mate-academy/llm-gateway';
|
|
34
42
|
|
|
35
43
|
// Define provider options
|
|
@@ -60,6 +68,20 @@ const assistanceService = LLMServiceFactory.getAssistanceService(
|
|
|
60
68
|
logger, // your logger instance
|
|
61
69
|
llmProviderOptions,
|
|
62
70
|
);
|
|
71
|
+
|
|
72
|
+
// Get speech-to-text service
|
|
73
|
+
const speechToTextService = LLMServiceFactory.getSpeechToTextService(
|
|
74
|
+
LLMProviders.OpenAI,
|
|
75
|
+
logger, // your logger instance
|
|
76
|
+
llmProviderOptions,
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
// Get text-to-speech service
|
|
80
|
+
const textToSpeechService = LLMServiceFactory.getTextToSpeechService(
|
|
81
|
+
LLMProviders.OpenAI,
|
|
82
|
+
logger, // your logger instance
|
|
83
|
+
llmProviderOptions,
|
|
84
|
+
);
|
|
63
85
|
```
|
|
64
86
|
|
|
65
87
|
### Real-world Example
|
|
@@ -72,11 +94,15 @@ import {
|
|
|
72
94
|
LLMServiceFactory,
|
|
73
95
|
LLMCompletionService,
|
|
74
96
|
LLMAssistanceService,
|
|
97
|
+
LLMSpeechToTextService,
|
|
98
|
+
LLMTextToSpeechService,
|
|
75
99
|
} from '@mate-academy/llm-gateway';
|
|
76
100
|
|
|
77
101
|
class MyUseCase {
|
|
78
102
|
private llmCompletionService: LLMCompletionService<LLMProviders>;
|
|
79
103
|
private llmAssistanceService: LLMAssistanceService<LLMProviders>;
|
|
104
|
+
private llmSpeechToTextService: LLMSpeechToTextService<LLMProviders>;
|
|
105
|
+
private llmTextToSpeechService: LLMTextToSpeechService<LLMProviders>;
|
|
80
106
|
|
|
81
107
|
constructor(logger, config) {
|
|
82
108
|
const llmProviderOptions = LLMServiceFactory.resolveProviderOptions(
|
|
@@ -104,20 +130,135 @@ class MyUseCase {
|
|
|
104
130
|
logger,
|
|
105
131
|
llmProviderOptions,
|
|
106
132
|
);
|
|
133
|
+
|
|
134
|
+
this.llmSpeechToTextService = LLMServiceFactory.getSpeechToTextService(
|
|
135
|
+
config.llmProvider,
|
|
136
|
+
logger,
|
|
137
|
+
llmProviderOptions,
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
this.llmTextToSpeechService = LLMServiceFactory.getTextToSpeechService(
|
|
141
|
+
config.llmProvider,
|
|
142
|
+
logger,
|
|
143
|
+
llmProviderOptions,
|
|
144
|
+
);
|
|
107
145
|
}
|
|
108
146
|
|
|
109
147
|
async processRequest(prompt) {
|
|
110
148
|
// Use completion service
|
|
111
|
-
const completion = await this.llmCompletionService.
|
|
112
|
-
|
|
113
|
-
|
|
149
|
+
const completion = await this.llmCompletionService.sendMessage({
|
|
150
|
+
message: {
|
|
151
|
+
role: LLMRoles.User,
|
|
152
|
+
content: { type: LLMMessageContentType.TEXT, text: prompt }
|
|
153
|
+
},
|
|
154
|
+
model: this.getPreferredModel(),
|
|
114
155
|
});
|
|
115
156
|
|
|
116
157
|
return completion;
|
|
117
158
|
}
|
|
159
|
+
|
|
160
|
+
async transcribeAudio(audioPath) {
|
|
161
|
+
// Use speech-to-text service
|
|
162
|
+
const transcription = await this.llmSpeechToTextService.transcribe({
|
|
163
|
+
pathToAudio: audioPath,
|
|
164
|
+
model: this.getPreferredModel(),
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
return transcription;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async generateSpeech(text) {
|
|
171
|
+
// Use text-to-speech service
|
|
172
|
+
const speech = await this.llmTextToSpeechService.createSpeech({
|
|
173
|
+
text: text,
|
|
174
|
+
model: this.getPreferredModel(),
|
|
175
|
+
speechOptions: {
|
|
176
|
+
voice: 'alloy', // OpenAI voice option
|
|
177
|
+
response_format: 'mp3',
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
return speech;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
private getPreferredModel() {
|
|
185
|
+
// Get the appropriate model from the service's available models
|
|
186
|
+
const models = Object.values(this.llmCompletionService.models);
|
|
187
|
+
return models[0]; // Use the first available model
|
|
188
|
+
}
|
|
118
189
|
}
|
|
119
190
|
```
|
|
120
191
|
|
|
192
|
+
### Usage Examples
|
|
193
|
+
|
|
194
|
+
#### Basic Text Completion
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
const result = await completionService.sendMessage({
|
|
198
|
+
message: {
|
|
199
|
+
role: LLMRoles.User,
|
|
200
|
+
content: { type: LLMMessageContentType.TEXT, text: 'Hello, how are you?' }
|
|
201
|
+
},
|
|
202
|
+
model: preferredModel,
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
console.log(result.text); // AI response
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
#### File-based Assistance
|
|
209
|
+
|
|
210
|
+
```typescript
|
|
211
|
+
// Upload files for context
|
|
212
|
+
const uploadedFile = await assistanceService.uploadFile({
|
|
213
|
+
name: 'document.txt',
|
|
214
|
+
path: '/path/to/document.txt',
|
|
215
|
+
mimeType: LLMUploadFileMimeTypes.PLAIN_TEXT,
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
// Create file storage
|
|
219
|
+
const storage = await assistanceService.createFileStorage({
|
|
220
|
+
uploadedFiles: [uploadedFile],
|
|
221
|
+
model: preferredModel,
|
|
222
|
+
instructions: 'Help me analyze this document',
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
// Create chat with file context
|
|
226
|
+
const chat = await assistanceService.createChat({
|
|
227
|
+
storageId: storage.storageId,
|
|
228
|
+
model: preferredModel,
|
|
229
|
+
history: [],
|
|
230
|
+
files: [uploadedFile],
|
|
231
|
+
instructions: 'Answer questions about the uploaded document',
|
|
232
|
+
});
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
#### Speech-to-Text Transcription
|
|
236
|
+
|
|
237
|
+
```typescript
|
|
238
|
+
const transcription = await speechToTextService.transcribe({
|
|
239
|
+
pathToAudio: '/path/to/audio.mp3',
|
|
240
|
+
model: preferredModel,
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
console.log(transcription.text); // Transcribed text
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
#### Text-to-Speech Generation
|
|
247
|
+
|
|
248
|
+
```typescript
|
|
249
|
+
const speech = await textToSpeechService.createSpeech({
|
|
250
|
+
text: 'Hello, this will be converted to speech',
|
|
251
|
+
model: preferredModel,
|
|
252
|
+
speechOptions: {
|
|
253
|
+
voice: 'alloy', // OpenAI voice option
|
|
254
|
+
response_format: 'mp3',
|
|
255
|
+
},
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
// Save audio buffer to file
|
|
259
|
+
fs.writeFileSync('output.mp3', speech.audio);
|
|
260
|
+
```
|
|
261
|
+
|
|
121
262
|
## API Reference
|
|
122
263
|
|
|
123
264
|
### LLMProviders
|
|
@@ -126,12 +267,25 @@ Enum of supported LLM providers:
|
|
|
126
267
|
|
|
127
268
|
```typescript
|
|
128
269
|
enum LLMProviders {
|
|
129
|
-
OpenAI = '
|
|
130
|
-
GoogleGenerativeAI = '
|
|
270
|
+
OpenAI = 'OpenAI',
|
|
271
|
+
GoogleGenerativeAI = 'GoogleGenerativeAI',
|
|
131
272
|
// other providers may be added in the future
|
|
132
273
|
}
|
|
133
274
|
```
|
|
134
275
|
|
|
276
|
+
### LLMPurposes
|
|
277
|
+
|
|
278
|
+
Enum of supported service purposes:
|
|
279
|
+
|
|
280
|
+
```typescript
|
|
281
|
+
enum LLMPurposes {
|
|
282
|
+
Completion = 'completion',
|
|
283
|
+
Assistance = 'assistance',
|
|
284
|
+
SpeechToText = 'speech_to_text',
|
|
285
|
+
TextToSpeech = 'text_to_speech',
|
|
286
|
+
}
|
|
287
|
+
```
|
|
288
|
+
|
|
135
289
|
### LLMServiceFactory
|
|
136
290
|
|
|
137
291
|
Factory class for creating LLM service instances.
|
|
@@ -141,6 +295,8 @@ Factory class for creating LLM service instances.
|
|
|
141
295
|
- `resolveProviderOptions(provider, optionsMap)`: Resolves the options for the specified provider
|
|
142
296
|
- `getCompletionService(provider, logger, options)`: Creates a completion service instance
|
|
143
297
|
- `getAssistanceService(provider, logger, options)`: Creates an assistance service instance
|
|
298
|
+
- `getSpeechToTextService(provider, logger, options)`: Creates a speech-to-text service instance
|
|
299
|
+
- `getTextToSpeechService(provider, logger, options)`: Creates a text-to-speech service instance
|
|
144
300
|
|
|
145
301
|
### LLMCompletionService
|
|
146
302
|
|
|
@@ -148,30 +304,204 @@ Interface for text completion services.
|
|
|
148
304
|
|
|
149
305
|
#### Methods
|
|
150
306
|
|
|
151
|
-
- `
|
|
307
|
+
- `sendMessage(options)`: Send a message to the LLM and get a completion response
|
|
152
308
|
|
|
153
309
|
### LLMAssistanceService
|
|
154
310
|
|
|
155
|
-
Interface for chat/assistance services.
|
|
311
|
+
Interface for chat/assistance services with file handling capabilities.
|
|
312
|
+
|
|
313
|
+
#### Methods
|
|
314
|
+
|
|
315
|
+
- `uploadFile(file)`: Upload a file to the LLM service
|
|
316
|
+
- Parameters: `LLMUploadFile` with `name`, `path`, and `mimeType`
|
|
317
|
+
- Returns: `Promise<LLMUploadFileResult>`
|
|
318
|
+
- `deleteFile(fileId)`: Delete a file from the LLM service
|
|
319
|
+
- `createFileStorage(options)`: Create a file storage for organizing files
|
|
320
|
+
- Parameters: `LLMCreateFileStorageOptions` with `uploadedFiles`, `model`, and optional `instructions`
|
|
321
|
+
- Returns: `Promise<LLMCreateFileStorageResult>`
|
|
322
|
+
- `deleteFileStorage(fileStorageId)`: Delete a file storage
|
|
323
|
+
- `createAssistant(options)`: Create an AI assistant with specific instructions
|
|
324
|
+
- Parameters: `LLMCreateAssistantOptions` with `model`, optional `instructions` and `storageIds`
|
|
325
|
+
- Returns: `Promise<LLMCreateAssistantResult>`
|
|
326
|
+
- `deleteAssistant(assistantId)`: Delete an assistant
|
|
327
|
+
- `createChat(options)`: Create a new chat/conversation
|
|
328
|
+
- Parameters: `LLMCreateChatOptions` with `model`, optional `instructions`, `history`, `files`, and `storageId`
|
|
329
|
+
- Returns: `Promise<LLMCreateChatResult>`
|
|
330
|
+
- `deleteChat(chatId)`: Delete a chat
|
|
331
|
+
- `assistInChat(options)`: Send a message in an existing chat and get an assistant response
|
|
332
|
+
- Parameters: `LLMAssistanceOptions` with `model`, `message`, `chatId`, and `assistantId`
|
|
333
|
+
- Returns: `Promise<LLMAssistanceResult>`
|
|
334
|
+
|
|
335
|
+
### LLMSpeechToTextService
|
|
336
|
+
|
|
337
|
+
Interface for converting speech audio to text.
|
|
156
338
|
|
|
157
339
|
#### Methods
|
|
158
340
|
|
|
159
|
-
- `
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
341
|
+
- `transcribe(options)`: Convert audio file to text transcription
|
|
342
|
+
|
|
343
|
+
### LLMTextToSpeechService
|
|
344
|
+
|
|
345
|
+
Interface for converting text to speech audio.
|
|
346
|
+
|
|
347
|
+
#### Methods
|
|
348
|
+
|
|
349
|
+
- `createSpeech(options)`: Convert text to speech audio file
|
|
165
350
|
|
|
166
351
|
## Supported Providers
|
|
167
352
|
|
|
168
353
|
### OpenAI
|
|
169
354
|
|
|
170
|
-
Supports
|
|
355
|
+
Supports all service types: completion, assistance, speech-to-text, and text-to-speech APIs. For more information, see [OpenAI API documentation](https://platform.openai.com/docs/api-reference).
|
|
356
|
+
|
|
357
|
+
**Available Models:**
|
|
358
|
+
- GPT-4 models for completion and assistance
|
|
359
|
+
- Whisper models for speech-to-text transcription
|
|
360
|
+
- TTS models for text-to-speech generation
|
|
171
361
|
|
|
172
362
|
### Google Generative AI
|
|
173
363
|
|
|
174
|
-
Supports completion
|
|
364
|
+
Supports completion and assistance APIs through Google's Generative AI models. For more information, see [Google Generative AI documentation](https://ai.google.dev/docs).
|
|
365
|
+
|
|
366
|
+
**Available Models:**
|
|
367
|
+
- Gemini models for completion, assistance and speech-to-text
|
|
368
|
+
|
|
369
|
+
## Testing
|
|
370
|
+
|
|
371
|
+
The LLM Gateway includes a comprehensive testing suite with both unit and integration tests.
|
|
372
|
+
|
|
373
|
+
### Test Structure
|
|
374
|
+
|
|
375
|
+
The package includes:
|
|
376
|
+
- **Integration tests** for all service types (`src/tests/integration/`)
|
|
377
|
+
- **Test helpers** for common testing utilities (`src/tests/helpers.ts`)
|
|
378
|
+
- **Mock implementations** for testing environments
|
|
379
|
+
- **Audio test files** for speech-to-text testing
|
|
380
|
+
|
|
381
|
+
### Running Tests
|
|
382
|
+
|
|
383
|
+
```bash
|
|
384
|
+
# Run all tests with logging
|
|
385
|
+
npm test
|
|
386
|
+
|
|
387
|
+
# Run tests silently (without logs)
|
|
388
|
+
npm run test:silent
|
|
389
|
+
|
|
390
|
+
# Run with specific environment variables
|
|
391
|
+
ENABLE_LOGGING=true npm test
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
### Test Configuration
|
|
395
|
+
|
|
396
|
+
Integration tests require API keys for the respective providers:
|
|
397
|
+
|
|
398
|
+
```bash
|
|
399
|
+
# Required environment variables for OpenAI tests
|
|
400
|
+
OPENAI_SECRET_API_KEY=your_openai_api_key
|
|
401
|
+
OPENAI_ORG_ID=your_organization_id # optional
|
|
402
|
+
OPENAI_BASE_URL=https://api.openai.com/v1 # optional
|
|
403
|
+
|
|
404
|
+
# Required environment variables for Google AI tests
|
|
405
|
+
GOOGLE_GENERATIVE_AI_API_KEY=your_google_ai_api_key
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
### Test Coverage
|
|
409
|
+
|
|
410
|
+
The integration tests cover:
|
|
411
|
+
|
|
412
|
+
1. **Completion Service Tests:**
|
|
413
|
+
- Basic message sending and responses
|
|
414
|
+
- Message history handling
|
|
415
|
+
- Error handling for invalid inputs
|
|
416
|
+
- Model-specific functionality
|
|
417
|
+
|
|
418
|
+
2. **Assistance Service Tests:**
|
|
419
|
+
- File upload and storage creation
|
|
420
|
+
- Chat creation and management
|
|
421
|
+
- Assistant interactions
|
|
422
|
+
- File-based conversations
|
|
423
|
+
|
|
424
|
+
3. **Speech-to-Text Service Tests:**
|
|
425
|
+
- Audio file transcription
|
|
426
|
+
- Multiple audio format support
|
|
427
|
+
- Error handling for invalid files
|
|
428
|
+
- Model-specific transcription quality
|
|
429
|
+
|
|
430
|
+
4. **Text-to-Speech Service Tests:**
|
|
431
|
+
- Text-to-audio conversion
|
|
432
|
+
- Voice selection options
|
|
433
|
+
- Audio format configuration
|
|
434
|
+
- Error handling
|
|
435
|
+
|
|
436
|
+
### Test Helpers
|
|
437
|
+
|
|
438
|
+
The package provides several test utilities:
|
|
439
|
+
|
|
440
|
+
```typescript
|
|
441
|
+
import {
|
|
442
|
+
mockLogger,
|
|
443
|
+
resolveTestConfig,
|
|
444
|
+
hasText,
|
|
445
|
+
hasError,
|
|
446
|
+
} from '@mate-academy/llm-gateway/tests/helpers';
|
|
447
|
+
|
|
448
|
+
// Mock logger for testing
|
|
449
|
+
const logger = mockLogger;
|
|
450
|
+
|
|
451
|
+
// Get test configuration for specific service purpose
|
|
452
|
+
const testConfig = resolveTestConfig(LLMPurposes.Completion);
|
|
453
|
+
|
|
454
|
+
// Type guards for test assertions
|
|
455
|
+
if (hasText(result)) {
|
|
456
|
+
expect(result.text).toContain('expected content');
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
if (hasError(result)) {
|
|
460
|
+
expect(result.error).toBeDefined();
|
|
461
|
+
}
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
### Writing Custom Tests
|
|
465
|
+
|
|
466
|
+
Example of writing a custom integration test:
|
|
467
|
+
|
|
468
|
+
```typescript
|
|
469
|
+
import {
|
|
470
|
+
describe,
|
|
471
|
+
it,
|
|
472
|
+
expect,
|
|
473
|
+
beforeAll,
|
|
474
|
+
} from '@jest/globals';
|
|
475
|
+
import {
|
|
476
|
+
LLMServiceFactory,
|
|
477
|
+
LLMProviders,
|
|
478
|
+
LLMPurposes,
|
|
479
|
+
} from '@mate-academy/llm-gateway';
|
|
480
|
+
import { mockLogger, resolveTestConfig } from '@mate-academy/llm-gateway/tests/helpers';
|
|
481
|
+
|
|
482
|
+
describe('Custom LLM Integration Test', () => {
|
|
483
|
+
const testConfig = resolveTestConfig(LLMPurposes.Completion);
|
|
484
|
+
|
|
485
|
+
Object.values(testConfig).forEach(({ provider, clientOptions, requireCredentials }) => {
|
|
486
|
+
describe(`${provider} Provider`, () => {
|
|
487
|
+
let service;
|
|
488
|
+
|
|
489
|
+
beforeAll(() => {
|
|
490
|
+
requireCredentials();
|
|
491
|
+
service = LLMServiceFactory.getCompletionService(
|
|
492
|
+
provider,
|
|
493
|
+
mockLogger,
|
|
494
|
+
clientOptions
|
|
495
|
+
);
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
it('should process custom request', async () => {
|
|
499
|
+
// Your custom test logic here
|
|
500
|
+
});
|
|
501
|
+
});
|
|
502
|
+
});
|
|
503
|
+
});
|
|
504
|
+
```
|
|
175
505
|
|
|
176
506
|
## Developer Guide: Adding a New Provider
|
|
177
507
|
|
|
@@ -325,6 +655,18 @@ export const YOUR_PROVIDER_MODELS: LLMProviderModelsByPurpose<
|
|
|
325
655
|
YourProviderModelNames.MODEL_TWO, // Only MODEL_TWO supports assistance
|
|
326
656
|
],
|
|
327
657
|
),
|
|
658
|
+
[LLMPurposes.SpeechToText]: pick(
|
|
659
|
+
YOUR_PROVIDER_AVAILABLE_MODELS,
|
|
660
|
+
[
|
|
661
|
+
YourProviderModelNames.MODEL_ONE, // Speech-to-text capable model
|
|
662
|
+
],
|
|
663
|
+
),
|
|
664
|
+
[LLMPurposes.TextToSpeech]: pick(
|
|
665
|
+
YOUR_PROVIDER_AVAILABLE_MODELS,
|
|
666
|
+
[
|
|
667
|
+
YourProviderModelNames.MODEL_ONE, // Text-to-speech capable model
|
|
668
|
+
],
|
|
669
|
+
),
|
|
328
670
|
};
|
|
329
671
|
|
|
330
672
|
// Define service builders for each LLM purpose
|
|
@@ -339,6 +681,12 @@ export const YOUR_PROVIDER_SERVICE_BUILDERS: {
|
|
|
339
681
|
[LLMPurposes.Assistance]: (logger, options) => (
|
|
340
682
|
new YourProviderAssistanceService(logger, options)
|
|
341
683
|
),
|
|
684
|
+
[LLMPurposes.SpeechToText]: (logger, options) => (
|
|
685
|
+
new YourProviderSpeechToTextService(logger, options)
|
|
686
|
+
),
|
|
687
|
+
[LLMPurposes.TextToSpeech]: (logger, options) => (
|
|
688
|
+
new YourProviderTextToSpeechService(logger, options)
|
|
689
|
+
),
|
|
342
690
|
};
|
|
343
691
|
```
|
|
344
692
|
|
|
@@ -396,9 +744,8 @@ export class YourProviderCompletionService extends LLMCompletionService<LLMProvi
|
|
|
396
744
|
import { Logger } from '../../../LLMService.typedefs';
|
|
397
745
|
import { LLMAssistanceService } from '../../../services/LLMAssistanceService.abstract';
|
|
398
746
|
import {
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
MessageParams,
|
|
747
|
+
LLMAssistanceOptions,
|
|
748
|
+
LLMAssistanceResult,
|
|
402
749
|
LLMProviders
|
|
403
750
|
} from '../../../LLMService.typedefs';
|
|
404
751
|
import { YourProviderEntity } from '../YourProvider.entity';
|
|
@@ -412,6 +759,94 @@ export class YourProviderAssistanceService extends LLMAssistanceService<LLMProvi
|
|
|
412
759
|
}
|
|
413
760
|
|
|
414
761
|
// Implement required assistance methods
|
|
762
|
+
async uploadFile(file: LLMUploadFile): Promise<LLMUploadFileResult> {
|
|
763
|
+
// Implementation for file upload
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
async createFileStorage(options: LLMCreateFileStorageOptions): Promise<LLMCreateFileStorageResult> {
|
|
767
|
+
// Implementation for file storage creation
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
async createChat(options: LLMCreateChatOptions): Promise<LLMCreateChatResult> {
|
|
771
|
+
// Implementation for chat creation
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
async assistInChat(options: LLMAssistanceOptions): Promise<LLMAssistanceResult> {
|
|
775
|
+
// Implementation for chat assistance
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
```
|
|
779
|
+
|
|
780
|
+
**SpeechToTextService (if applicable)**:
|
|
781
|
+
|
|
782
|
+
```typescript
|
|
783
|
+
import { Logger } from '../../../LLMService.typedefs';
|
|
784
|
+
import { LLMSpeechToTextService } from '../../../services/LLMSpeechToTextService.abstract';
|
|
785
|
+
import {
|
|
786
|
+
LLMTranscribeOptions,
|
|
787
|
+
LLMTranscribeResult,
|
|
788
|
+
LLMProviders
|
|
789
|
+
} from '../../../LLMService.typedefs';
|
|
790
|
+
import { YourProviderEntity } from '../YourProvider.entity';
|
|
791
|
+
|
|
792
|
+
export class YourProviderSpeechToTextService extends LLMSpeechToTextService<LLMProviders.YourProvider> {
|
|
793
|
+
constructor(
|
|
794
|
+
logger: Logger,
|
|
795
|
+
private readonly providerEntity: YourProviderEntity,
|
|
796
|
+
) {
|
|
797
|
+
super(logger);
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
async transcribe(options: LLMTranscribeOptions<typeof this.provider>): Promise<LLMTranscribeResult> {
|
|
801
|
+
this.logger.info('Starting transcription with YourProvider', { options });
|
|
802
|
+
|
|
803
|
+
try {
|
|
804
|
+
// Implement provider-specific transcription logic
|
|
805
|
+
return {
|
|
806
|
+
text: 'Transcribed text from audio',
|
|
807
|
+
};
|
|
808
|
+
} catch (error) {
|
|
809
|
+
this.logger.error('Error in YourProvider transcription', { error });
|
|
810
|
+
throw error;
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
```
|
|
815
|
+
|
|
816
|
+
**TextToSpeechService (if applicable)**:
|
|
817
|
+
|
|
818
|
+
```typescript
|
|
819
|
+
import { Logger } from '../../../LLMService.typedefs';
|
|
820
|
+
import { LLMTextToSpeechService } from '../../../services/LLMTextToSpeechService.abstract';
|
|
821
|
+
import {
|
|
822
|
+
LLMCreateSpeechOptions,
|
|
823
|
+
LLMCreateSpeechResult,
|
|
824
|
+
LLMProviders
|
|
825
|
+
} from '../../../LLMService.typedefs';
|
|
826
|
+
import { YourProviderEntity } from '../YourProvider.entity';
|
|
827
|
+
|
|
828
|
+
export class YourProviderTextToSpeechService extends LLMTextToSpeechService<LLMProviders.YourProvider> {
|
|
829
|
+
constructor(
|
|
830
|
+
logger: Logger,
|
|
831
|
+
private readonly providerEntity: YourProviderEntity,
|
|
832
|
+
) {
|
|
833
|
+
super(logger);
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
async createSpeech(options: LLMCreateSpeechOptions<typeof this.provider>): Promise<LLMCreateSpeechResult> {
|
|
837
|
+
this.logger.info('Starting speech creation with YourProvider', { options });
|
|
838
|
+
|
|
839
|
+
try {
|
|
840
|
+
// Implement provider-specific speech creation logic
|
|
841
|
+
return {
|
|
842
|
+
audio: Buffer.from('audio data'),
|
|
843
|
+
mimeType: 'audio/mp3',
|
|
844
|
+
};
|
|
845
|
+
} catch (error) {
|
|
846
|
+
this.logger.error('Error in YourProvider speech creation', { error });
|
|
847
|
+
throw error;
|
|
848
|
+
}
|
|
849
|
+
}
|
|
415
850
|
}
|
|
416
851
|
```
|
|
417
852
|
|
|
@@ -528,7 +963,11 @@ export class LLMServiceFactory {
|
|
|
528
963
|
switch (provider) {
|
|
529
964
|
// ... other providers
|
|
530
965
|
case LLMProviders.YourProvider:
|
|
531
|
-
return YourProviderServiceFactory.
|
|
966
|
+
return YourProviderServiceFactory.createService(
|
|
967
|
+
LLMPurposes.Completion,
|
|
968
|
+
logger,
|
|
969
|
+
options
|
|
970
|
+
);
|
|
532
971
|
default:
|
|
533
972
|
throw new Error(`Unsupported provider: ${provider}`);
|
|
534
973
|
}
|
|
@@ -542,7 +981,47 @@ export class LLMServiceFactory {
|
|
|
542
981
|
switch (provider) {
|
|
543
982
|
// ... other providers
|
|
544
983
|
case LLMProviders.YourProvider:
|
|
545
|
-
return YourProviderServiceFactory.
|
|
984
|
+
return YourProviderServiceFactory.createService(
|
|
985
|
+
LLMPurposes.Assistance,
|
|
986
|
+
logger,
|
|
987
|
+
options
|
|
988
|
+
);
|
|
989
|
+
default:
|
|
990
|
+
throw new Error(`Unsupported provider: ${provider}`);
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
static getSpeechToTextService<T extends LLMProviders>(
|
|
995
|
+
provider: T,
|
|
996
|
+
logger: Logger,
|
|
997
|
+
options: any,
|
|
998
|
+
) {
|
|
999
|
+
switch (provider) {
|
|
1000
|
+
// ... other providers
|
|
1001
|
+
case LLMProviders.YourProvider:
|
|
1002
|
+
return YourProviderServiceFactory.createService(
|
|
1003
|
+
LLMPurposes.SpeechToText,
|
|
1004
|
+
logger,
|
|
1005
|
+
options
|
|
1006
|
+
);
|
|
1007
|
+
default:
|
|
1008
|
+
throw new Error(`Unsupported provider: ${provider}`);
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
static getTextToSpeechService<T extends LLMProviders>(
|
|
1013
|
+
provider: T,
|
|
1014
|
+
logger: Logger,
|
|
1015
|
+
options: any,
|
|
1016
|
+
) {
|
|
1017
|
+
switch (provider) {
|
|
1018
|
+
// ... other providers
|
|
1019
|
+
case LLMProviders.YourProvider:
|
|
1020
|
+
return YourProviderServiceFactory.createService(
|
|
1021
|
+
LLMPurposes.TextToSpeech,
|
|
1022
|
+
logger,
|
|
1023
|
+
options
|
|
1024
|
+
);
|
|
546
1025
|
default:
|
|
547
1026
|
throw new Error(`Unsupported provider: ${provider}`);
|
|
548
1027
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mate-academy/llm-gateway",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.2",
|
|
4
4
|
"description": "A gateway package for LLM services.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
"axios": "^1.9.0",
|
|
66
66
|
"js-tiktoken": "^1.0.20",
|
|
67
67
|
"mime-types": "^3.0.1",
|
|
68
|
-
"openai": "^5.
|
|
68
|
+
"openai": "^5.2.0",
|
|
69
69
|
"uuid": "^11.1.0"
|
|
70
70
|
}
|
|
71
71
|
}
|