Getting Started with Azure ChatGPT API

Author

Reads 550

Computer server in data center room
Credit: pexels.com, Computer server in data center room

To begin using the Azure ChatGPT API, you'll need to sign up for an Azure account, which can be done for free. This will give you access to the Azure portal, where you can create and manage your API resources.

The Azure ChatGPT API is a cloud-based service that allows you to build conversational AI experiences, and it's free to try for the first 12 months. This means you can experiment with the API without incurring any costs.

To get started, navigate to the Azure portal and search for the ChatGPT API. From there, you can create a new API resource and follow the prompts to set up your account.

Azure ChatGPT API Basics

To get started with the Azure ChatGPT API, you'll need to create a Python app. From the project directory, open the main.py file and add the following code.

The Azure OpenAI chat models are optimized to work with inputs formatted as a conversation. This means you should pass an array of dictionaries with different roles in the conversation delineated by system, user, tool, and assistant.

Credit: youtube.com, Getting Started with Azure OpenAI and GPT Models in 6-ish Minutes

To connect to your Azure Cognitive Search index, you'll need to set up the dataSources variable. This enables Azure OpenAI models to respond using your data.

To trigger a response from the model, you should end with a user message indicating that it's the assistant's turn to respond. This is a crucial step in getting the model to respond.

Here are the key roles in the conversation format:

  1. System
  2. User
  3. Tool
  4. Assistant

These roles are essential for the Azure OpenAI chat models to work effectively.

Authentication and Authorization

To use Azure OpenAI, you need to authenticate your requests. There are four authentication types supported: api_key, connection_string, system_assigned_managed_identity, and user_assigned_managed_identity.

You can authenticate via API key, which is a string of characters used to identify your API requests. This is done by setting the AZURE_OPENAI_API_KEY environment variable.

Alternatively, you can use a connection string, which is a unique identifier for your Azure resource. This is done by setting the AZURE_OPENAI_ENDPOINT environment variable.

Credit: youtube.com, Authentication and Authorization | Cloud Concepts | Azure for Beginners | Like, Share & Subscribe

If you're using a managed identity, you can authenticate via system-assigned or user-assigned managed identity. System-assigned managed identity is a type of identity that's automatically created and managed by Azure, while user-assigned managed identity is a type of identity that's created and managed by you.

Here are the authentication types supported with Azure OpenAI:

Credentials

To authenticate with Azure OpenAI, you'll need to set up your credentials correctly. There are several authentication types to choose from, including API key, connection string, system-assigned managed identity, and user-assigned managed identity.

The API key is a popular choice for authentication, as it's easy to set up and use. You can obtain an API key by creating a deployment in Azure and generating one.

To get started, you'll need to set the AZURE_OPENAI_API_KEY and AZURE_OPENAI_ENDPOINT environment variables. You can do this by following the instructions in the Azure docs.

If you want to enable automated tracing of your model calls, you can also set your LangSmith API key. This is done by uncommenting a specific line of code.

Here are the supported authentication types:

Role

Credit: youtube.com, Role-based access control (RBAC) vs. Attribute-based access control (ABAC)

In authentication and authorization, the role of the author is a crucial aspect to consider.

The role of the author of the response message is specifically defined as the assistant.

Understanding this role is essential for determining the level of access and permissions granted to users.

The assistant role is a clear indication of who is responsible for generating the response message.

In many systems, this role is used to differentiate between human users and automated systems like chatbots or AI assistants.

You might enjoy: Azure B

Model Configuration

To configure the Azure ChatGPT API, you'll need to specify several parameters. The most important ones are the HTTP and HTTPS port numbers, which are set to 80 and 443 respectively.

You'll also need to decide whether to use HTTPS or not, which is controlled by the ENABLE_SSL parameter. If you choose to use HTTPS, you'll need to provide the SSL certificate and private key file paths.

Here are the main configuration parameters you'll need to set:

By setting these parameters, you'll be able to configure the Azure ChatGPT API to suit your needs.

Token Properties

Credit: youtube.com, How to Configure a JWT Token in .NET Core to Never Expire and Remove Default Claims

Token properties are a crucial aspect of model configuration. They determine how the model processes and interprets input data.

In the context of token classification, token properties can be used to identify and categorize specific tokens within a sequence. For example, a token property might indicate whether a token is a noun, verb, or adjective.

Token properties can also be used to determine the type of token, such as a subtoken or a special token. A subtoken is a smaller unit of text that is part of a larger token, while a special token is a token that has a specific meaning or function.

Token properties can be used to adjust the model's behavior based on the type of token. For instance, the model might handle subtokens differently than regular tokens.

Token properties can be set at the model level or at the input level. Setting them at the model level applies the properties to all inputs, while setting them at the input level allows for more flexibility and customization.

Model Features

