Blog Post

Azure Integration Services Blog
3 MIN READ

Integrate Azure Open AI in Teams Channel via Logic App

Drac_Zhang's avatar
Drac_Zhang
Icon for Microsoft rankMicrosoft
Mar 23, 2023

Summary

Now in Azure, we can deploy Open AI with chatgpt-35-turbo model as an Azure resource, so this blog introduces how to integrate the Azure Open AI in Teams channel without using a bot.

 

Prerequisite

  1. Azure Open AI resource with a deployment which using "chatgpt-35-turbo".
     
  2. Azure Storage Account with a blob container which named "gpt". For Open AI conversion, if we need to enable the multi-turn conversations, we need to provide the whole conversation (channel replies) histories for Open AI API call. But in Logic App, we don't have built-in action for get all the replies of a channel message, so we need to use blob storage to save the conversation history based on message IDs.
     
  3. Prepare Teams channel ID and group ID in advance, you can get them via right on Channel and select "Get link to channel".   
    Once we have the URL, we need to do a URL decode (or just replace %3a as ':' and %40 as '@', otherwise the Logic App cannot get correct channel ID) to get actual channel and Group ID. In the following screenshot, the green section is the channel ID and the red one is the group ID.

Mechanism

  1. Logic App is monitoring the keywords posted in specific channel via trigger "When keywords are mentioned". In my case, I'm using ChatGPT as keyword, so once the messages/replies contian "ChatGPT", it will trigger Logic App.
  2. If this is a new message, Logic App will create a new blob for saving conversation history based on message ID (it is ReplyToId in Teams connector response). Otherwise it reads the existing blobs and re-use the conversation.
  3. Compose the content as Open AI payload and send request, transfer the response to specific Teams channel.
  4. Update blob with the latest conversation.

Template and Parameters Explanation

The sample template link: Drac-Zhang/LogicApp_For_Teams_OpenAI_Integration (github.com)

Parameters of the template:

Parameter Name Comments
openai_apikey The api key of Open AI resource, it can be found in Open AI -> Keys and Endpoints
openai_endpoint

Open AI api endpoint, the format is https://[Open AI resource Name].openai.azure.com/openai/deployments/[Deployment name]/chat/completions?api-version=2023-03-15-preview

The version must be 2023-03-15-preview since the previous version is using a different payload JSON schema

teams_channel_keyword The keywords you would like to trigger the Logic App, not case sensitive
teams_channel_id See prerequisite No.3
teams_group_id See prerequisite No.3
storage_account_name The storage account name for saving conversation history
storage_account_accesskey The access key for the storage account

 

Known Issues

  1. After the deployment, the Teams API connector need to be authorize manually.
  2. Logic App need to be disable and re-enable for registering the webhook on Teams side
  3. If the response from Open AI contains the keywords, then the Logic App will be trigger again
  4. If the response contains double quotas, then the next reply will failed due to invalid JSON format, but it can be easily fixed by replace expression

Sample Chat

 

Reference

How to work with the ChatGPT and GPT-4 models (preview) - Azure OpenAI Service | Microsoft Learn

 

Updated May 17, 2023
Version 3.0
  • Hi Razec ,

    I had the very same issue, and to fix it I did the following:

     

    1. Go to Logic app Designer and select the module "When keywords are mentioned"

    2. Under channels Channel-1 I removed the function which appeared there, and the X icon to close it, and a dropdown list appeared with all the channels from my Teams. I selected there the one I want to use.

    3. Click Save. 

    3.1 Here I wasn´t able to save because my API connection to the storage account failed. So I wen to API connections under Development Tools and selected my azureblob connection.

    3.2 I went to Edit API connection and selected Access Key option. Filled the fields requested and save.

    3.3 From overview tab, disabled and enabled the Logic App again.

     

    That worked for me. Hope it helps.

  • AdamLimedev I'm not sure about that, basically in Logic App, it is just calling the Azure Open AI api, if the api can do that, then I believe it should be possible. But might need to change the workflow a bit.

  • AdamLimedev's avatar
    AdamLimedev
    Copper Contributor

    Hi,

    thank you for a great example. Can I feed chat with my own data? It would be useful to ask about specific PDFs - I am thinking company chat? HR department could really use this kind of chatbot.

    Thanks,

    Adam.

  • Razec's avatar
    Razec
    Copper Contributor

    Hi Drac_Zhang I just deployed your code, and I'm getting this error, is anyone else having this problem?  thanks!

     

    ExpressionEvaluationFailed. The execution of template action 'For_each_3' failed: the result of the evaluation of 'foreach' expression '@triggerBody()?['value']' is of type 'Null'. The result must be a valid array.

     

    thanks

  • BartlomiejP's avatar
    BartlomiejP
    Copper Contributor

    Drac_Zhang  Hi, do you have any idea what can be a workaround for tenant error? Is changing region for account enough or it's based on the ms teams region?

    "error": {
    "code": "UnsupportedTenantRegion",
    "message": "Tenant home region 'pl' is not supported."
    }

  • RobinMalik you just need to login in with the different account which you would like to use for reply. Meanwhile, by default, it is reply as "bot" with current template.

  • RobinMalik's avatar
    RobinMalik
    Copper Contributor

    How would you set this up to reply as a different user/identity, and not yourself? Is it possible with the LogicApp method?