Skip to content

How-to:AIによる支援でCrewAIテンプレートを修正する

この基本ステップでは、CursorやWindsurfなどのAI支援開発環境を使用して、ローカルで開発されたエージェントワークフローをDataRobot Agent Templatesリポジトリのテンプレートに移行する方法を紹介します。 既存のAI開発ツールと任意のエージェントフレームワーク(LangGraph、CrewAI、LlamaIndexなど)をDataRobot Agentic AI Platformmとともに活用することで、エージェントワークフローの構築、ガバナンス、本番環境へのデプロイの反復プロセスを高速化できます。このプロセスは、体系的な評価、ガバナンス、セキュリティ、トレース、ライフサイクル管理などの機能によってサポートされます。 この例ではCursorを使用していますが、他のAIコーディングアシスタントにも同じ原則が適用されます。 AIによる支援を含まない、より単純な例については、カスタムエージェント統合の基本ステップを参照してください。

datarobot-agent-templatesリポジトリのクローンを作成する

カスタムエージェントコードの移行を開始するには、datarobot-agent-templatesパブリックリポジトリのクローンを作成します。 このリポジトリには、さまざまなマルチエージェントフレームワークでのAIエージェントの構築およびデプロイのための、すぐに使えるテンプレートが含まれています。 これらのテンプレートを使用すると、最小限の設定要件でエージェントを簡単に設定できます。

まず、開発環境のコマンドライン/ターミナルと以下のコマンドを使用して、DataRobot Agent Templatesリポジトリのクローンを作成します。

git clone --branch 11.3.5 --depth 1 https://github.com/datarobot-community/datarobot-agent-templates.git
cd datarobot-agent-templates 

datarobot-agent-templates version

この基本ステップでは、バージョン11.3.5のdatarobot-agent-templatesリポジトリを使用します。 Ensure that the workspace used for this walkthrough is on that version. Newer versions may not be compatible with the code provided below.

GitHubクローンのURL

GitHubリポジトリのクローン作成の詳細については、GitHubのドキュメントを参照してください。

AIの支援によるカスタムエージェントコードの統合

ローカルで開発されたエージェントワークフローをDataRobotエージェントテンプレートに移行する場合、DataRobot Agent Templatesリポジトリは、定型コード、テスト機能、デプロイパイプラインを提供することで開発を迅速化します。 エージェントフレームワークにテンプレートが存在する場合は、既存のテンプレートを使用します。 DataRobotには、CrewAI、LangGraph、Llama-Index向けのエージェントワークフローテンプレートが用意されています。 フレームワークにテンプレートが存在しない場合は、Generic Baseテンプレートを使用します。

環境の設定

カスタムコードをテンプレートに移行する前に、CrewAIテンプレートを設定して実装をブートストラップします。 datarobot-agent-templatesリポジトリディレクトリで、ターミナルを開き、以下のコマンドを使用して、提供されたテンプレート環境ファイル(.env.template)をコピーし、名前を変更します。 このファイルには、エージェントに必要な環境変数が含まれます。

cp .env.template .env 

新しい.envファイルを探して開き、以下の変数を設定します。

DataRobot credentials in codespaces

DataRobotのCodespaceを使用している場合は、ファイルからDATAROBOT_API_TOKENおよびDATAROBOT_ENDPOINT環境変数を削除します。これらはCodespace環境にすでに存在しているためです。

  • DATAROBOT_API_TOKENおよびDATAROBOT_ENDPOINT環境変数を定義して、ローカルエージェントがDataRobotと通信できるようにします。
  • 任意の文字列を指定して、PULUMI_CONFIG_PASSPHRASEを定義し、PulumiがDataRobotでエージェントアセットの状態を管理できるようにします。
  • この例ではDataRobot LLM Gatewayを使用しているため、LLM_DEPLOYMENT_IDをコメントアウトします。
  • DATAROBOT_DEFAULT_EXECUTION_ENVIRONMENTをコメントアウトして、DataRobotがエージェントのワークフローに必要なPythonライブラリを使用して新しい環境を構築できるようにします。
