Azure Communication Services Blog articles

Azure Communication Services Blog articles

https://techcommunity.microsoft.com/t5/azure-communication-services/bg-p/AzureCommunicationServicesBlog

Azure Communication Services Blog articles

Get insights from customer interactions with Azure Communication Services and Azure OpenAI Service

Published

Get insights from customer interactions with Azure Communication Services and Azure OpenAI Service

Customer conversations are a gold mine of information that can help service providers create a better customer experience. When a customer contacts a service provider, they are often routed between different departments and speak to different agents to address their concerns. During the conversation, the customer is usually asked to provide specifics around their issue. Service providers do their best to solve the issue. With Azure Communication Services and Azure OpenAI Service, we can automatically generate insights from the customer call including topic, summary, highlights, and more. These insights can help service providers during the call to get the necessary context to help the customer as well as organizations to learn about their customer support experience.  

 

You can access the completed code for this blog on GitHub.

 

Architecture 

Before we get started, we will quickly review the proposed architecture of our solution. We will leverage a console application in Python as our “orchestrating service” that has access to an Azure Communication Services resource. This console application will generate the chat threads from which we will generate insights. In a more complex architecture, this console application would be replaced by a service that is used by the clients to manage the creation and access to chat threads. The console application will then connect with Azure OpenAI Service to generate insights based on the transcribed thread and provided prompts. 

 

kdunning110_0-1680212763469.jpeg

Pre-requisites 

To get started we will need to: 

  • Create an Azure account with an active subscription. For details, see Create an account for free. 
  • Create an Azure Communication Services resource. For details, see Quickstart: Create and manage Communication Services resources. You'll need to record your resource endpoint and connection string for this quickstart. 
  • Create an Azure OpenAI Service resource. See instructions.   
  • Deploy an Azure OpenAI Service model (Can use GPT-3, ChatGPT or GPT-4 models). See instructions.  
  • Install Python 3.11.2 
  • Install dependencies with pip: pip install openai azure.communication.chat azure.communication.identity

Setting up the console application

Open your terminal or command windows, create a new directory for your application and go to it.

 

 

mkdir chatInsights && cd chatInsights

 

 

In your favorite text editor, create a file called chatInsights.py and save it to the folder we created before: chatInsights. In the file, configure the imports you will need for this project. Add a connection string and endpoint for Azure Communication Services which can be found on the Azure Portal under your Azure Communication Services resource.

 

 

from azure.communication.chat import ChatClient, CommunicationTokenCredential, ChatMessageType,ChatParticipant from azure.communication.identity import CommunicationIdentityClient, CommunicationUserIdentifier from datetime import datetime connection_string = "INSERT AZURE COMMUNICATION SERVICES CONNECTION STRING" endpoint = "INSERT AZURE COMMUNICATION SERVICES ENDPOINT" client = CommunicationIdentityClient.from_connection_string(connection_string) identity1 = client.create_user() token_result1 = client.get_token(identity1, ["chat"]) identity2 = client.create_user() token_result2 = client.get_token(identity2, ["chat"])

 

Configuring the chat client

For this example, we will generate a summary from a pre-populated chat thread. In a real-world scenario, you would be accessing existing threads that have been created on your resource. To create the sample thread, we will start by creating two identities that will be mapped to two users. We will then initialize our chat client with those identities. Finally, we will populate the chat thread with some sample messages we have created.

 

To set up the chat clients we will create two chat participants using the identities we had generated. We will add the participants to a chat thread.

 

 

Agent = ChatParticipant(identifier=identity1, display_name="Agent", share_history_time=datetime.utcnow()) Customer = ChatParticipant(identifier=identity2, display_name="Customer", share_history_time=datetime.utcnow()) participants = [Agent, Customer ] chat_client1 = ChatClient(endpoint, CommunicationTokenCredential(token_result1.token)) chat_client2 = ChatClient(endpoint, CommunicationTokenCredential(token_result2.token)) topic="Support conversation" create_chat_thread_result = chat_client1.create_chat_thread(topic, thread_participants=participants) chat_thread_client1 = chat_client1.get_chat_thread_client(create_chat_thread_result.chat_thread.id) chat_thread_client2 = chat_client2.get_chat_thread_client(create_chat_thread_result.chat_thread.id)

 

 

Next we will populate the chat thread with some sample conversation between a customer support agent and a customer chatting about a dishwasher.

 

 

agentText = [ "Thank you for calling our customer service hotline. How may I assist you today?", "I'm sorry to hear that. Can you provide me with your name and account number, so I can look up your account and assist you better?", "Thank you. Can you please tell me a little more about the problem you're having with your dishwasher?", "I see. That can be frustrating. Can you please check if the dishwasher is getting enough water supply? Also, please check if the spray arms inside the dishwasher are properly attached and not clogged.", "Alright. Please check the spray arms and see if they're clogged or damaged in any way. Also, make sure they're attached properly.", "That could be the reason why the dishes aren't getting cleaned properly. Can you please try to reattach the spray arm and run the dishwasher again?", "Great to hear that! Is there anything else I can help you with?", "You're welcome. Don't hesitate to call us back if you have any more issues. Have a nice day!" ] customerText = [ "Hi, I'm having trouble with my dishwasher. It doesn't seem to be cleaning the dishes properly.", "Yes, my name is Lisa and my account number is 12345.", "Well, it seems like the dishwasher isn't spraying enough water to clean the dishes properly. Some of the dishes are coming out dirty even after I run the dishwasher.", "I've checked the water supply and it seems to be okay. But I haven't checked the spray arms yet.", "Okay, let me check. Yes, it seems like one of the spray arms is loose and not attached properly.", "Sure, let me try that. Okay, I reattached the spray arm and ran the dishwasher again. It seems to be working fine now. The dishes are coming out clean.", "No, that's all. Thank you for your help.", "Bye." ] for x in range(len(agentText)): chat_thread_client1.send_message(content= agentText[x], sender_display_name="Agent", chat_message_type=ChatMessageType.TEXT) chat_thread_client2.send_message(content= customerText[x], sender_display_name="Customer", chat_message_type=ChatMessageType.TEXT)

 

