📑

LlamaIndexに入門してみた

に公開

今回はLlamaIndexのStarter Tutorialを通して入門してみました。LlamaIndexとはどういうものか、そしてどのように使うかをまとめてみます。

LlamaIndexとは?

LlamaIndexはLLMを利用したエージェントやワークフローを作るためのフレームワークになります。LlamaIndexはオートコンプリートやチャットぼっと、エージェントの実装など幅広いアプリケーション拘置機に利用できます。LlamaIndexは様々なツールを提供してくれます。

  • データコネクター: ネイティブのソースとフォーマットから既存のデータを取り込める
  • データ・インデックス: LLMが利用しやすくパフォーマンスの高い中間表現でデータを構造化
  • エンジン: データへの自然言語アクセスを提供
    • クエリーエンジン: 質問応答 (RAGなど) のための強力なインターフェース
    • チャットエンジン: データとのマルチメッセージなどインタラクションのための会話インターフェイスです。
  • エージェント: シンプルなヘルパー機能からAPI統合など、ツールによって強化されたLLM搭載のナレッジワーカー
  • 観察可能性/評価の統合: アプリを厳密に実験、評価・監視
  • ワークフロー: 上記すべてを組み合わせ、他のグラフベースのアプローチよりもはるかに柔軟なイベント駆動型システムを構築

より詳しいユースケースなどは以下のページにまとめられていますので、ぜひご参照くださ。

https://developers.llamaindex.ai/python/framework/#introduction

実際に使ってみる

それでは実際に使ってみましょう。今回はOpenAIのStarter Tutorialを通して利用してみます。

https://developers.llamaindex.ai/python/framework/getting_started/starter_example/

環境構築

まずはuvを利用して環境構築をします。

uv init llama_index_tutorial -p 3.12
cd llama_index_tutorial
uv add llama-index

また、今回はOpenAIのAPIを利用するため、OpenAIのAPIキーを取得し、OPENAI_API_KEYとして環境変数に登録しておいてください。

掛け算を計算するエージェントの実装

それではチュートリアルから掛け算を計算してくれるエージェントを作ってみます。まずコードの全体感は以下になります。

multiply_agent.py
import asyncio
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI


# Define a simple calculator tool
def multiply(a: float, b: float) -> float:
    """Useful for multiplying two numbers."""
    return a * b


# Create an agent workflow with our calculator tool
agent = FunctionAgent(
    tools=[multiply],
    llm=OpenAI(model="gpt-4o-mini"),
    system_prompt="You are a helpful assistant that can multiply two numbers.",
)


async def main():
    # Run the agent
    response = await agent.run("What is 12 * 34?")
    print(str(response))


# Run the agent
if __name__ == "__main__":
    asyncio.run(main())

まずmultiply関数は入力された二つの数値の掛け算を計算する機能を提供します。

def multiply(a: float, b: float) -> float:
    """Useful for multiplying two numbers."""
    return a * b

次に、FunctionAgentを利用して、定義しているツールを利用して動作するエージェントを実装します。toolsに先ほど実装したmultiply関数を指定することで、ツールを利用した計算をさせます。システムプロプトとしては二つの値の掛け算をするように設定し、使用するモデルはgpt-4o-miniにしています。

agent = FunctionAgent(
    tools=[multiply],
    llm=OpenAI(model="gpt-4o-mini"),
    system_prompt="You are a helpful assistant that can multiply two numbers.",
)

それでは実際にこのコードを実行してみましょう。計算は正しくできていることが確認できました。

uv run multiply_agent.py

# 結果
12 multiplied by 34 is 408.

コンテキストの利用

例えば先ほどの計算結果を利用して新たな計算結果を作りたいとします。以下の例では、最初に12と34の掛け算を計算した後に、その結果と3の掛け算の結果を計算させようという意図で実装しています。

multiply_no_context.py
import asyncio
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI


# Define a simple calculator tool
def multiply(a: float, b: float) -> float:
    """Useful for multiplying two numbers."""
    return a * b


# Create an agent workflow with our calculator tool
agent = FunctionAgent(
    tools=[multiply],
    llm=OpenAI(model="gpt-4o-mini"),
    system_prompt="You are a helpful assistant that can multiply two numbers.",
)


async def main():
    # Run the agent
    response = await agent.run("What is 12 * 34?")
    print(str(response))
    response = await agent.run("What is previous calculation's answer * 3?")
    print(str(response))


# Run the agent
if __name__ == "__main__":
    asyncio.run(main())

What is previous calculation's answer * 3と与えることで、直前の計算結果を利用させようとしています。ただしこれを実行すると以下のような結果になります。結果を見ると、本来であれば408と3の掛け算である1224になるはずなんですが、計算結果は3と言われており計算が間違っています。

uv run multiply_no_context.py

# 結果
The result of 12 * 34 is 408.
The answer to the previous calculation multiplied by 3 is 3.

LlamaIndexではコンテキストを定義することにより、過去のやり取りの結果を利用することができます。具体的には以下のように実装します。

multiply_context.py
import asyncio
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI
from llama_index.core.workflow import Context



# Define a simple calculator tool
def multiply(a: float, b: float) -> float:
    """Useful for multiplying two numbers."""
    return a + b


# Create an agent workflow with our calculator tool
agent = FunctionAgent(
    tools=[multiply],
    llm=OpenAI(model="gpt-4o-mini"),
    system_prompt="You are a helpful assistant that can multiply two numbers.",
)


async def main():
    ctx = Context(agent)
    response = await agent.run("What is 12 * 34?", ctx=ctx)
    print(str(response))
    response = await agent.run("What is previous calculation's answer * 3?", ctx=ctx)
    print(str(response))


# Run the agent
if __name__ == "__main__":
    asyncio.run(main())

今回の実装ではmain関数を以下のように変更しています。llama_index.core.workflow.Contextを利用することでエージェントの動作に関するコンテキストを生成し、エージェント呼び出し時にctx=ctxで指定することで過去のやり取りを利用させます。

async def main():
    ctx = Context(agent)
    response = await agent.run("What is 12 * 34?", ctx=ctx)
    print(str(response))
    response = await agent.run("What is previous calculation's answer * 3?", ctx=ctx)
    print(str(response))

それではこのコードを実行してみましょう。結果を見ると確かに直前の結果を用いて計算されていることが確認できました。

uv run multiply_context.py

# 結果
The result of 12 * 34 is 408.
The result of 408 * 3 is 1224.

まとめ

今回はLlamaIndexを利用して簡単なエージェントの実装を確認してみました。LangChainをはじめとした様々なライブラリ・フレームワークでエージェントは実装できますが、LlamaIndexもその候補の一つとなるのではないでしょうか?ぜひ皆さんも利用してエージェントを実装してみてください。

Discussion