@theia/ai-huggingface 1.46.0-next.241
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 +32 -0
- package/lib/browser/huggingface-frontend-application-contribution.d.ts +13 -0
- package/lib/browser/huggingface-frontend-application-contribution.d.ts.map +1 -0
- package/lib/browser/huggingface-frontend-application-contribution.js +91 -0
- package/lib/browser/huggingface-frontend-application-contribution.js.map +1 -0
- package/lib/browser/huggingface-frontend-module.d.ts +4 -0
- package/lib/browser/huggingface-frontend-module.d.ts.map +1 -0
- package/lib/browser/huggingface-frontend-module.js +32 -0
- package/lib/browser/huggingface-frontend-module.js.map +1 -0
- package/lib/browser/huggingface-preferences.d.ts +5 -0
- package/lib/browser/huggingface-preferences.d.ts.map +1 -0
- package/lib/browser/huggingface-preferences.js +42 -0
- package/lib/browser/huggingface-preferences.js.map +1 -0
- package/lib/common/huggingface-language-models-manager.d.ts +25 -0
- package/lib/common/huggingface-language-models-manager.d.ts.map +1 -0
- package/lib/common/huggingface-language-models-manager.js +21 -0
- package/lib/common/huggingface-language-models-manager.js.map +1 -0
- package/lib/common/index.d.ts +2 -0
- package/lib/common/index.d.ts.map +1 -0
- package/lib/common/index.js +20 -0
- package/lib/common/index.js.map +1 -0
- package/lib/node/huggingface-backend-module.d.ts +5 -0
- package/lib/node/huggingface-backend-module.d.ts.map +1 -0
- package/lib/node/huggingface-backend-module.js +29 -0
- package/lib/node/huggingface-backend-module.js.map +1 -0
- package/lib/node/huggingface-language-model.d.ts +29 -0
- package/lib/node/huggingface-language-model.d.ts.map +1 -0
- package/lib/node/huggingface-language-model.js +134 -0
- package/lib/node/huggingface-language-model.js.map +1 -0
- package/lib/node/huggingface-language-models-manager-impl.d.ts +11 -0
- package/lib/node/huggingface-language-models-manager-impl.d.ts.map +1 -0
- package/lib/node/huggingface-language-models-manager-impl.js +63 -0
- package/lib/node/huggingface-language-models-manager-impl.js.map +1 -0
- package/lib/package.spec.d.ts +1 -0
- package/lib/package.spec.d.ts.map +1 -0
- package/lib/package.spec.js +26 -0
- package/lib/package.spec.js.map +1 -0
- package/package.json +50 -0
- package/src/browser/huggingface-frontend-application-contribution.ts +95 -0
- package/src/browser/huggingface-frontend-module.ts +31 -0
- package/src/browser/huggingface-preferences.ts +42 -0
- package/src/common/huggingface-language-models-manager.ts +40 -0
- package/src/common/index.ts +16 -0
- package/src/node/huggingface-backend-module.ts +30 -0
- package/src/node/huggingface-language-model.ts +166 -0
- package/src/node/huggingface-language-models-manager-impl.ts +73 -0
- package/src/package.spec.ts +28 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// *****************************************************************************
|
|
3
|
+
// Copyright (C) 2024 EclipseSource GmbH.
|
|
4
|
+
//
|
|
5
|
+
// This program and the accompanying materials are made available under the
|
|
6
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
7
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
8
|
+
//
|
|
9
|
+
// This Source Code may also be made available under the following Secondary
|
|
10
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
11
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
12
|
+
// with the GNU Classpath Exception which is available at
|
|
13
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
14
|
+
//
|
|
15
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
16
|
+
// *****************************************************************************
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.HuggingFaceLanguageModelsManagerImpl = void 0;
|
|
19
|
+
const tslib_1 = require("tslib");
|
|
20
|
+
const ai_core_1 = require("@theia/ai-core");
|
|
21
|
+
const inversify_1 = require("@theia/core/shared/inversify");
|
|
22
|
+
const huggingface_language_model_1 = require("./huggingface-language-model");
|
|
23
|
+
let HuggingFaceLanguageModelsManagerImpl = class HuggingFaceLanguageModelsManagerImpl {
|
|
24
|
+
get apiKey() {
|
|
25
|
+
var _a;
|
|
26
|
+
return (_a = this._apiKey) !== null && _a !== void 0 ? _a : process.env.HUGGINGFACE_API_KEY;
|
|
27
|
+
}
|
|
28
|
+
async createOrUpdateLanguageModels(...modelDescriptions) {
|
|
29
|
+
for (const modelDescription of modelDescriptions) {
|
|
30
|
+
const model = await this.languageModelRegistry.getLanguageModel(modelDescription.id);
|
|
31
|
+
const apiKeyProvider = () => this.apiKey;
|
|
32
|
+
if (model) {
|
|
33
|
+
if (!(model instanceof huggingface_language_model_1.HuggingFaceModel)) {
|
|
34
|
+
console.warn(`Hugging Face: model ${modelDescription.id} is not a Hugging Face model`);
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
model.model = modelDescription.model;
|
|
38
|
+
model.apiKey = apiKeyProvider;
|
|
39
|
+
model.defaultRequestSettings = modelDescription.defaultRequestSettings;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
this.languageModelRegistry.addLanguageModels([
|
|
43
|
+
new huggingface_language_model_1.HuggingFaceModel(modelDescription.id, modelDescription.model, apiKeyProvider, undefined, undefined, undefined, undefined, undefined, undefined, modelDescription.defaultRequestSettings)
|
|
44
|
+
]);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
removeLanguageModels(...modelIds) {
|
|
49
|
+
this.languageModelRegistry.removeLanguageModels(modelIds);
|
|
50
|
+
}
|
|
51
|
+
setApiKey(apiKey) {
|
|
52
|
+
this._apiKey = apiKey || undefined;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
exports.HuggingFaceLanguageModelsManagerImpl = HuggingFaceLanguageModelsManagerImpl;
|
|
56
|
+
tslib_1.__decorate([
|
|
57
|
+
(0, inversify_1.inject)(ai_core_1.LanguageModelRegistry),
|
|
58
|
+
tslib_1.__metadata("design:type", Object)
|
|
59
|
+
], HuggingFaceLanguageModelsManagerImpl.prototype, "languageModelRegistry", void 0);
|
|
60
|
+
exports.HuggingFaceLanguageModelsManagerImpl = HuggingFaceLanguageModelsManagerImpl = tslib_1.__decorate([
|
|
61
|
+
(0, inversify_1.injectable)()
|
|
62
|
+
], HuggingFaceLanguageModelsManagerImpl);
|
|
63
|
+
//# sourceMappingURL=huggingface-language-models-manager-impl.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"huggingface-language-models-manager-impl.js","sourceRoot":"","sources":["../../src/node/huggingface-language-models-manager-impl.ts"],"names":[],"mappings":";AAAA,gFAAgF;AAChF,yCAAyC;AACzC,EAAE;AACF,2EAA2E;AAC3E,mEAAmE;AACnE,wCAAwC;AACxC,EAAE;AACF,4EAA4E;AAC5E,8EAA8E;AAC9E,6EAA6E;AAC7E,yDAAyD;AACzD,uDAAuD;AACvD,EAAE;AACF,gFAAgF;AAChF,gFAAgF;;;;AAEhF,4CAAuD;AACvD,4DAAkE;AAClE,6EAAgE;AAIzD,IAAM,oCAAoC,GAA1C,MAAM,oCAAoC;IAO7C,IAAI,MAAM;;QACN,OAAO,MAAA,IAAI,CAAC,OAAO,mCAAI,OAAO,CAAC,GAAG,CAAC,mBAAmB,CAAC;IAC3D,CAAC;IAED,KAAK,CAAC,4BAA4B,CAAC,GAAG,iBAAgD;QAClF,KAAK,MAAM,gBAAgB,IAAI,iBAAiB,EAAE,CAAC;YAC/C,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,qBAAqB,CAAC,gBAAgB,CAAC,gBAAgB,CAAC,EAAE,CAAC,CAAC;YACrF,MAAM,cAAc,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC;YAEzC,IAAI,KAAK,EAAE,CAAC;gBACR,IAAI,CAAC,CAAC,KAAK,YAAY,6CAAgB,CAAC,EAAE,CAAC;oBACvC,OAAO,CAAC,IAAI,CAAC,uBAAuB,gBAAgB,CAAC,EAAE,8BAA8B,CAAC,CAAC;oBACvF,SAAS;gBACb,CAAC;gBACD,KAAK,CAAC,KAAK,GAAG,gBAAgB,CAAC,KAAK,CAAC;gBACrC,KAAK,CAAC,MAAM,GAAG,cAAc,CAAC;gBAC9B,KAAK,CAAC,sBAAsB,GAAG,gBAAgB,CAAC,sBAAsB,CAAC;YAC3E,CAAC;iBAAM,CAAC;gBACJ,IAAI,CAAC,qBAAqB,CAAC,iBAAiB,CAAC;oBACzC,IAAI,6CAAgB,CAChB,gBAAgB,CAAC,EAAE,EACnB,gBAAgB,CAAC,KAAK,EACtB,cAAc,EACd,SAAS,EACT,SAAS,EACT,SAAS,EACT,SAAS,EACT,SAAS,EACT,SAAS,EACT,gBAAgB,CAAC,sBAAsB,CAC1C;iBACJ,CAAC,CAAC;YACP,CAAC;QACL,CAAC;IACL,CAAC;IAED,oBAAoB,CAAC,GAAG,QAAkB;QACtC,IAAI,CAAC,qBAAqB,CAAC,oBAAoB,CAAC,QAAQ,CAAC,CAAC;IAC9D,CAAC;IAED,SAAS,CAAC,MAA0B;QAChC,IAAI,CAAC,OAAO,GAAG,MAAM,IAAI,SAAS,CAAC;IACvC,CAAC;CACJ,CAAA;AAlDY,oFAAoC;AAK1B;IADlB,IAAA,kBAAM,EAAC,+BAAqB,CAAC;;mFACkC;+CALvD,oCAAoC;IADhD,IAAA,sBAAU,GAAE;GACA,oCAAoC,CAkDhD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=package.spec.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"package.spec.d.ts","sourceRoot":"","sources":["../src/package.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2024 EclipseSource GmbH and others.
|
|
3
|
+
//
|
|
4
|
+
// This program and the accompanying materials are made available under the
|
|
5
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
//
|
|
8
|
+
// This Source Code may also be made available under the following Secondary
|
|
9
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
// with the GNU Classpath Exception which is available at
|
|
12
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
//
|
|
14
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
/* note: this bogus test file is required so that
|
|
17
|
+
we are able to run mocha unit tests on this
|
|
18
|
+
package, without having any actual unit tests in it.
|
|
19
|
+
This way a coverage report will be generated,
|
|
20
|
+
showing 0% coverage, instead of no report.
|
|
21
|
+
This file can be removed once we have real unit
|
|
22
|
+
tests in place. */
|
|
23
|
+
describe('ai-huggingface package', () => {
|
|
24
|
+
it('support code coverage statistics', () => true);
|
|
25
|
+
});
|
|
26
|
+
//# sourceMappingURL=package.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"package.spec.js","sourceRoot":"","sources":["../src/package.spec.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,oDAAoD;AACpD,EAAE;AACF,2EAA2E;AAC3E,mEAAmE;AACnE,wCAAwC;AACxC,EAAE;AACF,4EAA4E;AAC5E,8EAA8E;AAC9E,6EAA6E;AAC7E,yDAAyD;AACzD,uDAAuD;AACvD,EAAE;AACF,gFAAgF;AAChF,gFAAgF;AAEhF;;;;;;qBAMqB;AAErB,QAAQ,CAAC,wBAAwB,EAAE,GAAG,EAAE;IAEpC,EAAE,CAAC,kCAAkC,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,CAAC;AACvD,CAAC,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@theia/ai-huggingface",
|
|
3
|
+
"version": "1.46.0-next.241+4295c1a8c",
|
|
4
|
+
"description": "Theia - Hugging Face Integration",
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"@huggingface/inference": "^2.0.0",
|
|
7
|
+
"@theia/ai-core": "1.46.0-next.241+4295c1a8c",
|
|
8
|
+
"@theia/core": "1.46.0-next.241+4295c1a8c"
|
|
9
|
+
},
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"theiaExtensions": [
|
|
14
|
+
{
|
|
15
|
+
"frontend": "lib/browser/huggingface-frontend-module",
|
|
16
|
+
"backend": "lib/node/huggingface-backend-module"
|
|
17
|
+
}
|
|
18
|
+
],
|
|
19
|
+
"keywords": [
|
|
20
|
+
"theia-extension"
|
|
21
|
+
],
|
|
22
|
+
"license": "EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/eclipse-theia/theia.git"
|
|
26
|
+
},
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/eclipse-theia/theia/issues"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/eclipse-theia/theia",
|
|
31
|
+
"files": [
|
|
32
|
+
"lib",
|
|
33
|
+
"src"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "theiaext build",
|
|
37
|
+
"clean": "theiaext clean",
|
|
38
|
+
"compile": "theiaext compile",
|
|
39
|
+
"lint": "theiaext lint",
|
|
40
|
+
"test": "theiaext test",
|
|
41
|
+
"watch": "theiaext watch"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@theia/ext-scripts": "1.56.0"
|
|
45
|
+
},
|
|
46
|
+
"nyc": {
|
|
47
|
+
"extends": "../../configs/nyc.json"
|
|
48
|
+
},
|
|
49
|
+
"gitHead": "4295c1a8c8b5d23471f274265c798bac9e6baecc"
|
|
50
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2024 EclipseSource GmbH.
|
|
3
|
+
//
|
|
4
|
+
// This program and the accompanying materials are made available under the
|
|
5
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
//
|
|
8
|
+
// This Source Code may also be made available under the following Secondary
|
|
9
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
// with the GNU Classpath Exception which is available at
|
|
12
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
//
|
|
14
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
|
|
17
|
+
import { FrontendApplicationContribution, PreferenceService } from '@theia/core/lib/browser';
|
|
18
|
+
import { inject, injectable } from '@theia/core/shared/inversify';
|
|
19
|
+
import { HuggingFaceLanguageModelsManager, HuggingFaceModelDescription } from '../common';
|
|
20
|
+
import { API_KEY_PREF, MODELS_PREF } from './huggingface-preferences';
|
|
21
|
+
import { PREFERENCE_NAME_REQUEST_SETTINGS, RequestSetting } from '@theia/ai-core/lib/browser/ai-core-preferences';
|
|
22
|
+
|
|
23
|
+
const HUGGINGFACE_PROVIDER_ID = 'huggingface';
|
|
24
|
+
@injectable()
|
|
25
|
+
export class HuggingFaceFrontendApplicationContribution implements FrontendApplicationContribution {
|
|
26
|
+
|
|
27
|
+
@inject(PreferenceService)
|
|
28
|
+
protected preferenceService: PreferenceService;
|
|
29
|
+
|
|
30
|
+
@inject(HuggingFaceLanguageModelsManager)
|
|
31
|
+
protected manager: HuggingFaceLanguageModelsManager;
|
|
32
|
+
|
|
33
|
+
protected prevModels: string[] = [];
|
|
34
|
+
|
|
35
|
+
onStart(): void {
|
|
36
|
+
this.preferenceService.ready.then(() => {
|
|
37
|
+
const apiKey = this.preferenceService.get<string>(API_KEY_PREF, undefined);
|
|
38
|
+
this.manager.setApiKey(apiKey);
|
|
39
|
+
|
|
40
|
+
const models = this.preferenceService.get<string[]>(MODELS_PREF, []);
|
|
41
|
+
const requestSettings = this.preferenceService.get<RequestSetting[]>(PREFERENCE_NAME_REQUEST_SETTINGS, []);
|
|
42
|
+
this.manager.createOrUpdateLanguageModels(...models.map(modelId => this.createHuggingFaceModelDescription(modelId, requestSettings)));
|
|
43
|
+
this.prevModels = [...models];
|
|
44
|
+
|
|
45
|
+
this.preferenceService.onPreferenceChanged(event => {
|
|
46
|
+
if (event.preferenceName === API_KEY_PREF) {
|
|
47
|
+
this.manager.setApiKey(event.newValue);
|
|
48
|
+
} else if (event.preferenceName === MODELS_PREF) {
|
|
49
|
+
this.handleModelChanges(event.newValue as string[]);
|
|
50
|
+
} else if (event.preferenceName === PREFERENCE_NAME_REQUEST_SETTINGS) {
|
|
51
|
+
this.handleRequestSettingsChanges(event.newValue as RequestSetting[]);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
protected handleModelChanges(newModels: string[]): void {
|
|
58
|
+
const oldModels = new Set(this.prevModels);
|
|
59
|
+
const updatedModels = new Set(newModels);
|
|
60
|
+
|
|
61
|
+
const modelsToRemove = [...oldModels].filter(model => !updatedModels.has(model));
|
|
62
|
+
const modelsToAdd = [...updatedModels].filter(model => !oldModels.has(model));
|
|
63
|
+
|
|
64
|
+
this.manager.removeLanguageModels(...modelsToRemove.map(model => `${HUGGINGFACE_PROVIDER_ID}/${model}`));
|
|
65
|
+
const requestSettings = this.preferenceService.get<RequestSetting[]>(PREFERENCE_NAME_REQUEST_SETTINGS, []);
|
|
66
|
+
this.manager.createOrUpdateLanguageModels(...modelsToAdd.map(modelId => this.createHuggingFaceModelDescription(modelId, requestSettings)));
|
|
67
|
+
this.prevModels = newModels;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
protected handleRequestSettingsChanges(newSettings: RequestSetting[]): void {
|
|
71
|
+
const models = this.preferenceService.get<string[]>(MODELS_PREF, []);
|
|
72
|
+
this.manager.createOrUpdateLanguageModels(...models.map(modelId => this.createHuggingFaceModelDescription(modelId, newSettings)));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
protected createHuggingFaceModelDescription(
|
|
76
|
+
modelId: string,
|
|
77
|
+
requestSettings: RequestSetting[]
|
|
78
|
+
): HuggingFaceModelDescription {
|
|
79
|
+
const id = `${HUGGINGFACE_PROVIDER_ID}/${modelId}`;
|
|
80
|
+
const matchingSettings = requestSettings.filter(
|
|
81
|
+
setting => (!setting.providerId || setting.providerId === HUGGINGFACE_PROVIDER_ID) && setting.modelId === modelId
|
|
82
|
+
);
|
|
83
|
+
if (matchingSettings.length > 1) {
|
|
84
|
+
console.warn(
|
|
85
|
+
`Multiple entries found for modelId "${modelId}". Using the first match and ignoring the rest.`
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
const modelRequestSetting = matchingSettings[0];
|
|
89
|
+
return {
|
|
90
|
+
id: id,
|
|
91
|
+
model: modelId,
|
|
92
|
+
defaultRequestSettings: modelRequestSetting?.requestSettings
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2024 EclipseSource GmbH.
|
|
3
|
+
//
|
|
4
|
+
// This program and the accompanying materials are made available under the
|
|
5
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
//
|
|
8
|
+
// This Source Code may also be made available under the following Secondary
|
|
9
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
// with the GNU Classpath Exception which is available at
|
|
12
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
//
|
|
14
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
|
|
17
|
+
import { ContainerModule } from '@theia/core/shared/inversify';
|
|
18
|
+
import { HuggingFacePreferencesSchema } from './huggingface-preferences';
|
|
19
|
+
import { FrontendApplicationContribution, PreferenceContribution, RemoteConnectionProvider, ServiceConnectionProvider } from '@theia/core/lib/browser';
|
|
20
|
+
import { HuggingFaceFrontendApplicationContribution } from './huggingface-frontend-application-contribution';
|
|
21
|
+
import { HUGGINGFACE_LANGUAGE_MODELS_MANAGER_PATH, HuggingFaceLanguageModelsManager } from '../common';
|
|
22
|
+
|
|
23
|
+
export default new ContainerModule(bind => {
|
|
24
|
+
bind(PreferenceContribution).toConstantValue({ schema: HuggingFacePreferencesSchema });
|
|
25
|
+
bind(HuggingFaceFrontendApplicationContribution).toSelf().inSingletonScope();
|
|
26
|
+
bind(FrontendApplicationContribution).toService(HuggingFaceFrontendApplicationContribution);
|
|
27
|
+
bind(HuggingFaceLanguageModelsManager).toDynamicValue(ctx => {
|
|
28
|
+
const provider = ctx.container.get<ServiceConnectionProvider>(RemoteConnectionProvider);
|
|
29
|
+
return provider.createProxy<HuggingFaceLanguageModelsManager>(HUGGINGFACE_LANGUAGE_MODELS_MANAGER_PATH);
|
|
30
|
+
}).inSingletonScope();
|
|
31
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2024 EclipseSource GmbH.
|
|
3
|
+
//
|
|
4
|
+
// This program and the accompanying materials are made available under the
|
|
5
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
//
|
|
8
|
+
// This Source Code may also be made available under the following Secondary
|
|
9
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
// with the GNU Classpath Exception which is available at
|
|
12
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
//
|
|
14
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
|
|
17
|
+
import { PreferenceSchema } from '@theia/core/lib/browser/preferences/preference-contribution';
|
|
18
|
+
import { AI_CORE_PREFERENCES_TITLE } from '@theia/ai-core/lib/browser/ai-core-preferences';
|
|
19
|
+
|
|
20
|
+
export const API_KEY_PREF = 'ai-features.huggingFace.apiKey';
|
|
21
|
+
export const MODELS_PREF = 'ai-features.huggingFace.models';
|
|
22
|
+
|
|
23
|
+
export const HuggingFacePreferencesSchema: PreferenceSchema = {
|
|
24
|
+
type: 'object',
|
|
25
|
+
properties: {
|
|
26
|
+
[API_KEY_PREF]: {
|
|
27
|
+
type: 'string',
|
|
28
|
+
markdownDescription: 'Enter an API Key for your Hugging Face Account. **Please note:** By using this preference the Hugging Face API key will be stored in clear text\
|
|
29
|
+
on the machine running Theia. Use the environment variable `HUGGINGFACE_API_KEY` to set the key securely.',
|
|
30
|
+
title: AI_CORE_PREFERENCES_TITLE,
|
|
31
|
+
},
|
|
32
|
+
[MODELS_PREF]: {
|
|
33
|
+
type: 'array',
|
|
34
|
+
description: 'Hugging Face models to use',
|
|
35
|
+
title: AI_CORE_PREFERENCES_TITLE,
|
|
36
|
+
default: ['bigcode/starcoder'],
|
|
37
|
+
items: {
|
|
38
|
+
type: 'string'
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2024 EclipseSource GmbH.
|
|
3
|
+
//
|
|
4
|
+
// This program and the accompanying materials are made available under the
|
|
5
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
//
|
|
8
|
+
// This Source Code may also be made available under the following Secondary
|
|
9
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
// with the GNU Classpath Exception which is available at
|
|
12
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
//
|
|
14
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
|
|
17
|
+
export const HUGGINGFACE_LANGUAGE_MODELS_MANAGER_PATH = '/services/huggingface/language-model-manager';
|
|
18
|
+
export const HuggingFaceLanguageModelsManager = Symbol('HuggingFaceLanguageModelsManager');
|
|
19
|
+
|
|
20
|
+
export interface HuggingFaceModelDescription {
|
|
21
|
+
/**
|
|
22
|
+
* The identifier of the model which will be shown in the UI.
|
|
23
|
+
*/
|
|
24
|
+
id: string;
|
|
25
|
+
/**
|
|
26
|
+
* The model ID as used by the Hugging Face API.
|
|
27
|
+
*/
|
|
28
|
+
model: string;
|
|
29
|
+
/**
|
|
30
|
+
* Default request settings for the Hugging Face model.
|
|
31
|
+
*/
|
|
32
|
+
defaultRequestSettings?: { [key: string]: unknown };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface HuggingFaceLanguageModelsManager {
|
|
36
|
+
apiKey: string | undefined;
|
|
37
|
+
setApiKey(key: string | undefined): void;
|
|
38
|
+
createOrUpdateLanguageModels(...models: HuggingFaceModelDescription[]): Promise<void>;
|
|
39
|
+
removeLanguageModels(...modelIds: string[]): void;
|
|
40
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2024 EclipseSource GmbH.
|
|
3
|
+
//
|
|
4
|
+
// This program and the accompanying materials are made available under the
|
|
5
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
//
|
|
8
|
+
// This Source Code may also be made available under the following Secondary
|
|
9
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
// with the GNU Classpath Exception which is available at
|
|
12
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
//
|
|
14
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
export * from './huggingface-language-models-manager';
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2024 EclipseSource GmbH.
|
|
3
|
+
//
|
|
4
|
+
// This program and the accompanying materials are made available under the
|
|
5
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
//
|
|
8
|
+
// This Source Code may also be made available under the following Secondary
|
|
9
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
// with the GNU Classpath Exception which is available at
|
|
12
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
//
|
|
14
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
|
|
17
|
+
import { ContainerModule } from '@theia/core/shared/inversify';
|
|
18
|
+
import { HUGGINGFACE_LANGUAGE_MODELS_MANAGER_PATH, HuggingFaceLanguageModelsManager } from '../common/huggingface-language-models-manager';
|
|
19
|
+
import { ConnectionHandler, RpcConnectionHandler } from '@theia/core';
|
|
20
|
+
import { HuggingFaceLanguageModelsManagerImpl } from './huggingface-language-models-manager-impl';
|
|
21
|
+
|
|
22
|
+
export const HuggingFaceModelFactory = Symbol('HuggingFaceModelFactory');
|
|
23
|
+
|
|
24
|
+
export default new ContainerModule(bind => {
|
|
25
|
+
bind(HuggingFaceLanguageModelsManagerImpl).toSelf().inSingletonScope();
|
|
26
|
+
bind(HuggingFaceLanguageModelsManager).toService(HuggingFaceLanguageModelsManagerImpl);
|
|
27
|
+
bind(ConnectionHandler).toDynamicValue(ctx =>
|
|
28
|
+
new RpcConnectionHandler(HUGGINGFACE_LANGUAGE_MODELS_MANAGER_PATH, () => ctx.container.get(HuggingFaceLanguageModelsManager))
|
|
29
|
+
).inSingletonScope();
|
|
30
|
+
});
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2024 EclipseSource GmbH.
|
|
3
|
+
//
|
|
4
|
+
// This program and the accompanying materials are made available under the
|
|
5
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
//
|
|
8
|
+
// This Source Code may also be made available under the following Secondary
|
|
9
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
// with the GNU Classpath Exception which is available at
|
|
12
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
//
|
|
14
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
|
|
17
|
+
import {
|
|
18
|
+
LanguageModel,
|
|
19
|
+
LanguageModelRequest,
|
|
20
|
+
LanguageModelRequestMessage,
|
|
21
|
+
LanguageModelResponse,
|
|
22
|
+
LanguageModelStreamResponsePart,
|
|
23
|
+
LanguageModelTextResponse,
|
|
24
|
+
MessageActor
|
|
25
|
+
} from '@theia/ai-core';
|
|
26
|
+
import { CancellationToken } from '@theia/core';
|
|
27
|
+
import { HfInference } from '@huggingface/inference';
|
|
28
|
+
|
|
29
|
+
export const HuggingFaceModelIdentifier = Symbol('HuggingFaceModelIdentifier');
|
|
30
|
+
|
|
31
|
+
function toHuggingFacePrompt(messages: LanguageModelRequestMessage[]): string {
|
|
32
|
+
if (messages.length === 1) {
|
|
33
|
+
return messages[0].query;
|
|
34
|
+
}
|
|
35
|
+
return messages.map(message => `${toRoleLabel(message.actor)}: ${message.query}`).join('\n');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function toRoleLabel(actor: MessageActor): string {
|
|
39
|
+
switch (actor) {
|
|
40
|
+
case 'user':
|
|
41
|
+
return 'User';
|
|
42
|
+
case 'ai':
|
|
43
|
+
return 'Assistant';
|
|
44
|
+
case 'system':
|
|
45
|
+
return 'System';
|
|
46
|
+
default:
|
|
47
|
+
return '';
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export class HuggingFaceModel implements LanguageModel {
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @param id the unique id for this language model. It will be used to identify the model in the UI.
|
|
55
|
+
* @param model the model id as it is used by the Hugging Face API
|
|
56
|
+
* @param apiKey function to retrieve the API key for Hugging Face
|
|
57
|
+
*/
|
|
58
|
+
constructor(
|
|
59
|
+
public readonly id: string,
|
|
60
|
+
public model: string,
|
|
61
|
+
public apiKey: () => string | undefined,
|
|
62
|
+
public readonly name?: string,
|
|
63
|
+
public readonly vendor?: string,
|
|
64
|
+
public readonly version?: string,
|
|
65
|
+
public readonly family?: string,
|
|
66
|
+
public readonly maxInputTokens?: number,
|
|
67
|
+
public readonly maxOutputTokens?: number,
|
|
68
|
+
public defaultRequestSettings?: Record<string, unknown>
|
|
69
|
+
) { }
|
|
70
|
+
|
|
71
|
+
async request(request: LanguageModelRequest, cancellationToken?: CancellationToken): Promise<LanguageModelResponse> {
|
|
72
|
+
const hfInference = this.initializeHfInference();
|
|
73
|
+
if (this.isStreamingSupported(this.model)) {
|
|
74
|
+
return this.handleStreamingRequest(hfInference, request, cancellationToken);
|
|
75
|
+
} else {
|
|
76
|
+
return this.handleNonStreamingRequest(hfInference, request);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
protected getSettings(request: LanguageModelRequest): Record<string, unknown> {
|
|
81
|
+
const settings = request.settings ? request.settings : this.defaultRequestSettings;
|
|
82
|
+
if (!settings) {
|
|
83
|
+
return {};
|
|
84
|
+
}
|
|
85
|
+
return settings;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
protected async handleNonStreamingRequest(hfInference: HfInference, request: LanguageModelRequest): Promise<LanguageModelTextResponse> {
|
|
89
|
+
const settings = this.getSettings(request);
|
|
90
|
+
|
|
91
|
+
const response = await hfInference.textGeneration({
|
|
92
|
+
model: this.model,
|
|
93
|
+
inputs: toHuggingFacePrompt(request.messages),
|
|
94
|
+
parameters: {
|
|
95
|
+
...settings
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
const stopWords = Array.isArray(settings.stop) ? settings.stop : [];
|
|
100
|
+
let cleanText = response.generated_text;
|
|
101
|
+
|
|
102
|
+
stopWords.forEach(stopWord => {
|
|
103
|
+
if (cleanText.endsWith(stopWord)) {
|
|
104
|
+
cleanText = cleanText.slice(0, -stopWord.length).trim();
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
text: cleanText
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
protected async handleStreamingRequest(
|
|
114
|
+
hfInference: HfInference,
|
|
115
|
+
request: LanguageModelRequest,
|
|
116
|
+
cancellationToken?: CancellationToken
|
|
117
|
+
): Promise<LanguageModelResponse> {
|
|
118
|
+
|
|
119
|
+
const settings = this.getSettings(request);
|
|
120
|
+
|
|
121
|
+
const stream = hfInference.textGenerationStream({
|
|
122
|
+
model: this.model,
|
|
123
|
+
inputs: toHuggingFacePrompt(request.messages),
|
|
124
|
+
parameters: {
|
|
125
|
+
...settings
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
const stopWords = Array.isArray(settings.stop) ? settings.stop : [];
|
|
130
|
+
|
|
131
|
+
const asyncIterator = {
|
|
132
|
+
async *[Symbol.asyncIterator](): AsyncIterator<LanguageModelStreamResponsePart> {
|
|
133
|
+
for await (const chunk of stream) {
|
|
134
|
+
let content = chunk.token.text;
|
|
135
|
+
|
|
136
|
+
stopWords.forEach(stopWord => {
|
|
137
|
+
if (content.endsWith(stopWord)) {
|
|
138
|
+
content = content.slice(0, -stopWord.length).trim();
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
yield { content };
|
|
143
|
+
|
|
144
|
+
if (cancellationToken?.isCancellationRequested) {
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
return { stream: asyncIterator };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
protected isStreamingSupported(model: string): boolean {
|
|
155
|
+
// Assuming all models support streaming for now; can be refined if needed
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private initializeHfInference(): HfInference {
|
|
160
|
+
const token = this.apiKey();
|
|
161
|
+
if (!token) {
|
|
162
|
+
throw new Error('Please provide a Hugging Face API token.');
|
|
163
|
+
}
|
|
164
|
+
return new HfInference(token);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2024 EclipseSource GmbH.
|
|
3
|
+
//
|
|
4
|
+
// This program and the accompanying materials are made available under the
|
|
5
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
//
|
|
8
|
+
// This Source Code may also be made available under the following Secondary
|
|
9
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
// with the GNU Classpath Exception which is available at
|
|
12
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
//
|
|
14
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
|
|
17
|
+
import { LanguageModelRegistry } from '@theia/ai-core';
|
|
18
|
+
import { inject, injectable } from '@theia/core/shared/inversify';
|
|
19
|
+
import { HuggingFaceModel } from './huggingface-language-model';
|
|
20
|
+
import { HuggingFaceLanguageModelsManager, HuggingFaceModelDescription } from '../common';
|
|
21
|
+
|
|
22
|
+
@injectable()
|
|
23
|
+
export class HuggingFaceLanguageModelsManagerImpl implements HuggingFaceLanguageModelsManager {
|
|
24
|
+
|
|
25
|
+
protected _apiKey: string | undefined;
|
|
26
|
+
|
|
27
|
+
@inject(LanguageModelRegistry)
|
|
28
|
+
protected readonly languageModelRegistry: LanguageModelRegistry;
|
|
29
|
+
|
|
30
|
+
get apiKey(): string | undefined {
|
|
31
|
+
return this._apiKey ?? process.env.HUGGINGFACE_API_KEY;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async createOrUpdateLanguageModels(...modelDescriptions: HuggingFaceModelDescription[]): Promise<void> {
|
|
35
|
+
for (const modelDescription of modelDescriptions) {
|
|
36
|
+
const model = await this.languageModelRegistry.getLanguageModel(modelDescription.id);
|
|
37
|
+
const apiKeyProvider = () => this.apiKey;
|
|
38
|
+
|
|
39
|
+
if (model) {
|
|
40
|
+
if (!(model instanceof HuggingFaceModel)) {
|
|
41
|
+
console.warn(`Hugging Face: model ${modelDescription.id} is not a Hugging Face model`);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
model.model = modelDescription.model;
|
|
45
|
+
model.apiKey = apiKeyProvider;
|
|
46
|
+
model.defaultRequestSettings = modelDescription.defaultRequestSettings;
|
|
47
|
+
} else {
|
|
48
|
+
this.languageModelRegistry.addLanguageModels([
|
|
49
|
+
new HuggingFaceModel(
|
|
50
|
+
modelDescription.id,
|
|
51
|
+
modelDescription.model,
|
|
52
|
+
apiKeyProvider,
|
|
53
|
+
undefined,
|
|
54
|
+
undefined,
|
|
55
|
+
undefined,
|
|
56
|
+
undefined,
|
|
57
|
+
undefined,
|
|
58
|
+
undefined,
|
|
59
|
+
modelDescription.defaultRequestSettings
|
|
60
|
+
)
|
|
61
|
+
]);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
removeLanguageModels(...modelIds: string[]): void {
|
|
67
|
+
this.languageModelRegistry.removeLanguageModels(modelIds);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
setApiKey(apiKey: string | undefined): void {
|
|
71
|
+
this._apiKey = apiKey || undefined;
|
|
72
|
+
}
|
|
73
|
+
}
|