見出し画像

gguf版、elyza-japanese-7b-instでチャットモードで会話してみました。(短期記憶あり)

gguf版、elyza-japanese-7b-instでチャットモードで会話してみました。(短期記憶あり)
前に投稿したyouri 7b chatとほとんど同じコードになります。
プロンプトの関数の中身が少し違います。

$ pip install llama-cpp-python

~ここからコード~
from llama_cpp import Llama

# LLMの準備
llm = Llama(model_path="./ELYZA-japanese-Llama-2-7b-instruct-q4_K_M.gguf",verbose=False)

# プロンプトの準備
DEFAULT_SYSTEM_PROMPT = """あなたは優秀なAIアシスタントです。質問に答えてください。"""

def get_prompt(message: str, chat_history: list[tuple[str, str]],
    system_prompt: str) -> str:
    texts = [f'[INST] <<SYS>>\n{system_prompt}\n<</SYS>>\n\n']
    # The first user input is _not_ stripped
    do_strip = False
    for user_input, response in chat_history:
        user_input = user_input.strip() if do_strip else user_input
        do_strip = True
        texts.append(f'{user_input} [/INST] {response.strip()} [INST] ')
        message = message.strip() if do_strip else message
            texts.append(f'{message} [/INST]')
        return ''.join(texts)

messages = []
# 推論の実行
while True:
    message = input("You: ")
    if not message:
        break
    prompt = get_prompt(message, messages, DEFAULT_SYSTEM_PROMPT)
    output = llm.create_completion(
        prompt,
        temperature=0.7,
        max_tokens=400,
        repeat_penalty=1.1,
     )
    print(output["choices"][0]["text"])

この記事が気に入ったらサポートをしてみませんか?