LangChain エージェントを使用する

このページでは、エージェントの使用に関する一般的な手順に加えて、LangchainAgent に固有の機能について説明します。

始める前に

このチュートリアルは、次の手順を読んで理解していることを前提としています。

サポートされているオペレーション

LangchainAgent でサポートされているオペレーションは次のとおりです。

  • query: クエリへのレスポンスを同期的に取得します。
  • stream_query: クエリへのレスポンスをストリーミングします。

query メソッドと stream_query メソッドは、同じタイプの引数をサポートしています。

  • input: エージェントに送信するメッセージ。
  • config: クエリのコンテキストの構成(該当する場合)。

エージェントに問い合わせる

コマンド:

agent.query(input="What is the exchange rate from US dollars to Swedish currency?")

は、次(完全な形式)と同等です。

agent.query(input={
    "input": [ # The input is represented as a list of messages (each message as a dict)
        {
            # The role (e.g. "system", "user", "assistant", "tool")
            "role": "user",
            # The type (e.g. "text", "tool_use", "image_url", "media")
            "type": "text",
            # The rest of the message (this varies based on the type)
            "text": "What is the exchange rate from US dollars to Swedish currency?",
        },
    ]
})

ロールは、モデルが応答時にさまざまなタイプのメッセージを区別するために使用されます。入力で role が省略されている場合、デフォルトは "user" になります。

ロール 説明
system チャットモデルの動作を指示し、追加のコンテキストを提供するために使用されます。一部のチャットモデル プロバイダではサポートされていません。
user モデルを操作するユーザーからの入力を表します。通常はテキストなどのインタラクティブな入力の形式で表されます。
assistant モデルからのレスポンスを表します。これには、テキストやツールを呼び出すリクエストが含まれます。
tool 外部データまたは処理が取得された後に、ツール呼び出しの結果をモデルに渡すために使用されるメッセージ。

メッセージの type によって、メッセージの残りの部分の解釈方法も決まります(マルチモーダル コンテンツを処理するをご覧ください)。

マルチモーダル コンテンツを使用してエージェントにクエリを実行する

マルチモーダル入力をエージェントに渡す方法を説明するために、次のエージェント(入力をモデルに転送し、ツールは使用しない)を使用します。

agent = agent_engines.LangchainAgent(
    model="gemini-1.5-pro-preview-0409",
    runnable_builder=lambda model, **kwargs: model,
)

マルチモーダル メッセージは、type と対応するデータを指定するコンテンツ ブロックで表されます。通常、マルチモーダル コンテンツの場合は、type"media" に、file_uri を Cloud Storage URI を指すように、mime_type をファイルの解釈用に指定します。

画像

agent.query(input={"input": [
    {"type": "text", "text": "Describe the attached media in 5 words!"},
    {"type": "media", "mime_type": "image/jpeg", "file_uri": "gs://cloud-samples-data/generative-ai/image/cricket.jpeg"},
]})

動画

agent.query(input={"input": [
    {"type": "text", "text": "Describe the attached media in 5 words!"},
    {"type": "media", "mime_type": "video/mp4", "file_uri": "gs://cloud-samples-data/generative-ai/video/pixel8.mp4"},
]})

オーディオ

agent.query(input={"input": [
    {"type": "text", "text": "Describe the attached media in 5 words!"},
    {"type": "media", "mime_type": "audio/mp3", "file_uri": "gs://cloud-samples-data/generative-ai/audio/pixel.mp3"},
]})

Gemini でサポートされている MIME タイプのリストについては、次のドキュメントをご覧ください。

実行可能な構成でエージェントにクエリを実行する

エージェントをクエリするときに、エージェントの configRunnableConfig のスキーマに従う)を指定することもできます。一般的なシナリオは次の 2 つです。

例:

import uuid

run_id = uuid.uuid4()  # Generate an ID for tracking the run later.

response = agent.query(
    input="What is the exchange rate from US dollars to Swedish currency?",
    config={  # Specify the RunnableConfig here.
        "run_id": run_id                               # Optional.
        "tags": ["config-tag"],                        # Optional.
        "metadata": {"config-key": "config-value"},    # Optional.
        "configurable": {"session_id": "SESSION_ID"}   # Optional.
    },
)

print(response)

次のステップ