遷移到全新的代理程式開發者體驗

提示

有 遷移工具 可協助將助理 API 的遷移自動化至代理。

Foundry Agent Service 提供升級的開發者體驗,協助打造易於建置、版本化、操作與觀察的智慧代理。 新的代理 API 引入了現代化的 SDK、新的企業級功能,並保留了您目前依賴的身份、治理與可觀察性功能。

選擇符合您目前申請的路徑:

起點 開始於 繼續進行
OpenAI 助理 API 將助理遷移至新的 Agent 將討論串與執行階段替換為交談和回覆。
Classic Foundry 代理程式 將經典代理程式遷移至新代理程式 檢查工具是否可用,然後更換螺紋部件和運轉組件。
現有執行緒或執行整合 將執行緒移轉至對話 將執行遷移至回應。
適用於自動化的 Assistants API 移轉 執行遷移工具 檢視已產生的資產並確認遷移情況。

至少,請完成以下四個步驟:

  1. 將目前使用的工具與 代理程式工具可用性,進行比對,並為無法沿用的工具選擇替代工具。
  2. 請依照 經典代理 或 助理 API 路徑遷移代理定義。
  3. 將 討論串 替換為對話,並將 runs 替換為回應。
  4. 驗證您的遷移,包括狀態、工具呼叫、輸出及錯誤處理。

先決條件

  • 一個 Azure 訂閱。 免費創建一個。
  • 一個Microsoft Foundry 計畫。
  • 專為你的語言設計的 Foundry Agent Service SDK 和對應的身份驗證套件。 安裝適合您語言的套件,然後用az login登入,或使用DefaultAzureCredential。
pip install "azure-ai-projects>=2.3.0"
  • 您想要遷移的現有代理或助理程式碼。

以下程式碼是本指南中所使用的用戶端初始化:

from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient

# Format: "https://resource_name.services.ai.azure.com/api/projects/project_name"
PROJECT_ENDPOINT = "your_project_endpoint"

project = AIProjectClient(
    endpoint=PROJECT_ENDPOINT,
    credential=DefaultAzureCredential(),
)
openai = project.get_openai_client()

用 project 進行代理程式建立與版本化。 對話和回應時可以使用 openai (或你語言中的等效詞)。

主要優點

新代理商提供以下好處:

開發者生產力

  • ⭐ 更多模特兒。 你可以在代理中使用任何 Foundry 模型,或直接作為回應產生呼叫來產生回應。
  • 更多功能。 網頁搜尋、檔案搜尋、程式碼解譯器、MCP 工具呼叫、影像生成及推理摘要。
  • 現代 API 基本構件。 它是建立在 Responses API 之上,而不是舊版的 Assistants API。
  • 背景模式。 支援長時間運行的工具(例如影像生成),以及持久的串流(支援斷線後重連的場景)
  • 為未來做好準備。 新功能和模型支援僅會添加到新的代理中。
  • 新的特工類型。 建立提示型 Agent 和代理 Agent。

企業就緒程度

  • ⭐ 單一租戶儲存. 使用單一租戶儲存,並可選擇自帶Azure Cosmos DB來儲存狀態並保護資料安全。
  • 加強了安全措施。 控制誰能執行或修改代理定義。
  • 職責分離。 定義一次代理,並用各種輸入執行。
  • 可部署的代理程式。 代理程式可公開為個別端點。

API 現代化

  • 改善國家管理。 用對話代替串程和訊息。
  • 狀態上下文。 自動在通話間保留上下文。
  • 回應 API 的超集合。 它建立在回應 API 之上,並增加了更多功能。
  • 單一或多代理的工作流程。 輕鬆連結代理程式以支援複雜工作流程。

主要變更

下表總結了過去與目前客服經驗之間的主要 API 變更。

之前 之後 詳情
線程 對話 支援項目流,而不僅限於訊息。
執行 回應 回應會傳送輸入項目或使用對話物件,並接收輸出項目。 工具呼叫迴圈是明確管理的。
助理/代理人 特工(新) 企業級提示和裝載式 Agent 的支援,以及預設用於任何 Foundry 模型的狀態型前後關聯。

代理工具可用性

下表比較了經典代理與新推出的 Foundry 代理服務中可用的代理工具。 用它來辨識哪些工具會直接延續,哪些已經改變,哪些是專屬於新體驗的。

