你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

Microsoft Foundry 快速入门(经典)

当前查看:Foundry(经典)门户版本 - 切换到新版Foundry门户

在本快速入门中,你将使用 Microsoft Foundry

  • 创建项目
  • 部署模型
  • 运行聊天完成
  • 创建并运行代理
  • 将文件上传到代理

Microsoft Foundry SDK 以多种语言提供,包括Python、Java、TypeScript 和 C# 。 本快速入门提供了每种语言的说明。

提示

本文的其余部分介绍如何创建和使用 Foundry 项目。 若要改用基于中心的项目,请参阅 Quickstart:开始使用 Microsoft Foundry (中心项目)我需要哪种类型的项目?

先决条件

重要

在开始之前,请确保开发环境已准备就绪。
本快速入门重点介绍 特定于方案的步骤 ,例如 SDK 安装、身份验证和运行示例代码。

在门户中,可以浏览来自许多不同提供商的丰富前沿模型目录。 对于本教程,搜索并选择 gpt-4o 模型。

  1. 登录到 Microsoft Foundry。 确保 New 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 Models 欢迎屏幕展示终结点 URL 和复制按钮。

  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 门户中,选择资源组,然后选择 Delete。 确认要删除资源组。