[ChatStream] ChatPrompt for lightblue/karasu-7B-chat-plus

[ChatStream] ChatPrompt for lightblue/karasu-7B-chat-plus

Hello, this is the Product Development Team at Qualiteg.


In this article, we introduce the ChatPrompt for lightblue/karasu-7B-chat-plus.
Customers using an older version of ChatStream can use this model by importing this ChatPrompt. (It is bundled in the latest release.)

from chatstream import AbstractChatPrompt
from chatstream.chat_prompt.role_type import RoleType

# Japanese system prompt kept as-is for the model ("You are an AI assistant.")
SYSTEM_PROMPT = """\
あなたはAIアシスタントです。\
"""


class ChatPromptLightblueKarasuChatPlus(AbstractChatPrompt):

    def __init__(self):
        super().__init__()  # Call the initialization of the base class
        self.set_system(f"<s>[INST] <<SYS>>\n{SYSTEM_PROMPT}\n<</SYS>>\n\n")
        self.set_requester("")
        self.set_responder("")

    def get_stop_strs(self):
        if not self.chat_mode:
            return None
        return []

    def get_custom_skip_echo_len(self, skip_echo_len):

        num_turn = self.get_turn()
        if num_turn >= 2:
            modified_skip_echo_len = skip_echo_len + 1 * self.get_turn() - 1  
            return modified_skip_echo_len
        return skip_echo_len

    def get_replacement_when_input(self):
        return None

    def get_replacement_when_output(self):  # replace when response_text gotten
        return None

    def create_prompt(self, opts={}):

        ret = self.system

        for chat_content in self.get_contents(opts):

            chat_content_role_type = chat_content.get_role_type()
            chat_content_message = chat_content.get_message()

            if chat_content_message:
                merged_message = ""
                if chat_content_role_type == RoleType.REQUESTER:
                    merged_message = f"{chat_content_message} [/INST] "
                elif chat_content_role_type == RoleType.RESPONDER:
                    merged_message = f"{chat_content_message} </s><s>[INST] "
                ret += merged_message
            else:
                pass

        return ret

    async def build_initial_prompt(self, chat_prompt):
        # No initial prompt is implemented
        pass

As the more observant readers may have noticed, this model is based on Llama 2, so the following part of the implementation, which handles Llama 2 compatibility, is its distinctive feature.

    def get_custom_skip_echo_len(self, skip_echo_len):

        num_turn = self.get_turn()
        if num_turn >= 2:
            modified_skip_echo_len = skip_echo_len + 1 * self.get_turn() - 1  
            return modified_skip_echo_len
        return skip_echo_len

For reference
https://journal.qualiteg.com/llama2-dui-ying-no-chatpromptshi-zhuang/

Read more