Skip to content

コードでのLLMプロバイダーの設定

LLMエージェントの重要な構成要素のひとつは、基盤となるLLMプロバイダーです。 DataRobotでは、ユーザーがエージェントワークフローのために、事実上あらゆるLLMバックエンドに接続できます。 LLM接続は、DataRobot LLM GatewayまたはDataRobotデプロイ(NIMデプロイを含む)を使用することで簡素化できます。 あるいは、OpenAI API標準をサポートする外部のLLMプロバイダーに接続することもできます。

DataRobotのエージェントテンプレートには、エージェントLLMを定義するための方法が複数用意されています。

  • エージェントLLMとしてDataRobot LLM Gatewayを使用し、Gatewayで利用可能な任意のモデルを使用できます。
  • デプロイIDを指定することで、DataRobot APIを使用して以前にデプロイされたカスタムモデルまたはNIMを使用するように接続します。
  • 必要なAPI資格情報を提供することで、LLMプロバイダーのAPI(OpenAI、Anthropic、Geminiなど)に直接接続し、互換性のあるAPIをサポートするプロバイダーにアクセスできるようにします。

このドキュメントでは、手動でagent.pyファイルに直接 LLM インスタンスを作成してLLMプロバイダーを設定することに焦点を当てます。 このアプローチは、LLMの初期化をきめ細かくコントロールすることができ、以下のフレームワーク別の例で示されています。

別の設定方法

環境変数とPulumi(インフラストラクチャレベルの設定)を使ってLLMプロバイダーを設定したい場合は、メタデータを使ってLLMプロバイダーを設定するを参照してください。

以下のセクションでは、CrewAI、LangGraph、LlamaIndex、およびNAT(NVIDIA NeMo Agent Toolkit)フレームワークを使用してさまざまなLLMプロバイダーに接続するためのコードスニペットの例を紹介します。 これらのスニペットを出発点として使用し、特定のユースケースに合うように必要に応じて修正することができます。

DataRobot LLM Gateway

LLM Gatewayは、DataRobot経由でプロキシされたLLMにアクセスする合理的な方法を提供します。 このGatewayは、クラウドとオンプレミスの両方のユーザーが利用できます。

以下の方法で、アカウントで利用可能なモデルのリストを取得できます。

curl -X GET -H "Authorization: Bearer $DATAROBOT_API_TOKEN" "$DATAROBOT_ENDPOINT/genai/llmgw/catalog/" | jq '[.data[] | select(.isActive == true) | .model]' 
import datarobot
print("\n".join(datarobot.genai.LLMGatewayCatalog.get_available_models())) 

以下のコード例は、CrewAI、LangGraph、LlamaIndexフレームワークでDataRobot LLM Gatewayにプログラムで接続する方法を示しています。 これらのサンプルは、モデル、APIエンドポイント、認証の設定方法を示しています。

LLM Gatewayでのモデル形式

DataRobot LLM Gatewayを使用する場合、モデル名の形式はdatarobot/<provider>/<model>です(例:datarobot/azure/gpt-5-mini-2025-08-07)。

from crewai import LLM

def llm(self) -> LLM:
    """Returns a CrewAI LLM instance configured to use DataRobot's LLM gateway."""
    return LLM(
        model="datarobot/azure/gpt-5-mini-2025-08-07",  # Define the model name you want to use (format: datarobot/<provider>/<model>)
        # Note: The `/chat/completions` endpoint will be automatically appended by LiteLLM
        api_base="https://app.datarobot.com",  # DataRobot endpoint
        api_key=self.api_key,  # Your DataRobot API key
        timeout=self.timeout,  # Optional timeout for requests
    ) 
from langchain_community.chat_models import ChatLiteLLM

