Power Automate: Integrate GPT-3/OpenAI in Microsoft Teams


It’s the new kid on the block and you can’t stop its rise to fame and the hype train! ChatGPT has generated a lot of attention lately, both positive and negative and it’s already being used by students and content writers to speed up their own work. It’s a buzzword that has been echoed as much as “cloud” was a few years ago (*puke*). And today we are going to learn how we can integrate and use the OpenAI API in Teams by building a Power Automate flow. You know… Just in case you can’t get enough of it in LinkedIn, let’s make it even more available into your own environment… 😂

Our goals:

  • Learn how you can easily setup your OpenAI API account.
  • Learn the basic requirements to send a request to OpenAI API and work with the responses you get back.
  • Learn how to create an integration between OpenAI GPT2-service and a dedicated channel in Microsoft Teams by leveraging Power Automate’s Teams-connector.

Prerequisites:

  • An OpenAI API account registered from – OpenAI API
  • Power Automate Premium license to use HTTP actions.
  • An active Office 365 account with access to create new/existing Teams channels


Create a new dedicated Teams channel (as administrator/owner)

Click on the three dots next to the Teams you want your new channel to be.

Give it a suitable name, and delegate it either to certain teams or publicly… In my case I’ll leave it for the public.


Create your OpenAI API account and token secret

Navigate to https://openai.com/api and create a new account by signing up, if you haven’t done so.

After registration, you need to verify your mail-account in which they will send you a verification in your mailbox. On your first login, you’ll be prompted to provide your personal details and phone no.

Login and navigate to your personal profile menu at the top right corner and click on “View API Keys
Create a new secret key, and make sure to copy and store this key in a safe place. Take note that each secret keys will last around 3 months according to the documentation (as of date 15.01.2023)

Create the Power Automate flow for integration to a Teams channel

For our simple scenario, create a new Power Automate flow that starts with a trigger – called “When a new channel message is added” and set your preferred Teams + Channel.

Continue with an HTTP action and fill out as seen above. A copy of the details is written below.

URI:

https://api-openai.com/v1/completions

Headers:

Authorization: Bearer {token secret}

Body:

{
  "model": "text-davinci-003",
  "prompt": "@{triggerOutputs()?['body/body']}",
  "max_tokens": 2048,
  "temperature": 0.5
}

A few explanations and context for our body:

  • model: Read more about model value here – Models – OpenAI API – short version: The higher the performance models are the more expensive. You can also list the available models to try out through its own HTTP request: GET https://api.openai.com/v1/models
  • max_tokens: Read more about max_tokens value here – What are tokens and how to count them? | OpenAI Help Center – short version: token represents a number count of either words or sentences. The limit is counting both the prompt (your question) + completion (the text result returned) < 4000 (as long as it’s below 4000 token counts together)
  • temperature: Read more about temperature combinations here – API Reference – OpenAI API – A measure of how often the model outputs a less likely text/token. The higher the temperature, the more random (and usually creative) the output. This, however, is not the same as “truthfulness”. For most factual use cases such as data extraction, and truthful Q&A, the temperature of 0 is best.

Now save and trigger the flow once from your Teams channel (ask about anything) and wait for a response, as we want to parse the JSON values from the returning body.

Post in the “Ask ChatGPT” channel to trigger the flow and go back to the run log to fetch the HTTP output information received back.

Copy the Body value outputs and go back to editing your flow.

Now we parse the response from the call, by specifying the body and click generate from schema.

Paste in the copied value and click done.

Your JSON schema is now defined to access the key-values for the rest of the flow.
To complete the process, add the Teams action “Reply with a message in a channel” and fill out the fields as shown. (Note that an “Apply to each” will wrap the action, as the “text” is sent as part of an array of other key-values.)

You can now save your flow one last time, as this is all that is required for this example, and you can use your Teams channels for text-based responses from OpenAI/ChatGPT. Remember, it does support multiple languages (if not all).

Example.

Summary

We’ve learned how to create an integration between ChatGPT/OpenAI and Microsoft Teams, by leveraging the Power Automate tool.

The flow result:

While this is a simple flow, Power Platform’s services and its ecosystem of connectors can be added to build more complex automation (E.g. Power Virtual Agents), depending on your project goals. This is just an example of how the OpenAI API can be used together with Azure and Microsoft 365 services – and even combine with other public facing APIs. Unfortunately, Microsoft Teams connector in this tutorial doesn’t have the most responsive triggers as it only scans new messages every 3 minutes. Because of this limit I sometimes ask in bulks and sips on my cup of tea while waiting… I could however replaced it’s trigger with “When keywords are mentioned“… But it will do just fine for me – for now! 😎

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *