preset.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #pragma once
  2. #include "common.h"
  3. #include "arg.h"
  4. #include <string>
  5. #include <vector>
  6. #include <map>
  7. #include <set>
  8. //
  9. // INI preset parser and writer
  10. //
  11. constexpr const char * COMMON_PRESET_DEFAULT_NAME = "default";
  12. struct common_preset_context;
  13. struct common_preset {
  14. std::string name;
  15. // options are stored as common_arg to string mapping, representing CLI arg and its value
  16. std::map<common_arg, std::string> options;
  17. // convert preset to CLI argument list
  18. std::vector<std::string> to_args(const std::string & bin_path = "") const;
  19. // convert preset to INI format string
  20. std::string to_ini() const;
  21. // TODO: maybe implement to_env() if needed
  22. // modify preset options where argument is identified by its env variable
  23. void set_option(const common_preset_context & ctx, const std::string & env, const std::string & value);
  24. // unset option by its env variable
  25. void unset_option(const std::string & env);
  26. // get option value by its env variable, return false if not found
  27. bool get_option(const std::string & env, std::string & value) const;
  28. // merge another preset into this one, overwriting existing options
  29. void merge(const common_preset & other);
  30. // apply preset options to common_params
  31. void apply_to_params(common_params & params) const;
  32. };
  33. // interface for multiple presets in one file
  34. using common_presets = std::map<std::string, common_preset>;
  35. // context for loading and editing presets
  36. struct common_preset_context {
  37. common_params default_params; // unused for now
  38. common_params_context ctx_params;
  39. std::map<std::string, common_arg> key_to_opt;
  40. bool filter_allowed_keys = false;
  41. std::set<std::string> allowed_keys;
  42. // if only_remote_allowed is true, only accept whitelisted keys
  43. common_preset_context(llama_example ex, bool only_remote_allowed = false);
  44. // load presets from INI file
  45. common_presets load_from_ini(const std::string & path, common_preset & global) const;
  46. // generate presets from cached models
  47. common_presets load_from_cache() const;
  48. // generate presets from local models directory
  49. // for the directory structure, see "Using multiple models" in server/README.md
  50. common_presets load_from_models_dir(const std::string & models_dir) const;
  51. // generate one preset from CLI arguments
  52. common_preset load_from_args(int argc, char ** argv) const;
  53. // cascade multiple presets if exist on both: base < added
  54. // if preset does not exist in base, it will be added without modification
  55. common_presets cascade(const common_presets & base, const common_presets & added) const;
  56. // apply presets over a base preset (same idea as CSS cascading)
  57. common_presets cascade(const common_preset & base, const common_presets & presets) const;
  58. };