def llm(self) -> ChatLiteLLM:
    """Returns a ChatLiteLLM instance configured to use DataRobot's LLM gateway."""
    return ChatLiteLLM(
        model="datarobot/azure/gpt-5-mini-2025-08-07",  # Define the model name you want to use (format: datarobot/<provider>/<model>)
        # Note: The `/chat/completions` endpoint will be automatically appended by LiteLLM
        api_base="https://app.datarobot.com",  # DataRobot endpoint
        api_key=self.api_key,  # Your DataRobot API key
        timeout=self.timeout,  # Optional timeout for requests
    ) 
# DataRobotLiteLLM class is included in the `agent.py` file

def llm(self) -> DataRobotLiteLLM:
    """Returns a DataRobotLiteLLM instance configured to use DataRobot's LLM gateway."""
    return DataRobotLiteLLM(
        model="datarobot/azure/gpt-5-mini-2025-08-07",  # Define the model name you want to use (format: datarobot/<provider>/<model>)
        # Note: The `/chat/completions` endpoint will be automatically appended by LiteLLM
        api_base="https://app.datarobot.com",  # DataRobot endpoint
        api_key=self.api_key,  # Your DataRobot API key
        timeout=self.timeout,  # Optional timeout for requests
    ) 

NATのテンプレートでは、LLMはworkflow.yamlファイルで設定されます。 DataRobot LLM Gatewayを使用するには、llmsセクションでLLMを定義します。

workflow.yaml
llms:
  datarobot_llm:
    _type: datarobot-llm-gateway
    model_name: azure/gpt-4o-mini  # Define the model name you want to use
    temperature: 0.0 

次に、特定のエージェントが使用するLLMを、functionsセクション内のそのエージェントの定義でllm_nameによって定義します。

workflow.yaml
functions:
  planner:
    _type: chat_completion
    llm_name: datarobot_llm  # Reference the LLM defined below
    system_prompt: |
      You are a content planner... 

llmsセクションに複数のLLMが定義されている場合、各functionsはタスクに合わせて異なるLLMを使用できます。

NATが提供するLLMインターフェイス

LLM Gatewayの代わりに、NATが提供するLLMインターフェイスを利用することもできます。 NAT LLMインターフェイスを利用するには、api_keyurlなどの必要な設定パラメーターやプロバイダー固有のその他の設定をworkflow.yamlファイルに直接追加します。

DataRobotがホストするLLMデプロイ

エージェントのLLMプロバイダーとして、DataRobotがホストするLLMデプロイに簡単に接続できます。 そのためには、DataRobotのプレイグラウンドからLLMをデプロイするか、Hugging FaceモデルをDataRobot上のLLMデプロイとしてホストする。 DataRobotがホストするLLMは、モデルを管理・運営するためのモデレーションやガードレールへのアクセスも提供できます。

デプロイされたカスタムモデルを使用するには、以下の例に従って、デプロイURLをエージェントコード内でapi_base=パラメーターに対して直接手動で設定します。

デプロイID

以下の例では、DEPLOYMENT_IDを実際のDataRobotデプロイIDに置き換える必要があります。このIDは、DataRobotプラットフォームから取得できます。

モデル名の文字列構造

DataRobotのデプロイでは、OpenAI互換のチャット補完エンドポイントを使用します。 そのため、modelの名前文字列はopenai/で始まり、OpenAIクライアントを使用していることを示す必要があります。 openai/の後のモデル名文字列には、デプロイ内のモデルの名前を指定します。

  • プレイグラウンドからデプロイされたLLMの場合、model文字列にはプロバイダー名とモデル名を含める必要があります。 以下の例では、完全なモデル名はazure/gpt-4o-miniであり、単にgpt-4o-miniではなく、プロバイダーが含まれています。 これにより、最終的な値はmodel="openai/azure/gpt-4o-mini"になります。

  • NIMモデルの場合、model文字列は、NIMデプロイの予測タブまたはNIMのドキュメントで確認できます。 NIMのデプロイはopenaiまたはmeta_llamaインターフェイスのどちらでも動作しますが、一貫性を保つためにopenaiを使用することをお勧めします。

