gen-docs.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 common_arg & opt) {
  11. file << "| `";
  12. // args
  13. auto all_args = opt.get_args();
  14. for (const auto & arg : all_args) {
  15. if (arg == all_args.front()) {
  16. file << arg;
  17. if (all_args.size() > 1) file << ", ";
  18. } else {
  19. file << arg << (arg != all_args.back() ? ", " : "");
  20. }
  21. }
  22. // value hint
  23. if (opt.value_hint) {
  24. std::string md_value_hint(opt.value_hint);
  25. string_replace_all(md_value_hint, "|", "\\|");
  26. file << " " << md_value_hint;
  27. }
  28. if (opt.value_hint_2) {
  29. std::string md_value_hint_2(opt.value_hint_2);
  30. string_replace_all(md_value_hint_2, "|", "\\|");
  31. file << " " << md_value_hint_2;
  32. }
  33. // help text
  34. std::string md_help(opt.help);
  35. string_replace_all(md_help, "\n", "<br/>");
  36. string_replace_all(md_help, "|", "\\|");
  37. file << "` | " << md_help << " |\n";
  38. }
  39. static void write_table(std::ofstream & file, std::vector<common_arg *> & opts) {
  40. write_table_header(file);
  41. for (const auto & opt : opts) {
  42. write_table_entry(file, *opt);
  43. }
  44. }
  45. static void export_md(std::string fname, llama_example ex, std::string name) {
  46. std::ofstream file(fname, std::ofstream::out | std::ofstream::trunc);
  47. common_params params;
  48. auto ctx_arg = common_params_parser_init(params, ex);
  49. std::vector<common_arg *> common_options;
  50. std::vector<common_arg *> sparam_options;
  51. std::vector<common_arg *> specific_options;
  52. for (auto & opt : ctx_arg.options) {
  53. // in case multiple LLAMA_EXAMPLE_* are set, we prioritize the LLAMA_EXAMPLE_* matching current example
  54. if (opt.is_sparam) {
  55. sparam_options.push_back(&opt);
  56. } else if (opt.in_example(ctx_arg.ex)) {
  57. specific_options.push_back(&opt);
  58. } else {
  59. common_options.push_back(&opt);
  60. }
  61. }
  62. file << "**Common params**\n\n";
  63. write_table(file, common_options);
  64. file << "\n\n**Sampling params**\n\n";
  65. write_table(file, sparam_options);
  66. file << "\n\n**" << name << "-specific params**\n\n";
  67. write_table(file, specific_options);
  68. }
  69. int main(int, char **) {
  70. // TODO: add CLI
  71. export_md("autogen-completion.md", LLAMA_EXAMPLE_COMPLETION, "Tool");
  72. export_md("autogen-server.md", LLAMA_EXAMPLE_SERVER, "Server");
  73. return 0;
  74. }