Skip to content

Customize agents

Developing an agent involves editing the custom_model source code. A variety of tools and commands are provided to help you test and deploy your agent during the development process.

Generic base template

You can use the generic_base template to build an agent using any framework of your choice; however, you need to implement the agent logic and structure yourself, as this template does not include any pre-defined agent code.

Modify the agent code

The first step in developing your agent is to modify the agent code to implement your desired functionality. The main agent code is located in the custom_model directory inside the framework-specific directory you selected when you created the project. For example, if you selected the CrewAI framework, the path would be agent_crewai/custom_model.

custom_model/ directory
custom_model/
├── __init__.py           # Package initialization
├── agent.py              # Main agent implementation, including prompts
├── custom.py             # DataRobot integration hooks
├── config.py             # Configuration management
├── mcp_client.py         # MCP server integration (optional, for tool use)
└── model-metadata.yaml   # Agent metadata configuration 
ファイル 説明
__init__.py ディレクトリをPythonパッケージとして識別し、インポートを有効にします。
model-metadata.yaml エージェントの設定、ランタイムパラメーター、デプロイ設定を定義します。
custom.py エージェントを実行するためのDataRobot統合フック(load_modelchat)を実装します。
agent.py ワークフローのコアロジックとフレームワーク固有の実装を持つ、メインのMyAgentクラスが含まれています。
config.py Manages configuration loading from environment variables, runtime parameters, and DataRobot credentials.
mcp_client.py Provides MCP server connection management for tool integration (optional, only needed when using MCP tools).

The main class you need to modify is in agent.py. 開発対象のフレームワークに基づく実装の詳細については、このクラスを参照してください。

エージェントテンプレートには、3つのエージェントと3つのタスクを含む簡単な例が用意されています。 You can modify this code to add more agents, tasks, and tools as needed. Each agent is connected to an LLM provider, which is specified by the llm property in the agent.py file. You can modify this property to change the LLM provider or its configuration. See the Configuring LLM providers documentation for more details. エージェントワークフローのテンプレートの全体的な構造については、エージェントのコンポーネントに関するドキュメントを参照してください。

datarobot_genai package

Agent templates use the datarobot_genai package to streamline development. This package provides helper functions and base classes that simplify agent implementation, including LLM configuration, response formatting, and integration with DataRobot services. The templates automatically include this package, so you don't need to install it separately.

Modify agent prompts

Each agent template uses different approaches for defining and customizing prompts. Understanding how to modify prompts in your chosen framework is crucial for tailoring agent behavior to your specific use case.

In CrewAI templates, prompts are defined through several properties in the MyAgent class within the agent.py file:

  • Agent prompts: Defined using role, goal, and backstory properties.
  • Task prompts: Defined using description and expected_output properties.
CrewAI Agent definition example
@property
def agent_planner(self) -> Agent:
    return Agent(
        role="Content Planner",
        goal="Plan engaging and factually accurate content on {topic}",
        backstory="You're working on planning a blog article about the topic: {topic}. You collect "
        "information that helps the audience learn something and make informed decisions. Your work is "
        "the basis for the Content Writer to write an article on this topic.",
        allow_delegation=False,
        verbose=self.verbose,
        llm=self.llm,
    ) 

To modify CrewAI agent prompts:

  • Update agent behavior: Modify the role, goal, and backstory properties in agent definitions.
  • Use variables: Leverage {topic} and other variables for dynamic prompt content.
CrewAI Task definition example
@property
def task_plan(self) -> Task:
    return Task(
        description=(
            "1. Prioritize the latest trends, key players, and noteworthy news on {topic}.\n"
            "2. Identify the target audience, considering their interests and pain points.\n"
            "3. Develop a detailed content outline including an introduction, key points, and a call to action.\n"
            "4. Include SEO keywords and relevant data or sources."
        ),
        expected_output="A comprehensive content plan document with an outline, audience analysis, SEO keywords, "
        "and resources.",
        agent=self.agent_planner,
    ) 

