共用方式為


Microsoft Foundry 快速入門(經典版)

備註

本文件指的是 Microsoft Foundry(經典版) 入口網站。

🔍 請參閱 Microsoft Foundry(新)文件 以了解新入口網站。

在這個快速入門中,你將使用 Microsoft Foundry 來:

  • 建立專案
  • 部署模型
  • 執行聊天完成
  • 建立和執行代理程式
  • 將檔案上傳至代理程式

Microsoft Foundry SDK 提供多種語言版本,包括 Python、Java、TypeScript 和 C#。 此快速入門提供每個語言的指示。

小提示

本文的其餘部分說明如何建立和使用 Foundry 專案。 請參考快速入門:如果你想使用以樞紐為基礎的專案,請開始 使用 Microsoft Foundry(Hub 專案 )。 我需要哪種類型的專案?

先決條件

這很重要

在開始之前,請確保您的開發環境已準備就緒。
本快速入門著重於 案例特定的步驟 ,例如 SDK 安裝、驗證和執行範例程式碼。

在入口網站中,您可以探索來自許多不同提供者的豐富尖端模型目錄。 在本教學課程中,搜尋並選取 gpt-4o 模型。

  1. 登入 Microsoft Foundry。 確定新鑄造廠的開關是關閉的。 這些步驟指的是經典 Foundry

  2. 如果你正在一個專案中,請在左上角的導覽列中選擇 Microsoft Foundry 以離開該專案。 您很快就會建立一個新的。

  3. 從登陸頁面或 模型目錄中,選取 gpt-4o (或 gpt-4o-mini)。

    截圖顯示如何在 Foundry 平台入口處開始使用模型。

  4. 選取 [使用此模型]。 出現提示時,輸入新的專案名稱,然後選取 [建立]。

  5. 檢閱部署名稱,然後選取 [建立]。

  6. 然後選取部署類型之後,選取 [連線並部署 ]。

  7. 部署之後,從部署頁面選取 [ 在 Playground 中開啟 ]。

  8. 您進入聊天遊樂場,模型已預先部署並可供使用。

如果您要建立代理程式,則可以改為 從建立代理程式開始。 步驟類似,但順序不同。 建立專案後,您將到達客服專員遊樂場,而不是聊天遊樂場。

準備好編碼

小提示

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

  1. 安裝下列套件:

    pip install openai azure-identity azure-ai-projects==1.0.0
    
  2. 在專案的歡迎畫面中找到你的專案終點。

    Microsoft Foundry 模型歡迎畫面的截圖,顯示端點網址和複製按鈕。

  3. 在執行 Python 腳本之前,請務必先使用 CLI az login (或 az login --use-device-code) 命令登入以驗證。

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

與模特兒聊天

聊天完成是 AI 應用程式的基本建置組塊。 使用聊天完成功能,您可以傳送一系列訊息,並從模型取得回應。

小提示

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

以此程式碼中的 endpoint 取代您的端點:

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

project = AIProjectClient(
    endpoint="https://your-foundry-resource-name.ai.azure.com/api/projects/project-name",
    credential=DefaultAzureCredential(),
)

models = project.get_openai_client(api_version="2024-10-21")
response = models.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "You are a helpful writing assistant"},
        {"role": "user", "content": "Write me a poem about flowers"},
    ],
)

print(response.choices[0].message.content)

與代理程式聊天

創建一個代理並與之聊天。

小提示

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

以此程式碼中的 endpoint 取代您的端點:

from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
from azure.ai.agents.models import ListSortOrder, FilePurpose

project = AIProjectClient(
    endpoint="https://your-foundry-resource-name.ai.azure.com/api/projects/project-name",
    credential=DefaultAzureCredential(),
)

agent = project.agents.create_agent(
    model="gpt-4o",
    name="my-agent",
    instructions="You are a helpful writing assistant")

thread = project.agents.threads.create()
message = project.agents.messages.create(
    thread_id=thread.id, 
    role="user", 
    content="Write me a poem about flowers")

run = project.agents.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
if run.status == "failed":
    # Check if you got "Rate limit is exceeded.", then you want to get more quota
    print(f"Run failed: {run.last_error}")

# Get messages from the thread
messages = project.agents.messages.list(thread_id=thread.id)

# Get the last message from the sender
messages = project.agents.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING)
for message in messages:
    if message.run_id == run.id and message.text_messages:
        print(f"{message.role}: {message.text_messages[-1].text.value}")

# Delete the agent once done
project.agents.delete_agent(agent.id)
print("Deleted agent")

將檔案新增至代理程式

代理程式透過使用工具具有強大的功能。 讓我們添加一個文件搜索工具,使我們能夠進行知識檢索。

小提示

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

以此程式碼中的 endpoint 取代您的端點:

from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
from azure.ai.agents.models import ListSortOrder, FileSearchTool

project = AIProjectClient(
    endpoint="https://your-foundry-resource-name.ai.azure.com/api/projects/project-name",
    credential=DefaultAzureCredential(),
)

# Upload file and create vector store
file = project.agents.files.upload(file_path="./product_info_1.md", purpose=FilePurpose.AGENTS)
vector_store = project.agents.vector_stores.create_and_poll(file_ids=[file.id], name="my_vectorstore")

# Create file search tool and agent
file_search = FileSearchTool(vector_store_ids=[vector_store.id])
agent = project.agents.create_agent(
    model="gpt-4o",
    name="my-assistant",
    instructions="You are a helpful assistant and can search information from uploaded files",
    tools=file_search.definitions,
    tool_resources=file_search.resources,
)

# Create thread and process user message
thread = project.agents.threads.create()
project.agents.messages.create(thread_id=thread.id, role="user", content="Hello, what Contoso products do you know?")
run = project.agents.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)

# Handle run status
if run.status == "failed":
    print(f"Run failed: {run.last_error}")

# Print thread messages
messages = project.agents.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING)
for message in messages:
    if message.run_id == run.id and message.text_messages:
        print(f"{message.role}: {message.text_messages[-1].text.value}")

# Cleanup resources
project.agents.vector_stores.delete(vector_store.id)
project.agents.files.delete(file_id=file.id)
project.agents.delete_agent(agent.id)

清理資源

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

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