Skip to content

Commit

Permalink
Merge pull request #14 from jeffmaury/playground-manager
Browse files Browse the repository at this point in the history
feat: add playground manager
  • Loading branch information
feloy authored Jan 11, 2024
2 parents 32ddc85 + 99a4e53 commit 18eb886
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions packages/backend/src/playground.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { provider, containerEngine, type ProviderContainerConnection, type ImageInfo } from '@podman-desktop/api';
import path from 'node:path';

const LOCALAI_IMAGE = 'quay.io/go-skynet/local-ai:v2.5.1';

function findFirstProvider(): ProviderContainerConnection | undefined {
const engines = provider
.getContainerConnections()
.filter(connection => connection.connection.type === 'podman')
.filter(connection => connection.connection.status() == 'started');
return engines.length > 0 ? engines[0] : undefined;
}

export class PlayGroundManager {
async selectImage(connection: ProviderContainerConnection, image: string): Promise<ImageInfo | undefined> {
const images = (await containerEngine.listImages()).filter(im => im.RepoTags.some(tag => tag === image));
return images.length > 0 ? images[0] : undefined;
}

async startPlayground(modelId: string, modelPath: string): Promise<string> {
const connection = findFirstProvider();
if (!connection) {
throw new Error('Unable to find an engine to start playground');
}

let image = await this.selectImage(connection, LOCALAI_IMAGE);
if (!image) {
await containerEngine.pullImage(connection.connection, LOCALAI_IMAGE, () => {});
image = await this.selectImage(connection, LOCALAI_IMAGE);
if (!image) {
throw new Error(`Unable to find ${LOCALAI_IMAGE} image`);
}
}
const result = await containerEngine.createContainer(image.engineId, {
Image: image.Id,
Detach: true,
ExposedPorts: { '9000': '8080' },
HostConfig: {
AutoRemove: true,
Mounts: [
{
Target: '/models',
Source: path.dirname(modelPath),
Type: 'bind',
},
],
},
Cmd: ['--models-path', '/models', '--context-size', '700', '--threads', '4'],
});
return result.id;
}

async stopPlayground(playgroundId: string): Promise<void> {
const connection = findFirstProvider();
if (!connection) {
throw new Error('Unable to find an engine to start playground');
}
return containerEngine.stopContainer(connection.providerId, playgroundId);
}
}

0 comments on commit 18eb886

Please sign in to comment.