工具 鑄造廠(經典) 鑄造廠(新建)
代理人對代理人(A2A) 不 是的(GA; a2a_preview 仍處於公開預覽階段)
Foundry IQ(Azure AI 搜尋服務) 是(喬治亞州) 是(喬治亞州)
Azure Functions 是(喬治亞州) 不
瀏覽器自動化 是的(公開預覽) 是的(公開預覽)
程式碼直譯器 是(喬治亞州) 是(喬治亞州)
電腦使用 是的(公開預覽) 是的(公開預覽)
連結代理 是的(公開預覽) 不(推薦:A2A 工具)
深度研究 是的(公開預覽) 不建議(建議:深度研究模型搭配網頁搜尋工具)
Fabric 資料代理程式 是的(公開預覽) 是的(公開預覽)
檔案搜尋 是(喬治亞州) 是(喬治亞州)
功能 是(喬治亞州) 是(喬治亞州)
Bing 搜尋基礎 是(喬治亞州) 是(喬治亞州)
使用 Bing 自訂搜尋進行基礎設置 是的(公開預覽) 是的(公開預覽)
影像生成 不 是的(公開預覽)
MCP 是的(公開預覽) 是(喬治亞州)
OpenAPI 是(喬治亞州) 是(喬治亞州)
SharePoint 基礎設置 是的(公開預覽) 是的(公開預覽)
網路內容搜尋 不 是(喬治亞州)

重要

在新的 API 中,對話與回應 API 使用 OpenAI 客戶端 (或其語言對應版本)。 在 Python 中調用project.get_openai_client()。 在 C# 中,使用 projectClient.ProjectOpenAIClient.GetProjectResponsesClientForAgent()。 在 JavaScript 中,呼叫 project.getOpenAIClient()。 在Java中,使用 AgentsClientBuilder 來建立一個 ResponsesClient。 代理程式的建立與版本控制仍由 專案客戶端負責。 每個章節中的範例說明了該使用哪個客戶端。

將討論串遷移到對話

執行緒在伺服器端儲存訊息。 對話可以儲存項目,包括訊息、工具呼叫、工具輸出及其他資料。

請求

以下範例比較了線上討論串建立(先前)與對話創建(目前)。 目前的方法使用來自 project.get_openai_client()的 OpenAI 用戶端。

先前版本 - 執行緒

thread = client.agents.threads.create( 
     messages=[{"role": "user", "content": "Tell me a one line funny story about unicorns"}], 
     metadata={"agent": "my-awesome-agent"}, 
) 

目前版本 - 交談

conversation = openai.conversations.create(
    items=[
        {
            "type": "message",
            "role": "user",
            "content": "Tell me a one line funny "
                       "story about unicorns",
        }
    ],
    metadata={"agent": "my-awesome-agent"},
)

回應

JSON 回應顯示了執行緒物件與對話物件之間的結構差異。

上一個 - 討論串

{ 
  "id": "thread_1234abcd",  
  "object": "thread",  
  "created_at": 1762217858,  
  "metadata": {"agent": "my-awesome-agent"},  
  "tool_resources": {} 
} 

目前版本 - 交談

{ 
  "id":"conv_1234abcd", 
  "created_at":1762217961, 
  "metadata":{"agent":"my-awesome-agent"}, 
  "object":"conversation" 
} 

將項目加入現有對話

建立對話後,使用 conversations.items.create() 添加後續訊息。 此模式以client.agents.messages.create()取代新增訊息至執行緒的行為。

先前版本 - 將訊息新增至執行緒

message = client.agents.messages.create(
    thread_id=thread.id,
    role="user",
    content="Follow-up question about the same topic",
)

目前版本 - 將項目新增至交談

openai.conversations.items.create(
    conversation_id=conversation.id,
    items=[
        {
            "type": "message",
            "role": "user",
            "content": "Follow-up question "
                       "about the same topic",
        }
    ],
)

將執行移轉至回應

執行是針對執行緒運行的非同步程序。 回應則較簡單:提供一組輸入項目執行,並返回輸出項目清單。 回應可以單獨使用,或與對話物件一起使用以儲存上下文。 回應 API 使用 OpenAI 客戶端。

請求

以下範例比較了你如何呼叫代理邏輯。 先前的方法使用具輪詢機制的非同步執行作業。 目前的做法會在 OpenAI 客戶端上呼叫responses.create()。

上一個 - 執行

