gen-docs.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "arg.h"
  2. #include "common.h"
  3. #include <fstream>
  4. #include <sstream>
  5. #include <string>
  6. // Export usage message (-h) to markdown format
  7. // Automatically update the markdown docs
  8. #define HELP_START_MARKER "<!-- HELP_START -->"
  9. #define HELP_END_MARKER "<!-- HELP_END -->"
  10. #define NOTE_MESSAGE "<!-- IMPORTANT: The list below is auto-generated by llama-gen-docs; do NOT modify it manually -->"
  11. struct md_file {
  12. llama_example ex;
  13. std::string fname;
  14. std::string specific_section_header;
  15. };
  16. std::vector<md_file> md_files = {
  17. {LLAMA_EXAMPLE_CLI, "tools/cli/README.md", "CLI-specific params"},
  18. {LLAMA_EXAMPLE_COMPLETION, "tools/completion/README.md", "Completion-specific params"},
  19. {LLAMA_EXAMPLE_SERVER, "tools/server/README.md", "Server-specific params"},
  20. };
  21. static void write_table_header(std::ostringstream & ss) {
  22. ss << "| Argument | Explanation |\n";
  23. ss << "| -------- | ----------- |\n";
  24. }
  25. static void write_table_entry(std::ostringstream & ss, const common_arg & opt) {
  26. ss << "| `";
  27. // args
  28. auto all_args = opt.get_args();
  29. for (const auto & arg : all_args) {
  30. if (arg == all_args.front()) {
  31. ss << arg;
  32. if (all_args.size() > 1) ss << ", ";
  33. } else {
  34. ss << arg << (arg != all_args.back() ? ", " : "");
  35. }
  36. }
  37. // value hint
  38. if (opt.value_hint) {
  39. std::string md_value_hint(opt.value_hint);
  40. string_replace_all(md_value_hint, "|", "\\|");
  41. ss << " " << md_value_hint;
  42. }
  43. if (opt.value_hint_2) {
  44. std::string md_value_hint_2(opt.value_hint_2);
  45. string_replace_all(md_value_hint_2, "|", "\\|");
  46. ss << " " << md_value_hint_2;
  47. }
  48. // help text
  49. std::string md_help(opt.help);
  50. md_help = string_strip(md_help);
  51. string_replace_all(md_help, "\n", "<br/>");
  52. string_replace_all(md_help, "|", "\\|");
  53. ss << "` | " << md_help << " |\n";
  54. }
  55. static void write_table(std::ostringstream & ss, std::vector<common_arg *> & opts) {
  56. write_table_header(ss);
  57. for (const auto & opt : opts) {
  58. write_table_entry(ss, *opt);
  59. }
  60. }
  61. static void write_help(std::ostringstream & ss, const md_file & md) {
  62. common_params params;
  63. auto ctx_arg = common_params_parser_init(params, md.ex);
  64. std::vector<common_arg *> common_options;
  65. std::vector<common_arg *> sparam_options;
  66. std::vector<common_arg *> specific_options;
  67. for (auto & opt : ctx_arg.options) {
  68. // in case multiple LLAMA_EXAMPLE_* are set, we prioritize the LLAMA_EXAMPLE_* matching current example
  69. if (opt.is_sparam) {
  70. sparam_options.push_back(&opt);
  71. } else if (opt.in_example(ctx_arg.ex)) {
  72. specific_options.push_back(&opt);
  73. } else {
  74. common_options.push_back(&opt);
  75. }
  76. }
  77. ss << HELP_START_MARKER << "\n\n";
  78. ss << NOTE_MESSAGE << "\n\n";
  79. ss << "### Common params\n\n";
  80. write_table(ss, common_options);
  81. ss << "\n\n### Sampling params\n\n";
  82. write_table(ss, sparam_options);
  83. ss << "\n\n### " << md.specific_section_header << "\n\n";
  84. write_table(ss, specific_options);
  85. ss << "\n" << HELP_END_MARKER;
  86. }
  87. int main(int, char **) {
  88. for (const auto & md : md_files) {
  89. std::ifstream infile(md.fname);
  90. if (!infile.is_open()) {
  91. fprintf(stderr, "failed to open file '%s' for reading\n", md.fname.c_str());
  92. return 1;
  93. }
  94. std::ostringstream ss;
  95. ss << infile.rdbuf();
  96. infile.close();
  97. std::string content = ss.str();
  98. size_t help_start = content.find(HELP_START_MARKER);
  99. size_t help_end = content.find(HELP_END_MARKER);
  100. if (help_start == std::string::npos || help_end == std::string::npos || help_end <= help_start) {
  101. fprintf(stderr, "failed to find help markers in file '%s'\n", md.fname.c_str());
  102. return 1;
  103. }
  104. std::ostringstream new_help_ss;
  105. write_help(new_help_ss, md);
  106. std::string new_help = new_help_ss.str();
  107. content = content.substr(0, help_start) + new_help + content.substr(help_end + strlen(HELP_END_MARKER));
  108. std::ofstream outfile(md.fname);
  109. if (!outfile.is_open()) {
  110. fprintf(stderr, "failed to open file '%s' for writing\n", md.fname.c_str());
  111. return 1;
  112. }
  113. outfile << content;
  114. outfile.close();
  115. printf("Updated help in '%s'\n", md.fname.c_str());
  116. }
  117. return 0;
  118. }