
GPT indexの仕組みを理解する
GPTindexの仕組みを手っ取り早く理解する方法を解説します
使い方は説明しません
GPT indexがブラックボックスであることが嫌な人向けです
0.そもそも何?
前提: GPT3は構造上4097トークン以上前の情報は忘れる
目的: LLMに長期記憶を追加し、QAタスクを実行
LLMが大量の情報を参考にすることが可能に
従来はすべての情報をLLMに入力しないといけなかったが、これを利用するとLLMがGPT-indexから情報の入力を受け、質問に関連する情報を獲得することができる
token消費量を減らすことは目的ではない
GPT-indexは、GitHubでオープンソースとして公開されています。詳細は以下のリンクを参照してください。
1.利用の流れ
ざっくり以下のようになっています。それぞれ詳しいことは後で解説します。
手動でテキストを分割し、適切なデータの構造に整理する
QAに利用するLLMのインスタンスを作成
emmbedingモデルのインスタンスも作成
indexの定義
index.queryでクエリ(質問内容)を入力し実行 この時にQAモデルとembeddingモデルを渡す
n個ノードを選抜 : 問い合わせ(Querying)
プロンプトをテンプレートから作成
ノードを一個ずつQAモデルに入力していく(Refine)
なお、QAモデルへ入力されるプロンプトテンプレートでは以下のようなものが渡されます
# gpt-index 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"
)
2.データ検索手法
入力されたテキストを分割し、その分割された者たちから適切な対象を選択する
分割は自分でやる
分割長い: embeddingが不正確に
分割短い: コンテキストが失われる
How Each Index Works — GPT Index documentation (gpt-index.readthedocs.io)
2.1リスト型索引 : List Index
決められた順序でノードをつなげていく

問い合わせ方法(Querying)
様々な方法がある

すべてのノードを順番につなげてそのまま入力

キーワードでフィルタリングしたり、ノードを埋め込んだときクエリとの類似度がtop_k位のものを入れたりもできる
2.2ベクトル保存索引 : Vector Store Index

ノード内文章の埋め込みベクトルも保存
なお、埋め込みは自分で用意した埋め込みベクトルモデル等の任意のモデルが利用可能
問い合わせ方法(Querying)

問い合わせ(クエリ)自体の埋め込みベクトルと、ノードの埋め込みのコサイン類似度がtop_k位のものを取り出す
※List Indexのembedding利用時との違いは順序があるかないかだけ?教えて
2.3ツリー索引: Tree Index

ドキュメントを何かしらの方法でツリー状に変換
問い合わせ方法(Querying)

クエリを受け取り、ツリーのノードを順に訪問(トラバース)する。どっちの子ノードへ進むかは以下の方法のどれかで決定
default: QAモデルでプロンプトテンプレートを利用し適切なノードを選択させる
embeddings: ノードの埋め込みとクエリの類似度で検索
retrive: ルートノードをそのまま利用
リーフノードまで下ればそのリーフノードの要約をGPTに入力し質問に答えさせる

要約タスクにはこの方法が最適
下のノードからクエリに対する回答をしていき、それらをどんどん上へ実行し最終的にすべてのノードを統合したクエリに対する回答を得られる
2.4キーワード表索引: Keyword Table Index

各ノードからキーワードを選定しそれぞれのキーワードとノードの関連性を整理していく
なお、キーワードを選定するためにGPT+プロンプトテンプレートかRAKEといったライブラリを活用
問い合わせ方法(Querying)

与えられたクエリに対してGPT+プロンプトテンプレートを利用して、キーワード取り出しを行う
3.Refine

最終的に複数のノードが選抜された場合に、ノードごとに毎度QAタスクを実行していく
具体的には以下のようなプロンプトテンプレートが利用されます
DEFAULT_REFINE_PROMPT_TMPL = (
"The original question is as follows: {query_str}\n"
"We have provided an existing answer: {existing_answer}\n"
"We have the opportunity to refine the existing answer"
"(only if needed) with some more context below.\n"
"------------\n"
"{context_msg}\n"
"------------\n"
"Given the new context, refine the original answer to better "
"answer the question. "
"If the context isn't useful, return the original answer."
4.その他GPTに渡すときのプロンプト例
上記の方法ではGPTを活用し、ワードを選んだりノードを選んだりしていたが、その時にGPTにそれをさせるために入力されるプロンプトは以下のようになる
関連するノードを選択するとき
DEFAULT_QUERY_PROMPT_TMPL = (
"Some choices are given below. It is provided in a numbered list "
"(1 to {num_chunks}),"
"where each item in the list corresponds to a summary.\\n"
"---------------------\\n"
"{context_list}"
"\\n---------------------\\n"
"Using only the choices above and not prior knowledge, return "
"the choice that is most relevant to the question: '{query_str}'\\n"
"Provide choice in the following format: 'ANSWER: <number>' and explain why "
"this summary was selected in relation to the question.\\n"
)
複数ノード選択
DEFAULT_QUERY_PROMPT_MULTIPLE_TMPL = (
"Some choices are given below. It is provided in a numbered "
"list (1 to {num_chunks}), "
"where each item in the list corresponds to a summary.\\n"
"---------------------\\n"
"{context_list}"
"\\n---------------------\\n"
"Using only the choices above and not prior knowledge, return the top choices "
"(no more than {branching_factor}, ranked by most relevant to least) that "
"are most relevant to the question: '{query_str}'\\n"
"Provide choices in the following format: 'ANSWER: <numbers>' and explain why "
"these summaries were selected in relation to the question.\\n"
)
要約
DEFAULT_SUMMARY_PROMPT_TMPL = (
"Write a summary of the following. Try to use only the "
"information provided. "
"Try to include as many key details as possible.\\n"
"\\n"
"\\n"
"{context_str}\\n"
"\\n"
"\\n"
'SUMMARY:"""\\n'
)
関連、参考文献
Welcome to GPT Index! — GPT Index documentation (gpt-index.readthedocs.io)
gpt_index/index_guide.md at main · jerryjliu/gpt_index (github.com)
GPT Index のインデックスの種類|npaka|note