An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
Hi Kalpana,
Please check in Azure AI Foundry whether the deployment exists with the correct model_name and deployment_name, as you need to use both when accessing them in your code:
Select Models + endpoints in Azure AI Foundry, then copy the deployment_name (highlighted in brown) and the model_name as shown below:
Now use the below code to access the model:
If using Key Based Authentication:
import os
from openai import AzureOpenAI
endpoint = "https://oai-cs-gpt-bsox-zxcv.openai.azure.com/"
model_name = "<model-name>"
deployment = "<deployment-name>"
subscription_key = "<your-api-key>"
api_version = "2024-12-01-preview"
client = AzureOpenAI(
api_version=api_version,
azure_endpoint=endpoint,
api_key=subscription_key,
)
response = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You are a helpful assistant.",
},
{
"role": "user",
"content": "I am going to Paris, what should I see?",
}
],
max_tokens=4096,
temperature=1.0,
top_p=1.0,
model=deployment
)
print(response.choices[0].message.content)
If using EntraID identity:
import os
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
endpoint = "https://oai-cs-gpt-bsox-zxcv.openai.azure.com/"
model_name = "<model-name>"
deployment = "<deployment-name>"
token_provider = get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default")
api_version = "2024-12-01-preview"
client = AzureOpenAI(
api_version=api_version,
azure_endpoint=endpoint,
azure_ad_token_provider=token_provider,
)
response = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You are a helpful assistant.",
},
{
"role": "user",
"content": "I am going to Paris, what should I see?",
}
],
max_tokens=4096,
temperature=1.0,
top_p=1.0,
model=deployment
)
print(response.choices[0].message.content)
Here is the supported documentation:
https://learn.microsoft.com/en-us/azure/ai-foundry/quickstarts/get-started-code?tabs=python
Feel free to accept it as an answer.
Thankyou for reaching out to to the Microsoft QNA Portal.