CoolFace
Modelpublic

Codeprocastinator/optimized-tinyllama-covalent

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