共用方式為


Microsoft Foundry 快速入門

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

你將:

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

先決條件

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

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

PROJECT_ENDPOINT=<endpoint copied from welcome screen>
AGENT_NAME="MyAgent"
MODEL_DEPLOYMENT_NAME="gpt-4.1-mini"

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

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

安裝和驗證

請確保你安裝的是正確的預覽版/預發布版本,就像這裡所示。

  1. 安裝這些套件,包括 azure-ai-projects 的預覽版。 此版本使用 Foundry 專案(新)API (預覽版)。

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

小提示

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

與模特兒聊天

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

import os
from dotenv import load_dotenv
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient

load_dotenv()

print(f"Using PROJECT_ENDPOINT: {os.environ['PROJECT_ENDPOINT']}")
print(f"Using MODEL_DEPLOYMENT_NAME: {os.environ['MODEL_DEPLOYMENT_NAME']}")

project_client = AIProjectClient(
    endpoint=os.environ["PROJECT_ENDPOINT"],
    credential=DefaultAzureCredential(),
)

openai_client = project_client.get_openai_client()

response = openai_client.responses.create(
    model=os.environ["MODEL_DEPLOYMENT_NAME"],
    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 不相容。 請參閱 Azure AI Projects 1.x 版本的 Foundry(經典版)文件

建立專員

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

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

import os
from dotenv import load_dotenv
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import PromptAgentDefinition

load_dotenv()

project_client = AIProjectClient(
    endpoint=os.environ["PROJECT_ENDPOINT"],
    credential=DefaultAzureCredential(),
)

agent = project_client.agents.create_version(
    agent_name=os.environ["AGENT_NAME"],
    definition=PromptAgentDefinition(
        model=os.environ["MODEL_DEPLOYMENT_NAME"],
        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 不相容。 請參閱 Azure AI Projects 1.x 版本的 Foundry(經典版)文件

與代理程式聊天

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

import os
from dotenv import load_dotenv
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient

load_dotenv()

project_client = AIProjectClient(
    endpoint=os.environ["PROJECT_ENDPOINT"],
    credential=DefaultAzureCredential(),
)

agent_name = os.environ["AGENT_NAME"]
openai_client = project_client.get_openai_client()

# Optional Step: Create a conversation to use with the agent
conversation = openai_client.conversations.create()
print(f"Created conversation (id: {conversation.id})")

# Chat with the agent to answer questions
response = openai_client.responses.create(
    conversation=conversation.id, #Optional conversation context for multi-turn
    extra_body={"agent_reference": {"name": agent_name, "type": "agent_reference"}},
    input="What is the size of France in square miles?",
)
print(f"Response output: {response.output_text}")

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

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

小提示

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

清理資源

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

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

下一個步驟