@mate-academy/llm-gateway 2.2.3-canary.2 → 2.2.3-canary.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.
- package/dist/LLMService.constants.js +26 -1
- package/dist/LLMService.factory.js +97 -1
- package/dist/LLMService.typedefs.js +60 -1
- package/dist/functional.utils.js +9 -1
- package/dist/index.js +23 -1
- package/dist/promptBuilder.js +75 -1
- package/dist/providers/GoogleGenerativeAI/GoogleGenerativeAI.constants.js +119 -1
- package/dist/providers/GoogleGenerativeAI/GoogleGenerativeAI.entity.js +168 -1
- package/dist/providers/GoogleGenerativeAI/GoogleGenerativeAI.typedefs.js +20 -1
- package/dist/providers/GoogleGenerativeAI/GoogleGenerativeAIService.factory.js +17 -1
- package/dist/providers/GoogleGenerativeAI/index.js +22 -1
- package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAIAssistance.service.js +285 -1
- package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAICompletion.service.js +59 -1
- package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAISpeechToText.service.js +78 -1
- package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAITextToSpeech.service.js +57 -1
- package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAITextToSpeech.service.js.map +1 -1
- package/dist/providers/GoogleGenerativeAI/services/index.js +21 -1
- package/dist/providers/OpenAI/OpenAI.constants.js +105 -1
- package/dist/providers/OpenAI/OpenAI.entity.js +152 -1
- package/dist/providers/OpenAI/OpenAI.typedefs.js +19 -1
- package/dist/providers/OpenAI/OpenAIService.factory.js +17 -1
- package/dist/providers/OpenAI/index.js +22 -1
- package/dist/providers/OpenAI/services/OpenAIAssistance.service.js +292 -1
- package/dist/providers/OpenAI/services/OpenAICompletion.service.js +60 -1
- package/dist/providers/OpenAI/services/OpenAISpeechToText.service.js +69 -1
- package/dist/providers/OpenAI/services/OpenAITextToSpeech.service.js +52 -1
- package/dist/providers/OpenAI/services/index.js +21 -1
- package/dist/providers/index.js +19 -1
- package/dist/services/LLMAssistanceService.abstract.js +19 -1
- package/dist/services/LLMBaseService.abstract.js +27 -1
- package/dist/services/LLMCompletionService.abstract.js +12 -1
- package/dist/services/LLMServicePurposeFactory.abstract.js +7 -1
- package/dist/services/LLMSpeechToTextService.abstract.js +12 -1
- package/dist/services/LLMTextToSpeechService.abstract.js +12 -1
- package/dist/services/index.js +23 -1
- package/package.json +3 -2
|
@@ -1 +1,26 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ALL_IMAGE_MIME_TYPES = exports.APPROXIMATE_TOKENS_COUNT_IN_IMAGE = exports.LLM_SERVICE_MODELS = exports.LLM_SERVICE_FACTORIES = void 0;
|
|
4
|
+
const LLMService_typedefs_1 = require("./LLMService.typedefs");
|
|
5
|
+
const providers_1 = require("./providers");
|
|
6
|
+
const GoogleGenerativeAI_constants_1 = require("./providers/GoogleGenerativeAI/GoogleGenerativeAI.constants");
|
|
7
|
+
const OpenAI_constants_1 = require("./providers/OpenAI/OpenAI.constants");
|
|
8
|
+
exports.LLM_SERVICE_FACTORIES = {
|
|
9
|
+
[LLMService_typedefs_1.LLMProviders.OpenAI]: new providers_1.OpenAIServiceFactory(),
|
|
10
|
+
[LLMService_typedefs_1.LLMProviders.GoogleGenerativeAI]: new providers_1.GoogleGenerativeAIServiceFactory(),
|
|
11
|
+
};
|
|
12
|
+
exports.LLM_SERVICE_MODELS = {
|
|
13
|
+
[LLMService_typedefs_1.LLMProviders.OpenAI]: OpenAI_constants_1.OPEN_AI_MODELS,
|
|
14
|
+
[LLMService_typedefs_1.LLMProviders.GoogleGenerativeAI]: GoogleGenerativeAI_constants_1.GOOGLE_AI_MODELS,
|
|
15
|
+
};
|
|
16
|
+
exports.APPROXIMATE_TOKENS_COUNT_IN_IMAGE = 1000;
|
|
17
|
+
exports.ALL_IMAGE_MIME_TYPES = [
|
|
18
|
+
LLMService_typedefs_1.LLMUploadFileMimeTypes.IMAGE_PNG,
|
|
19
|
+
LLMService_typedefs_1.LLMUploadFileMimeTypes.IMAGE_JPEG,
|
|
20
|
+
LLMService_typedefs_1.LLMUploadFileMimeTypes.IMAGE_JPG,
|
|
21
|
+
LLMService_typedefs_1.LLMUploadFileMimeTypes.IMAGE_GIF,
|
|
22
|
+
LLMService_typedefs_1.LLMUploadFileMimeTypes.IMAGE_WEBP,
|
|
23
|
+
LLMService_typedefs_1.LLMUploadFileMimeTypes.IMAGE_SVG_XML,
|
|
24
|
+
LLMService_typedefs_1.LLMUploadFileMimeTypes.IMAGE_BMP,
|
|
25
|
+
];
|
|
26
|
+
//# sourceMappingURL=LLMService.constants.js.map
|
|
@@ -1 +1,97 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LLMServiceFactory = void 0;
|
|
4
|
+
const LLMService_typedefs_1 = require("./LLMService.typedefs");
|
|
5
|
+
const LLMService_constants_1 = require("./LLMService.constants");
|
|
6
|
+
class LLMServiceFactory {
|
|
7
|
+
/**
|
|
8
|
+
* Gets a service for the specified LLM purpose and provider
|
|
9
|
+
*
|
|
10
|
+
* @private
|
|
11
|
+
* @static
|
|
12
|
+
* @template Purpose - The purpose of the LLM service (Completion or Assistance)
|
|
13
|
+
* @template Provider - The LLM provider (OpenAI, GoogleGenerativeAI)
|
|
14
|
+
* @param {Purpose} purpose - The purpose of the service
|
|
15
|
+
* @param {Provider} provider - The LLM provider
|
|
16
|
+
* @param {Logger} logger - Logger instance
|
|
17
|
+
* @param {LLMInstanceOptions[Provider]} options - Provider-specific options
|
|
18
|
+
* @returns {LLMServiceByPurpose<Provider>[Purpose]} The LLM service instance
|
|
19
|
+
* @throws {Error} If the provider is not supported
|
|
20
|
+
*/
|
|
21
|
+
static getService(purpose, provider, logger, options) {
|
|
22
|
+
switch (provider) {
|
|
23
|
+
case LLMService_typedefs_1.LLMProviders.OpenAI:
|
|
24
|
+
case LLMService_typedefs_1.LLMProviders.GoogleGenerativeAI:
|
|
25
|
+
return LLMService_constants_1.LLM_SERVICE_FACTORIES[provider]
|
|
26
|
+
.createService(purpose, logger, options);
|
|
27
|
+
default:
|
|
28
|
+
throw new Error(`Provider [${provider}] is not supported`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Gets a completion service for the specified LLM provider
|
|
33
|
+
*
|
|
34
|
+
* @static
|
|
35
|
+
* @template Provider - The LLM provider type
|
|
36
|
+
* @param {Provider} provider - The LLM provider (OpenAI, GoogleGenerativeAI)
|
|
37
|
+
* @param {Logger} logger - Logger instance
|
|
38
|
+
* @param {LLMInstanceOptions[Provider]} options - Provider-specific options
|
|
39
|
+
* @returns {LLMServiceByPurpose<Provider>[LLMPurposes.Completion]} A completion service instance
|
|
40
|
+
*/
|
|
41
|
+
static getCompletionService(provider, logger, options) {
|
|
42
|
+
return this.getService(LLMService_typedefs_1.LLMPurposes.Completion, provider, logger, options);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Gets an assistance service for the specified LLM provider
|
|
46
|
+
*
|
|
47
|
+
* @static
|
|
48
|
+
* @template Provider - The LLM provider type
|
|
49
|
+
* @param {Provider} provider - The LLM provider (OpenAI, GoogleGenerativeAI)
|
|
50
|
+
* @param {Logger} logger - Logger instance
|
|
51
|
+
* @param {LLMInstanceOptions[Provider]} options - Provider-specific options
|
|
52
|
+
* @returns {LLMServiceByPurpose<Provider>[LLMPurposes.Assistance]} An assistance service instance
|
|
53
|
+
*/
|
|
54
|
+
static getAssistanceService(provider, logger, options) {
|
|
55
|
+
return this.getService(LLMService_typedefs_1.LLMPurposes.Assistance, provider, logger, options);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Gets a speech-to-text service for the specified LLM provider
|
|
59
|
+
*
|
|
60
|
+
* @static
|
|
61
|
+
* @template Provider - The LLM provider type
|
|
62
|
+
* @param {Provider} provider - The LLM provider (OpenAI, GoogleGenerativeAI)
|
|
63
|
+
* @param {Logger} logger - Logger instance
|
|
64
|
+
* @param {LLMInstanceOptions[Provider]} options - Provider-specific options
|
|
65
|
+
* @returns {LLMServiceByPurpose<Provider>[LLMPurposes.SpeechToText]} A speech-to-text service instance
|
|
66
|
+
*/
|
|
67
|
+
static getSpeechToTextService(provider, logger, options) {
|
|
68
|
+
return this.getService(LLMService_typedefs_1.LLMPurposes.SpeechToText, provider, logger, options);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Gets a text-to-speech service for the specified LLM provider
|
|
72
|
+
*
|
|
73
|
+
* @static
|
|
74
|
+
* @template Provider - The LLM provider type
|
|
75
|
+
* @param {Provider} provider - The LLM provider (OpenAI, GoogleGenerativeAI)
|
|
76
|
+
* @param {Logger} logger - Logger instance
|
|
77
|
+
* @param {LLMInstanceOptions[Provider]} options - Provider-specific options
|
|
78
|
+
* @returns {LLMServiceByPurpose<Provider>[LLMPurposes.TextToSpeech]} A text-to-speech service instance
|
|
79
|
+
*/
|
|
80
|
+
static getTextToSpeechService(provider, logger, options) {
|
|
81
|
+
return this.getService(LLMService_typedefs_1.LLMPurposes.TextToSpeech, provider, logger, options);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Resolves the options for the specified provider from an options map
|
|
85
|
+
*
|
|
86
|
+
* @static
|
|
87
|
+
* @template Provider - The LLM provider type
|
|
88
|
+
* @param {Provider} provider - The LLM provider to get options for
|
|
89
|
+
* @param {Record<Provider, LLMInstanceOptions[Provider]>} options - Map of provider options
|
|
90
|
+
* @returns {LLMInstanceOptions[Provider]} The options for the specified provider
|
|
91
|
+
*/
|
|
92
|
+
static resolveProviderOptions(provider, options) {
|
|
93
|
+
return options[provider];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
exports.LLMServiceFactory = LLMServiceFactory;
|
|
97
|
+
//# sourceMappingURL=LLMService.factory.js.map
|
|
@@ -1 +1,60 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LLMUploadFileMimeTypes = exports.LLMMessageContentType = exports.LLMRoles = exports.LLMPurposes = exports.LLMProviders = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Enum of supported LLM providers.
|
|
6
|
+
*/
|
|
7
|
+
var LLMProviders;
|
|
8
|
+
(function (LLMProviders) {
|
|
9
|
+
LLMProviders["OpenAI"] = "OpenAI";
|
|
10
|
+
LLMProviders["GoogleGenerativeAI"] = "GoogleGenerativeAI";
|
|
11
|
+
})(LLMProviders || (exports.LLMProviders = LLMProviders = {}));
|
|
12
|
+
/**
|
|
13
|
+
* Supported service purposes/types that the LLM gateway provides.
|
|
14
|
+
*/
|
|
15
|
+
var LLMPurposes;
|
|
16
|
+
(function (LLMPurposes) {
|
|
17
|
+
LLMPurposes["Completion"] = "completion";
|
|
18
|
+
LLMPurposes["Assistance"] = "assistance";
|
|
19
|
+
LLMPurposes["SpeechToText"] = "speech_to_text";
|
|
20
|
+
LLMPurposes["TextToSpeech"] = "text_to_speech";
|
|
21
|
+
})(LLMPurposes || (exports.LLMPurposes = LLMPurposes = {}));
|
|
22
|
+
/**
|
|
23
|
+
* Roles that can be assigned to messages in an LLM conversation.
|
|
24
|
+
*/
|
|
25
|
+
var LLMRoles;
|
|
26
|
+
(function (LLMRoles) {
|
|
27
|
+
LLMRoles["User"] = "user";
|
|
28
|
+
LLMRoles["Assistant"] = "assistant";
|
|
29
|
+
})(LLMRoles || (exports.LLMRoles = LLMRoles = {}));
|
|
30
|
+
/**
|
|
31
|
+
* Possible content types for messages in an LLM conversation.
|
|
32
|
+
*/
|
|
33
|
+
var LLMMessageContentType;
|
|
34
|
+
(function (LLMMessageContentType) {
|
|
35
|
+
LLMMessageContentType["TEXT"] = "text";
|
|
36
|
+
LLMMessageContentType["IMAGE_URL"] = "image_url";
|
|
37
|
+
LLMMessageContentType["IMAGE_FILE"] = "image_file";
|
|
38
|
+
LLMMessageContentType["AUDIO_FILE"] = "audio_file";
|
|
39
|
+
})(LLMMessageContentType || (exports.LLMMessageContentType = LLMMessageContentType = {}));
|
|
40
|
+
/**
|
|
41
|
+
* Supported MIME types for files that can be uploaded to LLM services.
|
|
42
|
+
*/
|
|
43
|
+
var LLMUploadFileMimeTypes;
|
|
44
|
+
(function (LLMUploadFileMimeTypes) {
|
|
45
|
+
LLMUploadFileMimeTypes["IMAGE_PNG"] = "image/png";
|
|
46
|
+
LLMUploadFileMimeTypes["IMAGE_JPEG"] = "image/jpeg";
|
|
47
|
+
LLMUploadFileMimeTypes["IMAGE_JPG"] = "image/jpg";
|
|
48
|
+
LLMUploadFileMimeTypes["IMAGE_GIF"] = "image/gif";
|
|
49
|
+
LLMUploadFileMimeTypes["IMAGE_WEBP"] = "image/webp";
|
|
50
|
+
LLMUploadFileMimeTypes["IMAGE_SVG_XML"] = "image/svg+xml";
|
|
51
|
+
LLMUploadFileMimeTypes["IMAGE_BMP"] = "image/bmp";
|
|
52
|
+
LLMUploadFileMimeTypes["PLAIN_TEXT"] = "text/plain";
|
|
53
|
+
LLMUploadFileMimeTypes["MARKDOWN"] = "text/markdown";
|
|
54
|
+
LLMUploadFileMimeTypes["AUDIO_MP3"] = "audio/mp3";
|
|
55
|
+
LLMUploadFileMimeTypes["AUDIO_MPEG"] = "audio/mpeg";
|
|
56
|
+
LLMUploadFileMimeTypes["AUDIO_WAV"] = "audio/wav";
|
|
57
|
+
LLMUploadFileMimeTypes["AUDIO_WEBM"] = "audio/webm";
|
|
58
|
+
LLMUploadFileMimeTypes["AUDIO_OGG"] = "audio/ogg";
|
|
59
|
+
})(LLMUploadFileMimeTypes || (exports.LLMUploadFileMimeTypes = LLMUploadFileMimeTypes = {}));
|
|
60
|
+
//# sourceMappingURL=LLMService.typedefs.js.map
|
package/dist/functional.utils.js
CHANGED
|
@@ -1 +1,9 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.pick = void 0;
|
|
4
|
+
const pick = (obj, keys) => (keys.reduce((acc, key) => {
|
|
5
|
+
Object.assign(acc, { [key]: obj[key] });
|
|
6
|
+
return acc;
|
|
7
|
+
}, {}));
|
|
8
|
+
exports.pick = pick;
|
|
9
|
+
//# sourceMappingURL=functional.utils.js.map
|
package/dist/index.js
CHANGED
|
@@ -1 +1,23 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./LLMService.constants"), exports);
|
|
18
|
+
__exportStar(require("./LLMService.factory"), exports);
|
|
19
|
+
__exportStar(require("./LLMService.typedefs"), exports);
|
|
20
|
+
__exportStar(require("./providers"), exports);
|
|
21
|
+
__exportStar(require("./services"), exports);
|
|
22
|
+
__exportStar(require("./promptBuilder"), exports);
|
|
23
|
+
//# sourceMappingURL=index.js.map
|
package/dist/promptBuilder.js
CHANGED
|
@@ -1 +1,75 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Utility types and functions for handling prompts with dynamic replacements and conditional sections.
|
|
4
|
+
* This allows for type-safe string templates with placeholders that can be replaced with actual values.
|
|
5
|
+
* The `createPromptTemplate` function helps create typed prompts with automatic extraction of replacement keys.
|
|
6
|
+
*
|
|
7
|
+
* @module promptBuilder
|
|
8
|
+
* @example
|
|
9
|
+
* // Example usage:
|
|
10
|
+
* const PROMPTS = {
|
|
11
|
+
* welcomeMessage: createPromptTemplate(`
|
|
12
|
+
* Generate a welcome message for a user who has just started their auto tech check attempt on {{topicTitle}}.
|
|
13
|
+
* {{#hasBonus}}
|
|
14
|
+
* This topic includes bonus content: {{bonusContent}}
|
|
15
|
+
* {{/hasBonus}}
|
|
16
|
+
*
|
|
17
|
+
* {{#valueAsCondition}}
|
|
18
|
+
* This is a conditional section that will only appear if the value is truthy.
|
|
19
|
+
* Value can be used inside the section: {{valueAsCondition}}
|
|
20
|
+
* {{/valueAsCondition}}
|
|
21
|
+
* `),
|
|
22
|
+
* };
|
|
23
|
+
*
|
|
24
|
+
* const instruction = PROMPTS.welcomeMessage({
|
|
25
|
+
* topicTitle: 'JavaScript Basics',
|
|
26
|
+
* hasBonus: true,
|
|
27
|
+
* bonusContent: 'Advanced debugging techniques',
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* // This will generate a prompt:
|
|
31
|
+
* // "Generate a welcome message for a user who has just started their auto tech check attempt on JavaScript Basics.
|
|
32
|
+
* // This topic includes bonus content: Advanced debugging techniques"
|
|
33
|
+
*/
|
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
exports.createPromptTemplate = createPromptTemplate;
|
|
36
|
+
function promptBuilder(prompt, variables) {
|
|
37
|
+
if (!variables) {
|
|
38
|
+
return prompt;
|
|
39
|
+
}
|
|
40
|
+
const replaceVariables = (source) => (
|
|
41
|
+
// Replace all occurrences of the variable in the source string
|
|
42
|
+
Object.entries(variables).reduce((acc, [key, value]) => (acc.replace(new RegExp(`{{\\s*${key}\\s*}}`, 'g'), String(value))), source));
|
|
43
|
+
const processConditionalSections = (source) => {
|
|
44
|
+
// Process conditional sections recursively to handle nested sections
|
|
45
|
+
return source.replace(/{{#(\w+)}}([\s\S]*?){{\/\1}}/g, (_, sectionName, content) => {
|
|
46
|
+
const value = variables[sectionName];
|
|
47
|
+
if (value) {
|
|
48
|
+
// Recursively process nested conditional sections and then replace variables
|
|
49
|
+
const processedContent = processConditionalSections(content);
|
|
50
|
+
return replaceVariables(processedContent);
|
|
51
|
+
}
|
|
52
|
+
return '';
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
let result = prompt;
|
|
56
|
+
// Process conditional sections first (with recursive handling for nested sections)
|
|
57
|
+
result = processConditionalSections(result);
|
|
58
|
+
// Then process regular variable replacements
|
|
59
|
+
result = replaceVariables(result);
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Creates a prompt template function that can be used to generate prompts with dynamic replacements and conditional sections.
|
|
64
|
+
* @template T The type of the template string. Inferred from the provided template.
|
|
65
|
+
* @param {T} template The template string with placeholders:
|
|
66
|
+
* - for dynamic values (e.g., `{{topicTitle}}`)
|
|
67
|
+
* - for conditional sections (e.g., `{{#section}}...{{/section}}`)
|
|
68
|
+
* - for sections that use conditional variables as values (e.g., `{{#bonus}}...{{bonus}}...{{/bonus}}`)
|
|
69
|
+
* @returns A function that takes the dynamic values and returns the generated prompt.
|
|
70
|
+
*/
|
|
71
|
+
function createPromptTemplate(template) {
|
|
72
|
+
const trimmedTemplate = template.trim();
|
|
73
|
+
return (...args) => promptBuilder(trimmedTemplate, ...args);
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=promptBuilder.js.map
|
|
@@ -1 +1,119 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GOOGLE_AI_SERVICE_BUILDERS = exports.GOOGLE_AI_MODELS = exports.MIN_CACHE_CONTENT_LENGTH = void 0;
|
|
4
|
+
const LLMService_typedefs_1 = require("../../LLMService.typedefs");
|
|
5
|
+
const GoogleGenerativeAI_typedefs_1 = require("./GoogleGenerativeAI.typedefs");
|
|
6
|
+
const services_1 = require("../../providers/GoogleGenerativeAI/services");
|
|
7
|
+
const functional_utils_1 = require("../../functional.utils");
|
|
8
|
+
exports.MIN_CACHE_CONTENT_LENGTH = 32_768;
|
|
9
|
+
const GOOGLE_AI_AVAILABLE_MODELS = {
|
|
10
|
+
[GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_1_5_FLASH]: {
|
|
11
|
+
name: GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_1_5_FLASH,
|
|
12
|
+
limits: {
|
|
13
|
+
maxInputTokens: 1_048_576,
|
|
14
|
+
maxOutputTokens: 8_192,
|
|
15
|
+
},
|
|
16
|
+
config: {
|
|
17
|
+
temperature: 0.2,
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
[GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_1_5_PRO]: {
|
|
21
|
+
name: GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_1_5_PRO,
|
|
22
|
+
limits: {
|
|
23
|
+
maxInputTokens: 2_097_152,
|
|
24
|
+
maxOutputTokens: 8_192,
|
|
25
|
+
},
|
|
26
|
+
config: {
|
|
27
|
+
temperature: 0.2,
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
[GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_1_5_PRO_STABLE]: {
|
|
31
|
+
name: GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_1_5_PRO_STABLE,
|
|
32
|
+
limits: {
|
|
33
|
+
maxInputTokens: 2_097_152,
|
|
34
|
+
maxOutputTokens: 8_192,
|
|
35
|
+
},
|
|
36
|
+
config: {
|
|
37
|
+
temperature: 0.2,
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
[GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_2_0_FLASH]: {
|
|
41
|
+
name: GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_2_0_FLASH,
|
|
42
|
+
limits: {
|
|
43
|
+
maxInputTokens: 1_048_576,
|
|
44
|
+
maxOutputTokens: 8_192,
|
|
45
|
+
},
|
|
46
|
+
config: {
|
|
47
|
+
temperature: 0.2,
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
[GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_2_0_FLASH_STABLE]: {
|
|
51
|
+
name: GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_2_0_FLASH_STABLE,
|
|
52
|
+
limits: {
|
|
53
|
+
maxInputTokens: 1_048_576,
|
|
54
|
+
maxOutputTokens: 8_192,
|
|
55
|
+
},
|
|
56
|
+
config: {
|
|
57
|
+
temperature: 0.2,
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
[GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_2_5_PRO]: {
|
|
61
|
+
name: GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_2_5_PRO,
|
|
62
|
+
limits: {
|
|
63
|
+
maxInputTokens: 1_048_576,
|
|
64
|
+
maxOutputTokens: 65_536,
|
|
65
|
+
},
|
|
66
|
+
config: {
|
|
67
|
+
temperature: 0.2,
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
[GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_2_5_FLASH_PREVIEW_TTS]: {
|
|
71
|
+
name: GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_2_5_FLASH_PREVIEW_TTS,
|
|
72
|
+
limits: {
|
|
73
|
+
maxInputTokens: 8_000,
|
|
74
|
+
maxOutputTokens: 16_000,
|
|
75
|
+
},
|
|
76
|
+
config: {
|
|
77
|
+
temperature: 0.2,
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
[GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_2_5_PRO_PREVIEW_TTS]: {
|
|
81
|
+
name: GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_2_5_PRO_PREVIEW_TTS,
|
|
82
|
+
limits: {
|
|
83
|
+
maxInputTokens: 8_000,
|
|
84
|
+
maxOutputTokens: 16_000,
|
|
85
|
+
},
|
|
86
|
+
config: {
|
|
87
|
+
temperature: 0.2,
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
exports.GOOGLE_AI_MODELS = {
|
|
92
|
+
[LLMService_typedefs_1.LLMPurposes.Completion]: (0, functional_utils_1.pick)(GOOGLE_AI_AVAILABLE_MODELS, [
|
|
93
|
+
GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_1_5_FLASH,
|
|
94
|
+
GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_1_5_PRO,
|
|
95
|
+
GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_2_0_FLASH,
|
|
96
|
+
GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_2_5_PRO,
|
|
97
|
+
]),
|
|
98
|
+
// Stable models are used for assistance as they support additional methods
|
|
99
|
+
[LLMService_typedefs_1.LLMPurposes.Assistance]: (0, functional_utils_1.pick)(GOOGLE_AI_AVAILABLE_MODELS, [
|
|
100
|
+
GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_1_5_PRO_STABLE,
|
|
101
|
+
GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_2_0_FLASH_STABLE,
|
|
102
|
+
GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_2_5_PRO,
|
|
103
|
+
]),
|
|
104
|
+
[LLMService_typedefs_1.LLMPurposes.SpeechToText]: (0, functional_utils_1.pick)(GOOGLE_AI_AVAILABLE_MODELS, [
|
|
105
|
+
GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_2_0_FLASH,
|
|
106
|
+
GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_2_5_PRO,
|
|
107
|
+
]),
|
|
108
|
+
[LLMService_typedefs_1.LLMPurposes.TextToSpeech]: (0, functional_utils_1.pick)(GOOGLE_AI_AVAILABLE_MODELS, [
|
|
109
|
+
GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_2_5_FLASH_PREVIEW_TTS,
|
|
110
|
+
GoogleGenerativeAI_typedefs_1.GoogleGenerativeAIModelNames.GEMINI_2_5_PRO_PREVIEW_TTS,
|
|
111
|
+
]),
|
|
112
|
+
};
|
|
113
|
+
exports.GOOGLE_AI_SERVICE_BUILDERS = {
|
|
114
|
+
[LLMService_typedefs_1.LLMPurposes.Completion]: (logger, options) => (new services_1.GoogleGenerativeAICompletionService(logger, options)),
|
|
115
|
+
[LLMService_typedefs_1.LLMPurposes.Assistance]: (logger, options) => (new services_1.GoogleGenerativeAIAssistanceService(logger, options)),
|
|
116
|
+
[LLMService_typedefs_1.LLMPurposes.SpeechToText]: (logger, options) => (new services_1.GoogleGenerativeAISpeechToTextService(logger, options)),
|
|
117
|
+
[LLMService_typedefs_1.LLMPurposes.TextToSpeech]: (logger, options) => (new services_1.GoogleGenerativeAITextToSpeechService(logger, options)),
|
|
118
|
+
};
|
|
119
|
+
//# sourceMappingURL=GoogleGenerativeAI.constants.js.map
|
|
@@ -1 +1,168 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.GoogleGenerativeAIEntity = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const LLMService_typedefs_1 = require("../../LLMService.typedefs");
|
|
9
|
+
class GoogleGenerativeAIEntity {
|
|
10
|
+
static getContentParameters(options) {
|
|
11
|
+
const { model, messages, instructions, abortSignal, } = options;
|
|
12
|
+
return {
|
|
13
|
+
model: model.name,
|
|
14
|
+
contents: messages,
|
|
15
|
+
config: {
|
|
16
|
+
systemInstruction: instructions,
|
|
17
|
+
temperature: model.config.temperature,
|
|
18
|
+
abortSignal,
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
static getFileDataParts(uploadedFiles) {
|
|
23
|
+
return uploadedFiles.map((file) => ({
|
|
24
|
+
fileData: {
|
|
25
|
+
fileUri: file.path,
|
|
26
|
+
mimeType: file.mimeType,
|
|
27
|
+
},
|
|
28
|
+
}));
|
|
29
|
+
}
|
|
30
|
+
static getTextParts(messages) {
|
|
31
|
+
return messages.map((message) => ({ text: message }));
|
|
32
|
+
}
|
|
33
|
+
static async getContent(message, instance, logger) {
|
|
34
|
+
const role = message.role === LLMService_typedefs_1.LLMRoles.Assistant
|
|
35
|
+
? 'model'
|
|
36
|
+
: message.role;
|
|
37
|
+
const parts = [];
|
|
38
|
+
for (const contentItem of message.content) {
|
|
39
|
+
switch (contentItem.type) {
|
|
40
|
+
case LLMService_typedefs_1.LLMMessageContentType.TEXT:
|
|
41
|
+
parts.push({
|
|
42
|
+
text: contentItem.text,
|
|
43
|
+
});
|
|
44
|
+
break;
|
|
45
|
+
case LLMService_typedefs_1.LLMMessageContentType.IMAGE_URL: {
|
|
46
|
+
const head = await axios_1.default.head(contentItem.image_url.url);
|
|
47
|
+
const mimeType = head.headers['content-type']?.split(';')[0]
|
|
48
|
+
?? LLMService_typedefs_1.LLMUploadFileMimeTypes.IMAGE_PNG;
|
|
49
|
+
const response = await axios_1.default.get(contentItem.image_url.url, {
|
|
50
|
+
responseType: 'arraybuffer',
|
|
51
|
+
});
|
|
52
|
+
const fileData = new Uint8Array(response.data);
|
|
53
|
+
const blob = new Blob([fileData], { type: mimeType });
|
|
54
|
+
const uploadedFile = await instance.files.upload({
|
|
55
|
+
file: blob,
|
|
56
|
+
});
|
|
57
|
+
parts.push({
|
|
58
|
+
fileData: {
|
|
59
|
+
fileUri: uploadedFile.uri,
|
|
60
|
+
mimeType,
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
case LLMService_typedefs_1.LLMMessageContentType.IMAGE_FILE:
|
|
66
|
+
parts.push({
|
|
67
|
+
fileData: {
|
|
68
|
+
fileUri: contentItem.image_file.path,
|
|
69
|
+
mimeType: contentItem.image_file.mime_type,
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
break;
|
|
73
|
+
default:
|
|
74
|
+
logger
|
|
75
|
+
.child('GoogleGenerativeAIEntity.getContent')
|
|
76
|
+
.warn('Unsupported content type in message item', {
|
|
77
|
+
contentItem,
|
|
78
|
+
});
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if ('attachments' in message
|
|
83
|
+
&& message.attachments) {
|
|
84
|
+
parts.push(...this.getFileDataParts(message.attachments));
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
role,
|
|
88
|
+
parts,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
static getCountTokensFn(instance) {
|
|
92
|
+
const isValidContent = (content) => Boolean(content
|
|
93
|
+
&& typeof content === 'object'
|
|
94
|
+
&& 'role' in content
|
|
95
|
+
&& 'parts' in content);
|
|
96
|
+
return async (inputContents, model) => {
|
|
97
|
+
if (!inputContents) {
|
|
98
|
+
return 0;
|
|
99
|
+
}
|
|
100
|
+
const contentsArray = [];
|
|
101
|
+
if (Array.isArray(inputContents)) {
|
|
102
|
+
contentsArray.push(...inputContents.filter(isValidContent));
|
|
103
|
+
}
|
|
104
|
+
else if (isValidContent(inputContents)) {
|
|
105
|
+
contentsArray.push(inputContents);
|
|
106
|
+
}
|
|
107
|
+
const structurallyValidContents = contentsArray.filter((content) => (content
|
|
108
|
+
&& typeof content.role === 'string'
|
|
109
|
+
&& Array.isArray(content.parts)));
|
|
110
|
+
if (structurallyValidContents.length === 0) {
|
|
111
|
+
return 0;
|
|
112
|
+
}
|
|
113
|
+
const { totalTokens } = await instance.models.countTokens({
|
|
114
|
+
model: model.name,
|
|
115
|
+
contents: structurallyValidContents,
|
|
116
|
+
});
|
|
117
|
+
return totalTokens ?? 0;
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
static async combineMessagesWithLimit({ modelContext = [], history = [], message, model, countTokensFn, instance, logger, }) {
|
|
121
|
+
const modelContextContent = await Promise.all(modelContext.map((msgCtx) => GoogleGenerativeAIEntity.getContent(msgCtx, instance, logger)));
|
|
122
|
+
const historyContent = await Promise.all(history.map((histMsg) => GoogleGenerativeAIEntity.getContent(histMsg, instance, logger)));
|
|
123
|
+
const currentMessageAsLLMCompletionMessage = {
|
|
124
|
+
role: LLMService_typedefs_1.LLMRoles.User,
|
|
125
|
+
content: message.content,
|
|
126
|
+
};
|
|
127
|
+
const currentMessageContent = await GoogleGenerativeAIEntity
|
|
128
|
+
.getContent(currentMessageAsLLMCompletionMessage, instance, logger);
|
|
129
|
+
const finalMessages = [
|
|
130
|
+
...modelContextContent,
|
|
131
|
+
currentMessageContent,
|
|
132
|
+
];
|
|
133
|
+
const messagesToConsiderForAddition = [
|
|
134
|
+
...historyContent,
|
|
135
|
+
].reverse();
|
|
136
|
+
let currentTokenCount = await countTokensFn(finalMessages, model) ?? 0;
|
|
137
|
+
let messagesAdded = 0;
|
|
138
|
+
for (const contentMsg of messagesToConsiderForAddition) {
|
|
139
|
+
const messageTokenCount = await countTokensFn(contentMsg, model) ?? 0;
|
|
140
|
+
if (currentTokenCount + messageTokenCount <= model.limits.maxInputTokens) {
|
|
141
|
+
messagesAdded += 1;
|
|
142
|
+
finalMessages.splice(-messagesAdded, 0, contentMsg);
|
|
143
|
+
currentTokenCount += messageTokenCount;
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return finalMessages;
|
|
150
|
+
}
|
|
151
|
+
static get transcribeLimits() {
|
|
152
|
+
return {
|
|
153
|
+
maxFileSize: 2 * (1024 ** 3), // 2 GB
|
|
154
|
+
supportedMimeTypes: [
|
|
155
|
+
LLMService_typedefs_1.LLMUploadFileMimeTypes.AUDIO_WAV,
|
|
156
|
+
LLMService_typedefs_1.LLMUploadFileMimeTypes.AUDIO_MP3,
|
|
157
|
+
LLMService_typedefs_1.LLMUploadFileMimeTypes.AUDIO_MPEG,
|
|
158
|
+
LLMService_typedefs_1.LLMUploadFileMimeTypes.AUDIO_WEBM,
|
|
159
|
+
LLMService_typedefs_1.LLMUploadFileMimeTypes.AUDIO_OGG,
|
|
160
|
+
],
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
static get transcribeInstructions() {
|
|
164
|
+
return 'Transcribe the audio file to text.';
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
exports.GoogleGenerativeAIEntity = GoogleGenerativeAIEntity;
|
|
168
|
+
//# sourceMappingURL=GoogleGenerativeAI.entity.js.map
|
|
@@ -1 +1,20 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GoogleGenerativeAIRoles = exports.GoogleGenerativeAIModelNames = void 0;
|
|
4
|
+
var GoogleGenerativeAIModelNames;
|
|
5
|
+
(function (GoogleGenerativeAIModelNames) {
|
|
6
|
+
GoogleGenerativeAIModelNames["GEMINI_1_5_FLASH"] = "gemini-1.5-flash";
|
|
7
|
+
GoogleGenerativeAIModelNames["GEMINI_1_5_PRO"] = "gemini-1.5-pro";
|
|
8
|
+
GoogleGenerativeAIModelNames["GEMINI_1_5_PRO_STABLE"] = "gemini-1.5-pro-002";
|
|
9
|
+
GoogleGenerativeAIModelNames["GEMINI_2_0_FLASH"] = "gemini-2.0-flash";
|
|
10
|
+
GoogleGenerativeAIModelNames["GEMINI_2_0_FLASH_STABLE"] = "gemini-2.0-flash-001";
|
|
11
|
+
GoogleGenerativeAIModelNames["GEMINI_2_5_PRO"] = "gemini-2.5-pro";
|
|
12
|
+
GoogleGenerativeAIModelNames["GEMINI_2_5_FLASH_PREVIEW_TTS"] = "gemini-2.5-flash-preview-tts";
|
|
13
|
+
GoogleGenerativeAIModelNames["GEMINI_2_5_PRO_PREVIEW_TTS"] = "gemini-2.5-pro-preview-tts";
|
|
14
|
+
})(GoogleGenerativeAIModelNames || (exports.GoogleGenerativeAIModelNames = GoogleGenerativeAIModelNames = {}));
|
|
15
|
+
var GoogleGenerativeAIRoles;
|
|
16
|
+
(function (GoogleGenerativeAIRoles) {
|
|
17
|
+
GoogleGenerativeAIRoles["User"] = "user";
|
|
18
|
+
GoogleGenerativeAIRoles["Model"] = "model";
|
|
19
|
+
})(GoogleGenerativeAIRoles || (exports.GoogleGenerativeAIRoles = GoogleGenerativeAIRoles = {}));
|
|
20
|
+
//# sourceMappingURL=GoogleGenerativeAI.typedefs.js.map
|
|
@@ -1 +1,17 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GoogleGenerativeAIServiceFactory = void 0;
|
|
4
|
+
const LLMService_typedefs_1 = require("../../LLMService.typedefs");
|
|
5
|
+
const services_1 = require("../../services");
|
|
6
|
+
const GoogleGenerativeAI_constants_1 = require("../../providers/GoogleGenerativeAI/GoogleGenerativeAI.constants");
|
|
7
|
+
class GoogleGenerativeAIServiceFactory extends services_1.LLMServicePurposeFactory {
|
|
8
|
+
createService(purpose, logger, options) {
|
|
9
|
+
const serviceBuilder = GoogleGenerativeAI_constants_1.GOOGLE_AI_SERVICE_BUILDERS[purpose];
|
|
10
|
+
if (!serviceBuilder) {
|
|
11
|
+
throw new Error(`Purpose [${purpose}] is not supported for [${LLMService_typedefs_1.LLMProviders.GoogleGenerativeAI}] service`);
|
|
12
|
+
}
|
|
13
|
+
return serviceBuilder(logger, options);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.GoogleGenerativeAIServiceFactory = GoogleGenerativeAIServiceFactory;
|
|
17
|
+
//# sourceMappingURL=GoogleGenerativeAIService.factory.js.map
|