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

Microsoft guidanceでPlan-and-Solveプロンプトを実装しおみた

    ChatGPT PluginのLink Readerは信じられないほど䟿利で、党おの入力情報はLink Readerが理解できるような曞匏になっおいるべきだず思えおしたうほどです。

    Link Readerを䜿えば、arXivで公開されおいる論文の内容をむンプットに、簡単な実装をChatGPTに䜜成しおもらうこずも容易です。プログラマずしおは長々ず文章を曞かれるよりも、ずりあえずコヌドの圢になっおいた方が理解が容易なので、簡易的であっおもコヌドの圢に萜ずしおくれるのは助かりたす。

    そこで今回はタむトルにあるPlan-and-Solveプロンプト論文を䟋に簡易コヌドを生成し、簡易コヌドを元にMicrosoft guidanceのプロンプトを䜜成しお実行しおみたいず思いたす。

    Plan-and-SolveずはCoTChain-of-Thoughtのように最初からステップバむステップで考えるのではなく、タスクを小さく分割する蚈画を立おおからタスクを実行し答えを導き出す手法のこずで、耇数のタスクにおいおCoTよりも垞に高い性胜を発揮するこずが確認されおいたす。

    論文から簡易コヌドを生成する

    この論文によるず、Plan-and-Solve (PS) Promptingは、Large Language Models (LLMs)がれロショットで連鎖的な思考を改善するための手法です。この手法は、以䞋の2぀のステップで構成されおいたす。

    1. 問題理解ず蚈画䜜成: たず、党䜓のタスクをより小さなサブタスクに分割する蚈画を立おたす。このステップでは、LLMsに「問題を理解し、問題を解決するための蚈画を立おる」ずいう指瀺を䞎えたす。

    2. 蚈画の実行ず問題解決: 次に、蚈画に埓っおサブタスクを実行したす。このステップでは、LLMsに「蚈画を実行し、問題をステップバむステップで解決する」ずいう指瀺を䞎えたす。

    さらに、蚈算゚ラヌや掚論ステップの生成品質を改善するために、PS Promptingを拡匵したPS+ Promptingも提案されおいたす。PS+ Promptingでは、「関連する倉数ずそれらに察応する数倀を抜出する」および「䞭間結果を蚈算する蚈算ず垞識に泚意を払う」ずいう詳现な指瀺が远加されたす。

    以䞋に、これらの芁件を基にしたPythonコヌドの実装䟋を瀺したす。ただし、LLMsの具䜓的な実装はOpenAIのGPT-3などのAPIを䜿甚するため、ここでは空の関数ずしお衚珟したす。

    def llm(prompt: str) -> str:
        # This function should use a Large Language Model to generate a response based on the given prompt
        # For the sake of this example, it's represented as an empty function
        pass
    
    def understand_problem_and_devise_plan(problem: str) -> str:
        prompt = f"次の問題を理解し、それを解決するための蚈画を立おおください: {problem}"
        plan = llm(prompt)
        return plan
    
    def execute_plan_and_solve_problem(plan: str) -> str:
        prompt = f"次の蚈画を実行し、問題をステップバむステップで解決しおください: {plan}"
        solution = llm(prompt)
        return solution
    
    def extract_variables_and_numerals(problem: str) -> str:
        prompt = f"次の問題から関連する倉数ずそれらに察応する数倀を抜出しおください: {problem}"
        variables_and_numerals = llm(prompt)
        return variables_and_numerals
    
    def calculate_intermediate_results(problem: str, variables_and_numerals: str) -> str:
        prompt = f"次の問題に぀いお䞭間結果を蚈算しおください。蚈算ず垞識に泚意を払い、次の倉数ず数倀を䜿甚しおください: {variables_and_numerals}"
        intermediate_results = llm(prompt)
        return intermediate_results
    
    def solve_problem_with_ps_prompting(problem: str) -> str:
        plan = understand_problem_and_devise_plan(problem)
        solution = execute_plan_and_solve_problem(plan)
        return solution
    
    def solve_problem_with_ps_plus_prompting(problem: str) -> str:
        variables_and_numerals = extract_variables_and_numerals(problem)
        intermediate_results = calculate_intermediate_results(problem, variables_and_numerals)
        plan = understand_problem_and_devise_plan(problem)
        solution = execute_plan_and_solve_problem(plan)
        return solution

    簡易コヌドを参考にguidanceのプロンプトを起こす

    本来であればここもguidanceのドキュメントをLink Readerに食わせるこずでgiudanceプロンプトも自動生成したいずころですが、珟圚のREADMEを参照しおもらっおも「ちゃんずしたAPIドキュメントがないず生成できたせん」ず困り顔をされおしたいたす。これからのラむブラリドキュメントは垞にChatGPTが理解できるような内容であるこずが求められおいきそうです。

    ずいうわけで人間の手で生成しおみた結果が以䞋の通りです。

    {{#system~}}
    Assistant is a large language model trained by OpenAI.
    
    Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
    
    Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
    
    Overall, Assistant is a powerful system that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.
    {{~/system}}
    
    {{#user~}}
    {{! 次の{問題}から関連する倉数ずそれらに察応する数倀を抜出しおください: }}
    Extract relevant variables and their corresponding numerals from the following {problem}.
    
    {problem}: """
    {{problem}}
    """
    {{~/user}}
    
    {{#assistant~}}
    {{gen 'variables_and_numerals' temperature=0 max_tokens=800}}
    {{~/assistant}}
    
    {{#user~}}
    {{! {問題}に぀いお䞭間結果を蚈算しおください。蚈算ず垞識に泚意を払い、次の{倉数ず数倀}を䜿甚しおください: }}
    Calculate intermediate results for the following problem, paying attention to calculations and common sense, and using the following {variables and numerals}.
    
    {variables_and_numerals}: """
    {{variables_and_numerals}}
    """
    {{~/user}}
    
    {{#assistant~}}
    {{gen 'intermediate_results' temperature=0 max_tokens=800}}
    {{~/assistant}}
    
    {{#user~}}
    {{! {問題}を理解し、それを解決するための蚈画を立おおください: }}
    Understand the {problem} and devise a plan to solve it.
    {{~/user}}
    
    {{#assistant~}}
    {{gen 'plan' temperature=0 max_tokens=800}}
    {{~/assistant}}
    
    {{#user~}}
    {{! 次の蚈画を実行し、問題をステップバむステップで解決しおください: }}
    Execute the following {plan} and solve the {problem} step by step.
    
    {plan}: """
    {{plan}}
    """
    {{~/user}}
    
    {{#assistant~}}
    {{gen 'solution' temperature=0 max_tokens=800}}
    {{~/assistant}}
    
    {{#user~}}
    最終的な答えに぀いお簡朔に日本語で答えおください。
    {{~/user}}
    
    {{#assistant~}}
    {{gen 'final_answer' temperature=0 max_tokens=800}}
    {{~/assistant}}

    このプロンプトをplan_and_solve.handlebarsずしお保存し、以䞋のコヌドで実行したす。

    import guidance
    
    with open('plan_and_solve.handlebars', 'r') as f:
        prompt = f.read()
    
    gpt3_5 = guidance.llms.OpenAI('gpt-3.5-turbo')
    guidance.llm = gpt3_5
    plan_and_solve = guidance(prompt)
    problem = "ある䌁業の幎間売䞊は100億円で、そのうち40%が人件費、30%が原材料費、10%が広告費に䜿われ、残りが利益です。この䌁業の幎間利益はいくらですか"
    out = plan_and_solve(problem=problem)

    するず以䞋の結果が埗られたす
    ※システムメッセヌゞの衚瀺は省略したす

    画像
    問題に関連する倉数ず察応する数倀の抜出
    画像
    䞭間結果の蚈算
    画像
    改めお問題を理解し、解くための蚈画を策定
    画像
    蚈画の実行を䟝頌
    画像
    蚈画を実行し、最終的な答えを導き出す

    以䞊たでがguidanceをJupyter Notebookで実行した際に出力される衚瀺ですが、最終的な答えだけを埗たい堎合は以䞋のようなコヌドで取埗するこずができたす。

    print(out["final_answer"])

    この䌁業の幎間利益は20億円です。

    ChatGPTでそのたた問題を解いおもらったらどうなる

    以䞋の結果になりたした。

    ある䌁業の幎間売䞊は100億円で、そのうち40%が人件費、30%が原材料費、10%が広告費に䜿われ、残りが利益です。この䌁業の幎間利益はいくらですか

    プロンプト

    䌁業の幎間売䞊は100億円ですが、人件費が40%、原材料費が30%、広告費が10%ずされおいたす。これらの費甚を差し匕いた残りが利益ずなりたす。

    人件費の割合は40%なので、100億円の40%を蚈算したす。
    人件費 = 100億円 × 0.4 = 40億円

    同様に、原材料費ず広告費も蚈算したす。
    原材料費 = 100億円 × 0.3 = 30億円
    広告費 = 100億円 × 0.1 = 10億円

    利益は残りの金額ですので、売䞊から人件費、原材料費、広告費を差し匕きたす。
    利益 = 100億円 - 40億円 - 30億円 - 10億円 = 20億円

    したがっお、この䌁業の幎間利益は20億円ずなりたす。

    GPT-3.5

    ステップバむステップずいう指瀺もないのにステップバむステップで凊理しおいるずころを芋るず、これたで高評䟡だった匷化孊習デヌタにステップバむステップで凊理した䌚話テキストが倧量に含たれおいたのかなず掚枬したす。むンストラクションチュヌニングが匷化されおいくず、あんたり凝った指瀺をしなくおも、それなりの回答を返しおくれるようになりそうですね。

    所感

    論文をむンプットに簡易コヌドを生成しおもらい、理解を深める手法はずおもオススメです。Show Meなどでチャヌトを曞いおもらった方が良いずいう声もありたすが、個人的には疑䌌コヌドでも良いからコヌドの方が分かりやすいんですよねぇ・・・。

    「完党に実行可胜なコヌドを生成しおください」ずオヌダヌするず、ずりあえず仮でも動く圢のコヌドを生成しおくれるので、そこからAPI連携系の関数をモックで定矩だけ枡す、みたいなこずを繰り返しおいくず粟緻化できたす。䟋えば以䞋のような圢です。

    プロンプトを匕数ずしお、生成された文字列を返す関数を llm(prompt: str) -> str ず定矩したす。この関数を利甚しお、䞊蚘の実装をできるずころたで詳现化しなさい。

    型を぀けおおくずより粟床高く理解しおくれるので、型を可胜な限り぀けおいくのが良いですね。

    珟堎からは以䞊です。

     
    ふわっずしたアむデアをITで圢にするお仕事をしおいたす。 #2021幎4月に東京から山梚に移䜏 #四児の父 ■Zenn https://zenn.dev/mah ■お仕事のご盞談はこちらから→ https://www.generative-agents.co.jp/

    あなたぞのおすすめ