共用方式為


Microsoft Foundry 快速入門

在這個快速入門中,你將開始使用 Foundry 中的模型和代理人。

您將:

  • 從模型產生回應
  • 建立一個有明確提示的代理人
  • 與 Agent 進行多回合對話。

先決條件

  • 一個已部署於 Microsoft Foundry 的模型。 如果你還沒有模型,先完成 快速入門:設定 Microsoft Foundry 資源。
  • 所需的語言執行環境、全域工具及 Visual Studio Code 擴充如 準備你的開發環境 所描述。

設定環境變數並取得程式碼

將 你的專案端 點儲存為環境變數。 此外,請設定這些值以用於指令碼。

- Python and JavaScript

PROJECT_ENDPOINT=<endpoint copied from welcome screen>
AGENT_NAME="MyAgent"

- C# and Java

ProjectEndpoint = <endpoint copied from welcome screen>
AgentName = "MyAgent"
  • Python
  • C#
  • TypeScript
  • Java
  • REST API
  • Foundry 入口網站

按照以下說明或取得程式碼:

取得程式碼

在執行 Python 腳本前,請使用 CLI az login 指令登入驗證。

安裝和驗證

請確保你安裝的是這裡顯示的正確版本的套件。

  • Python
  • C#
  • TypeScript
  • Java
  • REST API
  • Foundry 入口網站
  1. 安裝目前版本的 . 此版本使用 Foundry 專案(新)API 。

    pip install azure-ai-projects>=2.0.0
    
  2. 在執行 Python 腳本前,請使用 CLI az login 指令登入驗證。

小提示

程式碼使用 Azure AI Projects 2.x,與 Azure AI Projects 1.x 不相容。 請參閱 Foundry(經典)文件 以了解 Azure AI Projects 1.x 版本。

與模特兒聊天

與模型互動是 AI 應用程式的基本建構區塊。 傳送輸入並接收模型的回應:

  • Python
  • C#
  • TypeScript
  • Java
  • REST API
  • Foundry 入口網站
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient

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

# Create project and openai clients to call Foundry API
project = AIProjectClient(
    endpoint=PROJECT_ENDPOINT,
    credential=DefaultAzureCredential(),
)
openai = project.get_openai_client()

# Run a responses API call
response = openai.responses.create(
    model="gpt-5-mini",  # supports all Foundry direct models
    input="What is the size of France in square miles?",
)
print(f"Response output: {response.output_text}")

執行程式碼後,你會在主控台看到模型生成的回應(例如一首短詩或對提示的回答)。 這能確認你的專案端點、認證和模型部署都正常運作。

小提示

程式碼使用 Azure AI Projects 2.x,與 Azure AI Projects 1.x 不相容。 請參閱 Foundry(經典)文件 以了解 Azure AI Projects 1.x 版本。

建立專員

使用您部署的模型建立代理程式。

代理定義核心行為。 一旦創建,它確保用戶互動中的一致響應,而無需每次都重複指令。 您可以隨時更新或刪除代理程式。

  • Python
  • C#
  • TypeScript
  • Java
  • REST API
  • Foundry 入口網站
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import PromptAgentDefinition

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

# Create project client to call Foundry API
project = AIProjectClient(
    endpoint=PROJECT_ENDPOINT,
    credential=DefaultAzureCredential(),
)

# Create an agent with a model and instructions
agent = project.agents.create_version(
    agent_name=AGENT_NAME,
    definition=PromptAgentDefinition(
        model="gpt-5-mini",  # supports all Foundry direct models"
        instructions="You are a helpful assistant that answers general questions",
    ),
)
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")

輸出確認代理程式已被建立。 SDK 分頁中,你會看到代理程式名稱和 ID 印在主控台上。

小提示

程式碼使用 Azure AI Projects 2.x,與 Azure AI Projects 1.x 不相容。 請參閱 Foundry(經典)文件 以了解 Azure AI Projects 1.x 版本。

與代理程式聊天

使用先前建立名為「MyAgent」的代理程式,透過提問及相關的追問來進行互動。 對話會保留這些互動的歷史記錄。

  • Python
  • C#
  • TypeScript
  • Java
  • REST API
  • Foundry 入口網站
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient

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

# Create project and openai clients to call Foundry API
project = AIProjectClient(
    endpoint=FOUNDRY_PROJECT_ENDPOINT,
    credential=DefaultAzureCredential(),
)
openai = project.get_openai_client()

# Create a conversation for multi-turn chat
conversation = openai.conversations.create()

# Chat with the agent to answer questions
response = openai.responses.create(
    conversation=conversation.id,
    extra_body={"agent_reference": {"name": FOUNDRY_AGENT_NAME, "type": "agent_reference"}},
    input="What is the size of France in square miles?",
)
print(response.output_text)

# Ask a follow-up question in the same conversation
response = openai.responses.create(
    conversation=conversation.id,
    extra_body={"agent_reference": {"name": FOUNDRY_AGENT_NAME, "type": "agent_reference"}},
    input="And what is the capital city?",
)
print(response.output_text)

你會看到代理人對兩個提示的回應。 後續回應顯示代理人會維持跨回合的對話紀錄。

小提示

程式碼使用 Azure AI Projects 2.x,與 Azure AI Projects 1.x 不相容。 請參閱 Foundry(經典)文件 以了解 Azure AI Projects 1.x 版本。

清理資源

如果您不再需要您所建立的任何資源,請刪除與您的專案相關聯的資源群組。

  • Azure 入口網站,選擇資源群組,然後選擇 Delete。 確認您想要刪除資源群組。

下一個步驟

從構思到原型 - 建構並評估企業智能代理