Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Hugging Face #14412

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@theia/ai-code-completion": "1.55.0",
"@theia/ai-core": "1.55.0",
"@theia/ai-history": "1.55.0",
"@theia/ai-huggingface": "1.55.0",
"@theia/ai-llamafile": "1.55.0",
"@theia/ai-ollama": "1.55.0",
"@theia/ai-openai": "1.55.0",
Expand Down
3 changes: 3 additions & 0 deletions examples/browser/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
{
"path": "../../packages/ai-history"
},
{
"path": "../../packages/ai-hugging-face"
},
{
"path": "../../packages/ai-llamafile"
},
Expand Down
10 changes: 10 additions & 0 deletions packages/ai-hugging-face/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @type {import('eslint').Linter.Config} */
module.exports = {
extends: [
'../../configs/build.eslintrc.json'
],
parserOptions: {
tsconfigRootDir: __dirname,
project: 'tsconfig.json'
}
};
32 changes: 32 additions & 0 deletions packages/ai-hugging-face/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<div align='center'>

<br />

<img src='https://raw.githubusercontent.com/eclipse-theia/theia/master/logo/theia.svg?sanitize=true' alt='theia-ext-logo' width='100px' />

<h2>ECLIPSE THEIA - Hugging Face AI EXTENSION</h2>

<hr />

</div>

## Description

The `@theia/ai-huggingface` integrates Hugging Face's models with Theia AI.
The Hugging Face API key and the models to use can be configured via preferences.
Alternatively, the Hugging Face API key can also be provided via the `HUGGINGFACE_API_KEY` environment variable.

## Additional Information

