(メモ) LlamaIndexだけを利用して多段推論を試してみる
以下の記事で、LlamaIndexのquery
だけでは文書を参照した多段推論(問いが複数のステップに分解され、それぞれのステップにおいて文書を参照して解く必要があるタスク)が解けないため、LangChainのAgentと組み合わせて使用することを試みました。
しかしLlamaIndexのドキュメントを読んでいると、多段推論が可能な機能が公開されていることに気付きました。
StepDecomposeQueryTransform
とMultiStepQueryEngine
を利用することで、上図のように問いを分解し、それぞれの問いを解くために外部データを参照するプロセスが実現可能となるようです。
そこでhttps://tripdancer0916.hatenablog.com/entry/llamaindex_agent の記事同様に関内のWikipediaを外部データとして、以下の問いに答えられるかを試します。
横浜三塔の歴史を日本語で答えてください。
ちなみに横浜三塔とはキングの塔(神奈川県庁本庁舎)・クイーンの塔(横浜税関)・ジャックの塔(横浜市開港記念会館)のことです。
MultiStepQueryEngineの活用
example notebookを参考に、以下のようなコードを書きました(関連する箇所のみ抜粋)。
from llama_index.indices.query.query_transform.base import StepDecomposeQueryTransform from llama_index.query_engine.multistep_query_engine import MultiStepQueryEngine step_decompose_transform = StepDecomposeQueryTransform( llm_predictor, verbose=True ) index_summary = "横浜三塔と呼ばれる塔の名前を調べるのに利用します。" query_engine = index.as_query_engine( text_qa_template=QA_PROMPT, refine_template=REFINE_TEMPLATE, service_context=service_context, similarity_top_k=5, ) query_engine = MultiStepQueryEngine( query_engine, query_transform=step_decompose_transform, index_summary=index_summary, ) response = query_engine.query( '横浜三塔の歴史を日本語で答えてください。', ) print(response)
ここで、index_summary
には外部データの説明(どのように活用するか)を記述します。Agent
におけるTool
のdescription
のようなイメージですね。
tools = [ Tool( name = "WikipediaSearch", func=lambda q: str(query_engine.query(q)), description="Wikipediaのページから検索します。" ), ]
ただし、Agentの時よりも具体で活用法を書かないと上手く動かないようでした。
実行結果
実際に上記コードを実行してみると、以下のようなプロセスが得られました。
> Current query: 横浜三塔の歴史を日本語で答えてください。 > Formatted prompt: The original question is as follows: 横浜三塔の歴史を日本語で答えてください。 We have an opportunity to answer some, or all of the question from a knowledge source. Context information for the knowledge source is provided below, as well as previous reasoning steps. (中略) Question: 横浜三塔の歴史を日本語で答えてください。 Knowledge source context: 横浜三塔と呼ばれる塔の名前を調べるのに利用します。 Previous reasoning: None New question: > New query: What are the names of the towers referred to as Yokohama San-tower?
(中略)の中身ですが、以下のようなQuestion
とKnowledge source context
, New question
のペアが例示として複数与えられます。
Question: How many Grand Slam titles does the winner of the 2020 Australian Open have? Knowledge source context: Provides names of the winners of the 2020 Australian Open Previous reasoning: None Next question: Who was the winner of the 2020 Australian Open?
プロンプトによって、Chain-of-Thoughtが行われるようです。
このプロセスにより、
New query: What are the names of the towers referred to as Yokohama San-tower?
という、「まず横浜三塔の名前を調べる」という推論フェーズに入ることが確認できました(index_summary
で天下り的に与えてしまっているのでアレですが・・)。
いくつかの推論プロセスを経て、以下のような回答が得られました。
Question: 横浜三塔の歴史を日本語で答えてください。 Knowledge source context: 横浜三塔と呼ばれる塔の名前を調べるのに利用します。 Previous reasoning: - What are the names of the towers referred to as Yokohama San-tower? - The names of the towers referred to as Yokohama San-tower are King's Tower (Kanagawa Prefectural Government Building), Queen's Tower (Yokohama Customs), and Jack's Tower (Yokohama Port Opening Memorial Hall). - What are the names of the towers referred to as Yokohama San-tower? - The names of the towers referred to as Yokohama San-tower are King's Tower (Kanagawa Prefectural Government Building), Queen's Tower (Yokohama Customs), and Jack's Tower (Yokohama Port Opening Memorial Hall). New question: > New query: None. INFO:llama_index.token_counter.token_counter:> [get_response] Total LLM token usage: 450 tokens INFO:llama_index.token_counter.token_counter:> [get_response] Total embedding token usage: 0 tokens INFO:llama_index.token_counter.token_counter:> [get_response] Total LLM token usage: 450 tokens INFO:llama_index.token_counter.token_counter:> [get_response] Total embedding token usage: 0 tokens 横浜三塔は、横浜港開港記念館(ジャックスタワー)、神奈川県庁舎(キングスタワー)、横浜税関(クイーンズタワー)の3つの建物からなる横浜のランドマークです。横浜港開港記念館は、1859年に横浜港が開港したことを記念して建てられました。神奈川県庁舎は、1926年に完成しました。横浜税関は、1930年に完成しました。
New query
がNone
になると、最終的な出力が得られるようになっています。
出てきた答えはそれっぽいですが、元のWikipediaを見るとそれぞれの完成年は
- 横浜港開港記念館: 1917年
- 神奈川県庁舎: 1928年
- 横浜税関: 1934年
なので、だいぶ間違っています。。。
プロンプトのチューニングで改善できる余地はありそうですが、パッと使った感じだとAgent
の方が性能は良さそうですね。
各手法の比較
最後に、横浜三塔の歴史を日本語で答えてください。
という問いに対する
- シンプルなクエリを使った場合
MultiStepQueryEngine
を使った場合- Agentと組み合わせた場合
に得られた答えを紹介したいと思います。
- シンプルなクエリを使った場合
横浜三塔は、キングの塔(神奈川県庁本庁舎)、クイーンの塔(横浜税関)、ジャックの塔(横浜市開港記念会館)の3つの塔で、横浜港のシンボルとして親しまれています。地元では「横浜三塔」と呼ばれ、長年市民に愛されています。
MultiStepQueryEngine
を使った場合
横浜三塔は、横浜港開港記念館(ジャックスタワー)、神奈川県庁舎(キングスタワー)、横浜税関(クイーンズタワー)の3つの建物からなる横浜のランドマークです。横浜港開港記念館は、1859年に横浜港が開港したことを記念して建てられました。神奈川県庁舎は、1926年に完成しました。横浜税関は、1930年に完成しました。
- Agentと組み合わせた場合
横浜三塔は、キングの塔(神奈川県庁本庁舎)、クイーンの塔(横浜税関)、ジャックの塔(横浜市開港記念会館)の3つの塔で、横浜港のシンボルとして親しまれています。ジャックの塔は、横浜町会所として建てられ、後に横浜市開港記念会館として再建されました。キングの塔の詳細な歴史については不明ですが、クイーンの塔は1934年に完成し、現在は横浜税関として知られています。