thread_id = "thread_abcd1234" 
assistant_id = "asst_efgh5678" 
run = project_client.agents.runs.create( 
  thread_id=thread_id,  
  agent_id=assistant_id, 
  additional_instructions="Please address the user as Jane Doe. The user has a premium account" 
) 
while run.status in ("queued", "in_progress"): 
  time.sleep(1) 
  run = project_client.agents.runs.get(thread_id=thread_id, run_id=run.id) 

目前 - 回應

conversation_id = "conv_11112222AAAABBBB"

response = openai.responses.create(
    input="Hi, Agent! Draw a graph for a line "
          "with a slope of 4 and "
          "y-intercept of 9.",
    conversation=conversation_id,
    extra_body={
        "agent_reference": {
            "name": "my-agent",
            "type": "agent_reference",
        }
    },
)

回應

上一個 - 執行

{
  "id": "run_xyz",
  "object": "thread.run",
  "created_at": 1762218810,
  "assistant_id": "asst_efgh5678",
  "thread_id": "thread_abcd1234",
  "status": "completed",
  "started_at": 1762218810,
  "expires_at": null,
  "cancelled_at": null,
  "failed_at": null,
  "completed_at": 1762218812,
  "required_action": null,
  "last_error": null,
  "model": "gpt-4.1",
  "instructions": "You politely help with math questions. Use the Code Interpreter tool when asked to visualize numbers. Please address the user as Jane Doe. The user has a premium account",
  "tools": [
    {
      "type": "code_interpreter"
    }
  ],
  "tool_resources": {},
  "metadata": {},
  "temperature": 1.0,
  "top_p": 1.0,
  "max_completion_tokens": null,
  "max_prompt_tokens": null,
  "truncation_strategy": {
    "type": "auto",
    "last_messages": null
  },
  "incomplete_details": null,
  "usage": {
    "prompt_tokens": 1216,
    "completion_tokens": 76,
    "total_tokens": 1292,
    "prompt_token_details": {
      "cached_tokens": 0
    }
  },
  "response_format": "auto",
  "tool_choice": "auto",
  "parallel_tool_calls": true
}

目前 - 回應

{
  "id": "resp_3483e9c8dda4f165006909550333588190afc76a645a0e877a",
  "created_at": 1762219267.0,
  "error": null,
  "incomplete_details": null,
  "instructions": null,
  "metadata": {
    "x-ms-telemetry-agent-kind": "",
    "x-ms-telemetry-user-agent": "OpenAI/Python 2.4.0",
    "x-ms-telemetry-response-start-time": "2025-11-04T01:21:06.5346780+00:00"
  },
  "model": "gpt-4.1",
  "object": "response",
  "output": [
    {
      "id": "msg_3483e9c8dda4f1650069095503abf881909917865574cddf2c",
      "content": [
        {
          "annotations": [],
          "text": "Of course! Here's a simple plot for the line with a rate of change of 4 and a y-intercept of 9.\\n\\nThe equation of the line is:\\n\\n\\\\[ y = 4x + 9 \\\\]\\n\\nLet's draw a graph for it:\\n\\n---\\n\\n```plaintext\\n  |\\n20|                     *\\n  |                  *\\n  |               *\\n  |            *\\n10|         *\\n  |      *\\n  |   *\\n  |*\\n  +---------------------------\\n   -2 -1  0  1  2  3\\n```\\n\\n**Key points:**\\n- The y-intercept is **9**, so at \\\\(x = 0\\\\), \\\\(y = 9\\\\) (point: (0,9))\\n- For each step right (increase in x), y goes up 4 units (rate of change \\\\(m = 4\\\\))\\n  - For \\\\(x = 1\\\\): \\\\(y = 4(1) + 9 = 13\\\\) (point: (1,13))\\n  - For \\\\(x = -1\\\\): \\\\(y = 4(-1) + 9 = 5\\\\) (point: (-1,5))\\n\\nIf you'd like a precise graph or want to visualize it interactively, let me know!",

          "type": "output_text",
          "logprobs": []
        }
      ],
      "role": "assistant",
      "status": "completed",
      "type": "message"
    }
  ],
  "parallel_tool_calls": true,
  "temperature": 1.0,
  "tool_choice": "auto",
  "tools": [],
  "top_p": 1.0,
  "background": false,
  "conversation": {
    "id": "conv_3483e9c8dda4f16500GwcAgtdWlSmbMPzYLjWvDjiSe6LSFcC6"
  },
  "max_output_tokens": null,
  "max_tool_calls": null,
  "previous_response_id": null,
  "prompt": null,
  "prompt_cache_key": null,
  "reasoning": {
    "effort": null,
    "generate_summary": null,
    "summary": null
  },
  "safety_identifier": null,
  "service_tier": "default",
  "status": "completed",
  "text": {
    "format": {
      "type": "text"
    },
    "verbosity": "medium"
  },
  "top_logprobs": 0,
  "truncation": "disabled",
  "usage": {
    "input_tokens": 45,
    "input_tokens_details": {
      "cached_tokens": 0
    },
    "output_tokens": 264,
    "output_tokens_details": {
      "reasoning_tokens": 0
    },
    "total_tokens": 309
  },
  "user": null,
  "content_filters": null,
  "store": true
}