# Refer to https://docs.datarobot.com/en/docs/api/dev-learning/api-quickstart.html#create-a-datarobot-api-key
# and https://docs.datarobot.com/en/docs/api/dev-learning/api-quickstart.html#retrieve-the-api-endpoint
# Can be deleted on a DataRobot codespace
DATAROBOT_API_TOKEN=
DATAROBOT_ENDPOINT=

# Required, unless logged in to Pulumi cloud. Choose your own alphanumeric passphrase to be used for encrypting Pulumi config
PULUMI_CONFIG_PASSPHRASE=

# If empty, a new use case will be created
DATAROBOT_DEFAULT_USE_CASE=

# To create an new LLM Deployment instead of using the LLM Gateway, set this to false.
# If you are using the LLM Gateway you must enable the `ENABLE_LLM_GATEWAY` feature flag in the DataRobot UI.
USE_DATAROBOT_LLM_GATEWAY=true

# Set this to an existing LLM deployment ID if you want to use a custom LLM deployment
# instead of creating new
# LLM_DEPLOYMENT_ID=

# Specify the default execution environment for agents.
# The default agents environment is "[DataRobot] Python 3.11 GenAI Agents", you can alternatively replace
# this with the ID of an existing execution environment. If you leave this empty, a new execution environment
# will be created for each agent.
# DATAROBOT_DEFAULT_EXECUTION_ENVIRONMENT="[DataRobot] Python 3.11 GenAI Agents" 

これらの環境変数は、.envファイルからキーと値のペアを読み込むために、(Taskfileに実装されている)dotenvを使ってagent.pyファイルにロードされます。

CrewAI実行のクイックスタート

次に、task startを使ってquickstart.pyを実行し、CrewAIのテンプレートを選択します。

task start 

CrewAIのテンプレートを選択するには、1を押し、Enterを押して選択を確定します。

Example start command
task start
task: [start] uv run quickstart.py
*****           *          ****        *             *  
*    *  ***   *****  ***   *   *  ***  ****   ***  *****
*    * *   *    *   *   *  ****  *   * *  *  *   *   *  
*****   *** *   *    *** * *   *  ***  ****   ***    *  

--------------------------------------------------------
           Quickstart for DataRobot AI Agents           
--------------------------------------------------------
Checking environment setup for required pre-requisites...
All pre-requisites are installed.


You will now select an agentic framework to use for this project.
For more information on the different agentic frameworks please go to:
  https://github.com/datarobot-community/datarobot-agent-templates/blob/main/docs/getting-started.md

Please select an agentic framework to use:
1. agent_crewai
2. agent_generic_base
3. agent_langgraph
4. agent_llamaindex
5. agent_nat
Enter your choice (1-5): 1 

次に、YEnterの順に押して、前提条件をインストールし、選択したエージェントの環境を設定します。

Example install prompt
Would you like to setup the uv python environments and install pre-requisites now?
(y/n): y 

これらのコマンドを実行すると、agent_crewaiテンプレート用の環境が設定され、不要なファイルが削除され、選択したエージェントテンプレートの実行に必要な追加ライブラリをインストールする仮想環境が準備されます。

インストール済みの環境は、いつでも以下のコマンドを実行することで更新できます。

task install 

必要に応じて、エージェントテンプレートをカスタマイズする前に、変更なしでエージェントを実行して、コードをテストします。

task agent:cli START_DEV=1 -- execute --user_prompt 'Hi, how are you?' 
すべてのタスクコマンドを表示する

task startを実行する前に、プロジェクトで使用可能なタスクを表示するには、以下のようにtaskコマンドを実行します。

 task
task: Available tasks for this project:
* default:       ℹ️ Show all available tasks (run `task --list-all` to see hidden tasks)
* install:       Install dependencies for all agent components and infra      (aliases: req, install-all)
* start:         ‼️ Quickstart for DataRobot Agent Templates ‼️ 

