“I hear a lot about ChatGPT lately, can I use it to improve my Power Automate flows, to simplify some otherwise complex tasks?”
Given the recent popularity of AI, especially ChatGPT, we can expect that it’ll stay with us. Which is a good thing, if you learn how to use it.
When you build a Power Automate flow, you often process some data. The more complicated the requirement, the more steps/expressions it might take to get the desired result. But what if it wasn’t necessary? If you could just ask AI and get the result you want? And since ChatGPT is the most popular one, how can you use it in your flow?
Note: this solution is using the generic HTTP request action, therefore, you need Power Automate premium license. There’re also some costs associated with using ChatGPT.
Get an authentication token
Before you can communicate with ChatGPT, you must create an account. Go the OpenAI site and ‘Get started’.
Create a new account or select an existing one to continue. Since you want to call ChatGPT from a flow, go to the API section.
The AI needs to know that you’re accessing it, that you’re a valid, registered user – that’s why you’ll need your personal token. Go to API keys and create a new secret key. That’s a key you’ll need for the flow.
Use it in a flow
Once you have the key, you can start sending HTTP requests to ChatGPT. Use the latest endpoint as the URI, at the time of this article it’s as below:
Method: POST
URI:
https://api.openai.com/v1/chat/completions
Headers:
{
"Content-Type": "application/json",
"Authorization": "Bearer <secretKeyFromAbove>"
}
The request body needs only 2 parameters. The first one is which model you want to use. There’re two models available at this endpoint: gpt-4 and gpt-3.5-turbo. Try to use gpt-3.5-turbo as it’s more cost efficient and will handle most of your requests. If it doesn’t give you the desired results, you can always switch to gpt-4 later.
The second parameter is the message, your question/request towards the AI. Since you want to interact directly as a user, not telling the AI to reply in a specific manner, use the role ‘user’, e.g.
{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "Tell me something about Tom Riha and his Power Automate experience"
}
]
}
Wait until it finishes and extract the response from the HTTP request output with an expression.
body('HTTP')?['choices'][0]?['message']?['content']
Use it to do more complex tasks in a flow
Until now it was just a simple question – answer communication. What about the more complex data processing mentioned at the beginning of the article?
When you’re communicating with ChatGPT, you can send not only the questions, but also the data with some instructions. You can send it an email content and tell it what you’d like to extract from it, or data from a file, as in the next example.
In the past I wrote an article on processing Teams attendance report to get the participants and how long they attended the meeting. It’s quite a complex solution using a lots of data operations – extract specific rows/columns, convert time, etc.
Now you can ask ChatGPT to do all the data processing with a single request.
Describe the request, add the content of the file, and send in the same HTTP request.
Note: my Teams attendance report is in Czech language, but I hope you can understand the request anyway.
There’s just one difference when extracting the data here. I asked for an array of JSON objects, hence I must convert the output to JSON to clean it.
json(body('HTTP')?['choices'][0]?['message']?['content'])
Summary
As you can see, it’s quite easy to use ChatGPT in a Power Automate flow. It’s not for free as you need Power Automate premium license and the interaction with ChatGPT will also cost you a few cents, but as a result you can build much simpler flows that can automate a lot more of your daily tasks.