diff --git a/src/models/llm/chat_template/base_chat_template.h b/src/models/llm/chat_template/base_chat_template.h deleted file mode 100644 index a15a790..0000000 --- a/src/models/llm/chat_template/base_chat_template.h +++ /dev/null @@ -1,121 +0,0 @@ -/************************************************ - * Copyright MaybeShewill-CV. All Rights Reserved. - * Author: MaybeShewill-CV - * File: base_chat_template.h - * Date: 24-11-26 - ************************************************/ - -#ifndef MORTRED_MODEL_SERVER_BASE_CHAT_TEMPLATE_H -#define MORTRED_MODEL_SERVER_BASE_CHAT_TEMPLATE_H - -#include -#include -#include - -#include "common/status_code.h" - -namespace jinq { -namespace models { -namespace llm { -namespace chat_template { - -struct ChatMessage { - std::string role; - std::string content; - - ChatMessage(std::string r, std::string c) : role(std::move(r)), content(std::move(c)) {} -}; - -class Dialog { - public: - Dialog() = default; - - ~Dialog() = default; - - Dialog(const Dialog &transformer) = default; - - Dialog& operator=(const Dialog &transformer) = default; - - explicit Dialog (const ChatMessage &msg) { - messages.push_back(msg); - } - - Dialog (const std::string& role, const std::string& content) { - messages.emplace_back(role, content); - } - - Dialog operator+(const Dialog& other) { - Dialog tmp(*this); - std::copy(other.messages.begin(), other.messages.end(), std::back_inserter(tmp.messages)); - return tmp; - } - - Dialog& operator+=(const Dialog& other) { - std::copy(other.messages.begin(), other.messages.end(), std::back_inserter(messages)); - return *this; - } - - inline ChatMessage& operator[](size_t index) { - return messages[index]; - } - - inline void push_back(const ChatMessage& msg) { - messages.push_back(msg); - } - - inline void clean_cache() { - messages.clear(); - } - - bool empty() const { - return messages.empty(); - }; - - inline size_t size() const { - return messages.size(); - } - - public: - std::vector messages; -}; - -class BaseChatTemplate { -public: - /*** - * - */ - virtual ~BaseChatTemplate() = default; - - /*** - * - * @param config - */ - BaseChatTemplate() = default; - - /*** - * - * @param transformer - */ - BaseChatTemplate(const BaseChatTemplate& BaseChatTemplate) = default; - - /*** - * - * @param transformer - * @return - */ - BaseChatTemplate& operator=(const BaseChatTemplate& transformer) = default; - - /*** - * - * @param input - * @param output - * @return - */ - virtual jinq::common::StatusCode apply_chat_template(const Dialog& messages, std::string& out_fmt_str) = 0; -}; -} -} -} -} - -#endif // MORTRED_MODEL_SERVER_BASE_CHAT_TEMPLATE_H diff --git a/src/models/llm/chat_template/llama3_chat_template.h b/src/models/llm/chat_template/llama3_chat_template.h deleted file mode 100644 index 343ee79..0000000 --- a/src/models/llm/chat_template/llama3_chat_template.h +++ /dev/null @@ -1,66 +0,0 @@ -/************************************************ - * Copyright MaybeShewill-CV. All Rights Reserved. - * Author: MaybeShewill-CV - * File: Llama3ChatTemplate_chat_template.h - * Date: 24-11-26 - ************************************************/ - -#ifndef MORTRED_MODEL_SERVER_LLAMA3_CHAT_TEMPLATE_CHAT_TEMPLATE_H -#define MORTRED_MODEL_SERVER_LLAMA3_CHAT_TEMPLATE_CHAT_TEMPLATE_H - -#include - -#include "models/llm/chat_template/base_chat_template.h" - -namespace jinq { -namespace models { -namespace llm { -namespace chat_template { - -class Llama3ChatTemplate : public BaseChatTemplate { - public: - /*** - * constructor - * @param config - */ - Llama3ChatTemplate(); - - /*** - * - */ - ~Llama3ChatTemplate() override; - - /*** - * constructor - * @param transformer - */ - Llama3ChatTemplate(const Llama3ChatTemplate &transformer) = delete; - - /*** - * constructor - * @param transformer - * @return - */ - Llama3ChatTemplate &operator=(const Llama3ChatTemplate &transformer) = delete; - - /*** - * - * @param input - * @param output - * @return - */ - jinq::common::StatusCode apply_chat_template(const models::llm::chat_template::Dialog& dialog, std::string& out_fmt_str) override; - - private: - class Impl; - std::unique_ptr _m_pimpl; -}; - -} -} -} -} - -#include "llama3_chat_template.inl" - -#endif // MORTRED_MODEL_SERVER_LLAMA3_CHAT_TEMPLATE_CHAT_TEMPLATE_H diff --git a/src/models/llm/chat_template/llama3_chat_template.inl b/src/models/llm/chat_template/llama3_chat_template.inl deleted file mode 100644 index b67c76b..0000000 --- a/src/models/llm/chat_template/llama3_chat_template.inl +++ /dev/null @@ -1,106 +0,0 @@ -/************************************************ - * Copyright MaybeShewill-CV. All Rights Reserved. - * Author: MaybeShewill-CV - * File: Llama3ChatTemplate_chat_template.inl - * Date: 24-11-26 - ************************************************/ - -#include "models/llm/chat_template/llama3_chat_template.h" - -#include "fmt/format.h" - -namespace jinq { -namespace models { -namespace llm { - -using jinq::common::StatusCode; - -namespace chat_template { - -class Llama3ChatTemplate::Impl { - public: - /*** - * - */ - Impl() = default; - - /*** - * - */ - ~Impl() = default; - - /*** - * - * @param transformer - */ - Impl(const Impl& transformer) = delete; - - /*** - * - * @param transformer - * @return - */ - Impl& operator=(const Impl& transformer) = delete; - - /*** - * - * @param dialog - * @param out_fmt_str - * @return - */ - StatusCode apply_chat_template(const Dialog& dialog, std::string& out_fmt_str); - - private: - std::string _m_header_fmt = "<|start_header_id|>{}<|end_header_id|>\n\n"; - std::string _m_message_fmt = "<|start_header_id|>{}<|end_header_id|>\n\n{}<|eot_id|>"; - -}; - -/*** - * - * @param messages - * @param out_fmt_str - * @return - */ -StatusCode Llama3ChatTemplate::Impl::apply_chat_template(const Dialog& dialog, std::string &out_fmt_str) { - if (dialog.empty()) { - return StatusCode::TOKENIZE_FAILED; - } - std::string fmt_dialog; - for (auto& message : dialog.messages) { - fmt_dialog += fmt::format(_m_message_fmt, message.role, message.content); - } - fmt_dialog += fmt::format(_m_header_fmt, "assistant"); - out_fmt_str = fmt_dialog; - return StatusCode::OK; -} - - -/************* Export Function Sets *************/ - -/*** - * - */ -Llama3ChatTemplate::Llama3ChatTemplate() { - _m_pimpl = std::make_unique(); -} - -/*** - * - */ -Llama3ChatTemplate::~Llama3ChatTemplate() = default; - -/*** - * - * @param messages - * @param out_fmt_str - * @return - */ -StatusCode Llama3ChatTemplate::apply_chat_template(const Dialog& dialog, std::string &out_fmt_str) { - return _m_pimpl->apply_chat_template(dialog, out_fmt_str); -} - -} -} -} -}