Reputation: 85
Issue Summary:
Encountering an AuthenticationError with error code 401 when attempting to access the OpenAI API. The error message indicates that the principal does not have access to the API/Operation.
Expected Outcome:
The service principal should have access to the specified API/Operation based on the roles and permissions assigned.
Actual Outcome:
Receiving the following error message: Principal does not have access to API/Operation. This prevents the service principal from accessing the API as expected.
Environment Details:
Subscription ID: Redacted
Role(s) Assigned: Contributor , User
Application ID: Redacted
API/Operation: Microsoft.CognitiveServices/accounts/listKeys/action
Azure Resource: Redacted
Appreciate any support on the above
Upvotes: 4
Views: 4996
Reputation: 8140
You need to have Cognitive Services OpenAI User or Cognitive Services OpenAI Contributor or Cognitive Services User role to access the open ai resource for inferencing.
Next, use below code.
from azure.identity import get_bearer_token_provider, ClientSecretCredential
from openai import AzureOpenAI
cred = 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)
Output:
Upvotes: 3