Microsoft Foundry 快速入門

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

你將:

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

先決條件

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

你的專案端 點儲存為環境變數。 另外,請將這些數值設定用於腳本。

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

請繼續以下操作或取得代碼:

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

安裝與認證

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

  1. 安裝目前版本的 azure-ai-projects. 此版本使用 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 應用的基本建構要素。 發送輸入並接收模型的回應:

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 版本。

建立代理人

用你部署的模型建立一個代理程式。

代理人定義核心行為。 一旦建立,它能確保使用者互動中回應一致,且不會每次重複指令。 你可以隨時更新或刪除代理人。

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」的代理,透過提問及相關追蹤來互動。 對話在這些互動中保存記錄。

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=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": 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": 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。 確認你想刪除該資源群組。

下一步