from crewai import LLM

def llm(self) -> LLM:
    """Returns a CrewAI LLM instance configured to use a DataRobot Deployment."""
    return LLM(
        # Note: For DataRobot deployments, use the openai provider format
        model="openai/azure/gpt-4o-mini",  # Format: openai/<model-name>
        # Note: The `/chat/completions` endpoint will be automatically appended by LiteLLM
        api_base=f"https://app.datarobot.com/api/v2/deployments/{DEPLOYMENT_ID}/",  # Deployment URL
        api_key=self.api_key,  # Your DataRobot API key
        timeout=self.timeout,  # Optional timeout for requests
    ) 
from langchain_community.chat_models import ChatLiteLLM

def llm(self) -> ChatLiteLLM:
    """Returns a ChatLiteLLM instance configured to use a DataRobot Deployment."""
    return ChatLiteLLM(
        # Note: LangGraph uses datarobot provider format for deployments
        model="openai/azure/gpt-4o-mini",  # Format: openai/<model-name>
        # Note: The `/chat/completions` endpoint will be automatically appended by LiteLLM
        api_base=f"https://app.datarobot.com/api/v2/deployments/{DEPLOYMENT_ID}/",  # Deployment URL
        api_key=self.api_key,  # Your DataRobot API key
        timeout=self.timeout,  # Optional timeout for requests
    ) 
# DataRobotLiteLLM class is included in the `agent.py` file

def llm(self) -> DataRobotLiteLLM:
    """Returns a DataRobotLiteLLM instance configured to use a DataRobot Deployment."""
    return DataRobotLiteLLM(
        # Note: For DataRobot deployments, use the openai provider format
        model="openai/azure/gpt-4o-mini",  # Format: openai/<model-name>
        # Note: The `/chat/completions` endpoint will be automatically appended by LiteLLM
        api_base=f"https://app.datarobot.com/api/v2/deployments/{DEPLOYMENT_ID}/",  # Deployment URL
        api_key=self.api_key,  # Your DataRobot API key
        timeout=self.timeout,  # Optional timeout for requests
    ) 

NATのテンプレートでは、LLMはworkflow.yamlファイルで設定されます。 DataRobotがホストするLLMデプロイを使用するには、llmsセクションでLLMを定義します。

workflow.yaml
llms:
  datarobot_deployment:
    _type: datarobot-llm-deployment
    model_name: datarobot-deployed-llm  # Optional: Define the model name to pass through to the deployment
    temperature: 0.0 

デプロイIDは、LLM_DEPLOYMENT_ID環境変数またはランタイムパラメーターから自動的に取得されます。

このデプロイを使用するには、functionsセクション内のエージェント定義のllm_nameを使用して、特定のエージェントが使用するLLMを定義します。

workflow.yaml
functions:
  planner:
    _type: chat_completion
    llm_name: datarobot_deployment  # Reference the LLM defined above
    system_prompt: |
      You are a content planner... 

llmsセクションに複数のLLMが定義されている場合、各functionsはタスクに合わせて異なるLLMを使用できます。

NATが提供するLLMインターフェイス

LLM Gatewayの代わりに、NATが提供するLLMインターフェイスを利用することもできます。 NAT LLMインターフェイスを利用するには、api_keyurlなどの必要な設定パラメーターやプロバイダー固有のその他の設定をworkflow.yamlファイルに直接追加します。

DataRobotのNIMデプロイ

このテンプレートは、NIMデプロイをLLMプロバイダーとして使用することをサポートしています。これにより、DataRobotでホストされている任意のNIMデプロイをエージェントのLLMプロバイダーとして使用できます。 NIMデプロイでLiteLLMを使用する場合は、openaiプロバイダーインターフェイスを使用します。 モデル名は具体的なデプロイによって異なり、DataRobotのデプロイの予測タブで確認できます。 たとえば、デプロイがmeta/llama-3.2-1b-instructという名前のモデルを使用する場合、モデルの文字列にはopenai/meta/llama-3.2-1b-instructを使用します。 これにより、LiteLLMはopenai APIアダプターとモデル名meta/llama-3.2-1b-instructを使用するよう指示されます。

