azure OpenAI在使用AAD身份验证时删除密钥访问

mwkjh3gx  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(111)

我在Python代码中调用Azure OpenAI API。要设置它,我们需要提供一些参数,其中之一是openai.api_key。有两个选项可以获得此值-
1.静态密钥

  1. AAD代币
    我正在使用AAD token(参考此-https://github.com/openai/openai-python#microsoft-azure-active-directory-authentication),但我仍然可以使用静态密钥访问它。我想删除静态密钥访问。
    有没有办法禁用静态密钥,只使用AAD身份验证进行身份验证?
jgwigjjp

jgwigjjp1#

有没有办法禁用静态密钥,只使用AAD身份验证进行身份验证?
如果您需要仅使用AAD身份验证而不使用**api.key进行身份验证,则可以使用下面的代码,使用Python的request方法。
首先,创建一个应用程序,并为您的应用程序分配
Cognitive Services User**角色。

入口:

验证码:

import requests
import json

tenantid="xxxxx"
clientid="xxxxx"
clientsecret="xxxxxx"

auth_url = f'https://login.microsoftonline.com/{tenantid}/oauth2/v2.0/token'
data = {
    'grant_type': 'client_credentials',
    'client_id': clientid,
    'client_secret': clientsecret,
    'scope': 'https://cognitiveservices.azure.com/.default'
}
response = requests.post(auth_url, data=data).json()
access_token=response['access_token']

url="https://YOUR_RESOURCE_NAME.openai.azure.com/openai/deployments/YOUR_DEPLOYMENT_NAME/completions?api-version=2023-05-15"

h1 = {
    "Authorization": f"Bearer {access_token}",
    "Content-Type":"application/json"
}

body={
    "prompt":"MS Dhoni is greatest captain",
    "max_tokens": 100
}
response = requests.post(url,headers=h1,data=json.dumps(body))
s=json.loads(response.text)
d=json.dumps(s,indent=3)
print(d)

输出:

{
   "id": "cmpl-880wqMQxxxxxxxPl",
   "object": "text_completion",
   "created": 1696xxxx0,
   "model": "text-davinci-002",
   "choices": [
      {
         "text": " India has had, he is motivation: Ravichandran AshwinR Ashwin tweeted saying MS Dhoni has given all he had to India cricket for over 15 years and has been a motivation for everybody. Michael holding reveals his best captain story and it involves Kapil and Viv. The Indians were taken by surprise and after the initial shock it took off from there.New Zealand lost by 15 runs. It is a catch that has a contenders for the most iconic cricketing pictures.\n\nIndia",
         "index": 0,
         "finish_reason": "length",
         "logprobs": null
      }
   ],
   "usage": {
      "completion_tokens": 100,
      "prompt_tokens": 6,
      "total_tokens": 106
   }
}

参考:

如何使用托管身份配置Azure OpenAI服务- Azure OpenAI|微软学习

相关问题