Skip to main content

Azure OpenAI logoAzure OpenAI

Azure OpenAI 通过专用部署提供企业级的 OpenAI 模型访问,保障安全性、合规性及服务级别协议(SLA)。

🌐 Azure OpenAI provides enterprise-grade access to OpenAI models through dedicated deployments with security, compliance, and SLA guarantees.

与其他使用固定模型名称的提供商不同,Azure 使用你在 Azure 门户中配置的 部署名称

🌐 Unlike other providers that have fixed model names, Azure uses deployment names that you configure in the Azure Portal.

Azure 部署的工作原理
Direct link to Azure 部署的工作原理

🌐 How Azure Deployments Work

Azure 模型 ID 遵循此模式:azure-openai/your-deployment-name

🌐 Azure model IDs follow this pattern: azure-openai/your-deployment-name

部署名称针对你的 Azure 账户是唯一的,并在你在 Azure 门户中创建部署时选择。常见示例:

🌐 The deployment name is specific to your Azure account and chosen when you create a deployment in Azure Portal. Common examples:

  • azure-openai/my-gpt4-deployment
  • azure-openai/production-gpt-35-turbo
  • azure-openai/staging-gpt-4o

设置
Direct link to 设置

🌐 Setup

Azure OpenAI Studio 中创建部署。资源名称和 API 密钥可在 Azure 门户的“密钥和终结点”中找到。

🌐 Create deployments in Azure OpenAI Studio. The resource name and API key are in Azure Portal under "Keys and Endpoint".

配置
Direct link to 配置

🌐 Configuration

实例化网关并将其传递给Mastra。提供三种配置模式。

🌐 Instantiate the gateway and pass it to Mastra. Three configuration modes are available.

静态部署
Direct link to 静态部署

🌐 Static Deployments

从 Azure 门户提供部署名称。

🌐 Provide deployment names from Azure Portal.

import { Mastra } from "@mastra/core";
import { AzureOpenAIGateway } from "@mastra/core/llm";

export const mastra = new Mastra({
gateways: [
new AzureOpenAIGateway({
resourceName: "my-openai-resource",
apiKey: process.env.AZURE_API_KEY!,
deployments: ["gpt-4-prod", "gpt-35-turbo-dev"],
}),
],
});

动态发现
Direct link to 动态发现

🌐 Dynamic Discovery

提供管理 API 凭据。网关查询 Azure 管理 API 以列出部署。

🌐 Provide Management API credentials. The gateway queries Azure Management API to list deployments.

import { Mastra } from "@mastra/core";
import { AzureOpenAIGateway } from "@mastra/core/llm";

export const mastra = new Mastra({
gateways: [
new AzureOpenAIGateway({
resourceName: "my-openai-resource",
apiKey: process.env.AZURE_API_KEY!,
management: {
tenantId: process.env.AZURE_TENANT_ID!,
clientId: process.env.AZURE_CLIENT_ID!,
clientSecret: process.env.AZURE_CLIENT_SECRET!,
subscriptionId: process.env.AZURE_SUBSCRIPTION_ID!,
resourceGroup: "my-resource-group",
},
}),
],
});

服务主体需要“Cognitive Services 用户”角色。请参阅 Azure 文档

🌐 The Service Principal requires "Cognitive Services User" role. See Azure documentation.

手动部署名称
Direct link to 手动部署名称

🌐 Manual Deployment Names

仅提供资源名称和 API 密钥。创建代理时请指定部署名称。不要使用 IDE 自动补全。

🌐 Provide resource name and API key only. Specify deployment names when creating agents. No IDE autocomplete.

import { Mastra } from "@mastra/core";
import { AzureOpenAIGateway } from "@mastra/core/llm";

export const mastra = new Mastra({
gateways: [
new AzureOpenAIGateway({
resourceName: "my-openai-resource",
apiKey: process.env.AZURE_API_KEY!,
}),
],
});

配置参考
Direct link to 配置参考

🌐 Configuration Reference

选项类型是否必填描述
resourceNamestringAzure OpenAI 资源名称
apiKeystring来自“密钥和终结点”的 API 密钥
apiVersionstringAPI 版本(默认: 2024-04-01-preview
deploymentsstring[]静态模式的部署名称
managementobject管理 API 凭据
management.tenantIdstring是*Azure AD 租户 ID
management.clientIdstring是*服务主体客户端 ID
management.clientSecretstring是*服务主体密钥
management.subscriptionIdstring是*Azure 订阅 ID
management.resourceGroupstring是*资源组名称
  • 如果提供了 management,则为必填

用法
Direct link to 用法

🌐 Usage

import { Agent } from "@mastra/core/agent";

const agent = new Agent({
id: "my-agent",
name: "My Agent",
instructions: "You are a helpful assistant",
model: "azure-openai/my-gpt4-deployment" // Use your Azure deployment name (autocompleted in dev mode)
});

// Generate a response
const response = await agent.generate("Hello!");

// Stream a response
const stream = await agent.stream("Tell me a story");
for await (const chunk of stream) {
console.log(chunk);
}

请查看 Azure OpenAI 模型可用性 以获取特定地区的选项。

🌐 Check Azure OpenAI model availability for region-specific options.