將經典軟體代理程式遷移到新軟體代理程式

如果你使用 client.agents.create_agent() 早期 SDK 版本的方法,請遷移到 client.agents.create_version()。 新方法引入了結構化代理人定義,明確包含 kind、 model、 和 instructions 欄位。

請求

上一頁

agent = client.agents.create_agent( 
    model="gpt-4.1", 
    name="my-agent",  # Name of the agent 
    instructions="You politely help with math questions. Use the Code Interpreter tool when asked to visualize numbers.",  # Instructions for the agent 
    tools=code_interpreter.definitions,  # Attach the tool 
) 

目前

from azure.ai.projects.models import (
    CodeInterpreterTool,
    PromptAgentDefinition,
)

agent = project.agents.create_version(
    agent_name="my-agent",
    definition=PromptAgentDefinition(
        model="gpt-4.1",
        instructions=(
            "You politely help with math "
            "questions. Use the Code "
            "Interpreter tool when asked to "
            "visualize numbers."
        ),
        tools=[CodeInterpreterTool()],
    ),
)

回應

以下 JSON 範例比較了先前與目前代理建立方法所回傳的回應物件。

上一頁

{ 
  'id': 'asst_AVKrdr2KJthDnZiJ51nca1jy', 
  'object': 'assistant', 
  'created_at': 1762218496, 
  'name': 'my-agent', 
  'description': None, 
  'model': 'gpt-4.1', 
  'instructions': 'You politely help with math questions. Use the Code Interpreter tool when asked to visualize numbers.', 
  'tools': [ 
    { 
      'type': 'code_interpreter' 
    } 
  ], 
  'top_p': 1.0, 
  'temperature': 1.0, 
  'tool_resources': { 
    'code_interpreter': { 
      'file_ids': [      
      ] 
    } 
  }, 
  'metadata': { 
  }, 
  'response_format': 'auto' 
} 

目前

{
  "metadata": {},
  "object": "agent.version",
  "id": "code-agent:1",
  "name": "code-agent",
  "version": "1",
  "description": "Agent with code interpreter",
  "created_at": 1772045947,
  "definition": {
    "kind": "prompt",
    "model": "gpt-4.1",
    "instructions": "You politely help with math questions. Use the Code Interpreter tool when asked to visualize numbers.",
    "tools": [
      {
        "type": "code_interpreter"
      }
    ]
  }
}

將助理移轉至新的代理程式

如果你的程式碼使用 OpenAI 助理 APIclient.beta.assistants.create()(),請透過 client.agents.create_version()遷移到 Foundry Agent 服務。 以下範例展示了結構上的差異。

先前版本 - 助理

assistant = client.beta.assistants.create( 
    model="gpt-4.1", 
    name="my-assistant", 
    instructions="You politely help with math questions. Use the Code Interpreter tool when asked to visualize numbers.",  # Instructions for the agent 
    tools=[{"type": "code_interpreter"}], 
) 

目前版本 - 新 Agent

from azure.ai.projects.models import (
    CodeInterpreterTool,
    PromptAgentDefinition,
)

agent = project.agents.create_version(
    agent_name="my-agent",
    definition=PromptAgentDefinition(
        model="gpt-4.1",
        instructions=(
            "You politely help with math "
            "questions. Use the Code "
            "Interpreter tool when asked to "
            "visualize numbers."
        ),
        tools=[CodeInterpreterTool()],
    ),
)

執行遷移工具

GitHub 上有 migration 工具,協助自動化您的客服人員與助理遷移。 此工具能遷移程式碼結構,如代理定義、執行緒建立、訊息建立及運行建立。 它不會遷移過去的執行紀錄、執行緒或訊息等狀態資料。 遷移後,你可以執行新程式碼,任何新的狀態資料都會以更新格式建立。

