Call AI Models Using Power FX

Power Fx is the general-purpose, strong-typed, declarative, and functional programming language used across the Microsoft Power Platform. This excel-like formula language helps app makers to create apps using low code, read more about Power FX here.

With the latest announcement, Microsoft is giving another level of possibilities with Power FX. Now, you can bind the AI model to any control using Power FX. You heard it right! any control. Earlier it was possible to use AI with the AI builder controls only. Now, Microsoft has made it easier than ever to add intelligence to your Power Apps directly through Power FX. Let’s see how to use this feature for a text control to detect the language entered.

Turn on the AI models as data sources option

Got App setting and turn on AI model as date source

Figure1: Turn on Ai model as data source

In your power app, under the app settings go to Upcoming features > Preview, and turn on the AI model as data sources.

Add your AI model as Data Source

Under the Data tab, go to Add data and In the AI models list, select a language detection model. To know more about AI models please check the official Microsoft docs here.

Screenshot of how to select your model.

Fig 2: Add your AI model to the APP

Add your Controls

Place a text input and two text labels on the canvas as explained below

  1. Rename the text input to TextInput1.
  2. Select Text label and place it on the canvas.
  3. Rename the text label to Language.
  4. Add another text label by selecting Text label and place it to the right of the Language text label.

Call the AI model using Power FX

Now we can use the power FX formula to call the AI model method. Select the text label you added in step 2 and enter the following Power Fx formula.

First('Language detection'.Predict(TextInput1.Text).results).language
Screenshot of the Power Fx formula.
Fig 3 : Power FX formaula

Now, save and run the app. You can type in random languages in the text box to see the AI model in action.

Screenshot of how to try out the app you created.
Fig 4: language prediction.

Now you have bound the AI model to a normal label control using Power FX.

Formula explained

‘Language detection’.Predict(TextInput1.Text).results returns a table of possible languages detected with the score. The language with the highest score (highest possible match) will be the first item in the table. So, using the function ‘First‘ the most matching language code is set to the label.

Another example of a prediction model can be found here.

Source: https://docs.microsoft.com/en-us/ai-builder/powerfx-in-powerapps

Hope this helps.

Leave a comment