task startを実行してフレームワークを選択した後に、プロジェクトで使用可能なタスクを表示するには、以下のようにtaskコマンドを実行します。

 task
task: Available tasks for this project:
* default:                           ℹ️ Show all available tasks (run `task --list-all` to see hidden tasks)
* install:                           🛠️ Install all dependencies for agent and infra
* agent:install:                     🛠️ [agent_crewai] Install agent uv dependencies      (aliases: agent:req)
* agent:add-dependency:              🛠️ [agent_crewai] Add provided packages as a new dependency to an agent
* agent:cli:                         🖥️ [agent_crewai] Run the CLI with provided arguments
* agent:dev:                         🔨 [agent_crewai] Run the development server
* agent:dev-stop:                    🛑 [agent_crewai] Stop the development server
* agent:chainlit:                    🛝 Run the Chainlit playground
* agent:create-docker-context:       🐳 [agent_crewai] Create the template for a local docker_context image
* agent:build-docker-context:        🐳 [agent_crewai] Build the Docker image
* infra:install:                     🛠️ [infra] Install infra uv dependencies
* infra:build:                       🔵 Deploy only playground testing resources with pulumi
* infra:deploy:                      🟢 Deploy all resources with pulumi
* infra:refresh:                     ⚪️ Refresh and sync local pulumi state
* infra:destroy:                     🔴 Teardown all deployed resources with pulumi 
 task --list-all
task: Available tasks for this project:
* build:                             
* default:                           ℹ️ Show all available tasks (run `task --list-all` to see hidden tasks)
* deploy:                            
* destroy:                           
* install:                           🛠️ Install all dependencies for agent and infra
* agent:add-dependency:              🛠️ [agent_crewai] Add provided packages as a new dependency to an agent
* agent:build-docker-context:        🐳 [agent_crewai] Build the Docker image
* agent:chainlit:                    🛝 Run the Chainlit playground
* agent:cli:                         🖥️ [agent_crewai] Run the CLI with provided arguments
* agent:create-docker-context:       🐳 [agent_crewai] Create the template for a local docker_context image
* agent:dev:                         🔨 [agent_crewai] Run the development server
* agent:dev-stop:                    🛑 [agent_crewai] Stop the development server
* agent:install:                     🛠️ [agent_crewai] Install agent uv dependencies      (aliases: agent:req)
* agent:lint:                        
* agent:lint-check:                  
* agent:test:                        
* agent:test-coverage:               
* agent:update:                      
* infra:build:                       🔵 Deploy only playground testing resources with pulumi
* infra:deploy:                      🟢 Deploy all resources with pulumi
* infra:destroy:                     🔴 Teardown all deployed resources with pulumi
* infra:info:                        
* infra:init:                        
* infra:install:                     🛠️ [infra] Install infra uv dependencies
* infra:install-pulumi-plugin:       
* infra:lint:                        
* infra:lint-check:                  
* infra:pulumi:                      
* infra:refresh:                     ⚪️ Refresh and sync local pulumi state
* infra:select:                      
* infra:select-env-stack:            
* infra:test:                        
* infra:test-coverage: 

また、使用可能なすべてのエージェントCLIコマンドを表示するには、task agent:cliを実行します。

AI支援コーディングのコンテキストとプロンプトを定義する

DataRobotのエージェントテンプレートの外でローカルに生成されたカスタムエージェントコードは、最小限の変更でagent_crewaiテンプレートに統合できます。 datarobot-agent-templatesリポジトリで、agent_crewai/ディレクトリを探します(他のすべてのテンプレートディレクトリはtask startコマンドによって削除されました)。 agent_crewai/ディレクトリでmy_agentディレクトリを作成し、以下のファイルを追加します。

エージェントのファイル:internet-research-agent.py
internet-research-agent.py
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import os
import json
from datetime import datetime
from typing import Type
from pprint import pprint
from crewai import Agent, Task, Crew, Process, LLM
from crewai.tools import BaseTool
from pydantic import BaseModel, Field
from ddgs import DDGS
import datarobot as dr
from dotenv import load_dotenv

