Understanding the Azure OpenAI GPT-4 API Chat Role Usage
Published in:2024-11-01 | Category: AI/ML
Words: 1.1k | Reading time: 7min

Azure OpenAI is a service provided by Microsoft that integrates OpenAI’s advanced language models into the Azure cloud platform. It allows developers to access and use OpenAI’s capabilities, such as natural language processing, code generation, and more, through Azure’s infrastructure.

Recently, we have deployed our first version of the OpenAI GPT-4 model into the Azure cloud platform. This model is a powerful natural language model that can generate text based on a given prompt.

What is GPT-4?

GPT-4 is a transformer-based language model that was developed by OpenAI. It is a powerful language model that can generate text based on a given prompt. It has been trained on a large dataset of text and can generate coherent and engaging text that is often considered to be the next big thing in language models.

How can I use the GPT-4 API?

To use the GPT-4 API, you need to follow these steps:

  1. Create an Azure account.
  2. Create a resource group.
  3. Create a new OpenAI resource.
  4. Generate an API key.
  5. Use the API key to make API requests.

1. Create an Azure account

To use the GPT-4 API, you need to have an Azure account. If you don’t have one, you can create one for free by following the steps in the Azure sign-up page.

2. Create a resource group

Create a resource group to organize your Azure resources. To create a new resource group, follow these steps:

  1. Go to the Azure portal.
  2. Click on the Resource groups option in the left-hand menu.
  3. Click on the + Create button.
  4. Enter a name for your resource group and select your subscription.
  5. Click on the Review + create button.

3. Create a new OpenAI resource

To create a new OpenAI resource, follow these steps:

  1. Go to the Azure portal.
  2. Click on the Create a resource button.
  3. Search for OpenAI in the search bar.
  4. Click on the OpenAI resource.
  5. Click on the Create button.
  6. Enter a name for your OpenAI resource and select your subscription.
  7. Select the resource group you created earlier.
  8. Select the pricing tier.
  9. Click on the Create button.

4. Generate an API key

To generate an API key, follow these steps:

  1. Go to the Azure portal.
  2. Click on the All resources option in the left-hand menu.
  3. Search for your OpenAI resource.
  4. Click on the resource.
  5. Click on the Show access keys button.
  6. Copy the Key 1 value.

5. Use the API key to make API requests

To make API requests, we need to include the API key in the request headers. Here’s an example of how to make a request to the GPT-4 API with REST styles.

1
2
3
4
curl $AZURE_OPENAI_ENDPOINT/openai/deployments/gpt-4o/chat/completions?api-version=2023-07-01-preview \
-H "Content-Type: application/json" \
-H "api-key: $AZURE_OPENAI_API_KEY" \
-d '{"messages":[{"role": "system", "content": "There are 5 classifications: Suggestion, Meanless, Compliment, Complaint, Please provide a classification for user input."},{"role": "user", "content": "Does Azure OpenAI support customer managed keys?"},{"role": "assistant", "content": "A classification word"},{"role": "user", "content": "its great and easy to use"}]}'

Place your API key and endpoint in the appropriate variables, and update which deployments model (gpt-4, gpt-4o or other models) and model version of your endpoint is using.

The Azure OpenAI also supports multiple programming languages, including Python, JavaScript, and C#. You can use the API to generate text in your preferred programming language.

GPT-4 Chat Roles

In above message parameter, you may notice thata there are three roles: system, user, and assistant. The GPT-4 API supports three chat roles. Let digger deeper into each role:

System: This role sets the context or guidelines for the conversation. It’s where you can specify instructions or constraints for how the assistant should behave throughout the interaction.

User: This role represents the input from the person interacting with the model. Any questions or prompts posed by the user fall under this role.

Assistant: This role is for the model’s responses. It contains the output generated by the assistant based on the user input and the context provided by the system.

In another word. The system role sets the context, the user role represents the input, and the assistant role contains the output.

Use system to define the conversation’s tone, behavior, or rules.
Use user for all queries or statements made by the person.
Use assistant for the model’s replies.

Example

Let’s say we want to classify the product feedback classification as Suggestion, Meanless, Compliment, Complaint, or Others. We can use the GPT-4 API to generate text based on the given prompt and classify the feedback.

First, we define the context or guidelines to let the assistant know what result we want to achieve. Given below content to the system role.

1
{"role": "system", "content": "There are 5 classifications: Suggestion, Meanless, Compliment, Complaint, Please provide a classification for user input."},

And, we only the OpenAI to reply me the classification word when user input is provided. So we define the assistant role as classification word.

1
{"role": "assistant", "content": "A classification word"}

Now, we can ask the user to provide the feedback and provide the user role.

1
{"role": "user", "content": "its great and easy to use"}

After the conversation, the assistant role will provide the classification word as Compliment. You will notice that there is a piece of json indicates the assistant role content value in the response. The OpenAI gpt-4o model knows “its great and easy to use” is a “Compliment” and provides the classification word as “Compliment”.

1
2
3
4
"message": {
"content": "Compliment",
"role": "assistant"
}
Classification - its great and easy to use
Classification - its great and easy to use

Let’s try another user input.

1
{"role": "user", "content": "I'm in your walls"}
Classification - I'm in your walls
Classification - I'm in your walls

The assistant role will provide the classification word as Meanless. Because this input is not meanful for any product feedback.

Conclusion

In this article, we have explored the role of the Azure OpenAI GPT-4 API and how it can be used to generate text. We have also learned about the chat roles and how to use them to classify the product feedback.

Prev:
Data Analysis Chart by Generative AI
Next:
What is Prompt Engineering? Best Practices and Examples