chat.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Chat support (incl. tool call grammar constraining & output parsing) w/ generic & custom template handlers.
  2. #pragma once
  3. #include "common.h"
  4. #include <json.hpp>
  5. #include <optional>
  6. #include <string>
  7. #include <vector>
  8. using json = nlohmann::ordered_json;
  9. struct common_chat_inputs {
  10. json messages;
  11. json tools;
  12. json tool_choice;
  13. json json_schema;
  14. bool parallel_tool_calls;
  15. bool stream;
  16. std::string grammar;
  17. bool add_generation_prompt = true;
  18. bool extract_reasoning = true;
  19. };
  20. enum common_chat_format {
  21. COMMON_CHAT_FORMAT_CONTENT_ONLY,
  22. COMMON_CHAT_FORMAT_GENERIC,
  23. COMMON_CHAT_FORMAT_MISTRAL_NEMO,
  24. COMMON_CHAT_FORMAT_LLAMA_3_X,
  25. COMMON_CHAT_FORMAT_LLAMA_3_X_WITH_BUILTIN_TOOLS,
  26. COMMON_CHAT_FORMAT_DEEPSEEK_R1,
  27. COMMON_CHAT_FORMAT_DEEPSEEK_R1_EXTRACT_REASONING,
  28. COMMON_CHAT_FORMAT_FIREFUNCTION_V2,
  29. COMMON_CHAT_FORMAT_FUNCTIONARY_V3_2,
  30. COMMON_CHAT_FORMAT_FUNCTIONARY_V3_1_LLAMA_3_1,
  31. COMMON_CHAT_FORMAT_HERMES_2_PRO,
  32. COMMON_CHAT_FORMAT_COMMAND_R7B,
  33. COMMON_CHAT_FORMAT_COMMAND_R7B_EXTRACT_REASONING,
  34. COMMON_CHAT_FORMAT_COUNT, // Not a format, just the # formats
  35. };
  36. struct common_chat_params {
  37. common_chat_format format = COMMON_CHAT_FORMAT_CONTENT_ONLY;
  38. json prompt;
  39. std::string grammar;
  40. bool grammar_lazy = false;
  41. std::vector<common_grammar_trigger> grammar_triggers;
  42. std::vector<std::string> preserved_tokens;
  43. std::vector<std::string> additional_stops;
  44. };
  45. struct common_chat_params common_chat_params_init(const common_chat_template & tmpl, const struct common_chat_inputs & params);
  46. std::string common_chat_format_name(common_chat_format format);
  47. common_chat_msg common_chat_parse( const std::string & input, common_chat_format format);