| #pragma once |
|
|
| #include "chat.h" |
| #include "common.h" |
| #include "jinja/caps.h" |
| #include "peg-parser.h" |
|
|
| #include <chrono> |
| #include <optional> |
| #include <string> |
| #include <utility> |
| #include <vector> |
|
|
| using json = nlohmann::ordered_json; |
|
|
| class common_chat_peg_builder; |
|
|
| |
| |
| |
| struct template_params { |
| json messages; |
| json tools; |
| bool add_generation_prompt = false; |
| bool enable_thinking = true; |
| std::optional<json> extra_context = std::nullopt; |
| }; |
|
|
| struct diff_split { |
| std::string prefix; |
| std::string suffix; |
| std::string left; |
| std::string right; |
|
|
| bool operator==(struct diff_split & other) const { |
| return prefix == other.prefix && suffix == other.suffix && left == other.left && right == other.right; |
| } |
| }; |
|
|
| |
| struct compare_variants_result { |
| diff_split diff; |
| std::string output_A; |
| std::string output_B; |
| }; |
|
|
| namespace autoparser { |
|
|
| |
| |
| |
|
|
| struct templates_params { |
| json messages; |
| json tools; |
| common_chat_tool_choice tool_choice = COMMON_CHAT_TOOL_CHOICE_AUTO; |
| json json_schema; |
| bool parallel_tool_calls = true; |
| common_reasoning_format reasoning_format = COMMON_REASONING_FORMAT_AUTO; |
| bool stream = true; |
| std::string grammar; |
| bool add_generation_prompt = false; |
| bool enable_thinking = true; |
| std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); |
| json extra_context; |
| bool add_bos = false; |
| bool add_eos = false; |
| bool is_inference = true; |
| bool add_inference = false; |
| bool mark_input = true; |
| }; |
|
|
| |
| |
| |
|
|
| |
| enum class reasoning_mode { |
| NONE, |
| TAG_BASED, |
| DELIMITER, |
| FORCED_OPEN, |
| FORCED_CLOSED, |
| |
| TOOLS_ONLY |
| }; |
|
|
| inline std::ostream & operator<<(std::ostream & os, const reasoning_mode & mode) { |
| switch (mode) { |
| case reasoning_mode::NONE: |
| return os << "NONE"; |
| case reasoning_mode::TAG_BASED: |
| return os << "TAG_BASED"; |
| case reasoning_mode::DELIMITER: |
| return os << "DELIMITER"; |
| case reasoning_mode::FORCED_OPEN: |
| return os << "FORCED_OPEN"; |
| case reasoning_mode::FORCED_CLOSED: |
| return os << "FORCED_CLOSED"; |
| case reasoning_mode::TOOLS_ONLY: |
| return os << "TOOLS_ONLY"; |
| default: |
| return os << "UNKNOWN"; |
| } |
| } |
|
|
| |
| enum class content_mode { |
| PLAIN, |
| ALWAYS_WRAPPED, |
| WRAPPED_WITH_REASONING, |
| }; |
|
|
| inline std::ostream & operator<<(std::ostream & os, const content_mode & mode) { |
| switch (mode) { |
| case content_mode::PLAIN: |
| return os << "PLAIN"; |
| case content_mode::ALWAYS_WRAPPED: |
| return os << "ALWAYS_WRAPPED"; |
| case content_mode::WRAPPED_WITH_REASONING: |
| return os << "WRAPPED_WITH_REASONING"; |
| default: |
| return os << "UNKNOWN"; |
| } |
| } |
|
|
| |
| enum class call_id_position { |
| NONE, |
| PRE_FUNC_NAME, |
| BETWEEN_FUNC_AND_ARGS, |
| POST_ARGS, |
| }; |
|
|
| inline std::ostream & operator<<(std::ostream & os, const call_id_position & pos) { |
| switch (pos) { |
| case call_id_position::NONE: |
| return os << "NONE"; |
| case call_id_position::PRE_FUNC_NAME: |
| return os << "PRE_FUNC_NAME"; |
| case call_id_position::BETWEEN_FUNC_AND_ARGS: |
| return os << "BETWEEN_FUNC_AND_ARGS"; |
| case call_id_position::POST_ARGS: |
| return os << "POST_ARGS"; |
| default: |
| return os << "UNKNOWN"; |
| } |
| } |
|
|
| |
| enum class tool_format { |
| NONE, |
| JSON_NATIVE, |
| TAG_WITH_JSON, |
| TAG_WITH_TAGGED, |
| }; |
|
|
| inline std::ostream & operator<<(std::ostream & os, const tool_format & format) { |
| switch (format) { |
| case tool_format::NONE: |
| return os << "NONE"; |
| case tool_format::JSON_NATIVE: |
| return os << "JSON_NATIVE"; |
| case tool_format::TAG_WITH_JSON: |
| return os << "TAG_WITH_JSON"; |
| case tool_format::TAG_WITH_TAGGED: |
| return os << "TAG_WITH_TAGGED"; |
| default: |
| return os << "UNKNOWN"; |
| } |
| } |
|
|
| |
| |
| |
|
|
| struct tool_format_analysis { |
| tool_format mode = tool_format::NONE; |
|
|
| std::string section_start; |
| std::string section_end; |
| std::string per_call_start; |
| std::string per_call_end; |
|
|
| bool fun_name_is_key = false; |
| bool tools_array_wrapped = false; |
| bool uses_python_dicts = false; |
|
|
| std::string function_field = "function"; |
| std::string name_field = "name"; |
| std::string args_field = "arguments"; |
| std::string id_field; |
| std::string gen_id_field; |
| std::vector<std::string> parameter_order; |
| }; |
|
|
| struct tool_function_analysis { |
| std::string name_prefix; |
| std::string name_suffix; |
| std::string close; |
| }; |
|
|
| struct tool_arguments_analysis { |
| std::string start; |
| std::string end; |
| std::string name_prefix; |
| std::string name_suffix; |
| std::string value_prefix; |
| std::string value_suffix; |
| std::string separator; |
| }; |
|
|
| struct tool_id_analysis { |
| call_id_position pos = call_id_position::NONE; |
|
|
| std::string prefix; |
| std::string suffix; |
| }; |
|
|
| |
| |
| |
|
|
| struct analyze_content; |
|
|
| struct parser_build_context { |
| common_chat_peg_builder & p; |
| const templates_params & inputs; |
| common_peg_parser reasoning_parser; |
| bool extracting_reasoning = false; |
| const analyze_content * content = nullptr; |
|
|
| parser_build_context(common_chat_peg_builder & p, const templates_params & inputs); |
| }; |
|
|
| |
| |
| |
|
|
| struct analyze_base { |
| virtual ~analyze_base() = default; |
| virtual common_peg_parser build_parser(parser_build_context & ctx) const = 0; |
|
|
| protected: |
| const common_chat_template * tmpl = nullptr; |
|
|
| analyze_base() = default; |
| explicit analyze_base(const common_chat_template & tmpl) : tmpl(&tmpl) {} |
| }; |
|
|
| |
| |
| |
|
|
| struct analyze_reasoning : analyze_base { |
| reasoning_mode mode = reasoning_mode::NONE; |
|
|
| std::string start; |
| std::string end; |
|
|
| analyze_reasoning() = default; |
| analyze_reasoning(const common_chat_template & tmpl, bool supports_tools); |
|
|
| common_peg_parser build_parser(parser_build_context & ctx) const override; |
|
|
| private: |
| |
| void compare_reasoning_presence(); |
|
|
| |
| void compare_thinking_enabled(); |
|
|
| |
| void compare_reasoning_scope(); |
| }; |
|
|
| |
| |
| |
|
|
| struct analyze_content : analyze_base { |
| content_mode mode = content_mode::PLAIN; |
|
|
| std::string start; |
| std::string end; |
|
|
| bool requires_nonnull_content = false; |
|
|
| analyze_content() = default; |
| analyze_content(const common_chat_template & tmpl, const analyze_reasoning & reasoning); |
|
|
| common_peg_parser build_parser(parser_build_context & ctx) const override; |
|
|
| bool is_always_wrapped() const; |
| common_peg_parser build_optional_wrapped(parser_build_context & ctx) const; |
| }; |
|
|
| |
| |
| |
|
|
| struct analyze_tools : analyze_base { |
| tool_format_analysis format; |
| tool_function_analysis function; |
| tool_arguments_analysis arguments; |
| tool_id_analysis call_id; |
|
|
| analyze_tools() = default; |
| analyze_tools(const common_chat_template & tmpl, |
| const jinja::caps & caps, |
| const analyze_reasoning & reasoning); |
|
|
| common_peg_parser build_parser(parser_build_context & ctx) const override; |
|
|
| private: |
| |
| void analyze_tool_calls(const analyze_reasoning & reasoning); |
|
|
| |
| void analyze_tool_call_format(const std::string & haystack, |
| const std::string & fun_name_needle, |
| const std::string & arg_name_needle, |
| const analyze_reasoning & reasoning); |
|
|
| |
| void analyze_tool_call_format_json_native(const std::string & clean_haystack, |
| const std::string & fun_name_needle, |
| const std::string & arg_name_needle); |
|
|
| |
| void analyze_tool_call_format_non_json(const std::string & clean_haystack, |
| const std::string & fun_name_needle); |
|
|
| |
| void check_per_call_markers(); |
|
|
| |
| void extract_function_markers(); |
|
|
| |
| void analyze_arguments(); |
|
|
| |
| void extract_argument_name_markers(); |
|
|
| |
| void extract_argument_value_markers(); |
|
|
| |
| void extract_argument_separator(); |
|
|
| |
| void extract_args_markers(); |
|
|
| |
| void extract_call_id_markers(); |
|
|
| |
| common_peg_parser build_tool_parser_json_native(parser_build_context & ctx) const; |
| common_peg_parser build_tool_parser_tag_json(parser_build_context & ctx) const; |
| common_peg_parser build_tool_parser_tag_tagged(parser_build_context & ctx) const; |
| }; |
|
|
| |
| |
| |
|
|
| struct autoparser { |
| jinja::caps jinja_caps; |
| analyze_reasoning reasoning; |
| analyze_content content; |
| analyze_tools tools; |
| bool analysis_complete = false; |
|
|
| |
| std::vector<std::string> preserved_tokens; |
|
|
| autoparser() = default; |
|
|
| |
| void analyze_template(const common_chat_template & tmpl); |
|
|
| |
| common_peg_arena build_parser(const templates_params & inputs) const; |
|
|
| private: |
| |
| void collect_preserved_tokens(); |
| }; |
|
|
| |
| |
| |
|
|
| class peg_generator { |
| public: |
| static common_chat_params generate_parser(const common_chat_template & tmpl, |
| const struct templates_params & inputs); |
|
|
| static common_chat_params generate_parser(const common_chat_template & tmpl, |
| const struct templates_params & inputs, |
| const autoparser & autoparser); |
| }; |
|
|
| } |
|
|
| enum segment_type { TEXT, MARKER }; |
|
|
| inline std::ostream & operator<<(std::ostream & os, const segment_type & type) { |
| switch (type) { |
| case segment_type::TEXT: |
| return os << "TEXT"; |
| case segment_type::MARKER: |
| return os << "MARKER"; |
| default: |
| return os << "UNKNOWN"; |
| } |
| } |
|
|
| struct segment { |
| segment_type type; |
| std::string value; |
|
|
| segment(segment_type type, std::string value) : type(type), value(std::move(value)) {} |
|
|
| bool operator==(const segment & other) const { |
| return type == other.type && value == other.value; |
| } |
|
|
| bool operator!=(const segment & other) const { |
| return !(*this == other); |
| } |
| }; |
|
|