# Load environment variables (for local development)
env_path = os.path.join(os.path.dirname(__file__), '..', '.env')
load_dotenv(env_path)

# Initialize DataRobot client
# Note: In DataRobot notebooks, you can use dr.Client() without credentials
dr_client = dr.Client(
    endpoint=os.getenv('DATAROBOT_ENDPOINT'),
    token=os.getenv('DATAROBOT_API_TOKEN')
)


class SearchInput(BaseModel):
    """Input schema for DuckDuckGo search tool."""
    query: str = Field(..., description="The search query to look up on the web")

class DuckDuckGoSearchTool(BaseTool):
    """Custom tool for searching the web using DuckDuckGo.

    This tool allows our agent to search for current information on the internet.
    It tries news search first, then falls back to regular web search.
    """
    name: str = "duckduckgo_search"
    description: str = "Search the web using DuckDuckGo for current information and news"
    args_schema: Type[BaseModel] = SearchInput

    def _run(self, query: str) -> str:
        """Execute the search and return formatted results."""
        try:
            with DDGS() as ddgs:
                # Try news search first (more current information)
                news_results = list(ddgs.news(query, max_results=5))

                if news_results:
                    return self._format_news_results(query, news_results)

                # If no news results, try regular web search
                web_results = list(ddgs.text(query, max_results=5))

                if web_results:
                    return self._format_web_results(query, web_results)
                else:
                    return f"No search results found for: {query}"

        except Exception as e:
            return f"Search error for '{query}': {str(e)}"

    def _format_news_results(self, query: str, results: list) -> str:
        """Format news search results for display."""
        formatted_results = [
            f"Search Results for: {query}",
            f"Search performed at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
            "\n=== NEWS RESULTS ==="
        ]

        for i, result in enumerate(results, 1):
            title = result.get('title', 'No title')
            body = result.get('body', '')
            url = result.get('url', '')
            date = result.get('date', 'Recent')

            formatted_results.append(f"\n{i}. {title}")
            if body:
                # Keep summaries concise for readability
                body_snippet = body[:300] + "..." if len(body) > 300 else body
                formatted_results.append(f"   Summary: {body_snippet}")
            formatted_results.append(f"   Date: {date}")
            formatted_results.append(f"   Source: {url}")

        return "\n".join(formatted_results)

    def _format_web_results(self, query: str, results: list) -> str:
        """Format web search results for display."""
        formatted_results = [
            f"Search Results for: {query}",
            f"Search performed at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
            "\n=== WEB RESULTS ==="
        ]

        for i, result in enumerate(results, 1):
            title = result.get('title', 'No title')
            body = result.get('body', '')
            url = result.get('href', '')

            formatted_results.append(f"\n{i}. {title}")
            if body:
                # Keep summaries concise for readability
                body_snippet = body[:300] + "..." if len(body) > 300 else body
                formatted_results.append(f"   Summary: {body_snippet}")
            formatted_results.append(f"   Source: {url}")

        return "\n".join(formatted_results)

print("✅ Custom DuckDuckGo search tool created successfully!")

# You can change this model to any supported DataRobot model
MODEL_NAME = os.getenv('DR_MODEL_NAME', 'vertex_ai/meta/llama-4-maverick-17b-128e-instruct-maas')

# This connects to DataRobot's LLM Gateway
llm = LLM(
    model=MODEL_NAME,
    api_key=dr_client.token,
    base_url=dr_client.endpoint + "/genai/llmgw",
    custom_llm_provider="openai"  # Tell CrewAI to use OpenAI-compatible API
)