To modify CrewAI task prompts:

  • Customize task instructions: Update the description property in task definitions.
  • Change expected outputs: Modify the expected_output property to match your requirements.
  • Use variables: Leverage {topic} and other variables for dynamic prompt content.

For more advanced CrewAI prompt engineering techniques, see the CrewAI Agents documentation and CrewAI Tasks documentation.

In LangGraph templates, prompts are defined using the system_prompt parameter in agent definitions and a make_system_prompt helper method within the MyAgent class:

LangGraph Agent definition example
@property
def agent_planner(self) -> Any:
    return create_react_agent(
        self.llm,
        tools=[],
        prompt=self.make_system_prompt(
            "You are a content planner. You are working with a content writer and editor colleague.\n"
            "You're working on planning a blog article about the topic. You collect information that helps the "
            "audience learn something and make informed decisions. Your work is the basis for the Content Writer "
            "to write an article on this topic.\n"
            "1. Prioritize the latest trends, key players, and noteworthy news on the topic.\n"
            "2. Identify the target audience, considering their interests and pain points.\n"
            "3. Develop a detailed content outline including an introduction, key points, and a call to action.\n"
            "4. Include SEO keywords and relevant data or sources."
        ),
    ) 

To modify LangGraph prompts:

  • Update system prompts: Modify the string passed to make_system_prompt() in agent definitions.
  • Customize the helper method: Update the make_system_prompt method to change the base prompt structure.
  • Add task-specific instructions: Include detailed instructions within the system prompt string.
  • Modify agent behavior: Update the prompt content to change how agents interpret and respond to tasks.

For more advanced LangGraph prompt engineering techniques, see the LangGraph documentation.

In LlamaIndex templates, prompts are defined using the system_prompt parameter in FunctionAgent definitions within the MyAgent class:

LlamaIndex Agent definition example
@property
def research_agent(self) -> FunctionAgent:
    return FunctionAgent(
        name="ResearchAgent",
        description="Useful for finding information on a given topic and recording notes on the topic.",
        system_prompt=(
            "You are the ResearchAgent that can find information on a given topic and record notes on the topic. "
            "Once notes are recorded and you are satisfied, you should hand off control to the "
            "WriteAgent to write a report on the topic. You should have at least some notes on a topic "
            "before handing off control to the WriteAgent."
        ),
        llm=self.llm,
        tools=[self.record_notes],
        can_handoff_to=["WriteAgent"],
    ) 

To modify LlamaIndex prompts:

  • Update system prompts: Modify the system_prompt string in FunctionAgent definitions.
  • Customize agent descriptions: Update the description parameter to change how agents are identified.
  • Modify handoff behavior: Update the can_handoff_to list and system prompt to control agent workflow.
  • Add tool-specific instructions: Include instructions about when and how to use specific tools.

For more advanced LlamaIndex prompt engineering techniques, see the LlamaIndex prompt engineering documentation.

NAT(NVIDIA NeMo Agent Toolkit)のテンプレートでは、プロンプトはworkflow.yamlファイルで、関数定義内のsystem_promptフィールドを使用して定義されます。

NAT workflow.yaml Agent definition example
functions:
  planner:
    _type: chat_completion
    llm_name: datarobot_llm
    system_prompt: |
      You are a content planner. You are working with a content writer and editor colleague.
      You're working on planning a blog article about the topic.
      You collect information that helps the audience learn something and make informed decisions.
      Your work is the basis for the Content Writer to write an article on this topic.
      1. Prioritize the latest trends, key players, and noteworthy news on the topic.
      2. Identify the target audience, considering their interests and pain points.
      3. Develop a detailed content outline including an introduction, key points, and a call to action.
      4. Include SEO keywords and relevant data or sources. 

NATのプロンプトを変更するには:

  • システムプロンプトを更新するworkflow.yaml内の関数定義のsystem_promptフィールドを変更します。
  • 関数ごとにLLMを設定するworkflow.yamlllmsセクションで定義されたLLMを参照するように、llm_nameフィールドを設定します。
  • ワークフローの構造を変更するworkflowセクションを更新して、実行順序とツールリストを変更します。
  • 新しい関数を追加するfunctionsセクションに追加の関数を定義して、エージェントの機能を拡張します。

