preset.h 2.5 KB

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