-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #588 from janhq/feat/update-model-setting
feat: add model settings and prompt template from hf
- Loading branch information
Showing
10 changed files
with
379 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
cortex-js/src/infrastructure/commanders/models/model-update.command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { CommandRunner, SubCommand, Option } from 'nest-commander'; | ||
import { ModelsCliUsecases } from '../usecases/models.cli.usecases'; | ||
import { exit } from 'node:process'; | ||
import { ModelParameterParser } from '../utils/model-parameter.parser'; | ||
import { | ||
ModelRuntimeParams, | ||
ModelSettingParams, | ||
} from '@/domain/models/model.interface'; | ||
|
||
type UpdateOptions = { | ||
model?: string; | ||
options?: string[]; | ||
}; | ||
|
||
@SubCommand({ name: 'update', description: 'Update configuration of a model.' }) | ||
export class ModelUpdateCommand extends CommandRunner { | ||
constructor(private readonly modelsCliUsecases: ModelsCliUsecases) { | ||
super(); | ||
} | ||
|
||
async run(_input: string[], option: UpdateOptions): Promise<void> { | ||
const modelId = option.model; | ||
if (!modelId) { | ||
console.error('Model Id is required'); | ||
exit(1); | ||
} | ||
|
||
const options = option.options; | ||
if (!options || options.length === 0) { | ||
console.log('Nothing to update'); | ||
exit(0); | ||
} | ||
|
||
const parser = new ModelParameterParser(); | ||
const settingParams: ModelSettingParams = {}; | ||
const runtimeParams: ModelRuntimeParams = {}; | ||
|
||
options.forEach((option) => { | ||
const [key, stringValue] = option.split('='); | ||
if (parser.isModelSettingParam(key)) { | ||
const value = parser.parse(key, stringValue); | ||
// @ts-expect-error did the check so it's safe | ||
settingParams[key] = value; | ||
} else if (parser.isModelRuntimeParam(key)) { | ||
const value = parser.parse(key, stringValue); | ||
// @ts-expect-error did the check so it's safe | ||
runtimeParams[key] = value; | ||
} | ||
}); | ||
|
||
if (Object.keys(settingParams).length > 0) { | ||
const updatedSettingParams = | ||
await this.modelsCliUsecases.updateModelSettingParams( | ||
modelId, | ||
settingParams, | ||
); | ||
console.log( | ||
'Updated setting params! New setting params:', | ||
updatedSettingParams, | ||
); | ||
} | ||
|
||
if (Object.keys(runtimeParams).length > 0) { | ||
await this.modelsCliUsecases.updateModelRuntimeParams( | ||
modelId, | ||
runtimeParams, | ||
); | ||
console.log('Updated runtime params! New runtime params:', runtimeParams); | ||
} | ||
} | ||
|
||
@Option({ | ||
flags: '-m, --model <model_id>', | ||
required: true, | ||
description: 'Model Id to update', | ||
}) | ||
parseModelId(value: string) { | ||
return value; | ||
} | ||
|
||
@Option({ | ||
flags: '-c, --options <options...>', | ||
description: | ||
'Specify the options to update the model. Syntax: -c option1=value1 option2=value2. For example: cortex models update -c max_tokens=100 temperature=0.5', | ||
}) | ||
parseOptions(option: string, optionsAccumulator: string[] = []): string[] { | ||
optionsAccumulator.push(option); | ||
return optionsAccumulator; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
cortex-js/src/infrastructure/commanders/prompt-constants.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//// HF Chat template | ||
export const OPEN_CHAT_3_5_JINJA = ``; | ||
|
||
export const ZEPHYR_JINJA = `{% for message in messages %} | ||
{% if message['role'] == 'user' %} | ||
{{ '<|user|> | ||
' + message['content'] + eos_token }} | ||
{% elif message['role'] == 'system' %} | ||
{{ '<|system|> | ||
' + message['content'] + eos_token }} | ||
{% elif message['role'] == 'assistant' %} | ||
{{ '<|assistant|> | ||
' + message['content'] + eos_token }} | ||
{% endif %} | ||
{% if loop.last and add_generation_prompt %} | ||
{{ '<|assistant|>' }} | ||
{% endif %} | ||
{% endfor %}`; | ||
|
||
//// Corresponding prompt template | ||
export const OPEN_CHAT_3_5 = `GPT4 Correct User: {prompt}<|end_of_turn|>GPT4 Correct Assistant:`; | ||
|
||
export const ZEPHYR = `<|system|> | ||
{system_message}</s> | ||
<|user|> | ||
{prompt}</s> | ||
<|assistant|> | ||
`; | ||
|
||
export const COMMAND_R = `<BOS_TOKEN><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>{system}<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|USER_TOKEN|>{prompt}<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>{response} | ||
`; | ||
|
||
// getting from https://huggingface.co/TheBloke/Llama-2-70B-Chat-GGUF | ||
export const LLAMA_2 = `[INST] <<SYS>> | ||
{system_message} | ||
<</SYS>> | ||
{prompt}[/INST]`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.