gen-docs.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "arg.h"
  2. #include "common.h"
  3. #include <fstream>
  4. #include <string>
  5. // Export usage message (-h) to markdown format
  6. static void write_table_header(std::ofstream & file) {
  7. file << "| Argument | Explanation |\n";
  8. file << "| -------- | ----------- |\n";
  9. }
  10. static void write_table_entry(std::ofstream & file, const llama_arg & opt) {
  11. file << "| `";
  12. // args
  13. for (const auto & arg : opt.args) {
  14. if (arg == opt.args.front()) {
  15. file << arg;
  16. if (opt.args.size() > 1) file << ", ";
  17. } else {
  18. file << arg << (arg != opt.args.back() ? ", " : "");
  19. }
  20. }
  21. // value hint
  22. if (opt.value_hint) {
  23. std::string md_value_hint(opt.value_hint);
  24. string_replace_all(md_value_hint, "|", "\\|");
  25. file << " " << md_value_hint;
  26. }
  27. if (opt.value_hint_2) {
  28. std::string md_value_hint_2(opt.value_hint_2);
  29. string_replace_all(md_value_hint_2, "|", "\\|");
  30. file << " " << md_value_hint_2;
  31. }
  32. // help text
  33. std::string md_help(opt.help);
  34. string_replace_all(md_help, "\n", "<br/>");
  35. string_replace_all(md_help, "|", "\\|");
  36. file << "` | " << md_help << " |\n";
  37. }
  38. static void write_table(std::ofstream & file, std::vector<llama_arg *> & opts) {
  39. write_table_header(file);
  40. for (const auto & opt : opts) {
  41. write_table_entry(file, *opt);
  42. }
  43. }
  44. static void export_md(std::string fname, llama_example ex) {
  45. std::ofstream file(fname, std::ofstream::out | std::ofstream::trunc);
  46. gpt_params params;
  47. auto ctx_arg = gpt_params_parser_init(params, ex);
  48. std::vector<llama_arg *> common_options;
  49. std::vector<llama_arg *> sparam_options;
  50. std::vector<llama_arg *> specific_options;
  51. for (auto & opt : ctx_arg.options) {
  52. // in case multiple LLAMA_EXAMPLE_* are set, we prioritize the LLAMA_EXAMPLE_* matching current example
  53. if (opt.is_sparam) {
  54. sparam_options.push_back(&opt);
  55. } else if (opt.in_example(ctx_arg.ex)) {
  56. specific_options.push_back(&opt);
  57. } else {
  58. common_options.push_back(&opt);
  59. }
  60. }
  61. file << "**Common params**\n\n";
  62. write_table(file, common_options);
  63. file << "\n\n**Sampling params**\n\n";
  64. write_table(file, sparam_options);
  65. file << "\n\n**Example-specific params**\n\n";
  66. write_table(file, specific_options);
  67. }
  68. int main(int, char **) {
  69. export_md("autogen-main.md", LLAMA_EXAMPLE_MAIN);
  70. export_md("autogen-server.md", LLAMA_EXAMPLE_SERVER);
  71. return 0;
  72. }