Hello André,
Welcome to the Microsoft Q&A and thank you for posting your questions here.
I understand that you would like to use the Agents service through an API key.
To use Azure AI Agents with API keys:
The first thing is to authenticate using the Azure SDK with your endpoint and API key, and configure agents via REST API or Azure AI Studio. Then, avoid the OpenAI SDK for Azure-specific agents.
The below are the steps to achieve it:
- Use the correct Azure SDK for Azure AI Agents from Azure AI Services and require the Azure SDK/REST API, not the OpenAI SDK. - https://learn.microsoft.com/en-us/azure/ai-services/agents/quickstart?view=azure-python-preview for Python.
- To authenticate with an API key in Azure SDK, use the AzureKeyCredential class along with your endpoint:
from azure.ai.openai import OpenAIClient from azure.core.credentials import AzureKeyCredential endpoint = "https://<your-resource-name>.openai.azure.com/" api_key = AzureKeyCredential("<your-api-key>") client = OpenAIClient(endpoint=endpoint, credential=api_key)
- If your Agents are configured via Azure AI Studio or REST APIs. There is no direct SDK method like
create_agent
. Instead use the REST API to create an agent:
Include the agent configuration in the request body, you can see an example here in the API documentation - https://learn.microsoft.com/en-us/rest/api/azureai/agents/createPOST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.CognitiveServices/accounts/{accountName}/agents?api-version=2024-03-01-preview Authorization: Bearer <API_KEY>
- The next is to avoid OpenAI SDK for Azure Agents because the OpenAI SDK (
openai
library) interacts with OpenAI's platform, not Azure. Use the Azure SDK (azure-ai-openai
) for Azure services.
NOTE: In case there is an issue.
- Make sure your resource is in a supported region for Azure Agents.
- Verify your API key has the correct permissions (e.g.,
Cognitive Services User
). - Use Azure AI Studio to preconfigure agents visually, then export the code for API integration.
Regarding additional clarifications:
Using AzureKeyCredential
would always result in an error because the Azure AI Project Client (AIProjectClient
) does not support API Key authentication. Instead, it is designed to work with Managed Identities or DefaultAzureCredential() for authentication https://learn.microsoft.com/enus/python/api/overview/azure/aiprojectsreadme?view=azurepythonpreview and https://learn.microsoft.com/enus/azure/aiservices/authentication
So, try to understand Direct API Key authentication is not possible with AIProjectClient
, and use the REST API directly with an API Key for authentication instead of AIProjectClient
will be the best option for you. Also, the provided Python below enables API Keybased authentication correctly.
- Retrieve Your API Key:
-
Locate the API Key under the "Keys and Endpoints" section.Go to Azure Portal > Azure AI Studio > Your AI Project.
- Use API Key for Direct REST Calls
Instead of using `AIProjectClient`, make direct API requests with the API Key, this is a code example:
import requests
api_key = "<yourapikey>"
endpoint = "https://<yourresourcename>.cognitiveservices.azure.com/"
headers = {
"OcpApimSubscriptionKey": api_key,
"ContentType": "application/json"
}
data = {
"query": "How many feet are in a mile?",
"parameters": {
"model": "gpt4o",
"temperature": 0.7
}
}
response = requests.post(f"{endpoint}/openai/deployments/gpt4o/chat/completions", json=data, headers=headers)
print(response.json())
You will need to replace <yourapikey>
with your actual API key and '<yourresourcename>` with your Azure AI instance name.
I hope this is helpful! Do not hesitate to let me know if you have any other questions or clarifications.
Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.