def get_crew():
    # Create the search tool instance
    search_tool = DuckDuckGoSearchTool()

    # Define the All-in-One Research & Writing Agent
    research_writer = Agent(
        role='Senior Research Analyst & Content Strategist for: {user_topic}',
        goal='Research thoroughly, then write brilliantly - delivering comprehensive insights in reports that people actually want to read',
        backstory='''You're the Swiss Army knife of information professionals - equal parts detective,
        fact-checker, and storyteller. You've mastered the art of diving deep into research rabbit holes
        and emerging with coherent, engaging narratives that make complex topics accessible.

        Your research philosophy: trust but verify, then verify again. You approach every topic like
        a curious journalist meeting a meticulous librarian - thorough enough to catch the details
        that matter, smart enough to ignore the noise that doesn't.

        Your writing superpower: taking information overload and distilling it into insights that
        stick. You write like you're explaining something fascinating to your smartest friend over
        coffee - authoritative but conversational, comprehensive but never overwhelming.

        You've seen enough bad research and corporate-speak to know what doesn't work. Your mission:
        find the truth, tell it well, and make sure people actually finish reading what you write.''',
        verbose=True,
        allow_delegation=False,
        tools=[search_tool],
        llm=llm
    )

    # Create the Combined Research & Writing Task
    research_and_write_task = Task(
        description='''Your mission: become the definitive expert on this topic, then write about it
        like the engaging expert you are.

        RESEARCH PHASE: Use DuckDuckGo to investigate every angle of the topic. Search for current
        developments, expert opinions, statistical data, case studies, controversies, and practical
        applications. Cross-reference sources, identify the most credible information, and note any
        conflicting viewpoints or gaps in available data.

        WRITING PHASE: Transform your research into a compelling, well-structured report. Start with
        the most important insights, support them with solid evidence, and end with clear takeaways
        or recommendations. Write for intelligent readers who want depth but also want to actually
        finish reading.

        Topic to research and write about: {user_topic}

        Make it thorough but readable, authoritative but accessible.''',
        expected_output='''A comprehensive 800-1500 word report featuring:
        - Executive summary with key insights upfront
        - Well-organized main findings with supporting evidence
        - Multiple credible source perspectives
        - Recent developments and trends
        - Clear implications or actionable recommendations  
        - Proper source citations
        - Written in engaging, scannable format with strategic headers and formatting''',
        agent=research_writer
    )

    # Create the streamlined crew
    crew = Crew(
        agents=[research_writer],
        tasks=[research_and_write_task],
        process=Process.sequential,
        verbose=False
    )
    return crew


if __name__ == '__main__':
    """Run the agent with a sample topic."""
    print("🚀 Starting Internet Research Agent...")

    # Create the crew
    crew = get_crew()

    # Run the agent with a sample topic
    # You can change this to any topic you want to research
    sample_topic = "Artificial Intelligence in Healthcare"
    print(f"📝 Researching topic: {sample_topic}")

    result = crew.kickoff({"user_topic": sample_topic})

    print("\n" + "="*50)
    print("📊 RESEARCH REPORT COMPLETE")
    print("="*50)
    print(result) 

この基本ステップでのコードのコピー

この基本ステップで、既存のテンプレートを修正するには、大きなコードブロックをコピーする必要があります。 コードスニペットの全内容をコピーするには、スニペットの右上隅にある クリップボードにコピーをクリックします。

次に、agent_crewai/custom_model/ディレクトリを探し、その内容を確認します。 これらのファイルは、DataRobotに送信されるエージェントを定義します。

custom_model/ directory
custom_model/
├── __init__.py              # Package initialization
├── agent.py                 # Main agent implementation
├── config.py                 # Configuration management
├── crewai_event_listener.py  # Event listener for pipeline interactions
├── custom.py                 # DataRobot integration hooks
├── mcp_client.py             # MCP server integration (optional)
└── model-metadata.yaml       # Agent metadata configuration 

この移行プロセスでは、agent_crewai/custom_modelディレクトリ内のエージェントコード、主にagent.pyがカスタマイズされます。 AI支援開発環境でエージェントコードを変更するために必要なコンテキストを提供するには、agent_crewai/ディレクトリをチャットコンテキストに追加します。 たとえば、Cursorでagent_crewaiを右クリックし、Add Directory to New Cursor Chatを選択します。

