如何通过Python获取Azure OpenAI部署模型的属性
获取Azure OpenAI部署的模型属性(Python实现)
方法1:使用Azure OpenAI Python SDK
该方法可获取部署关联的核心模型属性(如模型名称、上下文窗口大小、支持的功能等)。
- 安装依赖包:
pip install azure-openai - 代码示例:
from azure.openai import AzureOpenAI # 配置Azure OpenAI参数 client = AzureOpenAI( api_key="你的Azure OpenAI API密钥", azure_endpoint="你的Azure OpenAI资源端点", api_version="2024-02-15-preview" # 根据实际使用的API版本调整 ) # 指定部署名称,获取部署详情 deployment = client.deployments.retrieve("你的部署名称") # 打印关键属性 print(f"模型名称: {deployment.model}") print(f"上下文窗口大小: {deployment.capabilities.context_window_size}") print(f"支持聊天完成功能: {deployment.capabilities.chat_completion}") print(f"部署状态: {deployment.status}")
方法2:使用Azure管理SDK获取资源级属性
如果需要获取更全面的部署资源属性(如SKU、区域、创建时间等),可使用Azure认知服务管理SDK。
- 安装依赖包:
pip install azure-mgmt-cognitiveservices azure-identity - 代码示例(使用Azure AD身份验证):
from azure.identity import DefaultAzureCredential from azure.mgmt.cognitiveservices import CognitiveServicesManagementClient # 配置Azure管理参数 subscription_id = "你的Azure订阅ID" resource_group_name = "资源组名称" account_name = "OpenAI资源名称" deployment_name = "你的部署名称" # 初始化管理客户端 credential = DefaultAzureCredential() client = CognitiveServicesManagementClient(credential, subscription_id) # 获取部署资源详情 deployment = client.deployments.get(resource_group_name, account_name, deployment_name) # 打印关键属性 print(f"部署SKU: {deployment.sku.name}") print(f"部署区域: {deployment.location}") print(f"创建时间: {deployment.system_data.created_at}") print(f"关联模型: {deployment.properties.model.name}")
注意事项
- 使用管理SDK时,需确保执行代码的身份(如本地用户、服务主体)拥有
Cognitive Services Reader或更高权限的Azure角色。 - API版本需根据Azure OpenAI资源的实际支持版本调整,可在Azure门户的资源概述页查看。
内容的提问来源于stack exchange,提问作者Dom R.
相关产品推荐
相关产品推荐