Elegant portrait of a woman with a creative blurred backdrop, emphasizing facial features and beauty.
Credit: pexels.com, Elegant portrait of a woman with a creative blurred backdrop, emphasizing facial features and beauty.

When configuring your model, it's essential to understand its features. The model supports tool calling, which allows you to integrate it with other tools and services.

The model also offers structured output, enabling you to receive organized and easily interpretable results. This feature is particularly useful for tasks that require specific formatting or categorization.

In terms of input and output, the model is compatible with JSON mode, making it easy to work with data in this format. This is a significant advantage for developers who frequently work with JSON.

The model can also handle image input, which is useful for tasks like image classification or object detection. However, it's not compatible with audio or video input.

One of the model's notable features is token-level streaming, which allows for real-time processing and analysis of text data. This is particularly useful for tasks like sentiment analysis or language translation.

The model also supports native async, enabling you to run tasks asynchronously and improve overall system performance. Additionally, it allows for token usage, which is useful for tasks like text summarization or language modeling.

Finally, the model offers logprobs, which provide detailed information about the model's internal workings and can be used for debugging and optimization purposes.

Here's a summary of the model's features:

Specifying Model Version

Smartphone displaying ChatGPT interface on a vibrant background, showcasing AI technology.
Credit: pexels.com, Smartphone displaying ChatGPT interface on a vibrant background, showcasing AI technology.

Specifying Model Version is crucial when working with Azure OpenAI, as it helps you keep track of which model was used to generate a response.

Azure OpenAI responses don't contain the specific version of the model used, which can lead to issues like incorrect total cost calculations.

This is because the model_name response metadata property only shows the name of the model, not its version. For example, it won't distinguish between gpt-35-turbo-0125 and gpt-35-turbo-0301.

To solve this problem, you can pass the model_version parameter to the AzureChatOpenAI class. This will add the version to the model name in the llm output, making it easy to tell which version was used.

By doing so, you can ensure accurate total cost calculations and avoid potential issues with OpenAICallbackHandler.

Json Schema

Json Schema is a crucial aspect of model configuration. It defines the structure of the response format as a JSON Schema object.

The type of response format being defined is json_schema, which is the only possible value. Json_schema is an object that contains the schema for the response format.

A JSON Schema object is required to define the response format. The type property is always json_schema, and the json_schema property is an object that contains the schema details.

Here is a summary of the required properties for json_schema:

Model in Web App

Credit: youtube.com, ASWAD 2024 Rick van den Bosch : Azure Static Web Apps and Configuration

To deploy the Azure OpenAI model in a web app, you can use the Azure AI Foundry portal or example code provided on GitHub. This method allows you to chat with the model using a web app, and it's available for both models that use your data and those that don't.

You can deploy the web app using Azure app service, which provides a user interface for sending queries. The app can be customized by making changes to the source code.

To get started, you'll need to open the main.py file in your project directory and add the necessary code. This code will pass an array of dictionaries to the model, which is formatted as a conversation. The messages variable delineates different roles in the conversation, including system, user, tool, and assistant.

The dataSources variable connects to your Azure Cognitive Search index, enabling the Azure OpenAI models to respond using your data. To trigger a response from the model, you should end with a user message indicating that it's the assistant's turn to respond.

See what others are reading: Azure Web Api

Businessman reviewing data analytics dashboard on laptop in bright office.
Credit: pexels.com, Businessman reviewing data analytics dashboard on laptop in bright office.

Here are the steps to create the Python app:

  1. Open the main.py file in your project directory.
  2. Add the necessary code to pass an array of dictionaries to the model.
  3. Connect to your Azure Cognitive Search index using the dataSources variable.
  4. End with a user message to trigger a response from the model.

Request and Response

The Azure ChatGPT API allows you to send a chat completion request message, which can be one of several types.

One of the key components of a chat completion request message is the chatCompletionRequestMessage, which can be one of the following: The chatCompletionRequestMessage can be one of the following: message, role, or user.

This component is crucial in determining how the API responds to your request. The API will respond with a message that matches the type of chatCompletionRequestMessage you sent.

Uri Parameters

Uri Parameters are a crucial aspect of making requests to Azure OpenAI endpoints. They provide essential information about the request.

The endpoint parameter is required and specifies the supported Azure OpenAI endpoint. For example, you can use https://{your-resource-name}.openai.azure.com, replacing "your-resource-name" with your actual Azure OpenAI resource name.

The deployment-id parameter is also required and specifies the deployment ID of the model that was deployed.

API version is specified using the api-version parameter, which is required and can be passed as a query string.

Here are the Uri Parameters in a concise table:

Request

Credit: youtube.com, APIs Explained (in 4 Minutes)

A request is a crucial part of the conversation process. It's essentially a message that's sent to the model to initiate a response.

