@mate-academy/llm-gateway 0.0.1-canary.1 → 1.0.0
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/providers/GoogleGenerativeAI/index.d.ts +5 -0
- package/dist/providers/GoogleGenerativeAI/index.js +22 -0
- package/dist/providers/GoogleGenerativeAI/index.js.map +1 -0
- package/dist/providers/OpenAI/index.d.ts +5 -0
- package/dist/providers/OpenAI/index.js +22 -0
- package/dist/providers/OpenAI/index.js.map +1 -0
- package/dist/providers/index.d.ts +2 -2
- package/dist/providers/index.js +2 -2
- package/dist/providers/index.js.map +1 -1
- package/package.json +25 -2
- package/.lintstagedrc +0 -5
- package/.nvmrc +0 -1
- package/eslint.config.mjs +0 -39
- package/src/LLMService.constants.ts +0 -19
- package/src/LLMService.entity.ts +0 -71
- package/src/LLMService.factory.ts +0 -57
- package/src/LLMService.typedefs.ts +0 -174
- package/src/functional.utils.ts +0 -7
- package/src/index.ts +0 -6
- package/src/providers/GoogleGenerativeAI/GoogleGenerativeAI.constants.ts +0 -127
- package/src/providers/GoogleGenerativeAI/GoogleGenerativeAI.entity.ts +0 -67
- package/src/providers/GoogleGenerativeAI/GoogleGenerativeAI.typedefs.ts +0 -14
- package/src/providers/GoogleGenerativeAI/GoogleGenerativeAIService.factory.ts +0 -29
- package/src/providers/GoogleGenerativeAI/services/GoogleGenerativeAIAssistance.service.ts +0 -307
- package/src/providers/GoogleGenerativeAI/services/GoogleGenerativeAICompletion.service.ts +0 -79
- package/src/providers/GoogleGenerativeAI/services/index.ts +0 -2
- package/src/providers/OpenAI/OpenAI.constants.ts +0 -71
- package/src/providers/OpenAI/OpenAI.entity.ts +0 -17
- package/src/providers/OpenAI/OpenAI.typedefs.ts +0 -9
- package/src/providers/OpenAI/OpenAIService.factory.ts +0 -29
- package/src/providers/OpenAI/services/OpenAIAssistance.service.ts +0 -304
- package/src/providers/OpenAI/services/OpenAICompletion.service.ts +0 -74
- package/src/providers/OpenAI/services/index.ts +0 -2
- package/src/providers/index.ts +0 -2
- package/src/services/LLMAssistanceService.abstract.ts +0 -73
- package/src/services/LLMBaseService.abstract.ts +0 -34
- package/src/services/LLMCompletionService.abstract.ts +0 -32
- package/src/services/LLMServicePurposeFactory.abstract.ts +0 -15
- package/src/services/index.ts +0 -4
- package/tsconfig.json +0 -23
|
@@ -0,0 +1,22 @@
|
|
|
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("./GoogleGenerativeAI.constants"), exports);
|
|
18
|
+
__exportStar(require("./GoogleGenerativeAI.typedefs"), exports);
|
|
19
|
+
__exportStar(require("./GoogleGenerativeAI.entity"), exports);
|
|
20
|
+
__exportStar(require("./GoogleGenerativeAIService.factory"), exports);
|
|
21
|
+
__exportStar(require("./services"), exports);
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/providers/GoogleGenerativeAI/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iEAA+C;AAC/C,gEAA8C;AAC9C,8DAA4C;AAC5C,sEAAoD;AACpD,6CAA2B"}
|
|
@@ -0,0 +1,22 @@
|
|
|
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("./OpenAI.constants"), exports);
|
|
18
|
+
__exportStar(require("./OpenAI.typedefs"), exports);
|
|
19
|
+
__exportStar(require("./OpenAI.entity"), exports);
|
|
20
|
+
__exportStar(require("./OpenAIService.factory"), exports);
|
|
21
|
+
__exportStar(require("./services"), exports);
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/providers/OpenAI/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAmC;AACnC,oDAAkC;AAClC,kDAAgC;AAChC,0DAAwC;AACxC,6CAA2B"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './OpenAI
|
|
2
|
-
export * from './GoogleGenerativeAI
|
|
1
|
+
export * from './OpenAI';
|
|
2
|
+
export * from './GoogleGenerativeAI';
|
package/dist/providers/index.js
CHANGED
|
@@ -14,6 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./OpenAI
|
|
18
|
-
__exportStar(require("./GoogleGenerativeAI
|
|
17
|
+
__exportStar(require("./OpenAI"), exports);
|
|
18
|
+
__exportStar(require("./GoogleGenerativeAI"), exports);
|
|
19
19
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,uDAAqC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mate-academy/llm-gateway",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "A gateway package for LLM services.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -16,8 +16,31 @@
|
|
|
16
16
|
"major": "npm version major && npm publish",
|
|
17
17
|
"canary": "npm version prerelease --preid=canary && npm publish --tag=canary"
|
|
18
18
|
},
|
|
19
|
-
"author": "",
|
|
19
|
+
"author": "Mate academy developers, Sergii Nosachenko",
|
|
20
20
|
"license": "MIT",
|
|
21
|
+
"files": [
|
|
22
|
+
"dist/**/*",
|
|
23
|
+
"README.md"
|
|
24
|
+
],
|
|
25
|
+
"typesVersions": {
|
|
26
|
+
"*": {
|
|
27
|
+
".": [
|
|
28
|
+
"dist/index.d.ts"
|
|
29
|
+
],
|
|
30
|
+
"providers": [
|
|
31
|
+
"dist/providers/index.d.ts"
|
|
32
|
+
],
|
|
33
|
+
"providers/*": [
|
|
34
|
+
"dist/providers/*"
|
|
35
|
+
],
|
|
36
|
+
"services": [
|
|
37
|
+
"dist/services/index.d.ts"
|
|
38
|
+
],
|
|
39
|
+
"services/*": [
|
|
40
|
+
"dist/services/*"
|
|
41
|
+
]
|
|
42
|
+
}
|
|
43
|
+
},
|
|
21
44
|
"devDependencies": {
|
|
22
45
|
"@mate-academy/ts-config": "^1.0.5",
|
|
23
46
|
"@typescript-eslint/eslint-plugin": "^8.32.0",
|
package/.lintstagedrc
DELETED
package/.nvmrc
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
v20
|
package/eslint.config.mjs
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import eslint from '@eslint/js';
|
|
2
|
-
import tseslint from 'typescript-eslint';
|
|
3
|
-
import eslintPlugin from '@typescript-eslint/eslint-plugin';
|
|
4
|
-
|
|
5
|
-
export default tseslint.config(
|
|
6
|
-
{
|
|
7
|
-
files: ['src/**/*.ts'],
|
|
8
|
-
ignores: ['dist', 'node_modules'],
|
|
9
|
-
languageOptions: {
|
|
10
|
-
parser: tseslint.parser,
|
|
11
|
-
parserOptions: {
|
|
12
|
-
project: './tsconfig.json',
|
|
13
|
-
sourceType: 'module',
|
|
14
|
-
tsconfigRootDir: './',
|
|
15
|
-
},
|
|
16
|
-
},
|
|
17
|
-
plugins: {
|
|
18
|
-
'@typescript-eslint': eslintPlugin,
|
|
19
|
-
},
|
|
20
|
-
rules: {
|
|
21
|
-
...eslint.configs.recommended.rules,
|
|
22
|
-
...tseslint.configs.recommended.rules,
|
|
23
|
-
...tseslint.configs.stylistic.rules,
|
|
24
|
-
'@typescript-eslint/no-explicit-any': 'off',
|
|
25
|
-
'no-unused-vars': 'off',
|
|
26
|
-
'@typescript-eslint/no-unused-vars': [
|
|
27
|
-
'error',
|
|
28
|
-
{
|
|
29
|
-
vars: 'all',
|
|
30
|
-
args: 'after-used',
|
|
31
|
-
ignoreRestSiblings: true,
|
|
32
|
-
varsIgnorePattern: '^_',
|
|
33
|
-
argsIgnorePattern: '^_',
|
|
34
|
-
caughtErrors: 'none',
|
|
35
|
-
},
|
|
36
|
-
],
|
|
37
|
-
},
|
|
38
|
-
},
|
|
39
|
-
);
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { LLMProviderModelsByPurpose, LLMProviders, LLMPurposes } from './LLMService.typedefs';
|
|
2
|
-
import { OpenAIServiceFactory, GoogleGenerativeAIServiceFactory } from './providers';
|
|
3
|
-
import { GOOGLE_AI_MODELS } from './providers/GoogleGenerativeAI/GoogleGenerativeAI.constants';
|
|
4
|
-
import { OPEN_AI_MODELS } from './providers/OpenAI/OpenAI.constants';
|
|
5
|
-
import { LLMServicePurposeFactory } from './services/LLMServicePurposeFactory.abstract';
|
|
6
|
-
|
|
7
|
-
export const LLM_SERVICE_FACTORIES: {
|
|
8
|
-
[provider in LLMProviders]: LLMServicePurposeFactory<provider>;
|
|
9
|
-
} = {
|
|
10
|
-
[LLMProviders.OpenAI]: new OpenAIServiceFactory(),
|
|
11
|
-
[LLMProviders.GoogleGenerativeAI]: new GoogleGenerativeAIServiceFactory(),
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export const LLM_SERVICE_MODELS: {
|
|
15
|
-
[provider in LLMProviders]: LLMProviderModelsByPurpose<LLMPurposes, provider>;
|
|
16
|
-
} = {
|
|
17
|
-
[LLMProviders.OpenAI]: OPEN_AI_MODELS,
|
|
18
|
-
[LLMProviders.GoogleGenerativeAI]: GOOGLE_AI_MODELS,
|
|
19
|
-
};
|
package/src/LLMService.entity.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
LLMCountTokensFunction,
|
|
3
|
-
LLMMessage,
|
|
4
|
-
LLMModel,
|
|
5
|
-
LLMProviders,
|
|
6
|
-
LLMRoles,
|
|
7
|
-
} from './LLMService.typedefs';
|
|
8
|
-
|
|
9
|
-
interface CombineMessagesOptions<Provider extends LLMProviders> {
|
|
10
|
-
modelContext?: LLMMessage[];
|
|
11
|
-
history?: LLMMessage[];
|
|
12
|
-
message: string;
|
|
13
|
-
model: LLMModel<Provider>;
|
|
14
|
-
countTokensFn?: LLMCountTokensFunction<Provider>;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export class LLMServiceEntity {
|
|
18
|
-
private static countTokensFallback(messages: string[]): Promise<number> {
|
|
19
|
-
const message = messages.join('');
|
|
20
|
-
const basicTokens = message.split(/[\s,.;!?(){}[\]<>%&]+/).filter(Boolean);
|
|
21
|
-
const subwordTokens = basicTokens.flatMap((token) => (
|
|
22
|
-
token.split(
|
|
23
|
-
/(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|(?<=[a-zA-Z])(?=\d)|(?<=[\d])(?=[a-zA-Z])/,
|
|
24
|
-
)
|
|
25
|
-
));
|
|
26
|
-
|
|
27
|
-
return Promise.resolve(subwordTokens.length);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
static async combineMessagesWithLimit<Provider extends LLMProviders>({
|
|
31
|
-
modelContext = [],
|
|
32
|
-
history = [],
|
|
33
|
-
message,
|
|
34
|
-
model,
|
|
35
|
-
countTokensFn = this.countTokensFallback,
|
|
36
|
-
}: CombineMessagesOptions<Provider>): Promise<LLMMessage[]> {
|
|
37
|
-
const combinedMessages: LLMMessage[] = [...modelContext];
|
|
38
|
-
|
|
39
|
-
if (history[0]) {
|
|
40
|
-
const firstHistoryMessage = history[0];
|
|
41
|
-
|
|
42
|
-
combinedMessages.push(firstHistoryMessage);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const messagesToConsider = [
|
|
46
|
-
{ role: LLMRoles.User, content: message },
|
|
47
|
-
...history.slice(1).reverse(),
|
|
48
|
-
];
|
|
49
|
-
|
|
50
|
-
const messages = combinedMessages.map(
|
|
51
|
-
(combinedMessage) => combinedMessage.content,
|
|
52
|
-
);
|
|
53
|
-
|
|
54
|
-
let tokenCount = await countTokensFn(messages, model);
|
|
55
|
-
|
|
56
|
-
for (const msg of messagesToConsider) {
|
|
57
|
-
const tempTokenCount = tokenCount + (
|
|
58
|
-
await countTokensFn([msg.content], model)
|
|
59
|
-
);
|
|
60
|
-
|
|
61
|
-
if (tempTokenCount <= model.limits.maxInputTokens) {
|
|
62
|
-
combinedMessages.push(msg);
|
|
63
|
-
tokenCount = tempTokenCount;
|
|
64
|
-
} else {
|
|
65
|
-
break;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return combinedMessages;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { Logger } from '@mate-academy/core';
|
|
2
|
-
import {
|
|
3
|
-
LLMInstanceOptions,
|
|
4
|
-
LLMProviders,
|
|
5
|
-
LLMPurposes,
|
|
6
|
-
LLMServiceByPurpose,
|
|
7
|
-
} from './LLMService.typedefs';
|
|
8
|
-
import { LLM_SERVICE_FACTORIES } from './LLMService.constants';
|
|
9
|
-
|
|
10
|
-
export class LLMServiceFactory {
|
|
11
|
-
private static getService<
|
|
12
|
-
Purpose extends LLMPurposes,
|
|
13
|
-
Provider extends LLMProviders,
|
|
14
|
-
>(
|
|
15
|
-
purpose: Purpose,
|
|
16
|
-
provider: Provider,
|
|
17
|
-
logger: Logger,
|
|
18
|
-
options: LLMInstanceOptions[Provider ],
|
|
19
|
-
): LLMServiceByPurpose<Provider>[Purpose] {
|
|
20
|
-
switch (provider) {
|
|
21
|
-
case LLMProviders.OpenAI:
|
|
22
|
-
case LLMProviders.GoogleGenerativeAI:
|
|
23
|
-
return LLM_SERVICE_FACTORIES[provider]
|
|
24
|
-
.createService(
|
|
25
|
-
purpose,
|
|
26
|
-
logger,
|
|
27
|
-
options,
|
|
28
|
-
);
|
|
29
|
-
|
|
30
|
-
default:
|
|
31
|
-
throw new Error(`Provider [${provider}] is not supported`);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
static getCompletionService<Provider extends LLMProviders>(
|
|
36
|
-
provider: Provider,
|
|
37
|
-
logger: Logger,
|
|
38
|
-
options: LLMInstanceOptions[Provider],
|
|
39
|
-
): LLMServiceByPurpose<Provider>[LLMPurposes.Completion] {
|
|
40
|
-
return this.getService(LLMPurposes.Completion, provider, logger, options);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
static getAssistanceService<Provider extends LLMProviders>(
|
|
44
|
-
provider: Provider,
|
|
45
|
-
logger: Logger,
|
|
46
|
-
options: LLMInstanceOptions[Provider],
|
|
47
|
-
): LLMServiceByPurpose<Provider>[LLMPurposes.Assistance] {
|
|
48
|
-
return this.getService(LLMPurposes.Assistance, provider, logger, options);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
static resolveProviderOptions<Provider extends LLMProviders>(
|
|
52
|
-
provider: Provider,
|
|
53
|
-
options: Record<Provider, LLMInstanceOptions[Provider]>,
|
|
54
|
-
): LLMInstanceOptions[Provider] {
|
|
55
|
-
return options[provider];
|
|
56
|
-
}
|
|
57
|
-
}
|
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
import { Logger } from '@mate-academy/core';
|
|
2
|
-
import { ClientOptions, OpenAI } from 'openai';
|
|
3
|
-
import { GoogleGenAI } from '@google/genai';
|
|
4
|
-
import { OpenAIModelNames } from './providers/OpenAI/OpenAI.typedefs';
|
|
5
|
-
import { GoogleGenerativeAIModelNames } from './providers/GoogleGenerativeAI/GoogleGenerativeAI.typedefs';
|
|
6
|
-
import { LLMCompletionService } from './services/LLMCompletionService.abstract';
|
|
7
|
-
import { LLMAssistanceService } from './services/LLMAssistanceService.abstract';
|
|
8
|
-
|
|
9
|
-
export enum LLMProviders {
|
|
10
|
-
OpenAI = 'OpenAI',
|
|
11
|
-
GoogleGenerativeAI = 'GoogleGenerativeAI',
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export type LLMInstances = {
|
|
15
|
-
[LLMProviders.OpenAI]: OpenAI;
|
|
16
|
-
[LLMProviders.GoogleGenerativeAI]: GoogleGenAI;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export type LLMInstanceOptions = {
|
|
20
|
-
[LLMProviders.OpenAI]: ClientOptions;
|
|
21
|
-
[LLMProviders.GoogleGenerativeAI]: { apiKey: string };
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export enum LLMPurposes {
|
|
25
|
-
Completion = 'completion',
|
|
26
|
-
Assistance = 'assistance',
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export type LLMServiceByPurpose<Provider extends LLMProviders> = {
|
|
30
|
-
[LLMPurposes.Completion]: LLMCompletionService<Provider>;
|
|
31
|
-
[LLMPurposes.Assistance]: LLMAssistanceService<Provider>;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
export type LLMServiceBuilder<
|
|
35
|
-
Provider extends LLMProviders,
|
|
36
|
-
Purpose extends LLMPurposes,
|
|
37
|
-
> = (
|
|
38
|
-
logger: Logger,
|
|
39
|
-
options: LLMInstanceOptions[Provider],
|
|
40
|
-
) => LLMServiceByPurpose<Provider>[Purpose];
|
|
41
|
-
|
|
42
|
-
export interface LLMLimits {
|
|
43
|
-
maxInputTokens: number;
|
|
44
|
-
maxOutputTokens: number;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export interface LLMConfig {
|
|
48
|
-
temperature?: number;
|
|
49
|
-
top_p?: number | null;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export type LLMProviderAvailableModels<
|
|
53
|
-
Provider extends LLMProviders
|
|
54
|
-
> = Record<
|
|
55
|
-
LLMModelName[Provider],
|
|
56
|
-
LLMModel<Provider>
|
|
57
|
-
>;
|
|
58
|
-
|
|
59
|
-
export type LLMProviderModelsByPurpose<
|
|
60
|
-
Purpose extends LLMPurposes,
|
|
61
|
-
Provider extends LLMProviders
|
|
62
|
-
> = Record<
|
|
63
|
-
Purpose,
|
|
64
|
-
Partial<LLMProviderAvailableModels<Provider>>
|
|
65
|
-
>;
|
|
66
|
-
|
|
67
|
-
export type LLMModelName = {
|
|
68
|
-
[LLMProviders.OpenAI]: OpenAIModelNames;
|
|
69
|
-
[LLMProviders.GoogleGenerativeAI]: GoogleGenerativeAIModelNames;
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
export interface LLMModel<Provider extends LLMProviders> {
|
|
73
|
-
name: LLMModelName[Provider];
|
|
74
|
-
limits: LLMLimits;
|
|
75
|
-
config: LLMConfig;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export enum LLMRoles {
|
|
79
|
-
User = 'user',
|
|
80
|
-
Assistant = 'assistant',
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export interface LLMMessage {
|
|
84
|
-
role: LLMRoles;
|
|
85
|
-
content: string;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export type LLMRequestError = Error;
|
|
89
|
-
|
|
90
|
-
export type LLMRequestResult<Result> = Result | { error: LLMRequestError };
|
|
91
|
-
|
|
92
|
-
export type LLMCompletionResult = LLMRequestResult<{ text: string }>;
|
|
93
|
-
export interface LLMSendMessageOptions<Provider extends LLMProviders> {
|
|
94
|
-
message: LLMMessage;
|
|
95
|
-
model: LLMModel<Provider>;
|
|
96
|
-
history?: LLMMessage[];
|
|
97
|
-
instructions?: string;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export enum LLMUploadFileMimeTypes {
|
|
101
|
-
PNG = 'image/png',
|
|
102
|
-
PLAIN_TEXT = 'text/plain',
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export interface LLMUploadFile {
|
|
106
|
-
mimeType: LLMUploadFileMimeTypes;
|
|
107
|
-
name: string;
|
|
108
|
-
path: string;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
export interface LLMUploadedFile extends LLMUploadFile {
|
|
112
|
-
fileId: string;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export type LLMUploadFileResult = LLMRequestResult<LLMUploadedFile>;
|
|
116
|
-
|
|
117
|
-
export interface LLMCreateFileStorageOptions<Provider extends LLMProviders> {
|
|
118
|
-
uploadedFiles: LLMUploadedFile[];
|
|
119
|
-
model: LLMModel<Provider>;
|
|
120
|
-
instructions?: string;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export interface LLMCreatedFileStorage {
|
|
124
|
-
storageId: string;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export type LLMCreateFileStorageResult = LLMRequestResult<
|
|
128
|
-
LLMCreatedFileStorage
|
|
129
|
-
>;
|
|
130
|
-
|
|
131
|
-
export interface LLMCreateAssistantOptions<Provider extends LLMProviders> {
|
|
132
|
-
model: LLMModel<Provider>;
|
|
133
|
-
instructions?: string;
|
|
134
|
-
storageIds?: string[];
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
export interface LLMCreatedAssistant {
|
|
138
|
-
assistantId: string;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
export type LLMCreateAssistantResult = LLMRequestResult<LLMCreatedAssistant>;
|
|
142
|
-
|
|
143
|
-
export interface LLMCreateChatOptions<Provider extends LLMProviders> {
|
|
144
|
-
model: LLMModel<Provider>;
|
|
145
|
-
instructions?: string;
|
|
146
|
-
history?: string[];
|
|
147
|
-
files?: LLMUploadedFile[];
|
|
148
|
-
storageId?: string;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export interface LLMCreatedChat {
|
|
152
|
-
chatId: string;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
export type LLMCreateChatResult = LLMRequestResult<LLMCreatedChat>;
|
|
156
|
-
|
|
157
|
-
export interface LLMAssistanceMessage extends LLMMessage {
|
|
158
|
-
attachment?: LLMUploadFileResult;
|
|
159
|
-
instructions?: string;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
export interface LLMAssistanceOptions<Provider extends LLMProviders> {
|
|
163
|
-
model: LLMModel<Provider>;
|
|
164
|
-
message: LLMAssistanceMessage;
|
|
165
|
-
chatId: string;
|
|
166
|
-
assistantId: string;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
export type LLMAssistanceResult = LLMRequestResult<{ text: string }>;
|
|
170
|
-
|
|
171
|
-
export type LLMCountTokensFunction<Provider extends LLMProviders> = (
|
|
172
|
-
messages: string[],
|
|
173
|
-
model: LLMModel<Provider>,
|
|
174
|
-
) => Promise<number>;
|
package/src/functional.utils.ts
DELETED
package/src/index.ts
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
LLMProviderAvailableModels,
|
|
3
|
-
LLMProviderModelsByPurpose,
|
|
4
|
-
LLMProviders,
|
|
5
|
-
LLMPurposes,
|
|
6
|
-
LLMServiceBuilder,
|
|
7
|
-
} from '../../LLMService.typedefs';
|
|
8
|
-
import { GoogleGenerativeAIModelNames } from './GoogleGenerativeAI.typedefs';
|
|
9
|
-
import {
|
|
10
|
-
GoogleGenerativeAIAssistanceService,
|
|
11
|
-
GoogleGenerativeAICompletionService,
|
|
12
|
-
} from '../../providers/GoogleGenerativeAI/services';
|
|
13
|
-
import { pick } from '../../functional.utils';
|
|
14
|
-
|
|
15
|
-
export const MIN_CACHE_CONTENT_LENGTH = 32_768;
|
|
16
|
-
|
|
17
|
-
const GOOGLE_AI_AVAILABLE_MODELS: LLMProviderAvailableModels<
|
|
18
|
-
LLMProviders.GoogleGenerativeAI
|
|
19
|
-
> = {
|
|
20
|
-
[GoogleGenerativeAIModelNames.GEMINI_1_5_FLASH]: {
|
|
21
|
-
name: GoogleGenerativeAIModelNames.GEMINI_1_5_FLASH,
|
|
22
|
-
limits: {
|
|
23
|
-
maxInputTokens: 1_048_576,
|
|
24
|
-
maxOutputTokens: 8_192,
|
|
25
|
-
},
|
|
26
|
-
config: {
|
|
27
|
-
temperature: 0.2,
|
|
28
|
-
},
|
|
29
|
-
},
|
|
30
|
-
[GoogleGenerativeAIModelNames.GEMINI_1_5_FLASH_STABLE]: {
|
|
31
|
-
name: GoogleGenerativeAIModelNames.GEMINI_1_5_FLASH_STABLE,
|
|
32
|
-
limits: {
|
|
33
|
-
maxInputTokens: 1_048_576,
|
|
34
|
-
maxOutputTokens: 8_192,
|
|
35
|
-
},
|
|
36
|
-
config: {
|
|
37
|
-
temperature: 0.2,
|
|
38
|
-
},
|
|
39
|
-
},
|
|
40
|
-
[GoogleGenerativeAIModelNames.GEMINI_1_5_PRO]: {
|
|
41
|
-
name: GoogleGenerativeAIModelNames.GEMINI_1_5_PRO,
|
|
42
|
-
limits: {
|
|
43
|
-
maxInputTokens: 2_097_152,
|
|
44
|
-
maxOutputTokens: 8_192,
|
|
45
|
-
},
|
|
46
|
-
config: {
|
|
47
|
-
temperature: 0.2,
|
|
48
|
-
},
|
|
49
|
-
},
|
|
50
|
-
[GoogleGenerativeAIModelNames.GEMINI_1_5_PRO_STABLE]: {
|
|
51
|
-
name: GoogleGenerativeAIModelNames.GEMINI_1_5_PRO_STABLE,
|
|
52
|
-
limits: {
|
|
53
|
-
maxInputTokens: 2_097_152,
|
|
54
|
-
maxOutputTokens: 8_192,
|
|
55
|
-
},
|
|
56
|
-
config: {
|
|
57
|
-
temperature: 0.2,
|
|
58
|
-
},
|
|
59
|
-
},
|
|
60
|
-
[GoogleGenerativeAIModelNames.GEMINI_2_0_FLASH]: {
|
|
61
|
-
name: GoogleGenerativeAIModelNames.GEMINI_2_0_FLASH,
|
|
62
|
-
limits: {
|
|
63
|
-
maxInputTokens: 1_048_576,
|
|
64
|
-
maxOutputTokens: 8_192,
|
|
65
|
-
},
|
|
66
|
-
config: {
|
|
67
|
-
temperature: 0.2,
|
|
68
|
-
},
|
|
69
|
-
},
|
|
70
|
-
[GoogleGenerativeAIModelNames.GEMINI_2_0_FLASH_STABLE]: {
|
|
71
|
-
name: GoogleGenerativeAIModelNames.GEMINI_2_0_FLASH_STABLE,
|
|
72
|
-
limits: {
|
|
73
|
-
maxInputTokens: 1_048_576,
|
|
74
|
-
maxOutputTokens: 8_192,
|
|
75
|
-
},
|
|
76
|
-
config: {
|
|
77
|
-
temperature: 0.2,
|
|
78
|
-
},
|
|
79
|
-
},
|
|
80
|
-
[GoogleGenerativeAIModelNames.GEMINI_2_5_PRO_PREVIEW]: {
|
|
81
|
-
name: GoogleGenerativeAIModelNames.GEMINI_2_5_PRO_PREVIEW,
|
|
82
|
-
limits: {
|
|
83
|
-
maxInputTokens: 1_048_576,
|
|
84
|
-
maxOutputTokens: 65_536,
|
|
85
|
-
},
|
|
86
|
-
config: {
|
|
87
|
-
temperature: 0.2,
|
|
88
|
-
},
|
|
89
|
-
},
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
export const GOOGLE_AI_MODELS: LLMProviderModelsByPurpose<
|
|
93
|
-
LLMPurposes,
|
|
94
|
-
LLMProviders.GoogleGenerativeAI
|
|
95
|
-
> = {
|
|
96
|
-
[LLMPurposes.Completion]: pick(
|
|
97
|
-
GOOGLE_AI_AVAILABLE_MODELS,
|
|
98
|
-
[
|
|
99
|
-
GoogleGenerativeAIModelNames.GEMINI_1_5_FLASH,
|
|
100
|
-
GoogleGenerativeAIModelNames.GEMINI_1_5_PRO,
|
|
101
|
-
GoogleGenerativeAIModelNames.GEMINI_2_0_FLASH,
|
|
102
|
-
GoogleGenerativeAIModelNames.GEMINI_2_5_PRO_PREVIEW,
|
|
103
|
-
],
|
|
104
|
-
),
|
|
105
|
-
// Stable models are used for assistance as they support additional methods
|
|
106
|
-
[LLMPurposes.Assistance]: pick(
|
|
107
|
-
GOOGLE_AI_AVAILABLE_MODELS,
|
|
108
|
-
[
|
|
109
|
-
GoogleGenerativeAIModelNames.GEMINI_1_5_FLASH_STABLE,
|
|
110
|
-
GoogleGenerativeAIModelNames.GEMINI_1_5_PRO_STABLE,
|
|
111
|
-
GoogleGenerativeAIModelNames.GEMINI_2_0_FLASH_STABLE,
|
|
112
|
-
],
|
|
113
|
-
),
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
export const GOOGLE_AI_SERVICE_BUILDERS: {
|
|
117
|
-
[purpose in LLMPurposes]: (
|
|
118
|
-
LLMServiceBuilder<LLMProviders.GoogleGenerativeAI, purpose> | null
|
|
119
|
-
)
|
|
120
|
-
} = {
|
|
121
|
-
[LLMPurposes.Completion]: (logger, options) => (
|
|
122
|
-
new GoogleGenerativeAICompletionService(logger, options)
|
|
123
|
-
),
|
|
124
|
-
[LLMPurposes.Assistance]: (logger, options) => (
|
|
125
|
-
new GoogleGenerativeAIAssistanceService(logger, options)
|
|
126
|
-
),
|
|
127
|
-
};
|