Skip to content

Commit

Permalink
feat: Ollama integration for Theia AI
Browse files Browse the repository at this point in the history
Integrates Ollama language models into Theia via the new 'ai-ollama'
package. The endpoint and models can be configured via the preferences.
  • Loading branch information
dhuebner authored and sdirix committed Sep 11, 2024
1 parent 9e7f724 commit 1a32b06
Show file tree
Hide file tree
Showing 21 changed files with 696 additions and 11 deletions.
1 change: 1 addition & 0 deletions examples/browser-only/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@theia/ai-core": "1.53.0",
"@theia/ai-history": "1.53.0",
"@theia/ai-openai": "1.53.0",
"@theia/ai-ollama": "1.53.0",
"@theia/api-samples": "1.53.0",
"@theia/bulk-edit": "1.53.0",
"@theia/callhierarchy": "1.53.0",
Expand Down
3 changes: 3 additions & 0 deletions examples/browser-only/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-ollama"
},
{
"path": "../../packages/ai-openai"
},
Expand Down
1 change: 1 addition & 0 deletions examples/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@theia/ai-core": "1.53.0",
"@theia/ai-history": "1.53.0",
"@theia/ai-openai": "1.53.0",
"@theia/ai-ollama": "1.53.0",
"@theia/ai-terminal": "1.53.0",
"@theia/ai-workspace-agent": "1.53.0",
"@theia/api-provider-sample": "1.53.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-ollama"
},
{
"path": "../../packages/ai-openai"
},
Expand Down
1 change: 1 addition & 0 deletions examples/electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@theia/ai-core": "1.53.0",
"@theia/ai-history": "1.53.0",
"@theia/ai-openai": "1.53.0",
"@theia/ai-ollama": "1.53.0",
"@theia/ai-terminal": "1.53.0",
"@theia/ai-workspace-agent": "1.53.0",
"@theia/api-provider-sample": "1.53.0",
Expand Down
3 changes: 3 additions & 0 deletions examples/electron/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
{
"path": "../../packages/ai-history"
},
{
"path": "../../packages/ai-ollama"
},
{
"path": "../../packages/ai-openai"
},
Expand Down
10 changes: 10 additions & 0 deletions packages/ai-ollama/.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'
}
};
30 changes: 30 additions & 0 deletions packages/ai-ollama/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<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 - Ollama EXTENSION</h2>

<hr />

</div>

## Description

The `@theia/ai-ollama` integrates Ollama's models with Theia AI.

## 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>
53 changes: 53 additions & 0 deletions packages/ai-ollama/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "@theia/ai-ollama",
"version": "1.53.0",
"description": "Theia - Ollama Integration",
"dependencies": {
"@theia/core": "1.53.0",
"@theia/filesystem": "1.53.0",
"@theia/workspace": "1.53.0",
"minimatch": "^5.1.0",
"tslib": "^2.6.2",
"ollama": "^0.5.8",
"@theia/ai-core": "1.53.0"
},
"publishConfig": {
"access": "public"
},
"theiaExtensions": [
{
"frontend": "lib/browser/ollama-frontend-module",
"backend": "lib/node/ollama-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.53.0"
},
"nyc": {
"extends": "../../configs/nyc.json"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// *****************************************************************************
// Copyright (C) 2024 TypeFox 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 { OllamaLanguageModelsManager } from '../common';
import { HOST_PREF, MODELS_PREF } from './ollama-preferences';

@injectable()
export class OllamaFrontendApplicationContribution implements FrontendApplicationContribution {

@inject(PreferenceService)
protected preferenceService: PreferenceService;

@inject(OllamaLanguageModelsManager)
protected manager: OllamaLanguageModelsManager;

protected prevModels: string[] = [];

onStart(): void {
this.preferenceService.ready.then(() => {
const host = this.preferenceService.get<string>(HOST_PREF, 'http://localhost:11434');
this.manager.setHost(host);

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

this.preferenceService.onPreferenceChanged(event => {
if (event.preferenceName === HOST_PREF) {
this.manager.setHost(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);
this.manager.createLanguageModels(...modelsToAdd);
this.prevModels = [...event.newValue];
}
});
});
}
}
31 changes: 31 additions & 0 deletions packages/ai-ollama/src/browser/ollama-frontend-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// *****************************************************************************
// Copyright (C) 2024 TypeFox 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 { OllamaPreferencesSchema } from './ollama-preferences';
import { FrontendApplicationContribution, PreferenceContribution, RemoteConnectionProvider, ServiceConnectionProvider } from '@theia/core/lib/browser';
import { OllamaFrontendApplicationContribution } from './ollama-frontend-application-contribution';
import { OLLAMA_LANGUAGE_MODELS_MANAGER_PATH, OllamaLanguageModelsManager } from '../common';

export default new ContainerModule(bind => {
bind(PreferenceContribution).toConstantValue({ schema: OllamaPreferencesSchema });
bind(OllamaFrontendApplicationContribution).toSelf().inSingletonScope();
bind(FrontendApplicationContribution).toService(OllamaFrontendApplicationContribution);
bind(OllamaLanguageModelsManager).toDynamicValue(ctx => {
const provider = ctx.container.get<ServiceConnectionProvider>(RemoteConnectionProvider);
return provider.createProxy<OllamaLanguageModelsManager>(OLLAMA_LANGUAGE_MODELS_MANAGER_PATH);
}).inSingletonScope();
});
41 changes: 41 additions & 0 deletions packages/ai-ollama/src/browser/ollama-preferences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// *****************************************************************************
// Copyright (C) 2024 TypeFox 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 HOST_PREF = 'ai-features.ollama.host';
export const MODELS_PREF = 'ai-features.ollama.models';

export const OllamaPreferencesSchema: PreferenceSchema = {
type: 'object',
properties: {
[HOST_PREF]: {
type: 'string',
title: AI_CORE_PREFERENCES_TITLE,
description: 'Ollama Host',
default: 'http://localhost:11434'
},
[MODELS_PREF]: {
type: 'array',
title: AI_CORE_PREFERENCES_TITLE,
default: ['llama3', 'gemma2'],
items: {
type: 'string'
}
}
}
};
16 changes: 16 additions & 0 deletions packages/ai-ollama/src/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// *****************************************************************************
// Copyright (C) 2024 TypeFox 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 './ollama-language-models-manager';
23 changes: 23 additions & 0 deletions packages/ai-ollama/src/common/ollama-language-models-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// *****************************************************************************
// Copyright (C) 2024 TypeFox 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 OLLAMA_LANGUAGE_MODELS_MANAGER_PATH = '/services/ollama/language-model-manager';
export const OllamaLanguageModelsManager = Symbol('OllamaLanguageModelsManager');
export interface OllamaLanguageModelsManager {
host: string | undefined;
setHost(host: string | undefined): void;
createLanguageModels(...modelIds: string[]): Promise<void>;
removeLanguageModels(...modelIds: string[]): void
}
30 changes: 30 additions & 0 deletions packages/ai-ollama/src/node/ollama-backend-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// *****************************************************************************
// Copyright (C) 2024 TypeFox 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 { OLLAMA_LANGUAGE_MODELS_MANAGER_PATH, OllamaLanguageModelsManager } from '../common/ollama-language-models-manager';
import { ConnectionHandler, RpcConnectionHandler } from '@theia/core';
import { OllamaLanguageModelsManagerImpl } from './ollama-language-models-manager-impl';

export const OllamaModelFactory = Symbol('OllamaModelFactory');

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

0 comments on commit 1a32b06

Please sign in to comment.