- [Theia - GitHub](https://github.com/eclipse-theia/theia)
- [Theia - Website](https://theia-ide.org/)

## License

- [Eclipse Public License 2.0](http://www.eclipse.org/legal/epl-2.0/)
- [一 (Secondary) GNU General Public License, version 2 with the GNU Classpath Exception](https://projects.eclipse.org/license/secondary-gpl-2.0-cp)

## Trademark

"Theia" is a trademark of the Eclipse Foundation
https://www.eclipse.org/theia
49 changes: 49 additions & 0 deletions packages/ai-hugging-face/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "@theia/ai-huggingface",
"version": "1.55.0",
"description": "Theia - Hugging Face Integration",
"dependencies": {
"@theia/core": "1.55.0",
"@huggingface/inference": "^2.0.0",
"@theia/ai-core": "1.55.0"
},
"publishConfig": {
"access": "public"
},
"theiaExtensions": [
{
"frontend": "lib/browser/huggingface-frontend-module",
"backend": "lib/node/huggingface-backend-module"
}
],
"keywords": [
"theia-extension"
],
"license": "EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0",
"repository": {
"type": "git",
"url": "https://github.com/eclipse-theia/theia.git"
},
"bugs": {
"url": "https://github.com/eclipse-theia/theia/issues"
},
"homepage": "https://github.com/eclipse-theia/theia",
"files": [
"lib",
"src"
],
"scripts": {
"build": "theiaext build",
"clean": "theiaext clean",
"compile": "theiaext compile",
"lint": "theiaext lint",
"test": "theiaext test",
"watch": "theiaext watch"
},
"devDependencies": {
"@theia/ext-scripts": "1.55.0"
},
"nyc": {
"extends": "../../configs/nyc.json"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// *****************************************************************************
// Copyright (C) 2024 EclipseSource GmbH.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

import { FrontendApplicationContribution, PreferenceService } from '@theia/core/lib/browser';
import { inject, injectable } from '@theia/core/shared/inversify';
import { HuggingFaceLanguageModelsManager, HuggingFaceModelDescription } from '../common';
import { API_KEY_PREF, MODELS_PREF } from './huggingface-preferences';

@injectable()
export class HuggingFaceFrontendApplicationContribution implements FrontendApplicationContribution {

@inject(PreferenceService)
protected preferenceService: PreferenceService;

@inject(HuggingFaceLanguageModelsManager)
protected manager: HuggingFaceLanguageModelsManager;

protected prevModels: string[] = [];

onStart(): void {
this.preferenceService.ready.then(() => {
const apiKey = this.preferenceService.get<string>(API_KEY_PREF, undefined);
this.manager.setApiKey(apiKey);

const models = this.preferenceService.get<string[]>(MODELS_PREF, []);
this.manager.createOrUpdateLanguageModels(...models.map(createHuggingFaceModelDescription));
this.prevModels = [...models];

this.preferenceService.onPreferenceChanged(event => {
if (event.preferenceName === API_KEY_PREF) {
this.manager.setApiKey(event.newValue);
} else if (event.preferenceName === MODELS_PREF) {
const oldModels = new Set(this.prevModels);
const newModels = new Set(event.newValue as string[]);

const modelsToRemove = [...oldModels].filter(model => !newModels.has(model));
const modelsToAdd = [...newModels].filter(model => !oldModels.has(model));

this.manager.removeLanguageModels(...modelsToRemove.map(model => `huggingface/${model}`));
this.manager.createOrUpdateLanguageModels(...modelsToAdd.map(createHuggingFaceModelDescription));
this.prevModels = [...event.newValue];
}
});
});
}
}

function createHuggingFaceModelDescription(modelId: string): HuggingFaceModelDescription {
return {
id: `huggingface/${modelId}`,
model: modelId
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// *****************************************************************************
// Copyright (C) 2024 EclipseSource GmbH.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

import { ContainerModule } from '@theia/core/shared/inversify';
import { HuggingFacePreferencesSchema } from './huggingface-preferences';
import { FrontendApplicationContribution, PreferenceContribution, RemoteConnectionProvider, ServiceConnectionProvider } from '@theia/core/lib/browser';
import { HuggingFaceFrontendApplicationContribution } from './huggingface-frontend-application-contribution';
import { HUGGINGFACE_LANGUAGE_MODELS_MANAGER_PATH, HuggingFaceLanguageModelsManager } from '../common';

export default new ContainerModule(bind => {
bind(PreferenceContribution).toConstantValue({ schema: HuggingFacePreferencesSchema });
bind(HuggingFaceFrontendApplicationContribution).toSelf().inSingletonScope();
bind(FrontendApplicationContribution).toService(HuggingFaceFrontendApplicationContribution);
bind(HuggingFaceLanguageModelsManager).toDynamicValue(ctx => {
const provider = ctx.container.get<ServiceConnectionProvider>(RemoteConnectionProvider);
return provider.createProxy<HuggingFaceLanguageModelsManager>(HUGGINGFACE_LANGUAGE_MODELS_MANAGER_PATH);
}).inSingletonScope();
});
42 changes: 42 additions & 0 deletions packages/ai-hugging-face/src/browser/huggingface-preferences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// *****************************************************************************
// Copyright (C) 2024 EclipseSource GmbH.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

import { PreferenceSchema } from '@theia/core/lib/browser/preferences/preference-contribution';
import { AI_CORE_PREFERENCES_TITLE } from '@theia/ai-core/lib/browser/ai-core-preferences';

export const API_KEY_PREF = 'ai-features.huggingFace.apiKey';
export const MODELS_PREF = 'ai-features.huggingFace.models';

export const HuggingFacePreferencesSchema: PreferenceSchema = {
type: 'object',
properties: {
[API_KEY_PREF]: {
type: 'string',
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\
on the machine running Theia. Use the environment variable `HUGGINGFACE_API_KEY` to set the key securely.',
title: AI_CORE_PREFERENCES_TITLE,
},
[MODELS_PREF]: {
type: 'array',
description: 'Hugging Face models to use',
title: AI_CORE_PREFERENCES_TITLE,
default: ['bigcode/starcoder'],
items: {
type: 'string'
}
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// *****************************************************************************
// Copyright (C) 2024 EclipseSource GmbH.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

export const HUGGINGFACE_LANGUAGE_MODELS_MANAGER_PATH = '/services/huggingface/language-model-manager';
export const HuggingFaceLanguageModelsManager = Symbol('HuggingFaceLanguageModelsManager');

export interface HuggingFaceModelDescription {
/**
* The identifier of the model which will be shown in the UI.
*/
id: string;
/**
* The model ID as used by the Hugging Face API.
*/
model: string;
}

export interface HuggingFaceLanguageModelsManager {
apiKey: string | undefined;
setApiKey(key: string | undefined): void;
createOrUpdateLanguageModels(...models: HuggingFaceModelDescription[]): Promise<void>;
removeLanguageModels(...modelIds: string[]): void;
}
16 changes: 16 additions & 0 deletions packages/ai-hugging-face/src/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// *****************************************************************************
// Copyright (C) 2024 EclipseSource GmbH.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
export * from './huggingface-language-models-manager';
30 changes: 30 additions & 0 deletions packages/ai-hugging-face/src/node/huggingface-backend-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// *****************************************************************************
// Copyright (C) 2024 EclipseSource GmbH.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

import { ContainerModule } from '@theia/core/shared/inversify';
import { HUGGINGFACE_LANGUAGE_MODELS_MANAGER_PATH, HuggingFaceLanguageModelsManager } from '../common/huggingface-language-models-manager';
import { ConnectionHandler, RpcConnectionHandler } from '@theia/core';
import { HuggingFaceLanguageModelsManagerImpl } from './huggingface-language-models-manager-impl';

export const HuggingFaceModelFactory = Symbol('HuggingFaceModelFactory');

export default new ContainerModule(bind => {
bind(HuggingFaceLanguageModelsManagerImpl).toSelf().inSingletonScope();
bind(HuggingFaceLanguageModelsManager).toService(HuggingFaceLanguageModelsManagerImpl);
bind(ConnectionHandler).toDynamicValue(ctx =>
new RpcConnectionHandler(HUGGINGFACE_LANGUAGE_MODELS_MANAGER_PATH, () => ctx.container.get(HuggingFaceLanguageModelsManager))
).inSingletonScope();
});
Loading
Loading