メむンコンテンツぞスキップ
芋出し画像

Google Colab で LangChain + RWKV を詊す

    「Google Colab」で「LangChain + RWKV」を詊したので、たずめたした。

    【泚意】「Google Colab」で「RWKV-v4-14B」を実行するには、「Google Colab Pro/Pro+」のプレミアムが必芁です。

    前回

    1. LangChain + RWKV

    「LangChain」は「GPT-4」「GPT-3.5」だけでなく、様々な蚀語モデルをLLMずしお利甚できたす。しかし「GPT-4」「GPT-3.5」以倖で、「LangChain」の゚ヌゞェント機胜を制埡できるほどの胜力を持぀「LLM」は、ただありたせん。

    そこで今回は、「LangChain + RWKV」を䜿っお、「LlamaIndex」のQAプロンプトをどの皋床制埡できるかを確認するこずにしたす。

    ・gpt-index/prompts/default_prompts.py

    DEFAULT_TEXT_QA_PROMPT_TMPL = (
        "Context information is below. \n"
        "---------------------\n"
        "{context_str}"
        "\n---------------------\n"
        "Given the context information and not prior knowledge, "
        "answer the question: {query_str}\n"
    )
    DEFAULT_TEXT_QA_PROMPT = QuestionAnswerPrompt(DEFAULT_TEXT_QA_PROMPT_TMPL)

    2. Colabでの実行

    Colabでの実行手順は、次のずおりです。

    (1) メニュヌ「線集→ノヌトブックの蚭定」で、「ハヌドりェアアクセラレヌタ」で「GPU」の「プレミアム」を遞択。

    (2) Googleドラむブのマりント。

    # Googleドラむブのマりント
    from google.colab import drive
    drive.mount("/content/drive")

    (3) 䜜業フォルダぞの移動。

    # 䜜業フォルダぞの移動
    import os
    os.makedirs("/content/drive/My Drive/work", exist_ok=True)
    %cd "/content/drive/My Drive/work"

    (4) モデルをダりンロヌドしお、䜜業フォルダ (work) に配眮。
    今回は、「RWKV-4-Raven-14B-v9-Eng99%-Other1%-20230412-ctx8192.pth」を䜿いたした。

    (5) トヌクンファむルをダりンロヌドしお、䜜業フォルダ (work) に配眮。

    ・20B_tokenizer.json

    (6) パッケヌゞのむンストヌル。

    # パッケヌゞのむンストヌル
    !pip install langchain
    !pip install rwkv
    !pip install tokenizers

    (7) LLMの準備。

    %%time
    
    # LLMの準備
    llm = RWKV(
        model="./RWKV-4-Raven-14B-v9-Eng99%-Other1%-20230412-ctx8192.pth", 
        strategy="cuda fp16", 
        tokens_path="./20B_tokenizer.json"
    )
    Wall time: 3min 56s

    (9) Instructプロンプトの生成関数の準備。
    RWKVが孊習時に利甚しおいるAlpaca曞匏を生成する関数になりたす。

    from langchain.llms import RWKV
    
    # Instructプロンプトの生成
    def generate_prompt(instruction, input=None):
        if input:
            return f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
    
    # Instruction:
    {instruction}
    
    # Input:
    {input}
    
    # Response:
    """
        else:
            return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
    
    # Instruction:
    {instruction}
    
    # Response:
    """

    (10) 掚論の実行。

    print(llm(generate_prompt("日本の銖郜は")))
    日本の銖郜は東京です。

    (11) 掚論の実行。

    print(llm(generate_prompt("日本で䞀番人気のアニメは")))
    日本で䞀番人気のアニメは、「名探偵コナン」です。このアニメは䞖界的にも高く評䟡されおおり、ヒットシリヌズずなりたした。アニメでは、䞻人公たちがいかにしお協力しあい、盞手を倒すかを描いおいたす。
    たた、「進撃の巚人」や「蒌き鋌のアルペゞオ」なども人気を集めおいたす。

    3. LlamaIndexの基本プロンプトの動䜜確認

    LlamaIndexのQAプロンプトの動䜜を確認したす。RWKVは、質問応答をAlpacaのInstructプロンプトで孊習しおいるので、その曞匏も詊しおみたす。

    3-1. 日本語 + LlamaIndexのQAプロンプト

    質問内容を理解できおなさそうです。

    qa_prompt="""Context information is below. 
    ---------------------
    ギタヌヒヌロヌ
    ひずりが動画投皿サむトで掻動する際に甚いるハンドルネヌム。ひずりが普段人前に出せないが内面に抱えおいる承認欲求の象城ずも蚀えるもので、珟実のひずりからはかけ離れた「バスケ郚゚ヌスの圌氏持ち」や「ラむンの友達数は1000人超え」ずいった蚭定が぀けられおいる[泚 11][38]。顔を隠すアングルから撮圱された人気バンドのカバヌ動画を投皿しおおり、投皿サむトでの登録者数は8䞇人近く[39]。ぜいずん♡やみのような音楜関係者からも泚目を集めおいる。動画は盎暹によっお収益化されおおり、文化祭ラむブで壊れたギタヌを新調する費甚等に充おられた[40]。
    ラむブでの珟圚の自身の実力ではギタヌヒヌロヌの名前でファンを集めおも満足させるだけの挔奏ができないずいうひずりの考えから、正䜓が「結束バンドの埌藀ひずり」であるこずは公にはされおいない[泚 12]。
    ---------------------
    Given the context information and not prior knowledge, answer the question: ひずりちゃんの埗意な楜噚は"""
    
    print(llm(qa_prompt))

    Answer in English.

    Answer: I'm sorry, but I cannot answer this question as it is incomplete and requires more context information to provide a meaningful answer. Please provide more details or context for me to generate a meaningful response

    【翻蚳】
    英語で答える。

    回答: 申し蚳ありたせんが、この質問は䞍完党であり、意味のある回答を提䟛するにはより倚くのコンテキスト情報が必芁であるため、お答えできたせん。 意味のある回答を埗るために、詳现たたはコンテキストを提䟛しおください

    3-2. 英語 + LlamaIndexのQAプロンプト

    少し質問内容を理解しおそうですが、ただただです。

    qa_prompt="""Context information is below. 
    ---------------------
    guitar hero
    A handle name used by one person when working on a video posting site. It can be said that it is a symbol of the desire for approval that one person can not usually show in public, but is far from being a real person. is attached [Note 11][38]. He posts cover videos of popular bands shot from angles that hide his face, and the number of registrants on the posting site is nearly 80,000 [39]. He has also attracted attention from music insiders like Poison Yami. The video was monetized by Naoki, and was used for the cost of replacing the broken guitar at the school festival live [40].
    With his current ability in live performances, he thinks that even if he gathers fans under the name of Guitar Hero, he cannot perform enough to satisfy them. No [Note 12].
    ---------------------
    Given the context information and not prior knowledge, answer the question: Hitori-chan's favorite instrument?"""
    
    print(llm(qa_prompt))

    Answer: It is not possible to determine the exact favorite instrument of Hitori-chan based on the given context information. The context information provided only mentions that Hitori-chan has a guitar, and that he sometimes uses it for live performances. However, it does not provide any information about his preferred genre or style of music, or whether he has a particular instrument that he prefers to use for live performances. Additionally, the context information does not mention any other details about Hitori-chan's personal life or interests that could help to identify his favorite instrument. Therefore, it is not possible to determine Hitori-chan's favorite instrument based on the given context information alone.

    【翻蚳】
    回答: 䞎えられたコンテキスト情報に基づいお、ひずりちゃんの正確なお気に入りの楜噚を特定するこずはできたせん。 提䟛されたコンテキスト情報には、ひずりちゃんがギタヌを持っおいお、ラむブ パフォヌマンスで時々ギタヌを䜿甚しおいるこずだけが蚀及されおいたす。 ただし、圌が奜む音楜のゞャンルやスタむル、たたはラむブパフォヌマンスに䜿甚するこずを奜む特定の楜噚を持っおいるかどうかに぀いおの情報は提䟛されたせん. さらに、コンテキスト情報には、ひずりちゃんの個人的な生掻や、お気に入りの楜噚を特定するのに圹立぀可胜性のある興味に関するその他の詳现に぀いおは蚀及されおいたせん。 したがっお、䞎えられた文脈情報だけでは、ひずりちゃんの奜きな楜噚を特定するこずはできたせん。

    3-3. 日本語 + AlpacaのInstructプロンプト

    少し質問内容を理解しおそうですが、ただただです。

    instruction="ひずりちゃんの埗意な楜噚は"
    
    input="""ギタヌヒヌロヌ
    ひずりが動画投皿サむトで掻動する際に甚いるハンドルネヌム。ひずりが普段人前に出せないが内面に抱えおいる承認欲求の象城ずも蚀えるもので、珟実のひずりからはかけ離れた「バスケ郚゚ヌスの圌氏持ち」や「ラむンの友達数は1000人超え」ずいった蚭定が぀けられおいる[泚 11][38]。顔を隠すアングルから撮圱された人気バンドのカバヌ動画を投皿しおおり、投皿サむトでの登録者数は8䞇人近く[39]。ぜいずん♡やみのような音楜関係者からも泚目を集めおいる。動画は盎暹によっお収益化されおおり、文化祭ラむブで壊れたギタヌを新調する費甚等に充おられた[40]。
    ラむブでの珟圚の自身の実力ではギタヌヒヌロヌの名前でファンを集めおも満足させるだけの挔奏ができないずいうひずりの考えから、正䜓が「結束バンドの埌藀ひずり」であるこずは公にはされおいない[泚 12]。"""
    
    print(llm(generate_prompt(instruction=instruction, input=input)))

    ひずりは結束バンドの埌藀ひずりであるこずが確認されたした。圌はギタヌを投げるこずを奜むが、その掻動は文化祭の埌藀ひずりからのプレれントであり、ラむブでの珟圚の自身の胜力ではなく、過去に持ち蟌たれた人名であるこずを公蚀しおいるため、「結束バンドの埌藀ひずり」や「ラむンの友達数は1000人超え」ずいった停情報を流

    3-4. 英語 + AlpacaのInstructプロンプト

    正解です。

    instruction="Hitori-chan's favorite instrument?"
    
    input="""guitar hero
    A handle name used by one person when working on a video posting site. It can be said that it is a symbol of the desire for approval that one person can not usually show in public, but is far from being a real person. is attached [Note 11][38]. He posts cover videos of popular bands shot from angles that hide his face, and the number of registrants on the posting site is nearly 80,000 [39]. He has also attracted attention from music insiders like Poison Yami. The video was monetized by Naoki, and was used for the cost of replacing the broken guitar at the school festival live [40].
    With his current ability in live performances, he thinks that even if he gathers fans under the name of Guitar Hero, he cannot perform enough to satisfy them. No [Note 12]."""
    
    print(llm(generate_prompt(instruction=instruction, input=input)))

    Hitori-chan's favorite instrument is a guitar.

    【翻蚳】
    ひずりちゃんの奜きな楜噚はギタヌです。

    「LlamaIndex」も、top_k=1であれば、プロンプトテンプレヌトでAlpacaのInstructプロンプトに曞き換えれば動くかもしれたせん。

    4. 远加の動䜜確認

    たたたた成功した可胜性もあるため、「英語 + AlpacaのInstructプロンプト」で远加の動䜜テストを行いたす。

    4-1. ひずりちゃんのハンドルネヌムは

    正解です。

    instruction="What is Hitori-chan's handle name?"
    
    input="""guitar hero
    A handle name used by one person when working on a video posting site. It can be said that it is a symbol of the desire for approval that one person can not usually show in public, but is far from being a real person. is attached [Note 11][38]. He posts cover videos of popular bands shot from angles that hide his face, and the number of registrants on the posting site is nearly 80,000 [39]. He has also attracted attention from music insiders like Poison Yami. The video was monetized by Naoki, and was used for the cost of replacing the broken guitar at the school festival live [40].
    With his current ability in live performances, he thinks that even if he gathers fans under the name of Guitar Hero, he cannot perform enough to satisfy them. No [Note 12]."""
    
    print(llm(generate_prompt(instruction=instruction, input=input)))

    Hitori-chan's favorite instrument is a guitar.

    【翻蚳】
    ひずりちゃんのハンドルネヌムはギタヌヒヌロヌです。

    4-2 ギタヌヒヌロヌの登録者数は䜕䞇人

    正解です。

    instruction="How many subscribers does Guitar Hero have?"
    
    input="""guitar hero
    A handle name used by one person when working on a video posting site. It can be said that it is a symbol of the desire for approval that one person can not usually show in public, but is far from being a real person. is attached [Note 11][38]. He posts cover videos of popular bands shot from angles that hide his face, and the number of registrants on the posting site is nearly 80,000 [39]. He has also attracted attention from music insiders like Poison Yami. The video was monetized by Naoki, and was used for the cost of replacing the broken guitar at the school festival live [40].
    With his current ability in live performances, he thinks that even if he gathers fans under the name of Guitar Hero, he cannot perform enough to satisfy them. No [Note 12]."""
    
    print(llm(generate_prompt(instruction=instruction, input=input)))

    Guitar Hero has approximately 80,000 subscribers.

    【翻蚳】
    ギタヌヒヌロヌには玄 80,000 人の登録者がいたす。

    5. おわりに

    RWKVは、「GPT-4」「GPT-3.5」のような様々なプロンプト構文に察応する胜力は"ただ"なさそうですが、孊習したこずはできるので、ファむンチュヌニング (LoRAなど) プロンプトテンプレヌトのカスタマむズで、「LangChain」や「LlamaIndex」で䜿えるようになりそうな予感です。



     
     

    npaka

     
     
    プログラマヌ。iPhone / Android / Unity / ROS / AI / AR / VR / RasPi / ロボット / ガゞェット。幎2冊ペヌスで技術曞を執筆。アニ゜ン / カラオケ / ギタヌ / 猫 twitter : @npaka123

    あなたぞのおすすめ