Skip to content

Configure LLM providers in code

One of the key components of an LLM agent is the underlying LLM provider. DataRobot allows users to connect to virtually any LLM backend for their agentic workflows. LLM connections can be simplified by using the DataRobot LLM gateway or a DataRobot deployment (including NIM deployments). Alternatively, you can connect to any external LLM provider that supports the OpenAI API standard.

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

  • エージェントLLMとしてDataRobot LLM Gatewayを使用し、Gatewayで利用可能な任意のモデルを使用できます。
  • Connect to use a previously deployed custom model or NIM using the DataRobot API by providing the deployment ID.
  • Connect directly to an LLM provider API (such as OpenAI, Anthropic, or Gemini) by providing the necessary API credentials, enabling access to providers supporting a compatible API.

This document focuses on configuring LLM providers by manually creating LLM instances directly in your agent.py file. This approach gives you fine-grained control over LLM initialization and is shown in the framework-specific examples below.

Alternative configuration method

If you prefer to configure LLM providers using environment variables and Pulumi (infrastructure-level configuration), see Configure LLM providers with metadata.

以下のセクションでは、CrewAI、LangGraph、LlamaIndex、およびNAT(NVIDIA NeMo Agent Toolkit)フレームワークを使用してさまざまなLLMプロバイダーに接続するためのコードスニペットの例を紹介します。 You can use these snippets as a starting point and modify them as needed to fit your specific use case.

DataRobot LLM gateway

The LLM gateway provides a streamlined way to access LLMs proxied via DataRobot. The gateway is available for both cloud and on-premise users.

You can retrieve a list of available models for your account using the following methods:

curl -X GET -H "Authorization: Bearer $DATAROBOT_API_TOKEN" "$DATAROBOT_ENDPOINT/genai/llmgw/catalog/" | jq | grep 'model":' 
import datarobot
print("\n".join(datarobot.genai.LLMGatewayCatalog.get_available_models())) 

The following code examples demonstrate how to programmatically connect to the DataRobot LLM gateway in the CrewAI, LangGraph, and LlamaIndex frameworks. These samples show how to configure the model, API endpoint, and authentication.

from crewai import LLM

def llm(self) -> LLM:
    """Returns a CrewAI LLM instance configured to use DataRobot's LLM gateway."""
    return LLM(
        model="openai/azure/gpt-4o-mini",  # Define the model name you want to use
        # 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="openai/azure/gpt-4o-mini",  # Define the model name you want to use
        # 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="openai/azure/gpt-4o-mini", # Define the matching openai model name
        # 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 hosted LLM deployments

You can easily connect to DataRobot-hosted LLM deployments as an LLM provider for your agents. To do this, Deploy an LLM from the DataRobot Playground or host Hugging Face models as LLM deployments on DataRobot. DataRobot-hosted LLMs can also provide access to moderations and guardrails for managing and governing models.

To use a deployed custom model, manually configure the deployment URL directly in your agent code for api_base= param, following the examples below.

デプロイ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 deployments

このテンプレートは、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を使用するよう指示されます。

To create a new NIM deployment, you can follow the instructions in the DataRobot NIM documentation.

デプロイ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 configuration

There are cases where you may want to use an external LLM provider that supports the OpenAI API standard, such as OpenAI itself. The template supports connecting to any OpenAI-compatible LLM provider. Here are examples for directly connecting to OpenAI using the CrewAI and LangGraph frameworks.

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 configuration

You can connect to Anthropic's Claude models using the Anthropic API. The template supports connecting to Anthropic models through both CrewAI and LangGraph frameworks. You'll need an Anthropic API key to use these models.

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 configuration

You can also connect to Google's Gemini models using the Gemini API. The template supports connecting to Gemini models through both CrewAI and LangGraph frameworks. You'll need a Google AI API key to use these models.

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
    ) 

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

You can connect to any other LLM provider that supports the OpenAI API standard by following the patterns shown in the examples above. For providers that don't natively support the OpenAI API format, you have several options to help bridge the connection:

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

Each framework provides comprehensive documentation for connecting to various LLM providers:

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

LiteLLMは、100以上のLLMプロバイダーに接続するための統一インターフェイスを提供するライブラリです。 It translates requests to match each provider's specific API format, making it easier to connect to providers like:

  • Azure OpenAI
  • AWS Bedrock
  • Google Vertex AI
  • Cohere
  • Hugging Face
  • Ollama
  • And more

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へリクエストをルーティングすると同時に、そのプロバイダー用の正しいモデル識別子を使用することが可能になります。

For the most up-to-date list of supported providers and configuration examples, visit the LiteLLM documentation.