見出し画像

youri 7b chat gguf(短期記憶あり)

$ pip install llama-cpp-python

ここからコード
from llama_cpp import Llama

# LLMの準備
llm = Llama(model_path="./rinna-youri-7b-chat-q8_0.gguf",verbose=False)

# プロンプトの準備
DEFAULT_SYSTEM_PROMPT ="""あなたは優秀なAIアシスタント。名前は'Youri'です。楽しい会話をしましょう。
ユーザー: 私の名前はピノです。神奈川県住み。
Youri: うん。私は東京に住んでるよ。
ユーザー: 好物は寿司。趣味はプログラミング。ゲームも好き。音楽も好きです。
Youri: うん。
ユーザー: ユーザーの質問の内容がぼんやりしている場合、可愛らしい質問をしてください。
Youri: うん。わかったよ。
"""

def get_prompt(message: str, chat_history: list[tuple[str, str]], system_prompt: str) -> str:
    texts = [f'<s>\n<設定:>{system_prompt}\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} \n< Youri:>{response.strip()} </s><s>\n')
        message = message.strip() if do_strip else message
        texts.append(f'{message} ')
    return ''.join(texts)

chat_history = []

while True:
    message = input("ユーザー: ")
    if not message:
      break
    prompt = get_prompt(message, chat_history, DEFAULT_SYSTEM_PROMPT)
    output = llm.create_completion(
        prompt,
        temperature=1,
        top_k=40,
        top_p=0.95,
        repeat_penalty=1.3,
        max_tokens=200,
        )
    res = output["choices"][0]["text"]
    print(" Youri: " + res)
    chat_history = chat_history[:20]


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