企業向けAIエージェント:計画と実行 + 関数呼び出し

Plan-and-Executeフレームワーク

Posted by LuochuanAD on February 16, 2026 本文总阅读量

背景

完全なAIエージェントでは、ツールの呼び出しにChatGPTのFunction Callingの考え方を採用し、「Tools Schema」を定義することでLLMとツールの連携を実現しています。

1, まずTools Schemaを定義する

ここで使うツール:Search Database、Send Mail、Get Weatherなど。これらのツールはカスタムツールメソッドの名前になります。

tools = [

	{
		"type": "function",
		"function": {
			"name": "Search Database",
			"description": "Retrieve profile from database",
			"parameters":{
				"type": "object",
				"properties": {
					"name": {"type": "string"}
				},
				"required": ["name"]
			}
		}
	},
	{
		"type": "function",
		"function": {
			"name": "Send Mail",
			"description": "Send Mail to Gmail",
			"parameters":{
				"to": {"type": "string"},
				"subject": {"type": "string"},
				"body": {"type": "string"}
			}
		}
	},
	{
		"type": "function",
		"function": {
			"name": "Get Weather",
			"description": "Get Weather from location",
			"parameters":{
				"type": "object",
				"properties": {
					"location": {"type": "string"}
				},
				"required": ["location"]
			}
		}
	},
......

]

2, ループでExecuteを実行 (Plan-and-Executeフレームワーク)

Planner Prompt:

prompt = ‘
	You are a task planner in a multi-step AI system.
	User query: {Query}
	Your job:
	1, Break down the user query into executable steps.
	2, Each step must be atomic and tool-executable.
	3, Steps should be ordered logically.
	4, Do NOT execute anything.
	5, Only return JSON.
’
import json
 
# 1️⃣ Plannerに計画を生成させる
planner_response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": planner_prompt},
        {"role": "user", "content": user_query}
    ]
)
 
plans = json.loads(planner_response.choices[0].message.content)["plans"]
 
# 2️⃣ ステップごとに実行
context_memory = {}
 
for step in plans:
    tool_name = step["tool"]
    args = step["arguments"]
 
    result = execute_tool(tool_name, args, context_memory)
 
    # 結果をコンテキストに追加
    context_memory[f"step_{step['step_id']}"] = result

def execute_tool(tool_name, args, memory):
 
    if tool_name == "Search Database":
        return search_Database(args["name"])
 
    elif tool_name == "Send Mail":
        
        return send_Mail(args["to"],args["subject"],args["body"])
 
    elif tool_name == "Get Weather":
        return get_Weather(args["location"])
 
    else:
        raise Exception("Unknown tool")

評価: (Plan-and-Executeフレームワーク + Function Calling 推奨指数:🌟🌟)

メリット:

1, タスク分解: 大きなタスクを小さいタスクに分割することで、複雑な課題を効率的に管理・解決できる。
2, 詳細なガイド: 詳細な指示により推論プロセスの質と正確性を向上。
3, 適応性: 様々な複雑な問題に対して、異なるタイプのタスクに調整可能。

デメリット:

1, Planner Prompt は一度に計画を生成するため、ステップの欠落や順序の乱れ、中間状態の把握ができない。
2, ループのExecuteは機械的すぎて、条件分岐や動的意思決定、失敗時のリトライができない。
3, PlannerはExecuteの結果(空かどうかなど)を知らない。

適用に適したケース:

1, ステップが固定されていて、フローが安定している。
2, 動的な意思決定や分岐ロジックを必要としない。

適用に不向きなケース:

1, 意思決定型タスク
2, 条件分岐
3, データ駆動の推論
4, 長いチェーンのタスク