Integrating Open AI & PowerPlatform
Goal :
Build an app using Power Platform and Open AI.
Use case :
An in-House Translator App using canvas app as a front end.
Power automate connecting the Canvas App and Azure Open AI
Pre-requisites :
- Open AI with Credits
- PowerAutomate For connecting to the Open AI
- Power Apps For UI (Powerapps.com)
- Need premium license with access to the Premium connectors.
Open AI with Credits.
For this demo we are using Open AI.
- Sign up or Login into Open AI. (https://platform.openai.com/)
- Access your account page. (https://platform.openai.com/account)
- Click on the Billing tab.
- Ensure you have credits. If not add credits.
- Click on the API Keys tab.
- Create a new key if you already don’t have the key. (https://platform.openai.com/account/api-keys). Ensure to note down to the key as you will not be able to see once you have generated.
Now that we’ve the key, it is good to know about the models and pricing. You could find the pricing by accessing the URL https://openai.com/pricing
For brevity purpose, just mentioning the below :
GPT 4 : GPT-4 can follow complex instructions in natural language and solve difficult problems with accuracy.
GPT 3.5 Turbo : GPT-3.5 Turbo models are capable and cost-effective.
Embedding Models — Converts text numerical values for advanced search, clustering, topic modeling, and classification functionality
For this demo we will be using GPT 3.5 turbo considering the simplicity and cost. In future we will be covering the GPT 4.
GPT 3.5 costs at $ 0.0015 for input and $0.002 for output per 1k token.
Tokens can be thought of as pieces of words that is being sent as input for processing.
1–2 Sentences can approximately have 30 tokens.
PowerApp UI
First Step, Lets create a new canvas app for our Translator’s UI.
It is going to be a Simple UI.With Two Multiline Text Boxes
+One Text Box for the source English Text and another Text box for the Translated Text
Dropown with 3 non English Languages,For this demo, I m picking Spanish,Arabic and Hindi.
And a button that will translate the Text on Demand.
Power Automate
To connect to Open AI, we will be making use of the Power Automate Flows.
Choose the trigger for this flow as PowerApps V2.
Add Two Parameters one for the source text and another for the Language. These will be obtained from the UI. Meaning Canvas app will trigger this flow with Source Text and the Language it will need to convert to.
HTTP Request
We will be using the POST Method that will hit the Open AI API endpoint with necessary headers and body.
Endpoint : https://api.openai.com/v1/chat/completions
{
"model": "gpt-3.5-turbo",
"n": 1,
"messages": [
{
"role": "system",
"content": "You are a Language Translator. You convert the user text to the language <DYNAMIC VALUE>"
},
{
"role": "user",
"content": "<DYNAMIC Value>"
}
]
}
The body contains JSON which contains Model,n and Messages.
Model — Specifies the model. You could use gpt-4 as well if needed. I have used the gpt-3.5-turbo.
“n” — Signifies number of responses for given prompt.
Messages :
This is like a chat object. You can pass all the context of the conversation so far.
System Role signifies all the instruction that the model has to follow.
User Role can contain information from the user or basically our input in this case.
Here the system instruction is to translate the user’s text to a specified language. The user’s text and language is dynamically pulled.
Parse JSON
You will need to provide the Schema of the response.
{
"type": "object",
"properties": {
"id": {
"type": "string"
},
"object": {
"type": "string"
},
"created": {
"type": "integer"
},
"model": {
"type": "string"
},
"choices": {
"type": "array",
"items": {
"type": "object",
"properties": {
"index": {
"type": "integer"
},
"message": {
"type": "object",
"properties": {
"role": {
"type": "string"
},
"content": {
"type": "string"
}
}
},
"finish_reason": {
"type": "string"
}
},
"required": [
"index",
"message",
"finish_reason"
]
}
},
"usage": {
"type": "object",
"properties": {
"prompt_tokens": {
"type": "integer"
},
"completion_tokens": {
"type": "integer"
},
"total_tokens": {
"type": "integer"
}
}
}
}
}
Storing the value in Variable :
Sending the Response back to the App
Add the flow to the Canvas App.
Open the Canvas App, Click on the Power Automate Tab. Click on the Add Flow button click on the created Flow.
On the “Button.OnSelect” Property of the Translate Button, we triggering the flow and setting the output to the variable _translatedText.
Set(_translatedText,TranslatorV1.Run(TextInput1.Text,Dropdown1.SelectedText.Value))
Set the default property of the Translated Text box to _translatedtext.output.
Translator App is now ready.