Using Azure OpenAI Chat Completion API with Power Apps

2 minute read

Using Azure OpenAI Chat Completion API with Power Apps

Overview

Azure OpenAI service offers text generation using models like GPT. They are trained to generate a response or content from a prompt supplied to them. We can use this offering in our Power Apps app to create automated responses, generate texts, and more.

In this article, we will explore how to integrate the Azure OpenAI service text generation with GPT in Power Apps.

Set up Azure OpenAI Service

Access to Azure OpenAI is on request basis. If you do not have one, you need to raise a request at: https://aka.ms/oai/access.

From the Azure portal, find and create a resource, Azure OpenAI.

Deploy a model

Once the Azure OpenAI service is ready, deploy gpt-35-turbo model.

Once the model is deployed, click open the chat playground to test it out. Clicking View code will show you the options to start integrating your current prompt and settings into your application.

The sample code makes it clear that we can use the REST APIs to consume Azure OpenAI in our applications.

Create Custom Connector

As we know the custom connector is a wrapper around a REST API, we will build one for Azure OpenAI chat completion endpoint to consume it in Power Apps.

Follow the below steps to create a custom connector.

  1. Navigate to Power Automate.
  2. Under Data, click Custom connectors.
  3. Click New custom connector > Create from blank.

  4. Name it as “Azure OpenAI Connector”.
  5. Under General section, specify the Host as openai.azure.com.

  6. Under Security tab, specify the Authentication type as API Key.

  7. Under Definition tab, click New action to add an action for chat completion API.

  8. Under Request, click Import from sample.

  9. You can use the tool like Postman to analyze the output by passing headers and body.

  10. On the custom connector screen, under Response, click Add default response. Specify the Body from the Postman query output.

  11. Click Create connector to finish creating the custom connector.
  12. Test the connector before using it.

If you do not want to create a custom connector, alternatively you can use the custom connector available at: https://github.com/microsoft/PowerPlatformConnectors/blob/dev/custom-connectors/AzureOpenAIService/.

Integrate model with Power Apps app

We will now use the AzureOpenAIService connector available on Github.

  1. Create a Blank canvas app.
  2. Under Data, click Add data and search for AzureOpenAI connectors. This is an Independent Publisher Connector.

  3. Add text input, button, and label on the canvas to form a layout.

  4. On the Screen1 > OnVisible event create a global variable name labelText.

     Set(labelText, Blank())
    
  5. On the Button > OnSelect event write the code below.

     Set(labelText, AzureOpenAIService.ChatCompletion("gpt-35-turbo", "2023-03-15-preview", {
         messages: [
             { role: "system", content: "You are an AI assistant that sarcastically answer the questions." }
         ],
         user_message: TextInput.Text
     }).answer)
    
  6. Set the Text property of LabelAnswer to the global variable labelText.

Test the App

As we have created a prompt to let the bot sarcastically answer the questions, you will have a fun testing it out.

Scenario 1:

Scenario 2:

Scenario 3:

Summary

Azure OpenAI service offers text generation using models like GPT. They are trained to generate a response or content from a prompt supplied to them. We can build or use custom connector to surface the REST APIs to consume Azure OpenAI in our applications.

References

Leave a comment