0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LlamaIndexのv0.10系以降に非推奨となったクラスの例

Last updated at Posted at 2025-04-07

LlamaIndexとは?

LlamaIndex は、大規模言語モデル(LLM)を活用したアプリケーションを構築するためのフレームワークです。
データストア連携やテキストデータ取り込み、インデックス作成し、LLMを使って質問応答や情報抽出などのタスクを実行できます。
変更履歴より2025-2-11から2025-3-28までの間でもllama-index-core[0.12.17]から[0.12.27]までバージョンが存在し、頻繁なアップデートが行われています。
本記事では、LlamaIndex v0.10系(2024年前半)から、非推奨となった機能について一部取り上げます。

廃止または非推奨となったクラス等

LlamaIndexのアップデートに伴い、一部のモジュールやメソッドが廃止または非推奨となることがあります。移行パスが提供されている場合もありますが、既存のコードを修正する必要が生じる可能性があります。

ナレッジグラフインデックスの非推奨化

変更履歴[2024-07-08]時点でのバージョンllama-index-core0.10.53以降、KnowledgeGraphIndexは非推奨となりました。
より表現力豊かなグラフ構造を活用できるプロパティグラフインデックスの使用が推奨されています。
PropertyGraphIndexは、[2024-05-28]更新のバージョンllama-index-core0.10.40にて追加されています。

ServiceContextとLLMPredictorのllm引数の非推奨

変更履歴[2024-08-22]時点でのバージョンllama-index-core0.11.0以降、ServiceContextLLMPredictor非推奨となりました。
以前は、LLMPredictorを使って直接LLMモデルを渡し渡す方法が存在しましたが、現在はLLMの設定はグローバルなSettingsオブジェクトを通じて行うことが推奨されています。

GeminiとGoogleGenAIどちらも存在する

LlamaIndexでは、GoogleのGeminiモデルを利用することができます。
変更履歴[2025-03-13]のllama-index-core0.12.24の際にllama-index-llms-google-genaiが登場しています。
現在では、examplesのページで、「Gemini has largely been replaced by Google GenAI. Visit the Google GenAI page for the latest examples and documentation.」と注意書きされていることから、llama-index-llms-google-genaiを使用する方法が推奨されています。

GoogleGenAIのサンプルコード

from llama_index.llms.google_genai import GoogleGenAI
from llama_index.core import Settings

APIKEY = ""

# LlamaIndexのグローバル設定でLLMを設定
Settings.llm = GoogleGenAI(
    model="gemini-2.0-flash",  # 使用するGeminiモデルを指定します。
    api_key=APIKEY,  # Google Cloud APIキーを設定します。
    temperature=2,# 生成されるテキストのランダム性を制御します。0に近いほど決定的2に近いほど創造的になります。
    max_tokens=1024  
)
Settings.chunk_size = 512

プロパティグラフインデックスとは?

プロパティグラフインデックスは、ノードとエッジで構成されるグラフ構造を用いて、データ間の関係性を表現するインデックスです。
各ノードとエッジは、それぞれプロパティ(属性)を持つことができます。これにより、ナレッジグラフインデックスよりも、より複雑で詳細な関係性を表現することが可能になります。

データの準備
例えば、Notion, Google Drive、Slack、ChromaDBなどの様々なデータを取り込むためのデータコネクタが用意されています。
今回は例として、DBやクラウドサービス等を使用せず、ローカルのファイルを読み込む例を示します。

import nest_asyncio
from llama_index.core import(schema,
                             PropertyGraphIndex,
                             SimpleDirectoryReader,
                             Settings)
from llama_index.core.node_parser import SentenceSplitter
from llama_index.core import StorageContext, load_index_from_storage
from llama_index.llms.google_genai import GoogleGenAI


# ディレクトリ内のドキュメントを読み込む
documents = SimpleDirectoryReader("./data").load_data()

私の使用する無料枠ではインデックス作成時にドキュメントを全て投入すると429のレート制限超過エラーが発生するため、分割します。

    node_parser = SentenceSplitter(chunk_size=512,
                                   chunk_overlap=128)

    nodes = node_parser.get_nodes_from_documents(documents)

インデックスの保存

    nest_asyncio.apply()
    index = PropertyGraphIndex([], embed_kg_nodes=False)
    for node in nodes:
        index.insert_nodes([node])
        time.sleep(20)
    # ローカルのディレクトリに保存
    index.storage_context.persist("./Loveit!")

extractorについては、デフォルトではSimpleLLMPathExtractorImplicitPathExtractorが設定されます。
内部の非同期処理のRuntimeError: Event loop is closed対策のため、nest_asyncio.apply()を補足しています。
構造化したスキーマ(エンティティの種類と関係性の種類)を意識したい場合はSchemaLLMPathExtractorを使用します。

指定したディレクトリが作成され、以下のファイルが保存されます。

  • default__vector_store.json
  • docstore.json
  • graph_store.json
  • image__vector_store.json
  • index_store.json
  • property_graph_store.json

クエリの実行

  • クエリエンジンの使用
    # ローカルディレクトリからインデックス取得
    contexts = StorageContext.from_defaults(persist_dir="./Loveit!")
    index = load_index_from_storage(contexts)
    # クエリエンジン
    query_engine = index.as_query_engine()
    response = query_engine.query("「ラブィット!」の火曜日に出演しているメンバーを教えてください。")
    with open("response.txt", "w", encoding="utf-8", newline="\n") as f:
        f.write(str(response))

出力:

MCは川島明さんと田村真子さんです。火曜レギュラーは、若槻千夏さん、ビビる大木さん、アインシュタインさん(稲田直樹さん、河井ゆずるさん、隔週)、宮舘涼太さん(Snow Man、隔週)、佐久間大介さん(Snow Man、隔週)、モグライダーさん(芝大輔さん、ともしげさん、隔週)です。また、進行役として赤荻歩さん、ときには南波雅俊さん、熊崎風斗さん、その他TBSアナウンサーも出演します。

  • レトリバーを使用した場合
    デフォルトのレトリバーで実行
    retriever = index.as_retriever(
        include_text=True,  # include source chunk with matching paths
        similarity_top_k=2,  # top k for vector kg node retrieval
    )
    nodes = retriever.retrieve("「ラブィット!」に火曜日に出演しているメンバーを教えてください。")
    with open("out.txt", "w", encoding="utf-8", newline="\n") as f:
        for node in nodes:
            f.write(node.text)

出力の冒頭部分:

Here are some facts extracted from the provided text:

馬場裕之 -> 月曜レギュラー -> ラブィット!
川島 明 -> Mc -> ラブィット!
アインシュタイン -> 火曜レギュラー -> ラブィット!
田村真子 -> Mc -> ラブィット!
なすなかにし -> 月曜レギュラー -> ラブィット!
ぼる塾 -> 月曜レギュラー -> ラブィット!
若槻千夏 -> 火曜レギュラー -> ラブィット!
ビビる大木 -> 火曜レギュラー -> ラブィット!
本並健治&丸山桂里奈 -> 月曜レギュラー -> ラブィット!
宮舘涼太 -> 火曜レギュラー -> ラブィット!

まとめ

v0.10系からv0.12系において非推奨となったクラスや、Geminiとの連携、プロパティグラフインデックスについて記しました。
1年間でも多数の機能が変更しておりv0.10系からアップデートする際はコードを大きく修正する可能性があり、最新のアップデートを把握する必要があります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?