Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit ceb23ed

Browse files
authored
chore: inference chat preset support (#931)
1 parent 50551de commit ceb23ed

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

cortex-js/src/infrastructure/commanders/chat.command.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,13 @@ export class ChatCommand extends BaseCommand {
108108
);
109109

110110
const preset = await this.fileService.getPreset(options.preset);
111-
111+
112112
return this.cortex.models.start(modelId, preset).then(() =>
113113
this.chatClient.chat(
114114
modelId,
115115
options.threadId,
116116
message, // Accept both message from inputs or arguments
117+
preset ? preset : {},
117118
),
118119
);
119120
}

cortex-js/src/infrastructure/commanders/services/chat-client.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export class ChatClient {
1818
modelId: string,
1919
threadId?: string,
2020
message?: string,
21+
settings?: Partial<Cortex.ChatCompletionCreateParamsStreaming>,
2122
): Promise<void> {
2223
console.log(`In order to exit, type '${this.exitClause}'.`);
2324
const thread = await this.getOrCreateNewThread(modelId, threadId);
@@ -36,8 +37,16 @@ export class ChatClient {
3637
output: stdout,
3738
prompt: this.userIndicator,
3839
});
40+
3941
if (message)
40-
this.sendCompletionMessage(message, messages, modelId, thread.id, rl);
42+
this.sendCompletionMessage(
43+
message,
44+
messages,
45+
modelId,
46+
thread.id,
47+
rl,
48+
settings,
49+
);
4150
rl.prompt();
4251

4352
rl.on('close', async () => {
@@ -46,7 +55,7 @@ export class ChatClient {
4655
});
4756

4857
rl.on('line', (input) =>
49-
this.sendCompletionMessage(input, messages, modelId, thread.id, rl),
58+
this.sendCompletionMessage(input, messages, modelId, thread.id, rl, settings),
5059
);
5160
}
5261

@@ -56,6 +65,7 @@ export class ChatClient {
5665
modelId: string,
5766
threadId: string,
5867
rl: readline.Interface,
68+
settings: Partial<Cortex.ChatCompletionCreateParamsStreaming> = {},
5969
) {
6070
if (!userInput || userInput.trim() === '') return;
6171

@@ -64,6 +74,19 @@ export class ChatClient {
6474
return;
6575
}
6676

77+
if (
78+
'pre_prompt' in settings &&
79+
!messages.some((m) => m.role === 'system')
80+
) {
81+
messages = [
82+
{
83+
content: settings.pre_prompt as string,
84+
role: 'system',
85+
},
86+
...messages,
87+
];
88+
}
89+
6790
const model = await this.cortex.models.retrieve(modelId);
6891

6992
messages.push({
@@ -89,6 +112,7 @@ export class ChatClient {
89112
model: modelId,
90113
max_tokens: 4098,
91114
temperature: 0.7,
115+
...settings,
92116
// Override with model inference params
93117
...parser.parseModelInferenceParams(model),
94118
stream: true,

cortex-js/src/infrastructure/services/download-manager/download-manager.service.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,25 @@ export class DownloadManagerService {
234234
);
235235
if (downloadItem) {
236236
downloadItem.size.transferred = transferredBytes;
237-
downloadItem.progress = Math.floor(
237+
downloadItem.progress = Math.round(
238238
(transferredBytes / totalBytes) * 100,
239239
);
240240
bar.update(downloadItem.progress);
241241
}
242242
const lastProgress = currentDownloadState.progress;
243-
currentDownloadState.progress = Math.floor(
244-
currentDownloadState.children.reduce(
245-
(pre, curr) => pre + curr.progress,
243+
currentDownloadState.progress = Math.round(
244+
(currentDownloadState.children.reduce(
245+
(pre, curr) => pre + curr.size.transferred,
246246
0,
247-
) / Math.max(currentDownloadState.children.length, 1),
247+
) /
248+
Math.max(
249+
currentDownloadState.children.reduce(
250+
(pre, curr) => pre + curr.size.total,
251+
0,
252+
),
253+
1,
254+
)) *
255+
100,
248256
);
249257
// console.log(currentDownloadState.progress);
250258
if (currentDownloadState.progress !== lastProgress)

0 commit comments

Comments
 (0)