より高度なNATの使用方法については、NVIDIA NeMo Agent Toolkitのドキュメントを参照してください。

Best practices for prompt modification

When modifying prompts across any framework:

  • Be specific: Provide clear, detailed instructions for what you want the agent to accomplish.
  • Use consistent formatting: Maintain consistent prompt structure across all agents in your workflow.
  • Test incrementally: Make small changes and test them before implementing larger modifications.
  • Consider context: Ensure prompts work well together in multi-agent workflows.
  • Document changes: Keep track of prompt modifications for future reference and team collaboration.

ストリーミングレスポンスを有効にする

ストリーミングを使用すると、エージェントは、完全なレスポンスを待機することなく、生成されたレスポンスを段階的に送信できます。 これにより、進捗状況がリアルタイムで表示され、認識される遅延が短縮されて、エージェントのアクションが発生したときにユーザーが確認できるようになるため、ユーザーエクスペリエンスが向上します。

ストリーミングのサポートは、エージェントフレームワークによって異なります。 ストリーミングの実装には3つのレベルがあります。

  • チャンクストリーミング:LLMからの各チャンクは、生成時にストリーミングされます(トークンや部分テキストなど)。
  • ステップストリーミング:各サブエージェントからのレスポンスは、準備ができた時点でストリーミングされます。
  • イベントストリーミング:個々のイベント(新しいステップの開始、ツールの呼び出し、推論)がストリーミングされます。
フレームワーク ストリーミング 備考
LangGraph 有効 stream=Trueが渡されると、チャンクレベルのストリーミングが自動的に有効になります。 基本クラスであるLangGraphAgentが、ストリーミングレスポンスを処理します。
Generic Base サポートされています すべてのストリーミングレベル(チャンク、ステップ、イベント)にカスタム実装が必要です。 チャンクストリーミングのサンプルコードがagent.pyに用意されています。
CrewAI サポートされています すべてのストリーミングレベル(チャンク、ステップ、イベント)にカスタム実装が必要です。 イベントリスナーは、エージェント実行とツール使用のイベントを段階的にキャプチャします。これにより、カスタムコードを用いたステップおよびイベントのストリーミングが容易になります。 チャンクストリーミングには、LLMから直接ストリーミングするためのカスタム実装が必要です。
LlamaIndex サポートされています すべてのストリーミングレベル(チャンク、ステップ、イベント)にカスタム実装が必要です。 フレームワークはエージェントを段階的に実行するため、カスタムコードを用いたステップストリーミングが容易になります。 チャンクとイベントのストリーミングには、カスタム実装が必要です。
NAT サポートされています すべてのストリーミングレベル(チャンク、ステップ、イベント)にカスタム実装が必要です。

インフラストラクチャのサポート

すべてのエージェントテンプレートには、ストリーミングレスポンスを処理できるインフラストラクチャがcustom.pyに含まれています。 カスタム実装を必要とするフレームワーク(Generic Base、CrewAI、LlamaIndex、NAT)の場合、ストリーミングが要求されたときにAsyncGeneratorを返すようにエージェントのinvoke()メソッドを変更する必要があります。 エージェントのinvoke()メソッドがAsyncGeneratorを返すと、インフラストラクチャは自動的にそれを適切なストリーミングレスポンス形式に変換します。 is_streamingヘルパー関数は、from datarobot_genai.core.agents import is_streamingをインポートすることで、datarobot_genaiパッケージを通じて、すべてのフレームワークテンプレートで利用できます。 この関数は、チャット補完のリクエストボディパラメーターにstream=Trueが含まれているかどうかを確認します。

エージェントにストリーミングが実装されている場合は、ローカルでテストするとき(CLIを使用)、またはデプロイされたエージェントで予測を行うとき(APIを使用)にストリーミングを有効にします。

エージェントCLIを実行する際には、--streamフラグを使用します。

