使用 Azure Open AI 和 Python SDK 时遇到 AuthenticationError 401 – 主体无权访问 API/操作

问题摘要:

在尝试访问 OpenAI API 时遇到带有错误代码 401 的 AuthenticationError。错误消息显示主体无权访问 API/操作。

预期结果:

根据分配的角色和权限,服务主体应有权访问指定的 API/操作。

实际结果:

收到以下错误消息:主体无权访问 API/操作。这阻止了服务主体按预期访问 API。

环境详情:

  • 订阅 ID:已隐藏

  • 分配的角色:贡献者、用户

  • 应用程序 ID:已隐藏

  • API/操作:Microsoft.CognitiveServices/accounts/listKeys/action

  • Azure 资源:已隐藏

感谢对上述问题的任何支持


回答:

您需要拥有Cognitive Services OpenAI 用户Cognitive Services OpenAI 贡献者或认知服务用户角色,才能访问用于推理的 OpenAI 资源。

接下来,请使用以下代码。

from azure.identity import get_bearer_token_provider, ClientSecretCredentialfrom openai import AzureOpenAIcred = ClientSecretCredential(client_id="<yourClientId>",tenant_id="YourTenantId",client_secret="<ClientSeceret>")token_provider = get_bearer_token_provider(    cred, "https://cognitiveservices.azure.com/.default")client = AzureOpenAI(    api_version="2024-02-15-preview",    azure_endpoint="https://v-jgsopenai.openai.azure.com/",    azure_ad_token_provider=token_provider)response = client.chat.completions.create(    model="gpt35", # model = "deployment_name".    messages=[        {"role": "system", "content": "You are a helpful assistant."},        {"role": "user", "content": "Does Azure OpenAI support customer managed keys?"},        {"role": "assistant", "content": "Yes, customer managed keys are supported by Azure OpenAI."},        {"role": "user", "content": "Do other Azure AI services support this too?"}    ])print(response.choices[0].message.content)

输出:

enter image description here

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注