chat.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Chat support (incl. tool call grammar constraining & output parsing) w/ generic & custom template handlers.
  2. #pragma once
  3. #include "common.h"
  4. #include <string>
  5. #include <vector>
  6. struct common_chat_templates;
  7. struct common_chat_tool_call {
  8. std::string name;
  9. std::string arguments;
  10. std::string id;
  11. };
  12. struct common_chat_msg_content_part {
  13. std::string type;
  14. std::string text;
  15. };
  16. struct common_chat_msg {
  17. std::string role;
  18. std::string content;
  19. std::vector<common_chat_msg_content_part> content_parts = {};
  20. std::vector<common_chat_tool_call> tool_calls = {};
  21. std::string reasoning_content;
  22. std::string tool_name;
  23. std::string tool_call_id;
  24. };
  25. struct common_chat_tool {
  26. std::string name;
  27. std::string description;
  28. std::string parameters;
  29. };
  30. enum common_chat_tool_choice {
  31. COMMON_CHAT_TOOL_CHOICE_AUTO,
  32. COMMON_CHAT_TOOL_CHOICE_REQUIRED,
  33. COMMON_CHAT_TOOL_CHOICE_NONE,
  34. };
  35. enum common_chat_format {
  36. COMMON_CHAT_FORMAT_CONTENT_ONLY,
  37. COMMON_CHAT_FORMAT_GENERIC,
  38. COMMON_CHAT_FORMAT_MISTRAL_NEMO,
  39. COMMON_CHAT_FORMAT_LLAMA_3_X,
  40. COMMON_CHAT_FORMAT_LLAMA_3_X_WITH_BUILTIN_TOOLS,
  41. COMMON_CHAT_FORMAT_DEEPSEEK_R1,
  42. COMMON_CHAT_FORMAT_DEEPSEEK_R1_EXTRACT_REASONING,
  43. COMMON_CHAT_FORMAT_FIREFUNCTION_V2,
  44. COMMON_CHAT_FORMAT_FUNCTIONARY_V3_2,
  45. COMMON_CHAT_FORMAT_FUNCTIONARY_V3_1_LLAMA_3_1,
  46. COMMON_CHAT_FORMAT_HERMES_2_PRO,
  47. COMMON_CHAT_FORMAT_COMMAND_R7B,
  48. COMMON_CHAT_FORMAT_COMMAND_R7B_EXTRACT_REASONING,
  49. COMMON_CHAT_FORMAT_COUNT, // Not a format, just the # formats
  50. };
  51. struct common_chat_templates_inputs {
  52. std::vector<common_chat_msg> messages;
  53. std::string grammar;
  54. std::string json_schema;
  55. bool add_generation_prompt = true;
  56. bool use_jinja = true;
  57. // Parameters below only supported when use_jinja is true
  58. std::vector<common_chat_tool> tools;
  59. common_chat_tool_choice tool_choice = COMMON_CHAT_TOOL_CHOICE_AUTO;
  60. bool parallel_tool_calls = false;
  61. bool extract_reasoning = true;
  62. };
  63. struct common_chat_params {
  64. common_chat_format format = COMMON_CHAT_FORMAT_CONTENT_ONLY;
  65. std::string prompt;
  66. std::string grammar;
  67. bool grammar_lazy = false;
  68. std::vector<common_grammar_trigger> grammar_triggers;
  69. std::vector<std::string> preserved_tokens;
  70. std::vector<std::string> additional_stops;
  71. };
  72. // Check if the template supplied via "--chat-template" is supported or not. Returns true if it's valid
  73. bool common_chat_verify_template(const std::string & tmpl, bool use_jinja);
  74. void common_chat_templates_free(struct common_chat_templates * tmpls);
  75. struct common_chat_templates_deleter { void operator()(common_chat_templates * tmpls) { common_chat_templates_free(tmpls); } };
  76. typedef std::unique_ptr<struct common_chat_templates, common_chat_templates_deleter> common_chat_templates_ptr;
  77. common_chat_templates_ptr common_chat_templates_init(
  78. const struct llama_model * model,
  79. const std::string & chat_template_override,
  80. const std::string & bos_token_override = "",
  81. const std::string & eos_token_override = "");
  82. bool common_chat_templates_was_explicit(const struct common_chat_templates * tmpls);
  83. const char * common_chat_templates_source(const struct common_chat_templates * tmpls, const char * variant = nullptr);
  84. struct common_chat_params common_chat_templates_apply(
  85. const struct common_chat_templates * tmpls,
  86. const struct common_chat_templates_inputs & inputs);
  87. // Format single message, while taking into account the position of that message in chat history
  88. std::string common_chat_format_single(
  89. const struct common_chat_templates * tmpls,
  90. const std::vector<common_chat_msg> & past_msg,
  91. const common_chat_msg & new_msg,
  92. bool add_ass,
  93. bool use_jinja);
  94. // Returns an example of formatted chat
  95. std::string common_chat_format_example(
  96. const struct common_chat_templates * tmpls,
  97. bool use_jinja);
  98. std::string common_chat_format_name(common_chat_format format);
  99. common_chat_msg common_chat_parse( const std::string & input, common_chat_format format);
  100. common_chat_tool_choice common_chat_tool_choice_parse_oaicompat(const std::string & tool_choice);
  101. // Parses a JSON array of messages in OpenAI's chat completion API format.
  102. // T can be std::string containing JSON or nlohmann::ordered_json
  103. template <class T> std::vector<common_chat_msg> common_chat_msgs_parse_oaicompat(const T & messages);
  104. template <class T> T common_chat_msgs_to_json_oaicompat(const std::vector<common_chat_msg> & msgs, bool concat_typed_text = false);
  105. // Parses a JSON array of tools in OpenAI's chat completion tool call API format.
  106. // T can be std::string containing JSON or nlohmann::ordered_json
  107. template <class T> std::vector<common_chat_tool> common_chat_tools_parse_oaicompat(const T & tools);
  108. template <class T> T common_chat_tools_to_json_oaicompat(const std::vector<common_chat_tool> & tools);