-
Notifications
You must be signed in to change notification settings - Fork 22
/
playgroundV2Manager.ts
202 lines (178 loc) · 6.92 KB
/
playgroundV2Manager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**********************************************************************
* Copyright (C) 2024 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
import type { Disposable, Webview } from '@podman-desktop/api';
import type { InferenceManager } from './inference/inferenceManager';
import OpenAI from 'openai';
import type { ChatCompletionChunk, ChatCompletionMessageParam } from 'openai/src/resources/chat/completions';
import type { ModelOptions } from '@shared/src/models/IModelOptions';
import type { Stream } from 'openai/streaming';
import { ConversationRegistry } from '../registries/conversationRegistry';
import type { Conversation, PendingChat, UserChat } from '@shared/src/models/IPlaygroundMessage';
import type { PlaygroundV2 } from '@shared/src/models/IPlaygroundV2';
import { Publisher } from '../utils/Publisher';
import { Messages } from '@shared/Messages';
import type { ModelInfo } from '@shared/src/models/IModelInfo';
import { withDefaultConfiguration } from '../utils/inferenceUtils';
export class PlaygroundV2Manager extends Publisher<PlaygroundV2[]> implements Disposable {
#playgrounds: Map<string, PlaygroundV2>;
#conversationRegistry: ConversationRegistry;
#playgroundCounter = 0;
#UIDcounter: number;
constructor(
webview: Webview,
private inferenceManager: InferenceManager,
) {
super(webview, Messages.MSG_PLAYGROUNDS_V2_UPDATE, () => this.getPlaygrounds());
this.#playgrounds = new Map();
this.#conversationRegistry = new ConversationRegistry(webview);
this.#UIDcounter = 0;
}
async createPlayground(name: string, model: ModelInfo): Promise<void> {
const id = `${this.#playgroundCounter++}`;
if (!name) {
name = this.getFreeName();
}
this.#conversationRegistry.createConversation(id);
this.#playgrounds.set(id, {
id,
name,
modelId: model.id,
});
// create/start inference server if necessary
const servers = this.inferenceManager.getServers();
const server = servers.find(s => s.models.map(mi => mi.id).includes(model.id));
if (!server) {
await this.inferenceManager.createInferenceServer(
await withDefaultConfiguration({
modelsInfo: [model],
}),
`playground-tracking-${id}`,
);
} else if (server.status === 'stopped') {
await this.inferenceManager.startInferenceServer(server.container.containerId);
}
this.notify();
}
getPlaygrounds(): PlaygroundV2[] {
return Array.from(this.#playgrounds.values());
}
private getUniqueId(): string {
return `playground-${++this.#UIDcounter}`;
}
/**
* @param playgroundId
* @param userInput the user input
* @param options the model configuration
*/
async submit(playgroundId: string, userInput: string, systemPrompt: string, options?: ModelOptions): Promise<void> {
const playground = this.#playgrounds.get(playgroundId);
if (playground === undefined) throw new Error('Playground not found.');
const servers = this.inferenceManager.getServers();
const server = servers.find(s => s.models.map(mi => mi.id).includes(playground.modelId));
if (server === undefined) throw new Error('Inference server not found.');
if (server.status !== 'running') throw new Error('Inference server is not running.');
if (server.health?.Status !== 'healthy')
throw new Error(`Inference server is not healthy, currently status: ${server.health.Status}.`);
const modelInfo = server.models.find(model => model.id === playground.modelId);
if (modelInfo === undefined)
throw new Error(
`modelId '${playground.modelId}' is not available on the inference server, valid model ids are: ${server.models.map(model => model.id).join(', ')}.`,
);
const conversation = this.#conversationRegistry.get(playground.id);
if (conversation === undefined) throw new Error(`conversation with id ${playground.id} does not exist.`);
this.#conversationRegistry.submit(conversation.id, {
content: userInput,
options: options,
role: 'user',
id: this.getUniqueId(),
timestamp: Date.now(),
} as UserChat);
const client = new OpenAI({
baseURL: `http://localhost:${server.connection.port}/v1`,
apiKey: 'dummy',
});
const messages = this.getFormattedMessages(playground.id);
if (systemPrompt) {
messages.push({ role: 'system', content: systemPrompt });
}
client.chat.completions
.create({
messages,
stream: true,
model: modelInfo.file.file,
...options,
})
.then(response => {
// process stream async
this.processStream(playground.id, response).catch((err: unknown) => {
console.error('Something went wrong while processing stream', err);
});
})
.catch((err: unknown) => {
console.error('Something went wrong while creating model reponse', err);
});
}
/**
* Given a Stream from the OpenAI library update and notify the publisher
* @param conversationId
* @param stream
*/
private async processStream(conversationId: string, stream: Stream<ChatCompletionChunk>): Promise<void> {
const messageId = this.getUniqueId();
this.#conversationRegistry.submit(conversationId, {
role: 'assistant',
choices: [],
completed: undefined,
id: messageId,
timestamp: Date.now(),
} as PendingChat);
for await (const chunk of stream) {
this.#conversationRegistry.appendChoice(conversationId, messageId, {
content: chunk.choices[0]?.delta?.content || '',
});
}
this.#conversationRegistry.completeMessage(conversationId, messageId);
}
/**
* Transform the ChatMessage interface to the OpenAI ChatCompletionMessageParam
* @private
*/
private getFormattedMessages(conversationId: string): ChatCompletionMessageParam[] {
return this.#conversationRegistry.get(conversationId).messages.map(
message =>
({
name: undefined,
...message,
}) as ChatCompletionMessageParam,
);
}
getConversations(): Conversation[] {
return this.#conversationRegistry.getAll();
}
dispose(): void {
this.#conversationRegistry.dispose();
}
getFreeName(): string {
let i = 0;
let name: string;
do {
name = `playground ${++i}`;
} while (this.getPlaygrounds().find(p => p.name === name));
return name;
}
}