Team Ai
Apppublic

KBaba7/llama.cpp

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
0likes
chat.hpp53 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 <json.hpp>7#include <optional>8#include <string>9#include <vector>10 11using json = nlohmann::ordered_json;12 13struct common_chat_inputs {14    json messages;15    json tools;16    json tool_choice;17    json json_schema;18    bool parallel_tool_calls;19    bool stream;20    std::string grammar;21    bool add_generation_prompt = true;22};23 24enum common_chat_format {25    COMMON_CHAT_FORMAT_CONTENT_ONLY,26    COMMON_CHAT_FORMAT_GENERIC,27    COMMON_CHAT_FORMAT_MISTRAL_NEMO,28    COMMON_CHAT_FORMAT_LLAMA_3_X,29    COMMON_CHAT_FORMAT_LLAMA_3_X_WITH_BUILTIN_TOOLS,30    COMMON_CHAT_FORMAT_DEEPSEEK_R1,31    COMMON_CHAT_FORMAT_FIREFUNCTION_V2,32    COMMON_CHAT_FORMAT_FUNCTIONARY_V3_2,33    COMMON_CHAT_FORMAT_FUNCTIONARY_V3_1_LLAMA_3_1,34    COMMON_CHAT_FORMAT_HERMES_2_PRO,35    COMMON_CHAT_FORMAT_COMMAND_R7B,36 37    COMMON_CHAT_FORMAT_COUNT, // Not a format, just the # formats38};39 40struct common_chat_params {41    common_chat_format                  format = COMMON_CHAT_FORMAT_CONTENT_ONLY;42    json                                prompt;43    std::string                         grammar;44    bool                                grammar_lazy = false;45    std::vector<common_grammar_trigger> grammar_triggers;46    std::vector<std::string>            preserved_tokens;47    std::vector<std::string>            additional_stops;48};49 50struct common_chat_params common_chat_params_init(const common_chat_template & tmpl, const struct common_chat_inputs & params);51std::string               common_chat_format_name(common_chat_format format);52common_chat_msg           common_chat_parse(      const std::string & input, common_chat_format format);53