@mate-academy/llm-gateway 2.1.1 → 2.1.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 +143 -4
- package/dist/LLMService.typedefs.d.ts +1 -0
- package/dist/LLMService.typedefs.js +1 -1
- package/dist/LLMService.typedefs.js.map +1 -1
- package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAIAssistance.service.d.ts +2 -1
- package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAIAssistance.service.js +1 -1
- package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAIAssistance.service.js.map +1 -1
- package/dist/providers/OpenAI/services/OpenAIAssistance.service.d.ts +2 -1
- package/dist/providers/OpenAI/services/OpenAIAssistance.service.js +1 -1
- package/dist/providers/OpenAI/services/OpenAIAssistance.service.js.map +1 -1
- package/dist/services/LLMAssistanceService.abstract.d.ts +2 -1
- package/dist/services/LLMAssistanceService.abstract.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,6 +19,7 @@ npm install @mate-academy/llm-gateway
|
|
|
19
19
|
- Standardized assistance service interface with file handling
|
|
20
20
|
- Speech-to-text transcription capabilities
|
|
21
21
|
- Text-to-speech generation capabilities
|
|
22
|
+
- Type-safe prompt templates with dynamic replacements
|
|
22
23
|
- Factory pattern for easy provider selection
|
|
23
24
|
- Consistent logging across all providers
|
|
24
25
|
- Comprehensive testing suite with integration tests
|
|
@@ -38,6 +39,7 @@ import {
|
|
|
38
39
|
LLMRoles,
|
|
39
40
|
LLMMessageContentType,
|
|
40
41
|
LLMUploadFileMimeTypes,
|
|
42
|
+
createPromptTemplate,
|
|
41
43
|
} from '@mate-academy/llm-gateway';
|
|
42
44
|
|
|
43
45
|
// Define provider options
|
|
@@ -359,6 +361,147 @@ Interface for converting text to speech audio.
|
|
|
359
361
|
|
|
360
362
|
- `createSpeech(options)`: Convert text to speech audio file
|
|
361
363
|
|
|
364
|
+
### Prompt Builder
|
|
365
|
+
|
|
366
|
+
The LLM Gateway includes a powerful prompt template system that provides type-safe string templates with dynamic replacements. This allows you to create reusable prompt templates with placeholders that can be replaced with actual values at runtime.
|
|
367
|
+
|
|
368
|
+
#### Features
|
|
369
|
+
|
|
370
|
+
- **Type Safety**: Automatic extraction and validation of placeholder keys from template strings
|
|
371
|
+
- **Dynamic Replacements**: Replace placeholders like `{{variableName}}` with actual values
|
|
372
|
+
- **Template Reusability**: Create templates once and use them multiple times with different values
|
|
373
|
+
- **Zero Runtime Dependencies**: Pure TypeScript utility functions
|
|
374
|
+
|
|
375
|
+
#### Basic Usage
|
|
376
|
+
|
|
377
|
+
```typescript
|
|
378
|
+
import { createPromptTemplate } from '@mate-academy/llm-gateway';
|
|
379
|
+
|
|
380
|
+
// Create a prompt template with placeholders
|
|
381
|
+
const welcomePrompt = createPromptTemplate(`
|
|
382
|
+
Generate a welcome message for a user who has just started their auto tech check attempt on {{topicTitle}}.
|
|
383
|
+
The user's experience level is {{experienceLevel}} and they prefer {{learningStyle}} learning.
|
|
384
|
+
`);
|
|
385
|
+
|
|
386
|
+
// Use the template with actual values
|
|
387
|
+
const instruction = welcomePrompt({
|
|
388
|
+
topicTitle: 'JavaScript Basics',
|
|
389
|
+
experienceLevel: 'beginner',
|
|
390
|
+
learningStyle: 'hands-on',
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
// Result: "Generate a welcome message for a user who has just started their auto tech check attempt on JavaScript Basics. The user's experience level is beginner and they prefer hands-on learning."
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
#### Advanced Usage
|
|
397
|
+
|
|
398
|
+
```typescript
|
|
399
|
+
// Template without placeholders (no parameters required)
|
|
400
|
+
const staticPrompt = createPromptTemplate(`
|
|
401
|
+
Please analyze the provided code and suggest improvements.
|
|
402
|
+
`);
|
|
403
|
+
const staticInstruction = staticPrompt(); // No parameters needed
|
|
404
|
+
|
|
405
|
+
// Template with multiple placeholders
|
|
406
|
+
const codeReviewPrompt = createPromptTemplate(`
|
|
407
|
+
Review the {{language}} code below for {{focusArea}}.
|
|
408
|
+
Pay special attention to {{criteria}} and provide {{outputFormat}} feedback.
|
|
409
|
+
|
|
410
|
+
Code:
|
|
411
|
+
{{codeSnippet}}
|
|
412
|
+
`);
|
|
413
|
+
|
|
414
|
+
const reviewInstruction = codeReviewPrompt({
|
|
415
|
+
language: 'TypeScript',
|
|
416
|
+
focusArea: 'performance optimization',
|
|
417
|
+
criteria: 'algorithmic efficiency and memory usage',
|
|
418
|
+
outputFormat: 'structured',
|
|
419
|
+
codeSnippet: 'function example() { /* code here */ }',
|
|
420
|
+
});
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
#### Integration with LLM Services
|
|
424
|
+
|
|
425
|
+
```typescript
|
|
426
|
+
import {
|
|
427
|
+
createPromptTemplate,
|
|
428
|
+
LLMServiceFactory,
|
|
429
|
+
LLMProviders,
|
|
430
|
+
LLMRoles,
|
|
431
|
+
} from '@mate-academy/llm-gateway';
|
|
432
|
+
|
|
433
|
+
// Define reusable prompt templates
|
|
434
|
+
const PROMPTS = {
|
|
435
|
+
codeExplanation: createPromptTemplate(`
|
|
436
|
+
Explain the following {{language}} code in simple terms for a {{level}} developer:
|
|
437
|
+
{{code}}
|
|
438
|
+
`),
|
|
439
|
+
|
|
440
|
+
bugFinding: createPromptTemplate(`
|
|
441
|
+
Find potential bugs in this {{language}} code and suggest fixes:
|
|
442
|
+
{{code}}
|
|
443
|
+
`),
|
|
444
|
+
|
|
445
|
+
optimization: createPromptTemplate(`
|
|
446
|
+
Optimize the following code for {{optimizationType}}:
|
|
447
|
+
{{code}}
|
|
448
|
+
`),
|
|
449
|
+
};
|
|
450
|
+
|
|
451
|
+
// Use with completion service
|
|
452
|
+
async function explainCode(code: string, language: string, level: string) {
|
|
453
|
+
const prompt = PROMPTS.codeExplanation({
|
|
454
|
+
code,
|
|
455
|
+
language,
|
|
456
|
+
level,
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
return await completionService.sendMessage({
|
|
460
|
+
message: {
|
|
461
|
+
role: LLMRoles.User,
|
|
462
|
+
content: [prompt],
|
|
463
|
+
},
|
|
464
|
+
model: preferredModel,
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
#### Type Safety Features
|
|
470
|
+
|
|
471
|
+
The prompt builder provides compile-time type checking for template placeholders:
|
|
472
|
+
|
|
473
|
+
```typescript
|
|
474
|
+
// This will show TypeScript errors for missing or incorrect parameters
|
|
475
|
+
const template = createPromptTemplate(`Hello {{name}}, welcome to {{platform}}!`);
|
|
476
|
+
|
|
477
|
+
// ✅ Correct usage
|
|
478
|
+
template({ name: 'John', platform: 'LLM Gateway' });
|
|
479
|
+
|
|
480
|
+
// ❌ TypeScript error: missing required parameter 'platform'
|
|
481
|
+
template({ name: 'John' });
|
|
482
|
+
|
|
483
|
+
// ❌ TypeScript error: unknown parameter 'age'
|
|
484
|
+
template({ name: 'John', platform: 'LLM Gateway', age: 25 });
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
#### API Reference
|
|
488
|
+
|
|
489
|
+
**`createPromptTemplate<T extends string>(template: T)`**
|
|
490
|
+
|
|
491
|
+
Creates a prompt template function from a template string.
|
|
492
|
+
|
|
493
|
+
- **Parameters:**
|
|
494
|
+
- `template: T` - The template string with placeholders in `{{variableName}}` format
|
|
495
|
+
- **Returns:** A function that accepts replacement values and returns the processed string
|
|
496
|
+
- **Type Safety:** Automatically extracts placeholder names from the template string for type checking
|
|
497
|
+
|
|
498
|
+
**Template Placeholder Format**
|
|
499
|
+
|
|
500
|
+
- Placeholders must be enclosed in double curly braces: `{{variableName}}`
|
|
501
|
+
- Whitespace around variable names is ignored: `{{ variableName }}` works the same as `{{variableName}}`
|
|
502
|
+
- Variable names can contain letters, numbers, and underscores
|
|
503
|
+
- Replacement values can be strings or numbers (automatically converted to strings)
|
|
504
|
+
|
|
362
505
|
## Supported Providers
|
|
363
506
|
|
|
364
507
|
### OpenAI
|
|
@@ -1038,7 +1181,3 @@ export class LLMServiceFactory {
|
|
|
1038
1181
|
}
|
|
1039
1182
|
}
|
|
1040
1183
|
```
|
|
1041
|
-
|
|
1042
|
-
## License
|
|
1043
|
-
|
|
1044
|
-
[MIT](LICENSE)
|
|
@@ -201,6 +201,7 @@ export declare enum LLMUploadFileMimeTypes {
|
|
|
201
201
|
IMAGE_SVG_XML = "image/svg+xml",
|
|
202
202
|
IMAGE_BMP = "image/bmp",
|
|
203
203
|
PLAIN_TEXT = "text/plain",
|
|
204
|
+
MARKDOWN = "text/markdown",
|
|
204
205
|
AUDIO_MP3 = "audio/mp3",
|
|
205
206
|
AUDIO_MPEG = "audio/mpeg",
|
|
206
207
|
AUDIO_WAV = "audio/wav",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var LLMProviders,LLMPurposes,LLMRoles,LLMMessageContentType,LLMUploadFileMimeTypes;Object.defineProperty(exports,"__esModule",{value:!0}),exports.LLMUploadFileMimeTypes=exports.LLMMessageContentType=exports.LLMRoles=exports.LLMPurposes=exports.LLMProviders=void 0,function(e){e.OpenAI="OpenAI",e.GoogleGenerativeAI="GoogleGenerativeAI"}(LLMProviders||(exports.LLMProviders=LLMProviders={})),function(e){e.Completion="completion",e.Assistance="assistance",e.SpeechToText="speech_to_text",e.TextToSpeech="text_to_speech"}(LLMPurposes||(exports.LLMPurposes=LLMPurposes={})),function(e){e.User="user",e.Assistant="assistant"}(LLMRoles||(exports.LLMRoles=LLMRoles={})),function(e){e.TEXT="text",e.IMAGE_URL="image_url",e.IMAGE_FILE="image_file"}(LLMMessageContentType||(exports.LLMMessageContentType=LLMMessageContentType={})),function(e){e.IMAGE_PNG="image/png",e.IMAGE_JPEG="image/jpeg",e.IMAGE_JPG="image/jpg",e.IMAGE_GIF="image/gif",e.IMAGE_WEBP="image/webp",e.IMAGE_SVG_XML="image/svg+xml",e.IMAGE_BMP="image/bmp",e.PLAIN_TEXT="text/plain",e.AUDIO_MP3="audio/mp3",e.AUDIO_MPEG="audio/mpeg",e.AUDIO_WAV="audio/wav",e.AUDIO_WEBM="audio/webm"}(LLMUploadFileMimeTypes||(exports.LLMUploadFileMimeTypes=LLMUploadFileMimeTypes={}));
|
|
1
|
+
"use strict";var LLMProviders,LLMPurposes,LLMRoles,LLMMessageContentType,LLMUploadFileMimeTypes;Object.defineProperty(exports,"__esModule",{value:!0}),exports.LLMUploadFileMimeTypes=exports.LLMMessageContentType=exports.LLMRoles=exports.LLMPurposes=exports.LLMProviders=void 0,function(e){e.OpenAI="OpenAI",e.GoogleGenerativeAI="GoogleGenerativeAI"}(LLMProviders||(exports.LLMProviders=LLMProviders={})),function(e){e.Completion="completion",e.Assistance="assistance",e.SpeechToText="speech_to_text",e.TextToSpeech="text_to_speech"}(LLMPurposes||(exports.LLMPurposes=LLMPurposes={})),function(e){e.User="user",e.Assistant="assistant"}(LLMRoles||(exports.LLMRoles=LLMRoles={})),function(e){e.TEXT="text",e.IMAGE_URL="image_url",e.IMAGE_FILE="image_file"}(LLMMessageContentType||(exports.LLMMessageContentType=LLMMessageContentType={})),function(e){e.IMAGE_PNG="image/png",e.IMAGE_JPEG="image/jpeg",e.IMAGE_JPG="image/jpg",e.IMAGE_GIF="image/gif",e.IMAGE_WEBP="image/webp",e.IMAGE_SVG_XML="image/svg+xml",e.IMAGE_BMP="image/bmp",e.PLAIN_TEXT="text/plain",e.MARKDOWN="text/markdown",e.AUDIO_MP3="audio/mp3",e.AUDIO_MPEG="audio/mpeg",e.AUDIO_WAV="audio/wav",e.AUDIO_WEBM="audio/webm"}(LLMUploadFileMimeTypes||(exports.LLMUploadFileMimeTypes=LLMUploadFileMimeTypes={}));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LLMService.typedefs.js","sourceRoot":"","sources":["../src/LLMService.typedefs.ts"],"names":[],"mappings":";;;AAkBA;;GAEG;AACH,IAAY,YAGX;AAHD,WAAY,YAAY;IACtB,iCAAiB,CAAA;IACjB,yDAAyC,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B,qBAAqB,QAIhC;AAqFD;;GAEG;AACH,IAAY,sBAcX;AAdD,WAAY,sBAAsB;IAChC,iDAAuB,CAAA;IACvB,mDAAyB,CAAA;IACzB,iDAAuB,CAAA;IACvB,iDAAuB,CAAA;IACvB,mDAAyB,CAAA;IACzB,yDAA+B,CAAA;IAC/B,iDAAuB,CAAA;IACvB,mDAAyB,CAAA;IACzB,oDAA0B,CAAA;IAC1B,iDAAuB,CAAA;IACvB,mDAAyB,CAAA;IACzB,iDAAuB,CAAA;IACvB,mDAAyB,CAAA;AAC3B,CAAC,EAdW,sBAAsB,sCAAtB,sBAAsB,QAcjC"}
|
package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAIAssistance.service.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type Logger } from '@mate-academy/core';
|
|
2
2
|
import { GoogleGenAI } from '@google/genai';
|
|
3
|
-
import { type LLMAssistanceOptions, type LLMAssistanceResult, type LLMCreateAssistantResult, type LLMCreateChatOptions, type LLMCreateChatResult, type LLMCreateFileStorageOptions, type LLMCreateFileStorageResult, type LLMInstanceOptions, LLMProviders, type LLMUploadFile, type LLMUploadFileResult } from '../../../LLMService.typedefs';
|
|
3
|
+
import { type LLMAssistanceOptions, type LLMAssistanceResult, type LLMCompletionMessage, type LLMCreateAssistantResult, type LLMCreateChatOptions, type LLMCreateChatResult, type LLMCreateFileStorageOptions, type LLMCreateFileStorageResult, type LLMInstanceOptions, type LLMModel, LLMProviders, type LLMUploadFile, type LLMUploadFileResult } from '../../../LLMService.typedefs';
|
|
4
4
|
import { LLMAssistanceService } from '../../../services';
|
|
5
5
|
export declare class GoogleGenerativeAIAssistanceService extends LLMAssistanceService<LLMProviders.GoogleGenerativeAI> {
|
|
6
6
|
#private;
|
|
@@ -19,4 +19,5 @@ export declare class GoogleGenerativeAIAssistanceService extends LLMAssistanceSe
|
|
|
19
19
|
createAssistant(): Promise<LLMCreateAssistantResult>;
|
|
20
20
|
deleteAssistant(): Promise<void>;
|
|
21
21
|
assistInChat(options: LLMAssistanceOptions): Promise<LLMAssistanceResult>;
|
|
22
|
+
countTokens(messages: LLMCompletionMessage[], model: LLMModel<LLMProviders.GoogleGenerativeAI>): Promise<number>;
|
|
22
23
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.GoogleGenerativeAIAssistanceService=void 0;const uuid_1=require("uuid"),genai_1=require("@google/genai"),LLMService_typedefs_1=require("../../../LLMService.typedefs"),services_1=require("../../../services"),GoogleGenerativeAI_typedefs_1=require("../../../providers/GoogleGenerativeAI/GoogleGenerativeAI.typedefs"),GoogleGenerativeAI_entity_1=require("../../../providers/GoogleGenerativeAI/GoogleGenerativeAI.entity"),GoogleGenerativeAI_constants_1=require("../../../providers/GoogleGenerativeAI/GoogleGenerativeAI.constants");class GoogleGenerativeAIAssistanceService extends services_1.LLMAssistanceService{#e=null;cachedContent=new Map;chatSessions=new Map;uploadedFiles=new Map;uncachedFileStorages=new Map;constructor(e,t){super(LLMService_typedefs_1.LLMProviders.GoogleGenerativeAI,e,t)}get instance(){return this.#e||(this.#e=new genai_1.GoogleGenAI({apiKey:this.options.apiKey})),this.#e}async uploadFile(e){try{const{path:t,mimeType:r,name:i}=e,
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.GoogleGenerativeAIAssistanceService=void 0;const uuid_1=require("uuid"),genai_1=require("@google/genai"),LLMService_typedefs_1=require("../../../LLMService.typedefs"),services_1=require("../../../services"),GoogleGenerativeAI_typedefs_1=require("../../../providers/GoogleGenerativeAI/GoogleGenerativeAI.typedefs"),GoogleGenerativeAI_entity_1=require("../../../providers/GoogleGenerativeAI/GoogleGenerativeAI.entity"),GoogleGenerativeAI_constants_1=require("../../../providers/GoogleGenerativeAI/GoogleGenerativeAI.constants");class GoogleGenerativeAIAssistanceService extends services_1.LLMAssistanceService{#e=null;cachedContent=new Map;chatSessions=new Map;uploadedFiles=new Map;uncachedFileStorages=new Map;constructor(e,t){super(LLMService_typedefs_1.LLMProviders.GoogleGenerativeAI,e,t)}get instance(){return this.#e||(this.#e=new genai_1.GoogleGenAI({apiKey:this.options.apiKey})),this.#e}async uploadFile(e){try{const{path:t,mimeType:r,name:i}=e,n=this.uploadedFiles.get(t);if(n)return n;const o=await this.instance.files.upload({file:t,config:{mimeType:r,displayName:i}});if(!o.name)throw new Error("Upload result did not return a name");if(!o.uri)throw new Error("Upload result did not return a uri");if(o.error)throw o.error;const s={...e,fileId:o.name,path:o.uri};return this.uploadedFiles.set(t,s),s}catch(t){return this.logger.child("uploadFile").error("Error uploading file",{error:t,file:e}),{error:t}}}async deleteFile(e){this.uploadedFiles.delete(e),await this.instance.files.delete({name:e})}async createFileStorage(e){const{uploadedFiles:t,model:r,instructions:i}=e;try{const e=GoogleGenerativeAI_entity_1.GoogleGenerativeAIEntity.getFileDataParts(t),n=GoogleGenerativeAI_entity_1.GoogleGenerativeAIEntity.getCountTokensFn(this.instance),o=await n(e,r);if(o<GoogleGenerativeAI_constants_1.MIN_CACHE_CONTENT_LENGTH){const e=(0,uuid_1.v4)();return this.uncachedFileStorages.set(e,t),this.logger.child("createFileStorage").info("File storage created without caching (content too small)",{storageId:e,tokenCount:o,minRequired:GoogleGenerativeAI_constants_1.MIN_CACHE_CONTENT_LENGTH}),{storageId:e}}const s=await this.instance.caches.create({model:r.name,config:{contents:[{role:GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIRoles.User,parts:e}],systemInstruction:i,ttl:"1h"}});if(!s.name)throw new Error("Cache result did not return a name");return this.cachedContent.set(s.name,s),{storageId:s.name}}catch(e){return this.logger.child("createFileStorage").error("Error creating file storage",{error:e,uploadedFiles:t,model:r}),{error:e}}}async deleteFileStorage(e){if(""===e)return;this.cachedContent.get(e)&&(this.cachedContent.delete(e),await this.instance.caches.delete({name:e})),this.uncachedFileStorages.delete(e)}async createChat(e){try{const{storageId:t,model:r,instructions:i,history:n,files:o}=e,s=t?this.cachedContent.get(t):void 0,a=t?this.uncachedFileStorages.get(t):void 0,c=n?await Promise.all(n.map(e=>GoogleGenerativeAI_entity_1.GoogleGenerativeAIEntity.getContent(e,this.instance))):[];if(!s&&o){const e=GoogleGenerativeAI_entity_1.GoogleGenerativeAIEntity.getFileDataParts(o);c.push({role:GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIRoles.User,parts:e})}if(!s&&a){const e=GoogleGenerativeAI_entity_1.GoogleGenerativeAIEntity.getFileDataParts(a);c.push({role:GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIRoles.User,parts:e})}const l=this.instance.chats.create({model:r.name,history:c,config:{systemInstruction:i,temperature:r.config.temperature,...s&&{cachedContent:s.name}}}),g=(0,uuid_1.v4)();return this.chatSessions.set(g,l),Promise.resolve({chatId:g})}catch(t){return this.logger.child("createChat").error("Error creating chat",{error:t,options:e}),Promise.resolve({error:t})}}deleteChat(e){return this.chatSessions.delete(e),Promise.resolve()}createAssistant(){return Promise.resolve({assistantId:""})}deleteAssistant(){return Promise.resolve()}async assistInChat(e){try{const{message:t,chatId:r}=e,i=this.chatSessions.get(r);if(!i)throw new Error("Chat session not found");const n=await GoogleGenerativeAI_entity_1.GoogleGenerativeAIEntity.getContent(t,this.instance);if(!n.parts)return{text:""};return{text:(await i.sendMessage({message:n.parts})).text??""}}catch(t){return this.logger.child("assistInChat").error("Error assisting in chat",{error:t,options:e}),{error:t}}}async countTokens(e,t){return GoogleGenerativeAI_entity_1.GoogleGenerativeAIEntity.getCountTokensFn(this.instance)(e,t)}}exports.GoogleGenerativeAIAssistanceService=GoogleGenerativeAIAssistanceService;
|
package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAIAssistance.service.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GoogleGenerativeAIAssistance.service.js","sourceRoot":"","sources":["../../../../src/providers/GoogleGenerativeAI/services/GoogleGenerativeAIAssistance.service.ts"],"names":[],"mappings":";;;AACA,+BAAoC;AACpC,yCAKuB;AACvB,+
|
|
1
|
+
{"version":3,"file":"GoogleGenerativeAIAssistance.service.js","sourceRoot":"","sources":["../../../../src/providers/GoogleGenerativeAI/services/GoogleGenerativeAIAssistance.service.ts"],"names":[],"mappings":";;;AACA,+BAAoC;AACpC,yCAKuB;AACvB,+DAe+B;AAC/B,yCAAkD;AAClD,4GAAqG;AACrG,wGAAoG;AACpG,8GAAuG;AAEvG,MAAa,mCACX,SAAQ,+BAAqD;IAC7D,SAAS,GAAuB,IAAI,CAAC;IAE7B,aAAa,GAA+B,IAAI,GAAG,EAAE,CAAC;IAEtD,YAAY,GAAsB,IAAI,GAAG,EAAE,CAAC;IAE5C,aAAa,GAAiC,IAAI,GAAG,EAAE,CAAC;IAExD,oBAAoB,GAAmC,IAAI,GAAG,EAAE,CAAC;IAEzE,YACE,MAAc,EACd,OAA4D;QAE5D,KAAK,CAAC,kCAAY,CAAC,kBAAkB,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;IAC1D,CAAC;IAED,IAAc,QAAQ;QACpB,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACpB,IAAI,CAAC,SAAS,GAAG,IAAI,mBAAW,CAAC;gBAC/B,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,MAAM;aAC5B,CAAC,CAAC;QACL,CAAC;QAED,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,IAAmB;QAClC,IAAI,CAAC;YACH,MAAM,EACJ,IAAI,EACJ,QAAQ,EACR,IAAI,GACL,GAAG,IAAI,CAAC;YAET,MAAM,YAAY,GAAG,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YAElD,IAAI,YAAY,EAAE,CAAC;gBACjB,OAAO,YAAY,CAAC;YACtB,CAAC;YAED,MAAM,YAAY,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC;gBACpD,IAAI,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ACrB,KAAK,CAAC,yBAAyB,EAAE;gBAChC,KAAK;gBACL,OAAO;aACR,CAAC,CAAC;YAEL,OAAO,EAAE,KAAK,EAAE,CAAC;QACnB,CAAC;IACH,CAAC;IAED,KAAK,CAAC,WAAW,CACf,QAAgC,EAChC,KAAgD;QAEhD,MAAM,aAAa,GAAG,oDAAwB;aAC3C,gBAAgB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QAEnC,OAAO,aAAa,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IACxC,CAAC;CACF;AAjVD,kFAiVC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type Logger } from '@mate-academy/core';
|
|
2
2
|
import { OpenAI } from 'openai';
|
|
3
|
-
import { type LLMAssistanceOptions, type LLMAssistanceResult, type LLMCreateAssistantOptions, type LLMCreateAssistantResult, type LLMCreateChatOptions, type LLMCreateChatResult, type LLMCreateFileStorageOptions, type LLMCreateFileStorageResult, type LLMInstanceOptions, LLMProviders, type LLMUploadFile, type LLMUploadFileResult } from '../../../LLMService.typedefs';
|
|
3
|
+
import { type LLMAssistanceOptions, type LLMAssistanceResult, type LLMCompletionMessage, type LLMCreateAssistantOptions, type LLMCreateAssistantResult, type LLMCreateChatOptions, type LLMCreateChatResult, type LLMCreateFileStorageOptions, type LLMCreateFileStorageResult, type LLMInstanceOptions, LLMProviders, type LLMUploadFile, type LLMUploadFileResult } from '../../../LLMService.typedefs';
|
|
4
4
|
import { LLMAssistanceService } from '../../../services';
|
|
5
5
|
export declare class OpenAIAssistanceService extends LLMAssistanceService<LLMProviders.OpenAI> {
|
|
6
6
|
#private;
|
|
@@ -16,4 +16,5 @@ export declare class OpenAIAssistanceService extends LLMAssistanceService<LLMPro
|
|
|
16
16
|
createAssistant(options: LLMCreateAssistantOptions<LLMProviders.OpenAI>): Promise<LLMCreateAssistantResult>;
|
|
17
17
|
deleteAssistant(assistantId: string): Promise<void>;
|
|
18
18
|
assistInChat(options: LLMAssistanceOptions): Promise<LLMAssistanceResult>;
|
|
19
|
+
countTokens(messages: LLMCompletionMessage[]): Promise<number>;
|
|
19
20
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.OpenAIAssistanceService=void 0;const fs_1=require("fs"),openai_1=require("openai"),LLMService_typedefs_1=require("../../../LLMService.typedefs"),services_1=require("../../../services"),OpenAI_typedefs_1=require("../../../providers/OpenAI/OpenAI.typedefs");class OpenAIAssistanceService extends services_1.LLMAssistanceService{#e=null;uploadedFiles=new Map;constructor(e,t){super(LLMService_typedefs_1.LLMProviders.OpenAI,e,t)}get instance(){return this.#e||(this.#e=new openai_1.OpenAI(this.options)),this.#e}async uploadFile(e){try{const{path:t}=e,s=this.uploadedFiles.get(t);if(s){if((await this.instance.files.retrieve(s)).id)return{...e,fileId:s}}const r=(0,fs_1.createReadStream)(t),i=await this.instance.files.create({purpose:"assistants",file:r});return await this.instance.files.waitForProcessing(i.id),this.uploadedFiles.set(t,i.id),{...e,fileId:i.id}}catch(t){return this.logger.child("uploadFile").error("Error uploading file",{error:t,file:e}),{error:t}}}async deleteFile(e){await this.instance.files.delete(e),this.uploadedFiles.delete(e)}async createFileStorage(e){const{uploadedFiles:t}=e;try{const e=await this.instance.vectorStores.create({expires_after:{anchor:"last_active_at",days:1}});return await this.instance.vectorStores.fileBatches.createAndPoll(e.id,{file_ids:t.map(({fileId:e})=>e)}),{storageId:e.id}}catch(e){return this.logger.child("createFileStorage").error("Error creating file storage",{error:e,uploadedFiles:t}),{error:e}}}async deleteFileStorage(e){await this.instance.vectorStores.delete(e)}async createChat(e){try{const{storageId:t,history:s,files:r}=e,i=t?{tool_resources:{file_search:{vector_store_ids:[t]}}}:{},a=s?.map(e=>({...e,content:e.content,role:e.role===LLMService_typedefs_1.LLMRoles.User?OpenAI_typedefs_1.OpenAIRoles.User:OpenAI_typedefs_1.OpenAIRoles.Assistant}))??[];r&&r.length>0&&a.push({role:OpenAI_typedefs_1.OpenAIRoles.User,content:r.map(e=>({type:LLMService_typedefs_1.LLMMessageContentType.IMAGE_FILE,image_file:{file_id:e.fileId,detail:"high"}}))});return{chatId:(await this.instance.beta.threads.create({messages:a,...i})).id}}catch(t){return this.logger.child("createChat").error("Error creating chat",{error:t,options:e}),{error:t}}}async deleteChat(e){await this.instance.beta.threads.delete(e)}async createAssistant(e){try{const{model:t,instructions:s,storageIds:r,name:i}=e,a=r?.length?{tools:[{type:"file_search"}],tool_resources:{file_search:{vector_store_ids:r}}}:{};return{assistantId:(await this.instance.beta.assistants.create({model:t.name,instructions:s,temperature:t.config.temperature,top_p:t.config.top_p,name:i,...a})).id}}catch(t){return this.logger.child("createAssistant").error("Error creating assistant",{error:t,options:e}),{error:t}}}async deleteAssistant(e){await this.instance.beta.assistants.delete(e)}async assistInChat(e){try{const{message:t,chatId:s,assistantId:r}=e,{attachments:i,content:a,role:n}=t,o=[];i&&i.forEach(e=>{o.push({type:"image_file",image_file:{file_id:e.fileId,detail:"high"}})}),o.push(...a);const c={role:n===LLMService_typedefs_1.LLMRoles.User?OpenAI_typedefs_1.OpenAIRoles.User:OpenAI_typedefs_1.OpenAIRoles.Assistant,content:o};await this.instance.beta.threads.messages.create(s,c);const l=await this.instance.beta.threads.runs.createAndPoll(s,{assistant_id:r}),{status:d}=l;if("completed"!==d)throw new Error(`Thread run did not complete for message, status: ${d}`);const p=(await this.instance.beta.threads.messages.list(s,{run_id:l.id})).data.find(e=>e.role===OpenAI_typedefs_1.OpenAIRoles.Assistant),h=p?.content.find(e=>"text"===e.type&&"text"in e);return{text:h?.text?.value??""}}catch(t){return this.logger.child("assistInChat").error("Error assisting in chat",{error:t,options:e}),{error:t}}}}exports.OpenAIAssistanceService=OpenAIAssistanceService;
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.OpenAIAssistanceService=void 0;const fs_1=require("fs"),openai_1=require("openai"),LLMService_typedefs_1=require("../../../LLMService.typedefs"),services_1=require("../../../services"),OpenAI_typedefs_1=require("../../../providers/OpenAI/OpenAI.typedefs"),OpenAI_entity_1=require("../../../providers/OpenAI/OpenAI.entity");class OpenAIAssistanceService extends services_1.LLMAssistanceService{#e=null;uploadedFiles=new Map;constructor(e,t){super(LLMService_typedefs_1.LLMProviders.OpenAI,e,t)}get instance(){return this.#e||(this.#e=new openai_1.OpenAI(this.options)),this.#e}async uploadFile(e){try{const{path:t}=e,s=this.uploadedFiles.get(t);if(s){if((await this.instance.files.retrieve(s)).id)return{...e,fileId:s}}const r=(0,fs_1.createReadStream)(t),i=await this.instance.files.create({purpose:"assistants",file:r});return await this.instance.files.waitForProcessing(i.id),this.uploadedFiles.set(t,i.id),{...e,fileId:i.id}}catch(t){return this.logger.child("uploadFile").error("Error uploading file",{error:t,file:e}),{error:t}}}async deleteFile(e){await this.instance.files.delete(e),this.uploadedFiles.delete(e)}async createFileStorage(e){const{uploadedFiles:t}=e;try{const e=await this.instance.vectorStores.create({expires_after:{anchor:"last_active_at",days:1}});return await this.instance.vectorStores.fileBatches.createAndPoll(e.id,{file_ids:t.map(({fileId:e})=>e)}),{storageId:e.id}}catch(e){return this.logger.child("createFileStorage").error("Error creating file storage",{error:e,uploadedFiles:t}),{error:e}}}async deleteFileStorage(e){await this.instance.vectorStores.delete(e)}async createChat(e){try{const{storageId:t,history:s,files:r}=e,i=t?{tool_resources:{file_search:{vector_store_ids:[t]}}}:{},a=s?.map(e=>({...e,content:e.content,role:e.role===LLMService_typedefs_1.LLMRoles.User?OpenAI_typedefs_1.OpenAIRoles.User:OpenAI_typedefs_1.OpenAIRoles.Assistant}))??[];r&&r.length>0&&a.push({role:OpenAI_typedefs_1.OpenAIRoles.User,content:r.map(e=>({type:LLMService_typedefs_1.LLMMessageContentType.IMAGE_FILE,image_file:{file_id:e.fileId,detail:"high"}}))});return{chatId:(await this.instance.beta.threads.create({messages:a,...i})).id}}catch(t){return this.logger.child("createChat").error("Error creating chat",{error:t,options:e}),{error:t}}}async deleteChat(e){await this.instance.beta.threads.delete(e)}async createAssistant(e){try{const{model:t,instructions:s,storageIds:r,name:i}=e,a=r?.length?{tools:[{type:"file_search"}],tool_resources:{file_search:{vector_store_ids:r}}}:{};return{assistantId:(await this.instance.beta.assistants.create({model:t.name,instructions:s,temperature:t.config.temperature,top_p:t.config.top_p,name:i,...a})).id}}catch(t){return this.logger.child("createAssistant").error("Error creating assistant",{error:t,options:e}),{error:t}}}async deleteAssistant(e){await this.instance.beta.assistants.delete(e)}async assistInChat(e){try{const{message:t,chatId:s,assistantId:r}=e,{attachments:i,content:a,role:n}=t,o=[];i&&i.forEach(e=>{o.push({type:"image_file",image_file:{file_id:e.fileId,detail:"high"}})}),o.push(...a);const c={role:n===LLMService_typedefs_1.LLMRoles.User?OpenAI_typedefs_1.OpenAIRoles.User:OpenAI_typedefs_1.OpenAIRoles.Assistant,content:o};await this.instance.beta.threads.messages.create(s,c);const l=await this.instance.beta.threads.runs.createAndPoll(s,{assistant_id:r}),{status:d}=l;if("completed"!==d)throw new Error(`Thread run did not complete for message, status: ${d}`);const p=(await this.instance.beta.threads.messages.list(s,{run_id:l.id})).data.find(e=>e.role===OpenAI_typedefs_1.OpenAIRoles.Assistant),h=p?.content.find(e=>"text"===e.type&&"text"in e);return{text:h?.text?.value??""}}catch(t){return this.logger.child("assistInChat").error("Error assisting in chat",{error:t,options:e}),{error:t}}}async countTokens(e){return OpenAI_entity_1.OpenAIEntity.getCountTokensFn()(e.flatMap(e=>e.content))}}exports.OpenAIAssistanceService=OpenAIAssistanceService;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"OpenAIAssistance.service.js","sourceRoot":"","sources":["../../../../src/providers/OpenAI/services/OpenAIAssistance.service.ts"],"names":[],"mappings":";;;AAAA,2BAAsC;AAEtC,mCAAgC;AAChC,+
|
|
1
|
+
{"version":3,"file":"OpenAIAssistance.service.js","sourceRoot":"","sources":["../../../../src/providers/OpenAI/services/OpenAIAssistance.service.ts"],"names":[],"mappings":";;;AAAA,2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ChC,KAAK;gBACL,OAAO;aACR,CAAC,CAAC;YAEL,OAAO,EAAE,KAAK,EAAE,CAAC;QACnB,CAAC;IACH,CAAC;IAED,KAAK,CAAC,WAAW,CACf,QAAgC;QAEhC,MAAM,aAAa,GAAG,4BAAY;aAC/B,gBAAgB,EAAE,CAAC;QAEtB,MAAM,gBAAgB,GAAG,QAAQ,CAAC,OAAO,CACvC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,CAC7B,CAAC;QAEF,OAAO,aAAa,CAAC,gBAAgB,CAAC,CAAC;IACzC,CAAC;CACF;AAzVD,0DAyVC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Logger } from '@mate-academy/core';
|
|
2
|
-
import { type LLMAssistanceOptions, type LLMAssistanceResult, type LLMCreateAssistantOptions, type LLMCreateAssistantResult, type LLMCreateChatOptions, type LLMCreateChatResult, type LLMCreateFileStorageOptions, type LLMCreateFileStorageResult, type LLMInstanceOptions, type LLMProviders, LLMPurposes, type LLMUploadFile, type LLMUploadFileResult } from '../LLMService.typedefs';
|
|
2
|
+
import { type LLMAssistanceOptions, type LLMAssistanceResult, type LLMCompletionMessage, type LLMCreateAssistantOptions, type LLMCreateAssistantResult, type LLMCreateChatOptions, type LLMCreateChatResult, type LLMCreateFileStorageOptions, type LLMCreateFileStorageResult, type LLMInstanceOptions, type LLMModel, type LLMProviders, LLMPurposes, type LLMUploadFile, type LLMUploadFileResult } from '../LLMService.typedefs';
|
|
3
3
|
import { LLMBaseService } from '../services';
|
|
4
4
|
export declare abstract class LLMAssistanceService<Provider extends LLMProviders> extends LLMBaseService<LLMPurposes.Assistance, Provider> {
|
|
5
5
|
constructor(provider: Provider, logger: Logger, options: LLMInstanceOptions[Provider]);
|
|
@@ -12,4 +12,5 @@ export declare abstract class LLMAssistanceService<Provider extends LLMProviders
|
|
|
12
12
|
abstract createChat(options: LLMCreateChatOptions<Provider>): Promise<LLMCreateChatResult>;
|
|
13
13
|
abstract deleteChat(chatId: string): Promise<void>;
|
|
14
14
|
abstract assistInChat(options: LLMAssistanceOptions): Promise<LLMAssistanceResult>;
|
|
15
|
+
abstract countTokens(messages: LLMCompletionMessage[], model: LLMModel<Provider>): Promise<number>;
|
|
15
16
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LLMAssistanceService.abstract.js","sourceRoot":"","sources":["../../src/services/LLMAssistanceService.abstract.ts"],"names":[],"mappings":";;;AACA,+
|
|
1
|
+
{"version":3,"file":"LLMAssistanceService.abstract.js","sourceRoot":"","sources":["../../src/services/LLMAssistanceService.abstract.ts"],"names":[],"mappings":";;;AACA,+DAgB+B;AAC/B,yCAA4C;AAE5C,MAAsB,oBACpB,SAAQ,yBAGP;IACD,YACE,QAAkB,EAClB,MAAc,EACd,OAAqC;QAErC,KAAK,CACH,QAAQ,EACR,mBAAmB,EACnB,iCAAW,CAAC,UAAU,EACtB,MAAM,EACN,OAAO,CACR,CAAC;IACJ,CAAC;CA0CF;AA3DD,oDA2DC"}
|