新しいNIMデプロイを作成するには、DataRobot NIMドキュメントの指示に従います。

デプロイID

以下の例では、DEPLOYMENT_IDを実際のDataRobotデプロイIDに置き換える必要があります。このIDは、DataRobotプラットフォームから取得できます。

モデル名の文字列構造

DataRobotのデプロイでは、OpenAI互換のチャット補完エンドポイントを使用します。 そのため、modelの名前文字列はopenai/で始まり、OpenAIクライアントを使用していることを示す必要があります。 openai/の後のモデル名文字列には、デプロイ内のモデルの名前を指定します。

  • プレイグラウンドからデプロイされたLLMの場合、model文字列にはプロバイダー名とモデル名を含める必要があります。 以下の例では、完全なモデル名はazure/gpt-4o-miniであり、単にgpt-4o-miniではなく、プロバイダーが含まれています。 これにより、最終的な値はmodel="openai/azure/gpt-4o-mini"になります。

  • NIMモデルの場合、model文字列は、NIMデプロイの予測タブまたはNIMのドキュメントで確認できます。 NIMのデプロイはopenaiまたはmeta_llamaインターフェイスのどちらでも動作しますが、一貫性を保つためにopenaiを使用することをお勧めします。

from crewai import LLM

def llm(self) -> LLM:
    """Returns a CrewAI LLM instance configured to use a NIM deployed on DataRobot."""
    return LLM(
        # Use the openai provider with the model name from your deployment's Predictions tab
        model="openai/meta/llama-3.2-1b-instruct",  # Format: openai/<model-name-from-deployment>
        api_base=f"https://app.datarobot.com/api/v2/deployments/{DEPLOYMENT_ID}",  # NIM Deployment URL
        api_key=self.api_key,  # Your DataRobot API key
        timeout=self.timeout,  # Optional timeout for requests
    ) 
from langchain_openai import ChatOpenAI

def llm(self) -> ChatOpenAI:
    """Returns a ChatOpenAI instance configured to use a NIM deployed on DataRobot."""
    return ChatOpenAI(
        # Use the model name from your deployment's Predictions tab
        model="meta/llama-3.2-1b-instruct",  # Model name from deployment's Predictions tab
        api_base=f"https://app.datarobot.com/api/v2/deployments/{DEPLOYMENT_ID}",  # NIM deployment URL
        api_key=self.api_key,  # Your DataRobot API key
        timeout=self.timeout,  # Optional timeout for requests
    ) 
from llama_index.llms.openai_like import OpenAILike

def llm(self) -> OpenAILike:
    """Returns an OpenAILike instance configured to use a NIM deployed on DataRobot."""
    return OpenAILike(
        # Use the model name from your deployment's Predictions tab
        model="meta/llama-3.2-1b-instruct",  # Model name from the deployment's Predictions tab
        api_base=f"https://app.datarobot.com/api/v2/deployments/{DEPLOYMENT_ID}/v1",  # NIM deployment URL with /v1 endpoint
        api_key=self.api_key,  # Your DataRobot API key
        timeout=self.timeout,  # Optional timeout for requests
        is_chat_model=True,  # Enable chat model mode for NIM endpoints
    ) 

NATのテンプレートでは、LLMはworkflow.yamlファイルで設定されます。 DataRobotのNIMデプロイを使用するには、llmsセクションでLLMを定義します。

workflow.yaml
llms:
  datarobot_nim:
    _type: datarobot-nim
    model_name: meta/llama-3.2-1b-instruct  # Optional: Define the model name to pass through to the deployment
    temperature: 0.0 