Generating a conversations transcript

Now that the thread is generated, we will convert it into a transcript that can be fed to the Azure OpenAI Service model. We will use the list_messages method from our chat client to get the messages from the thread. We can select a date range for the messages we want. In this case we will get the last day. We will store each message into a string.

 

 

from datetime import datetime, timedelta start_time = datetime.utcnow() - timedelta(days=1) messages = [] chat_messages = chat_thread_client1.list_messages(results_per_page=1, start_time=start_time) for chat_message_page in chat_messages.by_page(): for chat_message in chat_message_page: if(chat_message.type == ChatMessageType.TEXT): messages.append(chat_message) # didn't know I had to filter out other messages prompt = "" for m in range(len(messages)-1, -1, -1): prompt = prompt + messages[m].sender_display_name + ": " + messages[m].content.message + "\n" print(prompt)

 

Summarize the chat thread

Finally, we will use the transcript of the conversation to ask the Azure OpenAI Service to generate insights. As part of the prompt to the GPT model, we will ask for:

  • A generated topic for the thread
  • A generated summary for the thread
  • A generated sentiment for the thread

 

import os import requests import json import openai openai.api_key = "INSERT YOUR AZURE OPENAI API KEY" openai.api_base = "INSERT YOUR AZURE OPENAI ENDPOINT" # your endpoint should look like the following https://YOUR_RESOURCE_NAME.openai.azure.com/ openai.api_type = 'azure' openai.api_version = '2022-12-01' # this may change in the future deployment_name='INSERT YOUR DEPLOyMENT NAME' #This will correspond to the custom name you chose for your deployment when you deployed a model. # Send a completion call to generate an answer start_phrase = 'For the following conversation, extract a topic, summary, highlights (1-3 bullet points of key information) and the sentiment of both of the users.\n\n' + prompt response = openai.Completion.create(engine=deployment_name, prompt=start_phrase, max_tokens=500) text = response['choices'][0]['text'].replace('\n', '').replace(' .', '.').strip() print(start_phrase + '\n' + text)

 

Run it

Now that everything is ready, we will run it. The application will create threads, populate it with messages, and then export them to summarize them. Remember, in a production environment the threads will be populated directly by your users and thus you will only need to focus on extracting the messages and summarizing them. To run the full code we generated run the following command within the ChatInsights folder.

 

 

python chatInsights.py

 

 

Once you run the application you will see insights generated by Azure OpenAI Service from the conversation.  You can leverage these insights to better inform your service providers on the context of a conversation or after the fact to help inform your customer support experience.

 

 

For the following conversation, extract a topic, summary, highlights (1-3 bullet points of key information) and the sentiment of both of the users. Agent: Thank you for calling our customer service hotline. How may I assist you today? Customer: Hi, I'm having trouble with my dishwasher. It doesn't seem to be cleaning the dishes properly. Agent: I'm sorry to hear that. Can you provide me with your name and account number, so I can look up your account and assist you better? Customer: Yes, my name is Lisa and my account number is 12345. Agent: Thank you. Can you please tell me a little more about the problem you're having with your dishwasher? Customer: Well, it seems like the dishwasher isn't spraying enough water to clean the dishes properly. Some of the dishes are coming out dirty even after I run the dishwasher. Agent: I see. That can be frustrating. Can you please check if the dishwasher is getting enough water supply? Also, please check if the spray arms inside the dishwasher are properly attached and not clogged. Customer: I've checked the water supply and it seems to be okay. But I haven't checked the spray arms yet. Agent: Alright. Please check the spray arms and see if they're clogged or damaged in any way. Also, make sure they're attached properly. Customer: Okay, let me check. Yes, it seems like one of the spray arms is loose and not attached properly. Agent: That could be the reason why the dishes aren't getting cleaned properly. Can you please try to reattach the spray arm and run the dishwasher again? Customer: Sure, let me try that. Okay, I reattached the spray arm and ran the dishwasher again. It seems to be working fine now. The dishes are coming out clean. Agent: Great to hear that! Is there anything else I can help you with? Customer: No, that's all. Thank you for your help. Agent: You're welcome. Don't hesitate to call us back if you have any more issues. Have a nice day! Customer: Bye -The customer is having trouble with their dishwasher not cleaning the dishes properly. -The customer's dishwasher wasn't spraying enough water to clean the dishes properly. -The customer's dishwasher had a loose spray arm which was causing the dishwasher not to clean the dishes properly. -The sentiment of both the customer and the agent is positive.

 

 

If you want to access the completed code find it on GitHub. Now it is your turn to try it! Let us know how it goes in the comments.

Continue to website...

More from Azure Communication Services Blog articles