The type of request can vary, but in this case, we're looking at chat completion requests. These requests can be one of three types: chatCompletionRequestMessage, chatCompletionRequestUserMessage, or chatCompletionRequestFunctionMessage.

The chatCompletionRequestMessage is a basic request that can take various forms. The chatCompletionRequestUserMessage, on the other hand, is a more specific type of request that requires certain information to be included, such as the user's message content and role.

The chatCompletionRequestFunctionMessage is another type of request that involves calling a function. This type of request requires the function name, content, and role to be specified.

Here's a breakdown of the required fields for each type of request:

The content field is a crucial part of the request, as it specifies the message or function to be executed. The role field indicates the type of message or function, whether it's a user message or a function call. The name field is also required for function calls, as it specifies the function to be executed.

In summary, the request is a critical component of the conversation process, and understanding the different types of requests and their required fields is essential for effective communication.

Stream and Output

Credit: youtube.com, Demo: Realtime Streaming to Frontend App of Azure OpenAI Service Response

When you're working with the Azure ChatGPT API, you'll often want to stream and output the chat completion responses. This can be especially useful when dealing with large amounts of text.

To stream a chat completion response, you'll need to use the `createChatCompletionStreamResponse` object, which represents a streamed chunk of the chat completion response.

Each chunk has a unique `id`, a list of `choices`, a `created` timestamp, a `model` identifier, and an `object` type. This allows you to keep track of the chat completion's progress and identify any issues that may arise.

Here are the required fields for `createChatCompletionStreamResponse`:

You can also use the `chatCompletionStreamOptions` object to customize the streaming response. This object has an `include_usage` field that, when set to `true`, will include an additional chunk with token usage statistics before the data.

Create Stream

To create a stream, you'll need to generate a chat completion message. A chat completion message generated by the model is represented by the "createChatCompletionStreamResponse" object.

Modern data center corridor with server racks and computer equipment. Ideal for technology and IT concepts.
Credit: pexels.com, Modern data center corridor with server racks and computer equipment. Ideal for technology and IT concepts.

This object contains several key pieces of information, including a unique identifier for the chat completion, which is the same for each chunk of the response. The "id" field is a string that serves as a unique identifier.

The "choices" field is an array that contains a list of chat completion choices. This list can contain more than one element if the number of choices is greater than 1.

The "created" field is an integer that represents the Unix timestamp in seconds when the chat completion was created. Each chunk of the response has the same timestamp.

You can use the "model" field to specify the model used to generate the completion. The "system_fingerprint" field is optional and represents the backend configuration that the model runs with.

Here are the required fields for the "createChatCompletionStreamResponse" object:

Output

To output the results of a chat completion, you'll need to use a JSON format that's suitable for many scenarios. This format includes both answers to your query and citations from your uploaded files.

Young man intensely focused on computer work in a tech office setting with a whiteboard in the background.
Credit: pexels.com, Young man intensely focused on computer work in a tech office setting with a whiteboard in the background.

You can expect the output to be a JSON object with several key-value pairs, including the model used to generate the completion and the system fingerprint, which represents the backend configuration.

The model used to generate the completion is a string that identifies the specific model used, such as a language model or a chat model.

The system fingerprint is a string that represents the backend configuration, which can be used to understand when backend changes have been made that might impact determinism.

Here are the required fields for the output:

Note that the system fingerprint is not required, but it can be included for additional context.

Environment and Configuration

To get started with the azure chatgpt api, you'll need to set up the environment and configuration correctly. The first step is to create and assign persistent environment variables for your key and endpoint. You can find the required variables in the "Retrieve required variables" section, which includes values like AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_API_KEY, and AZURE_OPEN_AI_DEPLOYMENT_ID.

Credit: youtube.com, How to setup Azure OpenAI service and use it in Python

You can also use environment variables to store sensitive information like API keys and endpoint URLs. For example, you can use the SPRING_AI_AZURE_OPENAI_MODEL variable to specify the model name, which defaults to gpt-35-turbo.

Here are the environment variables you'll need to set up:

By setting up these environment variables, you'll be able to access the azure chatgpt api and start using it to build your chatbot or conversational interface.

Environment Variables

Environment Variables are a crucial part of configuring your Azure OpenAI Service. You can create and assign persistent environment variables for your key and endpoint.

To start, you'll need to create environment variables for your key and endpoint. This can be done using the SPRING_AI_AZURE_OPENAI_KEY and SPRING_AI_AZURE_OPENAI_ENDPOINT variables. You can find the values for these variables in the Azure portal, specifically in the Keys & Endpoint section when examining your Azure OpenAI resource.

Here are the environment variables you'll need to set:

Note that you can use either KEY1 or KEY2 for the SPRING_AI_AZURE_OPENAI_KEY variable, as you can securely rotate and regenerate keys without causing a service disruption.

Extension Configuration