task agent:cli -- execute --user_prompt 'Write a document about the history of AI.' --stream 

構造化クエリーでストリーミングを使うこともできます。

task agent:cli -- execute --user_prompt '{"topic":"Generative AI"}' --stream 

APIを呼び出す際、補完パラメーターにstream=Trueを設定します。

Enable streaming in API calls
from openai import OpenAI

client = OpenAI(
    base_url=CHAT_API_URL,
    api_key=API_KEY,
)

completion = client.chat.completions.create(
    model="datarobot-deployed-llm",
    messages=[
        {"role": "user", "content": "What would it take to colonize Mars?"},
    ],
    stream=True,  # Enable streaming
)

# Process streaming response
for chunk in completion:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True) 

Testing the agent during local development

You can test your agent locally using the development server provided in the template. This allows you to run and debug your agent code without deploying it to DataRobot.

エージェントにテストクエリーを送信するには、開発サーバーが稼働している必要があります。 手動で起動するか、自動起動オプションを使用します。

複数のテストを実行する場合は、開発サーバーを手動で起動します。 開発サーバーは継続的に実行され、ターミナルをブロックするため、1つのターミナルで起動します。

task agent:dev 

このターミナルは起動したままにしておきます。 その後、別のターミナルでテストコマンドを実行します。

task agent:cli -- execute --user_prompt 'Write a document about the history of AI.' 

エージェントのワークフローが要求した場合、構造化クエリーをプロンプトとして送信することもできます。

task agent:cli -- execute --user_prompt '{"topic":"Generative AI"}' 

単一のテスト用に開発サーバーを自動起動します。 START_DEV=1を使用して、開発サーバーを自動的に起動および停止します。

task agent:cli START_DEV=1 -- execute --user_prompt 'Write a document about the history of AI.' 

エージェントのワークフローが要求した場合、構造化クエリーをプロンプトとして送信することもできます。

task agent:cli START_DEV=1 -- execute --user_prompt '{"topic":"Generative AI"}' 

This command will run the agent locally and print the output to the console. You can modify the query to test different inputs and scenarios. For additional local CLI commands that are available, see the Using the agent CLI documentation.

実行時依存関係を用いた高速イテレーション

開発を迅速に行うために、実行時依存関係を使用してDockerイメージを再構築することなく、Pythonの依存関係を追加できます。 このプロセスにより、イテレーションの速度が向上します。 実行時依存関係の追加の詳細については、Pythonパッケージの追加に関するドキュメントを参照してください。

DataRobot LLMプレイグラウンドでテスト用のエージェントを構築する

To create a custom model that can be refined using the DataRobot LLM Playground:

task build 

This command runs the Pulumi infrastructure to create a custom model in DataRobot but does not create a full production deployment. This is significantly faster for iterative cloud development and testing.

For more examples on working with agents in the DataRobot LLM Playground, see the Agentic playground documentation.

Build command considerations

The task build command will remove any existing deployments. These can be recreated using task deploy if they are removed, but the new deployments will have different deployment IDs.

Deploy an agent for production use

To create a full production-grade deployment:

task deploy 

This command builds the custom model and creates a production deployment with the necessary infrastructure, which takes longer but provides a complete production environment. The deployment is a standard DataRobot deployment that includes full monitoring, logging, and scaling capabilities. For more information about DataRobot deployments, see the Deployment documentation.

デプロイされたエージェントのログとトレースを確認する

エージェントがデプロイされたら、DataRobot UIでOpenTelemetry (OTel)のログとトレースを見ることができます。

ログを表示するには、デプロイタブでデプロイを検索してクリックし、アクティビティログタブ、ログの順にクリックします。 ログはOpenTelemetry形式で表示され、ログレベル(INFODEBUGWARNERROR)、期間指定によるフィルター(直近 15 分、直近 1 時間、直近 1 日、またはカスタム範囲)、およびサードパーティ製のオブザーバビリティツール(Datadogなど)と連携するためのOTelログAPI経由でのエクスポート機能が含まれます。

アクセスと保持

