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

Support Anthropic as an LLM provider #14614

Merged
merged 3 commits into from
Dec 13, 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 @@ -22,6 +22,7 @@
},
"theiaPluginsDir": "../../plugins",
"dependencies": {
"@theia/ai-anthropic": "1.56.0",
"@theia/ai-chat": "1.56.0",
"@theia/ai-chat-ui": "1.56.0",
"@theia/ai-code-completion": "1.56.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 @@ -8,6 +8,9 @@
{
"path": "../../dev-packages/cli"
},
{
"path": "../../packages/ai-anthropic"
},
{
"path": "../../packages/ai-chat"
},
Expand Down
10 changes: 10 additions & 0 deletions packages/ai-anthropic/.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'
}
};
31 changes: 31 additions & 0 deletions packages/ai-anthropic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<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 - Anthropic EXTENSION</h2>

<hr />

</div>

## Description

The `@theia/anthropic` integrates Anthropic's models with Theia AI.
The Anthropic API key and the models to use can be configured via preferences.
Alternatively the API key can also be handed in via the `ANTHROPIC_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-anthropic/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "@theia/ai-anthropic",
"version": "1.56.0",
"description": "Theia - Anthropic Integration",
"dependencies": {
"@theia/core": "1.56.0",
"@anthropic-ai/sdk": "^0.32.1",
"@theia/ai-core": "1.56.0"
},
"publishConfig": {
"access": "public"
},
"theiaExtensions": [
{
"frontend": "lib/browser/anthropic-frontend-module",
"backend": "lib/node/anthropic-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.56.0"
},
"nyc": {
"extends": "../../configs/nyc.json"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// *****************************************************************************
// 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 { AnthropicLanguageModelsManager, AnthropicModelDescription } from '../common';
import { API_KEY_PREF, MODELS_PREF } from './anthropic-preferences';
import { PREFERENCE_NAME_REQUEST_SETTINGS, RequestSetting } from '@theia/ai-core/lib/browser/ai-core-preferences';

const ANTHROPIC_PROVIDER_ID = 'anthropic';

@injectable()
export class AnthropicFrontendApplicationContribution implements FrontendApplicationContribution {

@inject(PreferenceService)
protected preferenceService: PreferenceService;

@inject(AnthropicLanguageModelsManager)
protected manager: AnthropicLanguageModelsManager;

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, []);
const requestSettings = this.getRequestSettingsPref();
this.manager.createOrUpdateLanguageModels(...models.map(modelId => this.createAnthropicModelDescription(modelId, requestSettings)));
this.prevModels = [...models];

this.preferenceService.onPreferenceChanged(event => {
if (event.preferenceName === API_KEY_PREF) {
this.manager.setApiKey(event.newValue);
} else if (event.preferenceName === MODELS_PREF) {
this.handleModelChanges(event.newValue as string[]);
} else if (event.preferenceName === PREFERENCE_NAME_REQUEST_SETTINGS) {
this.handleRequestSettingsChanges(event.newValue as RequestSetting[]);
}
});
});
}

protected handleModelChanges(newModels: string[]): void {
const oldModels = new Set(this.prevModels);
const updatedModels = new Set(newModels);

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

this.manager.removeLanguageModels(...modelsToRemove.map(model => `${ANTHROPIC_PROVIDER_ID}/${model}`));
const requestSettings = this.getRequestSettingsPref();
this.manager.createOrUpdateLanguageModels(...modelsToAdd.map(modelId => this.createAnthropicModelDescription(modelId, requestSettings)));
this.prevModels = newModels;
}

private getRequestSettingsPref(): RequestSetting[] {
return this.preferenceService.get<RequestSetting[]>(PREFERENCE_NAME_REQUEST_SETTINGS, []);
}

protected handleRequestSettingsChanges(newSettings: RequestSetting[]): void {
const models = this.preferenceService.get<string[]>(MODELS_PREF, []);
this.manager.createOrUpdateLanguageModels(...models.map(modelId => this.createAnthropicModelDescription(modelId, newSettings)));
}

protected createAnthropicModelDescription(modelId: string, requestSettings: RequestSetting[]): AnthropicModelDescription {
const id = `${ANTHROPIC_PROVIDER_ID}/${modelId}`;
const modelRequestSetting = this.getMatchingRequestSetting(modelId, ANTHROPIC_PROVIDER_ID, requestSettings);
return {
id: id,
model: modelId,
apiKey: true,
enableStreaming: true,
defaultRequestSettings: modelRequestSetting?.requestSettings
};
}

protected getMatchingRequestSetting(
modelId: string,
providerId: string,
requestSettings: RequestSetting[]
): RequestSetting | undefined {
const matchingSettings = requestSettings.filter(
setting => (!setting.providerId || setting.providerId === providerId) && setting.modelId === modelId
);
if (matchingSettings.length > 1) {
console.warn(
`Multiple entries found for provider "${providerId}" and model "${modelId}". Using the first match.`
);
}
return matchingSettings[0];
}
}
31 changes: 31 additions & 0 deletions packages/ai-anthropic/src/browser/anthropic-frontend-module.ts
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 { AnthropicPreferencesSchema } from './anthropic-preferences';
import { FrontendApplicationContribution, PreferenceContribution, RemoteConnectionProvider, ServiceConnectionProvider } from '@theia/core/lib/browser';
import { AnthropicFrontendApplicationContribution } from './anthropic-frontend-application-contribution';
import { ANTHROPIC_LANGUAGE_MODELS_MANAGER_PATH, AnthropicLanguageModelsManager } from '../common';

export default new ContainerModule(bind => {
bind(PreferenceContribution).toConstantValue({ schema: AnthropicPreferencesSchema });
bind(AnthropicFrontendApplicationContribution).toSelf().inSingletonScope();
bind(FrontendApplicationContribution).toService(AnthropicFrontendApplicationContribution);
bind(AnthropicLanguageModelsManager).toDynamicValue(ctx => {
const provider = ctx.container.get<ServiceConnectionProvider>(RemoteConnectionProvider);
return provider.createProxy<AnthropicLanguageModelsManager>(ANTHROPIC_LANGUAGE_MODELS_MANAGER_PATH);
}).inSingletonScope();
});
42 changes: 42 additions & 0 deletions packages/ai-anthropic/src/browser/anthropic-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.anthropic.AnthropicApiKey';
export const MODELS_PREF = 'ai-features.anthropic.AnthropicModels';

export const AnthropicPreferencesSchema: PreferenceSchema = {
type: 'object',
properties: {
[API_KEY_PREF]: {
type: 'string',
markdownDescription: 'Enter an API Key of your official Anthropic Account. **Please note:** By using this preference the Anthropic API key will be stored in clear text\
on the machine running Theia. Use the environment variable `ANTHROPIC_API_KEY` to set the key securely.',
title: AI_CORE_PREFERENCES_TITLE,
},
[MODELS_PREF]: {
type: 'array',
description: 'Official Anthropic models to use',
title: AI_CORE_PREFERENCES_TITLE,
default: ['claude-3-5-sonnet-latest', 'claude-3-5-haiku-latest', 'claude-3-opus-latest'],
items: {
type: 'string'
}
},
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// *****************************************************************************
// 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 ANTHROPIC_LANGUAGE_MODELS_MANAGER_PATH = '/services/anthropic/language-model-manager';
export const AnthropicLanguageModelsManager = Symbol('AnthropicLanguageModelsManager');
export interface AnthropicModelDescription {
/**
* The identifier of the model which will be shown in the UI.
*/
id: string;
/**
* The model ID as used by the Anthropic API.
*/
model: string;
/**
* The key for the model. If 'true' is provided the global Anthropic API key will be used.
*/
apiKey: string | true | undefined;
/**
* Indicate whether the streaming API shall be used.
*/
enableStreaming: boolean;
/**
* Default request settings for the Anthropic model.
*/
defaultRequestSettings?: { [key: string]: unknown };
}
export interface AnthropicLanguageModelsManager {
apiKey: string | undefined;
setApiKey(key: string | undefined): void;
createOrUpdateLanguageModels(...models: AnthropicModelDescription[]): Promise<void>;
removeLanguageModels(...modelIds: string[]): void
}
16 changes: 16 additions & 0 deletions packages/ai-anthropic/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 './anthropic-language-models-manager';
28 changes: 28 additions & 0 deletions packages/ai-anthropic/src/node/anthropic-backend-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// *****************************************************************************
// 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 { ANTHROPIC_LANGUAGE_MODELS_MANAGER_PATH, AnthropicLanguageModelsManager } from '../common/anthropic-language-models-manager';
import { ConnectionHandler, RpcConnectionHandler } from '@theia/core';
import { AnthropicLanguageModelsManagerImpl } from './anthropic-language-models-manager-impl';

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