This repository was archived by the owner on Jul 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 181
Model update command/api #1309
Merged
nguyenhoangthuan99
merged 15 commits into
feat/new-model-folder
from
feat/model-update-cmd
Sep 24, 2024
Merged
Model update command/api #1309
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
917d459
Model update command/api
nguyenhoangthuan99 4449292
Merge branch 'dev' of github.com:janhq/cortex.cpp into feat/model-upd…
nguyenhoangthuan99 2f9d03b
Merge branch 'feat/new-model-folder' into feat/model-update-cmd
nguyenhoangthuan99 b88b1bd
fix: resume download failed
namchuai ae8be75
Merge pull request #1314 from janhq/j/fix-resume-download
namchuai 5e7bda4
fix: align github syntax for cuda (#1316)
vansangpfiev 623e870
fix: require sudo for cortex update (#1318)
vansangpfiev 8fc9316
Merge branch 'dev' of github.com:janhq/cortex.cpp into feat/model-upd…
nguyenhoangthuan99 c5d00bf
refactor code
nguyenhoangthuan99 a4c2644
Merge branch 'feat/model-update-cmd' of github.com:janhq/cortex.cpp i…
nguyenhoangthuan99 5a4eb0c
Format code
nguyenhoangthuan99 309fd56
Merge branch 'feat/new-model-folder' of github.com:janhq/cortex.cpp i…
nguyenhoangthuan99 6a4e7dd
Add clean up when finish test
nguyenhoangthuan99 fe1c700
remove model.list after finish test
nguyenhoangthuan99 a6b2bc4
Fix windows CI build
nguyenhoangthuan99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,127 @@ | ||
| #include "model_upd_cmd.h" | ||
|
|
||
| #include "utils/logging_utils.h" | ||
|
|
||
| namespace commands { | ||
|
|
||
| ModelUpdCmd::ModelUpdCmd(std::string model_handle) | ||
| : model_handle_(std::move(model_handle)) {} | ||
|
|
||
| void ModelUpdCmd::Exec( | ||
| const std::unordered_map<std::string, std::string>& options) { | ||
| try { | ||
| auto model_entry = model_list_utils_.GetModelInfo(model_handle_); | ||
| yaml_handler_.ModelConfigFromFile(model_entry.path_to_model_yaml); | ||
| model_config_ = yaml_handler_.GetModelConfig(); | ||
|
|
||
| for (const auto& [key, value] : options) { | ||
| if (!value.empty()) { | ||
| UpdateConfig(key, value); | ||
| } | ||
| } | ||
|
|
||
| yaml_handler_.UpdateModelConfig(model_config_); | ||
| yaml_handler_.WriteYamlFile(model_entry.path_to_model_yaml); | ||
| CLI_LOG("Successfully updated model ID '" + model_handle_ + "'!"); | ||
| } catch (const std::exception& e) { | ||
| CLI_LOG("Failed to update model with model ID '" + model_handle_ + | ||
| "': " + e.what()); | ||
| } | ||
| } | ||
|
|
||
| void ModelUpdCmd::UpdateConfig(const std::string& key, | ||
| const std::string& value) { | ||
| static const std::unordered_map< | ||
| std::string, | ||
| std::function<void(ModelUpdCmd*, const std::string&, const std::string&)>> | ||
| updaters = { | ||
| {"name", | ||
| [](ModelUpdCmd* self, const std::string&, const std::string& v) { | ||
| self->model_config_.name = v; | ||
| }}, | ||
| {"model", | ||
| [](ModelUpdCmd* self, const std::string&, const std::string& v) { | ||
| self->model_config_.model = v; | ||
| }}, | ||
| {"version", | ||
| [](ModelUpdCmd* self, const std::string&, const std::string& v) { | ||
| self->model_config_.version = v; | ||
| }}, | ||
| {"stop", &ModelUpdCmd::UpdateVectorField}, | ||
| {"top_p", | ||
| [](ModelUpdCmd* self, const std::string& k, const std::string& v) { | ||
| self->UpdateNumericField( | ||
| k, v, [self](float f) { self->model_config_.top_p = f; }); | ||
| }}, | ||
| {"temperature", | ||
| [](ModelUpdCmd* self, const std::string& k, const std::string& v) { | ||
| self->UpdateNumericField(k, v, [self](float f) { | ||
| self->model_config_.temperature = f; | ||
| }); | ||
| }}, | ||
| {"frequency_penalty", | ||
| [](ModelUpdCmd* self, const std::string& k, const std::string& v) { | ||
| self->UpdateNumericField(k, v, [self](float f) { | ||
| self->model_config_.frequency_penalty = f; | ||
| }); | ||
| }}, | ||
| {"presence_penalty", | ||
| [](ModelUpdCmd* self, const std::string& k, const std::string& v) { | ||
| self->UpdateNumericField(k, v, [self](float f) { | ||
| self->model_config_.presence_penalty = f; | ||
| }); | ||
| }}, | ||
| {"max_tokens", | ||
| [](ModelUpdCmd* self, const std::string& k, const std::string& v) { | ||
| self->UpdateNumericField(k, v, [self](float f) { | ||
| self->model_config_.max_tokens = static_cast<int>(f); | ||
| }); | ||
| }}, | ||
| {"stream", | ||
| [](ModelUpdCmd* self, const std::string& k, const std::string& v) { | ||
| self->UpdateBooleanField( | ||
| k, v, [self](bool b) { self->model_config_.stream = b; }); | ||
| }}, | ||
| // Add more fields here... | ||
| }; | ||
|
|
||
| if (auto it = updaters.find(key); it != updaters.end()) { | ||
| it->second(this, key, value); | ||
| LogUpdate(key, value); | ||
| } | ||
| } | ||
|
|
||
| void ModelUpdCmd::UpdateVectorField(const std::string& key, | ||
| const std::string& value) { | ||
| std::vector<std::string> tokens; | ||
| std::istringstream iss(value); | ||
| std::string token; | ||
| while (std::getline(iss, token, ',')) { | ||
| tokens.push_back(token); | ||
| } | ||
| model_config_.stop = tokens; | ||
| } | ||
|
|
||
| void ModelUpdCmd::UpdateNumericField(const std::string& key, | ||
| const std::string& value, | ||
| std::function<void(float)> setter) { | ||
| try { | ||
| float numericValue = std::stof(value); | ||
| setter(numericValue); | ||
| } catch (const std::exception& e) { | ||
| CLI_LOG("Failed to parse numeric value for " << key << ": " << e.what()); | ||
| } | ||
| } | ||
|
|
||
| void ModelUpdCmd::UpdateBooleanField(const std::string& key, | ||
| const std::string& value, | ||
| std::function<void(bool)> setter) { | ||
| bool boolValue = (value == "true" || value == "1"); | ||
| setter(boolValue); | ||
| } | ||
|
|
||
| void ModelUpdCmd::LogUpdate(const std::string& key, const std::string& value) { | ||
| CLI_LOG("Updated " << key << " to: " << value); | ||
| } | ||
|
|
||
| } // namespace commands | ||
This file contains hidden or 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,30 @@ | ||
| #pragma once | ||
| #include <iostream> | ||
| #include <optional> | ||
| #include <string> | ||
| #include <unordered_map> | ||
| #include <vector> | ||
| #include "config/model_config.h" | ||
| #include "utils/modellist_utils.h" | ||
| #include "config/yaml_config.h" | ||
| namespace commands { | ||
| class ModelUpdCmd { | ||
| public: | ||
| ModelUpdCmd(std::string model_handle); | ||
| void Exec(const std::unordered_map<std::string, std::string>& options); | ||
|
|
||
| private: | ||
| std::string model_handle_; | ||
| config::ModelConfig model_config_; | ||
| config::YamlHandler yaml_handler_; | ||
| modellist_utils::ModelListUtils model_list_utils_; | ||
|
|
||
| void UpdateConfig(const std::string& key, const std::string& value); | ||
| void UpdateVectorField(const std::string& key, const std::string& value); | ||
| void UpdateNumericField(const std::string& key, const std::string& value, | ||
| std::function<void(float)> setter); | ||
| void UpdateBooleanField(const std::string& key, const std::string& value, | ||
| std::function<void(bool)> setter); | ||
| void LogUpdate(const std::string& key, const std::string& value); | ||
| }; | ||
| } // namespace commands |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I think there's a better way. We can define a function: UpdateModelConfig(const std::string& attr, std::variant<std::string, explicit_int, explicit_bool>) and handle the update logic there.
However if this works for you then I'm fine with it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think should keep it this way. We don't know which format user will input, so need to handle it for specific cases