ログはすべてのデプロイタイプで利用可能です。 デプロイで「オーナー」ロールと「ユーザー」ロールを持つユーザーのみが、これらのログを表示できます。 ログは30日間保存されます。

エージェントへのリクエストのエンドツーエンドのパスをたどるトレースを表示するには、デプロイのサービスの正常性タブで、予測の合計数チャートの右上隅にあるトレースを表示をクリックします。 トレーステーブルには、タイムスタンプ、ステータス、トレースID、期間、スパン数、コスト、プロンプト、補完などのトレース情報が表示されます。 トレース行をクリックすると、チャートまたはリスト形式で詳細なスパンが表示されます。これにより、LLM APIの呼び出し、ツールの起動、エージェントのアクションなど、エージェントの実行における個々のステップを確認できます。

詳細については、ログのドキュメントトレースのドキュメントを参照してください。

Pulumiを使って手動でエージェントをデプロイする

必要に応じて、Pulumiコマンドを手動で実行して、Pulumiコードのデバッグや改良を行うことができます。

# Load environment variables
set -o allexport && source .env

# For build mode only (custom model without deployment)
export AGENT_DEPLOY=0

# Or for full deployment mode (default)
# export AGENT_DEPLOY=1

# Navigate to the infrastructure directory
cd ./infra

# Run Pulumi deployment
pulumi up 

The AGENT_DEPLOY environment variable controls whether Pulumi creates only the custom model (AGENT_DEPLOY=0) or both the custom model and a production deployment (AGENT_DEPLOY=1). If not set, Pulumi defaults to full deployment mode.

Pulumi will prompt you to confirm the resources to be created or updated.

Make predictions with a deployed agentic workflow

After the agentic workflow is deployed, access real-time prediction snippets from the deployment's Predictions > Prediction API tab. For more information on deployment predictions, see the Prediction API snippets documentation.

Alternatively, you can modify the script below to make predictions with the deployed agentic workflow, replacing the placeholders for the API_KEY, DEPLOYMENT_ID, and CHAT_API_URL variables.

datarobot-llm-chat.py
import sys
import logging
import time
import os

from openai import OpenAI

API_KEY = '<API_KEY>' # Your API Key
DEPLOYMENT_ID = '<DEPLOYMENT_ID>' # The agentic workflow deployment ID
CHAT_API_URL = '<CHAT_API_URL>' # The chat API URL for the agentic workflow deployment
# For example, 'https://app.datarobot.com/api/v2/deployments/68824e9aa1946013exfc3415/'

logging.basicConfig(
    level=logging.INFO,
    stream=sys.stdout,
    format='%(asctime)s %(filename)s:%(lineno)d %(levelname)s %(message)s',
)
logger = logging.getLogger(__name__)


def main():
    openai_client = OpenAI(
        base_url=CHAT_API_URL,
        api_key=API_KEY,
        _strict_response_validation=False
    )

    prompt = "What would it take to colonize Mars?"
    logging.info(f"Trying Simple prompt first: \"{prompt}\"")
    completion = openai_client.chat.completions.create(
        model="datarobot-deployed-llm",
        messages=[
            {"role": "system", "content": "Explain your thoughts using at least 100 words."},
            {"role": "user", "content": prompt},
        ],
        max_tokens=512,  # omit if you want to use the model's default max
    )

    print(completion.choices[0].message.content)

    return 0

if __name__ == '__main__':
    sys.exit(main()) 

次のステップ

After deployment, your agent will be available in your DataRobot environment. 以下を実行することが可能です。

  1. Test your deployed agent using task agent:cli -- execute-deployment.
  2. Integrate your agent with other DataRobot services.
  3. Monitor usage and performance in the DataRobot dashboard.

For agentic platform-specific assistance beyond the scope of the examples provided in this repository, see the official documentation for each framework:

また、特定のフレームワーク向けのパブリックリポジトリには、より複雑なエージェントの構築、ツールの追加、ワークフローやタスクの定義に役立つ、多くの例やドキュメントが用意されています。