You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用OpenAI Python库和Azure OpenAI时,如何查询端点位置?

查询Azure OpenAI端点所在位置的方法

发起基础Azure OpenAI请求时,响应对象中不会返回端点的地理位置信息,若无法通过Azure门户查看所有实例,可通过以下两种方式查询:

方法1:使用Azure认知服务管理SDK

通过Azure官方管理SDK直接获取实例的位置信息,需要具备对应Azure订阅的RBAC权限(如认知服务读者/参与者)。

步骤:

  1. 安装依赖库:
pip install azure-mgmt-cognitiveservices azure-identity
  1. 编写代码获取位置:
from azure.identity import DefaultAzureCredential
from azure.mgmt.cognitiveservices import CognitiveServicesManagementClient

# 替换为你的Azure订阅ID
subscription_id = "你的订阅ID"
# 从端点URL(https://{resource-name}.openai.azure.com/)中提取资源名称
resource_name = "xxxxxx"
# 替换为资源所在的资源组名称
resource_group_name = "你的资源组名称"

# 初始化认证和管理客户端
credential = DefaultAzureCredential()
client = CognitiveServicesManagementClient(credential, subscription_id)

# 获取实例详情并输出位置
resource_details = client.accounts.get(resource_group_name, resource_name)
print(f"端点所在位置: {resource_details.location}")

方法2:从API响应头提取(非稳定方式)

部分Azure OpenAI API响应会携带x-ms-region头,可通过OpenAI Python库的with_raw_response方法捕获原始响应,尝试提取该字段:

修改原请求代码如下:

import json
import pprint
from openai import AzureOpenAI

client = AzureOpenAI(
  azure_endpoint = "https://xxxxxx.openai.azure.com/",
  api_key='xxxxxxxxxxxxxxxxxxxxx',
  api_version="2023-07-01-preview"
)

message_text = [{"role":"system","content":"You are an AI assistant that helps people find information."}]

# 发送请求并获取原始响应
raw_response = client.chat.completions.with_raw_response.create(
  model="gpt-4xxxxxxxx", 
  messages = message_text,
  temperature=0.7,
  max_tokens=800,
  top_p=0.95,
  frequency_penalty=0,
  presence_penalty=0,
  stop=None
)

# 尝试提取区域信息
region = raw_response.headers.get("x-ms-region")
if region:
    print(f"端点所在位置: {region}")
else:
    print("响应头中未返回区域信息")

# 后续处理原响应对象
completion = raw_response.parse()
print('completion:\n')
pprint.pprint(completion)

json_data = json.dumps(completion, default=lambda o: o.__dict__, indent=4)
print(json_data)

注意:该方法依赖Azure是否返回对应响应头,并非所有场景都有效。

若上述方法均无法使用,需联系Azure管理员获取对应实例的位置信息,或确认是否具备足够权限访问管理资源。

内容的提问来源于stack exchange,提问作者Franck Dernoncourt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 14:23:21