デプロイIDは、NIM_DEPLOYMENT_ID環境変数またはランタイムパラメーターから自動的に取得されます。

このデプロイを使用するには、functionsセクション内のエージェント定義のllm_nameを使用して、特定のエージェントが使用するLLMを定義します。

workflow.yaml
functions:
  planner:
    _type: chat_completion
    llm_name: datarobot_nim  # Reference the LLM defined above
    system_prompt: |
      You are a content planner... 

llmsセクションに複数のLLMが定義されている場合、各functionsはタスクに合わせて異なるLLMを使用できます。

NATが提供するLLMインターフェイス

LLM Gatewayの代わりに、NATが提供するLLMインターフェイスを利用することもできます。 NAT LLMインターフェイスを利用するには、api_keyurlなどの必要な設定パラメーターやプロバイダー固有のその他の設定をworkflow.yamlファイルに直接追加します。

OpenAI APIの設定

OpenAI自体など、OpenAI API標準をサポートする外部LLMプロバイダーを利用することが必要になる場合があります。 このテンプレートは、OpenAI互換のLLMプロバイダーへの接続をサポートしています。 CrewAIとLangGraphフレームワークを使ってOpenAIに直接接続する例を示します。

from crewai import LLM

def llm(self) -> LLM:
    """Returns a CrewAI LLM instance configured to use OpenAI."""
    return LLM(
        model="gpt-4o-mini", # Define the OpenAI model name
        api_key="YOUR_OPENAI_API_KEY", # Your OpenAI API key
        timeout=self.timeout,  # Optional timeout for requests
    ) 
from langchain_openai import ChatOpenAI

def llm(self) -> ChatOpenAI:
    """Returns a ChatOpenAI instance configured to use OpenAI."""
    return ChatOpenAI(
        model="gpt-4o-mini", # Define the OpenAI model name
        api_key="YOUR_OPENAI_API_KEY", # Your OpenAI API key
        timeout=self.timeout,  # Optional timeout for requests
    ) 
from llama_index.llms.openai import OpenAI

def llm(self) -> OpenAI:
    """Returns an OpenAI instance configured to use OpenAI."""
    return OpenAI(
        model="gpt-4o-mini", # Define the OpenAI model name
        api_key="YOUR_OPENAI_API_KEY", # Your OpenAI API key
        timeout=self.timeout,  # Optional timeout for requests
    ) 

Anthropic APIの設定

Anthropic APIを使ってAnthropicのクロードモデルに接続することができます。 このテンプレートは、CrewAIとLangGraphの両方のフレームワークを介してAnthropicモデルとの接続をサポートしています。 これらのモデルを使用するには、Anthropic APIキーが必要です。

from crewai import LLM

def llm(self) -> LLM:
    """Returns a CrewAI LLM instance configured to use Anthropic."""
    return LLM(
        model="claude-3-5-sonnet-20241022", # Define the Anthropic model name
        api_key="YOUR_ANTHROPIC_API_KEY", # Your Anthropic API key
        timeout=self.timeout,  # Optional timeout for requests
    ) 
from langchain_anthropic import ChatAnthropic

def llm(self) -> ChatAnthropic:
    """Returns a ChatAnthropic instance configured to use Anthropic."""
    return ChatAnthropic(
        model="claude-3-5-sonnet-20241022", # Define the Anthropic model name
        anthropic_api_key="YOUR_ANTHROPIC_API_KEY", # Your Anthropic API key
        timeout=self.timeout,  # Optional timeout for requests
    ) 
from llama_index.llms.anthropic import Anthropic

def llm(self) -> Anthropic:
    """Returns an Anthropic instance configured to use Anthropic."""
    return Anthropic(
        model="claude-3-5-sonnet-20241022", # Define the Anthropic model name
        api_key="YOUR_ANTHROPIC_API_KEY", # Your Anthropic API key
        timeout=self.timeout,  # Optional timeout for requests
    ) 