以下範例展示了完整的前後對比。 請注意,目前的程式碼同時用於 project 代理建立以及 openai 對話與回應。

上一頁

agent = project_client.agents.create_agent( 
    model="gpt-4.1", 
    name="my-agent", 
    instructions="You politely help with math questions. Use the Code Interpreter tool when asked to visualize numbers.",  # Instructions for the agent 
    tools=[{"type": "code_interpreter"}] 
) 
thread = project_client.agents.threads.create() 
message = project_client.agents.messages.create( 
    thread_id=thread.id, 
    role="user",  # Role of the message sender 
    content="Hi, Agent! Draw a graph for a line with a rate of change of 4 and y-intercept of 9.",  # Message content 
) 
run = project_client.agents.runs.create_and_process( 
    thread_id=thread.id, 
    agent_id=agent.id, 
    additional_instructions="Please address the user as Jane Doe. The user has a premium account", 
) 
messages = project_client.agents.messages.list(thread_id=thread.id) 
for message in messages: 
    print(f"Role: {message.role}, Content: {message.content}") 

目前

from azure.ai.projects.models import (
    CodeInterpreterTool,
    PromptAgentDefinition,
)

# Create the agent
agent = project.agents.create_version(
    agent_name="my-agent",
    definition=PromptAgentDefinition(
        model="gpt-4.1",
        instructions=(
            "You politely help with math "
            "questions. Use the Code "
            "Interpreter tool when asked "
            "to visualize numbers."
        ),
        tools=[CodeInterpreterTool()],
    ),
)

# Create a conversation with initial message
conversation = openai.conversations.create(
    items=[
        {
            "type": "message",
            "role": "user",
            "content": (
                "Hi, Agent! Draw a graph "
                "for a line with a rate "
                "of change of 4 and "
                "y-intercept of 9."
            ),
        }
    ],
)

# Send a response with the agent
response = openai.responses.create(
    conversation=conversation.id,
    extra_body={
        "agent_reference": {
            "name": agent.name,
            "type": "agent_reference",
        }
    },
    input=(
        "Please address the user as "
        "Jane Doe. The user has a "
        "premium account"
    ),
)

# Print the response output
for item in response.output:
    if item.type == "message":
        for block in item.content:
            print(block.text)

驗證你的遷移

遷移程式碼後,確認一切正常運作:

  1. 執行更新後的程式碼 並確認其執行時沒有錯誤。
  2. 透過確認 create_version() 會傳回帶有 id 和 version 欄位的物件,來檢查代理建立。
  3. 透過建立對話、傳送回應並驗證輸出項目是否回傳來測試對話。
  4. 透過對同一對話發送多個回應並檢查代理是否記得先前的訊息,以確認上下文保留。

排除常見問題

症狀 成因 解決方法
Python:AttributeError: 'AIProjectClient' has no attribute 'conversations' 您在專案用戶端上呼叫了 conversations.create(),而不是在 OpenAI 用戶端上呼叫。 使用 project.get_openai_client() 以取得 OpenAI 客戶端,然後呼叫 openai.conversations.create()。
C#:找不到 Azure.AI.Extensions.OpenAI 命名空間 Azure.AI.Extensions.OpenAI NuGet 套件遺失了。 安裝 Azure.AI.Projects (此會帶入 Azure.AI.Extensions.OpenAI 和 Azure.AI.Projects.Agents 做為相依性)。
JavaScript: getOpenAIClient is not a function 你使用的是較舊版本的 @azure/ai-projects。 更新至 @azure/ai-projects 2.8.0 或更新版本: npm install @azure/ai-projects。
Java:AgentsClientBuilder無法解析 azure-ai-agents Maven 相依性遺失或過期。 將 com.azure:azure-ai-agents:2.2.0 增加至你的 pom.xml 依賴。
create_agent() 被移除 早期的 SDK 版本使用 create_agent(),但在 v2.0.0 中被移除。 將 create_version()(Python/JS)或 CreateAgentVersionAsync()(C#)或 createAgentVersion()(Java)替換,並傳遞一個 PromptAgentDefinition 物件。
舊的執行緒資料無法取得 遷移工具不會遷移狀態資料(過去的執行紀錄、執行緒或訊息)。 遷移後開始新的對話。 歷史資料仍可透過先前的 API 存取,直到該 API 被棄用為止。
responses.create() 引發模型錯誤 型號名稱可能不正確或在您所在地區無法取得。 請在 Foundry 專案中確認型號名稱,並檢查 模型區域的可用性。