OpenAI remove key access while using AAD authentication

题意:"OpenAI 在使用 AAD 认证时移除了密钥访问权限"

问题背景:

I am calling Azure OpenAI API in my python code. To set it up, we need to provide a few parameters, one of which is openai.api_key. There are 2 options to get this value -

"我在 Python 代码中调用 Azure OpenAI API。要进行设置,我们需要提供一些参数,其中之一是 `openai.api_key`。获取此值有两种选择:"

  1. Static key
  2. AAD Token

I am using AAD token (refer this - https://github.com/openai/openai-python#microsoft-azure-active-directory-authentication) but still I can access it using the static key. I want to remove the static key access.

"我正在使用 AAD 令牌(参考这里 - https://github.com/openai/openai-python#microsoft-azure-active-directory-authentication),但我仍然可以使用静态密钥进行访问。我想移除静态密钥的访问权限。"

Is there any way to disable the static key and only authenticate using AAD authentication?

"有没有办法禁用静态密钥,并仅使用 AAD 认证进行身份验证?"

问题解决:

Is there any way to disable the static key and only authenticate using AAD authentication?

If you need to authenticate using only AAD authentication without using api.key you can use the below code with request method using Python.

"如果你需要仅使用 AAD 认证而不使用 `api.key` 进行身份验证,你可以使用以下代码,通过 Python 中的 `request` 方法实现。"

First, create an application and assign Cognitive Services User role to your application.

"首先,创建一个应用程序并为该应用程序分配 `Cognitive Services User` 角色。"

Code: 代码

python 复制代码
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)

Output: 输出

python 复制代码
 {
   "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
   }
}

Reference: 引用

How to configure Azure OpenAI Service with managed identities - Azure OpenAI | Microsoft Learn

相关推荐
花间相见几秒前
【AI应用开发|Agent记忆】—— Codex 与 Claude Code 持久化记忆对比:两条不用向量库的路线
后端·agent
葡萄城技术团队1 分钟前
GcExcel V9.2 新特性揭秘:公式对象的无损往返
后端
蜗牛互联网15 分钟前
Claude放宽生命科学限制:代价是验证、分级和30天留存
java·人工智能·后端
上进小菜猪19 分钟前
被 32 位整数卡了 30 年脖子:PostgreSQL 事务号困局与 V9 的 64 位解法
后端
论文复现现场29 分钟前
ComfyUI 怎么同时调用 4 张/8 张 RTX 4090?AI 视频批量生成的多实例队列与 Python 调度方案
人工智能·python·comfyui·rtx4090
泡海椒34 分钟前
评分系统最佳实践:JQuick-Java实现权重、阈值动态配置评分
java·人工智能·python
weixin1997010801637 分钟前
《二手ERP对接的对账机制:按日巡检 + 自动补偿,消灭“幽灵订单“》(附Python源码)
python
m0_5474866641 分钟前
《Python数据分析与实践》全套PPT课件(杭州电子科技大学)
python·数据分析
OKkankan1 小时前
Python 基础进阶(三):从函数、类到 asyncio 与 FastAPI 后端开发实战
开发语言·python
智购科技自动售卖机厂家1 小时前
设备一到夏天就频繁跳闸,从启动电流追到压缩机电容~YH
数据结构·人工智能·python·eclipse