1
0

chat.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_HERMES_2_PRO_EXTRACT_REASONING,
  48. COMMON_CHAT_FORMAT_COMMAND_R7B,
  49. COMMON_CHAT_FORMAT_COMMAND_R7B_EXTRACT_REASONING,
  50. COMMON_CHAT_FORMAT_COUNT, // Not a format, just the # formats
  51. };
  52. struct common_chat_templates_inputs {
  53. std::vector<common_chat_msg> messages;
  54. std::string grammar;
  55. std::string json_schema;
  56. bool add_generation_prompt = true;
  57. bool use_jinja = true;
  58. // Parameters below only supported when use_jinja is true
  59. std::vector<common_chat_tool> tools;
  60. common_chat_tool_choice tool_choice = COMMON_CHAT_TOOL_CHOICE_AUTO;
  61. bool parallel_tool_calls = false;
  62. bool extract_reasoning = true;
  63. };
  64. struct common_chat_params {
  65. common_chat_format format = COMMON_CHAT_FORMAT_CONTENT_ONLY;
  66. std::string prompt;
  67. std::string grammar;
  68. bool grammar_lazy = false;
  69. std::vector<common_grammar_trigger> grammar_triggers;
  70. std::vector<std::string> preserved_tokens;
  71. std::vector<std::string> additional_stops;
  72. };
  73. // Check if the template supplied via "--chat-template" is supported or not. Returns true if it's valid
  74. bool common_chat_verify_template(const std::string & tmpl, bool use_jinja);
  75. void common_chat_templates_free(struct common_chat_templates * tmpls);
  76. struct common_chat_templates_deleter { void operator()(common_chat_templates * tmpls) { common_chat_templates_free(tmpls); } };
  77. typedef std::unique_ptr<struct common_chat_templates, common_chat_templates_deleter> common_chat_templates_ptr;
  78. common_chat_templates_ptr common_chat_templates_init(
  79. const struct llama_model * model,
  80. const std::string & chat_template_override,
  81. const std::string & bos_token_override = "",
  82. const std::string & eos_token_override = "");
  83. bool common_chat_templates_was_explicit(const struct common_chat_templates * tmpls);
  84. const char * common_chat_templates_source(const struct common_chat_templates * tmpls, const char * variant = nullptr);
  85. struct common_chat_params common_chat_templates_apply(
  86. const struct common_chat_templates * tmpls,
  87. const struct common_chat_templates_inputs & inputs);
  88. // Format single message, while taking into account the position of that message in chat history
  89. std::string common_chat_format_single(
  90. const struct common_chat_templates * tmpls,
  91. const std::vector<common_chat_msg> & past_msg,
  92. const common_chat_msg & new_msg,
  93. bool add_ass,
  94. bool use_jinja);
  95. // Returns an example of formatted chat
  96. std::string common_chat_format_example(
  97. const struct common_chat_templates * tmpls,
  98. bool use_jinja);
  99. std::string common_chat_format_name(common_chat_format format);
  100. common_chat_msg common_chat_parse( const std::string & input, common_chat_format format);
  101. common_chat_tool_choice common_chat_tool_choice_parse_oaicompat(const std::string & tool_choice);
  102. // Parses a JSON array of messages in OpenAI's chat completion API format.
  103. // T can be std::string containing JSON or nlohmann::ordered_json
  104. template <class T> std::vector<common_chat_msg> common_chat_msgs_parse_oaicompat(const T & messages);
  105. template <class T> T common_chat_msgs_to_json_oaicompat(const std::vector<common_chat_msg> & msgs, bool concat_typed_text = false);
  106. // Parses a JSON array of tools in OpenAI's chat completion tool call API format.
  107. // T can be std::string containing JSON or nlohmann::ordered_json
  108. template <class T> std::vector<common_chat_tool> common_chat_tools_parse_oaicompat(const T & tools);
  109. template <class T> T common_chat_tools_to_json_oaicompat(const std::vector<common_chat_tool> & tools);