次に、agent_crewai/ディレクトリ(ローカルで開発されたエージェントを格納するmy_agentディレクトリを含む)をコンテキストに設定した新しいチャットで、以下のプロンプトを使用して、新しいエージェントをCrewAIエージェントテンプレートに移行します。

Thoroughly review the custom_model folder, especially the agent.py file and how it works with the other files in that folder.
The goal is to migrate an agent built without the template (currently in the my_agent folder) into the custom_model structure, specifically consolidating the migrated agent into the agent.py file.

📦 Migration Instructions
Migrate code and logic from the my_agent folder into a single consolidated file called agent.py inside the custom_model folder.
Guidelines:
1. Preserve functionality: Ensure all functionality from my_agent is carried over.
2. Merge intelligently:
    * Place utility functions and constants first.
    * Core classes next.
    * Entrypoints last.
3. Simplify definitions: Remove unnecessary definitions of agents, tasks, and tools from the original agent.py in custom_model.
4. Gateway integration: Ensure the migrated agent uses the LLM gateway defined in the function llm_with_datarobot_llm_gateway(self) -> LLM in the original agent.py.
5. Naming consistency: If duplicate/overlapping names exist, rename them carefully to avoid conflicts.
6. Docstring header: At the top of agent.py, include a clear docstring summarizing the agent’s purpose.
7. Self-contained: After migration, custom_model/agent.py should be runnable standalone without referencing the original my_agent folder.

📥 Dependency Management with uv
Any new dependencies introduced by the migration must be installed and aligned with the project’s virtual environment using uv.
Steps:
1. Navigate to the agent project directory: cd datarobot-agent-templates/agent_crewai
2. Add each new dependency: uv add <package>[extras]==<version>. For example: uv add perplexityai
3. Sync dependencies to the venv: uv sync

🔹 Docker Dependency Management (CRITICAL)
To prevent runtime errors like ModuleNotFoundError: No module named 'X' when running inside Docker:
1. Add new dependencies to agent_crewai/pyproject.toml (via uv add).
2. If you need a custom Docker context, run task create-docker-context and then add dependencies to the generated docker_context/requirements.in file.

✅ Post-Migration Test
After migration and dependency resolution, run the test to confirm the agent works:
From the root of datarobot-agent-templates, run:

task agent:cli START_DEV=1 -- execute --user_prompt "{user_prompt}"
* Replace {user_prompt} with a meaningful test prompt aligned with the agent’s purpose and task descriptions (e.g., if the agent is a research assistant, use: “Summarize the latest research on climate change adaptation strategies.”). 

AI支援コーディング環境で必要なコマンドを実行できるようにして、要件の導入、エージェントテンプレートへのその他の必要な変更、および作成されたエージェントのテストを実行できるようにします。

修正されたエージェントワークフローのテスト

AIの支援による移行が完了すると、Cursor(またはAI支援コーディング環境)はワークフローを自動的にテストします。ただし、ワークフローを手動でテストするには、ターミナルで以下のコマンドを使用します。

task agent:cli START_DEV=1 -- execute --user_prompt "Health benefits of carrots" 

トラブルシューティング

エージェントワークフローのテスト中に問題が発生した場合は、エージェントの環境と依存関係を更新することをお勧めします。 これを行うには、task install(またはtask setup)、task agent:install、およびtask agent:updateコマンドを使用します。 これらのコマンドやその他のコマンドの詳細については、taskコマンドを実行してください。

テスト中に接続エラーが表示された場合は、コマンドでSTART_DEV=1を使用していることを確認するか、別のターミナルウィンドウでtask agent:devを使用して開発サーバーを個別に起動します。

次のステップ

更新されたエージェントワークフローのテストに問題がなければ、ワークフローをワークショップに送信してエージェントのプレイグラウンドに接続するか、ワークフローをデプロイします。