Diverse team collaborating in an office setting with technology and reports.
Credit: pexels.com, Diverse team collaborating in an office setting with technology and reports.

Extension Configuration is a crucial aspect of Azure OpenAI chat extensions. It determines how the chat extension behaves and interacts with the user.

The type of configuration data is represented by the azureChatExtensionConfiguration, which is specifically designed for Azure OpenAI chat extensions. This configuration is required for chat completions requests that need to use Azure OpenAI chat extensions.

Azure OpenAI chat extensions can also be configured for Azure Cosmos DB, using the azureCosmosDBChatExtensionConfiguration. This configuration includes parameters for Azure Cosmos DB, such as authentication options and database settings.

The azureCosmosDBChatExtensionParameters configuration includes various settings for Azure Cosmos DB, including authentication options, database settings, and field mapping options. These settings are essential for configuring Azure OpenAI On Your Data chat extensions when using Azure Cosmos DB for MongoDB vCore.

Here are the required fields for azureCosmosDBChatExtensionParameters:

These fields are essential for configuring Azure OpenAI On Your Data chat extensions when using Azure Cosmos DB for MongoDB vCore.

Create Python App

Credit: youtube.com, Python Virtual Environments - Full Tutorial for Beginners

To create a Python app, you'll need to add the following code to your main.py file. The Azure OpenAI chat models are optimized to work with inputs formatted as a conversation, so you'll want to pass an array of dictionaries with different roles in the conversation.

The messages variable should be an array of dictionaries with system, user, tool, and assistant roles. This is necessary for the model to understand the context of the conversation. For example, you might have a dictionary like this: {"role": "user", "content": "Hello, how are you?"}.

To connect to your Azure Cognitive Search index and enable Azure OpenAI models to respond using your data, you'll need to specify the dataSources variable. This is where the model will draw from to generate its responses.

To trigger a response from the model, you should end with a user message indicating that it's the assistant's turn to respond. This is typically done by including a dictionary like this: {"role": "assistant", "content": "I'm here to help."}.

Additional reading: Azure User

Region Availability

Credit: youtube.com, Disaster Recovery vs High Availability vs Fault Tolerance Azure | Azure Tutorial

The responses API is currently available in 13 regions, including Australia, France, Japan, and the UK.

You can use the API in regions such as australiaeasteastuseastus2francecentraljapaneastnorwayeastpolandcentralsouthindiaswedencentralswitzerlandnorthuaenorthuksouthwestuswestus3.

These regions support various models, including gpt-5, gpt-5-mini, and gpt-4o.

Some of the specific models available in these regions include gpt-5 (Version: 2025-08-07)gpt-5-mini (Version: 2025-08-07)gpt-5-nano (Version: 2025-08-07)gpt-5-chat (Version: 2025-08-07)gpt-4o (Versions: 2024-11-20, 2024-08-06, 2024-05-13)gpt-4o-mini (Version: 2024-07-18)computer-use-previewgpt-4.1 (Version: 2025-04-14)gpt-4.1-nano (Version: 2025-04-14)gpt-4.1-mini (Version: 2025-04-14)gpt-image-1 (Version: 2025-04-15)o1 (Version: 2024-12-17)o3-mini (Version: 2025-01-31)o3 (Version: 2025-04-16)o4-mini (Version: 2025-04-16).

Curious to learn more? Check out: Azure Api Version

Advanced Topics

In Azure ChatGPT API, you can integrate custom models with the API by using the "Models" endpoint, which allows you to upload and manage your own models.

This endpoint supports models in the form of JSON files, and you can use it to upload your custom models for use with the API.

The API also supports model versioning, allowing you to manage different versions of your custom models and easily switch between them.

You can use the "Models" endpoint to manage your custom models, including updating and deleting them.

Credit: youtube.com, Complete Generative AI With Azure Cloud Open AI Services Crash Course

The API provides a robust set of tools for model management, making it easy to integrate your custom models into your applications.

Custom models can be used to improve the accuracy and relevance of the API's responses, making it a valuable feature for developers.

The API's model management capabilities are designed to be flexible and scalable, making it easy to integrate custom models into large-scale applications.

You can use the API's model management features to create and manage multiple custom models, each with its own unique characteristics and capabilities.

Frequently Asked Questions

What is the difference between Azure OpenAI and ChatGPT API?

Azure OpenAI offers customizable AI models for various uses, whereas ChatGPT API provides a standalone solution for creating human-like conversations. If you're looking to build tailored AI experiences, Azure OpenAI is the way to go.

Ismael Anderson

Lead Writer

Ismael Anderson is a seasoned writer with a passion for crafting informative and engaging content. With a focus on technical topics, he has established himself as a reliable source for readers seeking in-depth knowledge on complex subjects. His writing portfolio showcases a range of expertise, including articles on cloud computing and storage solutions, such as AWS S3.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.