Gemini APIの設定

また、Gemini APIを使ってGoogleのGeminiモデルに接続することもできます。 このテンプレートは、CrewAIとLangGraphの両方のフレームワークを介したGeminiモデルへの接続をサポートしています。 これらのモデルを使用するには、Google AI APIキーが必要です。

from crewai import LLM

def llm(self) -> LLM:
    """Returns a CrewAI LLM instance configured to use Gemini."""
    return LLM(
        model="gemini/gemini-1.5-flash", # Define the Gemini model name
        api_key="YOUR_GEMINI_API_KEY", # Your Google AI API key
        timeout=self.timeout,  # Optional timeout for requests
    ) 
from langchain_google_genai import ChatGoogleGenerativeAI

def llm(self) -> ChatGoogleGenerativeAI:
    """Returns a ChatGoogleGenerativeAI instance configured to use Gemini."""
    return ChatGoogleGenerativeAI(
        model="gemini-1.5-flash", # Define the Gemini model name
        google_api_key="YOUR_GEMINI_API_KEY", # Your Google AI API key
        timeout=self.timeout,  # Optional timeout for requests
    ) 
from llama_index.llms.gemini import Gemini

def llm(self) -> Gemini:
    """Returns a Gemini instance configured to use Google's Gemini."""
    return Gemini(
        model="gemini-1.5-flash", # Define the Gemini model name
        api_key="YOUR_GEMINI_API_KEY", # Your Google AI api key
        timeout=self.timeout,  # Optional timeout for requests
    ) 

他のプロバイダーに接続する

上記の例で示したパターンに従えば、OpenAI API標準をサポートする他のどのLLMプロバイダーにも接続できます。 OpenAI APIフォーマットをネイティブにサポートしていないプロバイダーには、接続の橋渡しをするためのいくつかのオプションがあります。

フレームワークのドキュメントを参照する

各フレームワークは、さまざまなLLMプロバイダーに接続するための包括的なドキュメントを提供します。

  • CrewAI:各プロバイダーへの接続の詳細な例については、CrewAI LLMドキュメントを参照してください。
  • LangGraph:LangChain LLM integrations](https://python.langchain.com/docs/integrations/llms/){ target=_blank }をチェックして、広範なプロバイダーをサポートしてください。
  • LlamaIndex:各LLM連携についてはLlamaIndex LLMモジュールを参照してください。
  • NAT:workflow.yamlでのLLMの設定については、NVIDIA NeMo Agent Toolkitのドキュメントを参照してください。

あらゆる接続にLiteLLMを利用する

LiteLLMは、100以上のLLMプロバイダーに接続するための統一インターフェイスを提供するライブラリです。 各プロバイダー固有のAPIフォーマットに合わせてリクエストを変換し、以下のようなプロバイダーへの接続を容易にします。

  • Azure OpenAI
  • AWS Bedrock
  • Google Vertex AI
  • Cohere
  • Hugging Face
  • Ollama
  • 他にもあります

LiteLLMを使用する場合、モデル文字列は複合形式(provider/model-name)を使用します。

  • プロバイダー:使用するAPIアダプター/プロバイダー(openaiazureなど)。
  • モデル名:そのプロバイダーに渡すモデル名。

たとえば、デプロイがmeta/llama-3.2-1b-instructという名前のモデルを使用する場合、モデルの文字列にはopenai/meta/llama-3.2-1b-instructを使用します。 これにより、LiteLLMはopenai APIアダプターとモデル名meta/llama-3.2-1b-instructを使用するよう指示されます。

この形式により、LiteLLMは適切なプロバイダーAPIへリクエストをルーティングすると同時に、そのプロバイダー用の正しいモデル識別子を使用することが可能になります。

サポートされているプロバイダーの最新リストと設定例については、LiteLLMのドキュメントを参照してください。