arg.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #pragma once
  2. #include "common.h"
  3. #include <set>
  4. #include <map>
  5. #include <string>
  6. #include <vector>
  7. #include <cstring>
  8. // pseudo-env variable to identify preset-only arguments
  9. #define COMMON_ARG_PRESET_LOAD_ON_STARTUP "__PRESET_LOAD_ON_STARTUP"
  10. //
  11. // CLI argument parsing
  12. //
  13. struct common_arg {
  14. std::set<enum llama_example> examples = {LLAMA_EXAMPLE_COMMON};
  15. std::set<enum llama_example> excludes = {};
  16. std::vector<const char *> args;
  17. std::vector<const char *> args_neg; // for negated args like --no-xxx
  18. const char * value_hint = nullptr; // help text or example for arg value
  19. const char * value_hint_2 = nullptr; // for second arg value
  20. const char * env = nullptr;
  21. std::string help;
  22. bool is_sparam = false; // is current arg a sampling param?
  23. bool is_preset_only = false; // is current arg preset-only (not treated as CLI arg)
  24. void (*handler_void) (common_params & params) = nullptr;
  25. void (*handler_string) (common_params & params, const std::string &) = nullptr;
  26. void (*handler_str_str)(common_params & params, const std::string &, const std::string &) = nullptr;
  27. void (*handler_int) (common_params & params, int) = nullptr;
  28. void (*handler_bool) (common_params & params, bool) = nullptr;
  29. common_arg() = default;
  30. common_arg(
  31. const std::initializer_list<const char *> & args,
  32. const char * value_hint,
  33. const std::string & help,
  34. void (*handler)(common_params & params, const std::string &)
  35. ) : args(args), value_hint(value_hint), help(help), handler_string(handler) {}
  36. common_arg(
  37. const std::initializer_list<const char *> & args,
  38. const char * value_hint,
  39. const std::string & help,
  40. void (*handler)(common_params & params, int)
  41. ) : args(args), value_hint(value_hint), help(help), handler_int(handler) {}
  42. common_arg(
  43. const std::initializer_list<const char *> & args,
  44. const std::string & help,
  45. void (*handler)(common_params & params)
  46. ) : args(args), help(help), handler_void(handler) {}
  47. common_arg(
  48. const std::initializer_list<const char *> & args,
  49. const std::initializer_list<const char *> & args_neg,
  50. const std::string & help,
  51. void (*handler)(common_params & params, bool)
  52. ) : args(args), args_neg(args_neg), help(help), handler_bool(handler) {}
  53. // support 2 values for arg
  54. common_arg(
  55. const std::initializer_list<const char *> & args,
  56. const char * value_hint,
  57. const char * value_hint_2,
  58. const std::string & help,
  59. void (*handler)(common_params & params, const std::string &, const std::string &)
  60. ) : args(args), value_hint(value_hint), value_hint_2(value_hint_2), help(help), handler_str_str(handler) {}
  61. common_arg & set_examples(std::initializer_list<enum llama_example> examples);
  62. common_arg & set_excludes(std::initializer_list<enum llama_example> excludes);
  63. common_arg & set_env(const char * env);
  64. common_arg & set_sparam();
  65. common_arg & set_preset_only();
  66. bool in_example(enum llama_example ex);
  67. bool is_exclude(enum llama_example ex);
  68. bool get_value_from_env(std::string & output) const;
  69. bool has_value_from_env() const;
  70. std::string to_string() const;
  71. // for using as key in std::map
  72. bool operator<(const common_arg& other) const {
  73. if (args.empty() || other.args.empty()) {
  74. return false;
  75. }
  76. return strcmp(args[0], other.args[0]) < 0;
  77. }
  78. bool operator==(const common_arg& other) const {
  79. if (args.empty() || other.args.empty()) {
  80. return false;
  81. }
  82. return strcmp(args[0], other.args[0]) == 0;
  83. }
  84. // get all args and env vars (including negated args/env)
  85. std::vector<std::string> get_args() const;
  86. std::vector<std::string> get_env() const;
  87. };
  88. namespace common_arg_utils {
  89. bool is_truthy(const std::string & value);
  90. bool is_falsey(const std::string & value);
  91. bool is_autoy(const std::string & value);
  92. }
  93. struct common_params_context {
  94. enum llama_example ex = LLAMA_EXAMPLE_COMMON;
  95. common_params & params;
  96. std::vector<common_arg> options;
  97. void(*print_usage)(int, char **) = nullptr;
  98. common_params_context(common_params & params) : params(params) {}
  99. };
  100. // parse input arguments from CLI
  101. // if one argument has invalid value, it will automatically display usage of the specific argument (and not the full usage message)
  102. bool common_params_parse(int argc, char ** argv, common_params & params, llama_example ex, void(*print_usage)(int, char **) = nullptr);
  103. // parse input arguments from CLI into a map
  104. bool common_params_to_map(int argc, char ** argv, llama_example ex, std::map<common_arg, std::string> & out_map);
  105. // populate preset-only arguments
  106. // these arguments are not treated as command line arguments
  107. // see: https://github.com/ggml-org/llama.cpp/issues/18163
  108. void common_params_add_preset_options(std::vector<common_arg> & args);
  109. // initialize argument parser context - used by test-arg-parser and preset
  110. common_params_context common_params_parser_init(common_params & params, llama_example ex, void(*print_usage)(int, char **) = nullptr);
  111. struct common_remote_params {
  112. std::vector<std::string> headers;
  113. long timeout = 0; // CURLOPT_TIMEOUT, in seconds ; 0 means no timeout
  114. long max_size = 0; // max size of the response ; unlimited if 0 ; max is 2GB
  115. };
  116. // get remote file content, returns <http_code, raw_response_body>
  117. std::pair<long, std::vector<char>> common_remote_get_content(const std::string & url, const common_remote_params & params);