Build a conversational SMS bot with Azure Communication Services and Azure OpenAI
Published Feb 13 2023 10:15 AM 6,911 Views
Microsoft

It is no secret that large language models (LLM) like ChatGPT have been all the rage in the last couple of months. These conversational models offer seamless and intuitive interfaces for users to interact with, enabling them to easily ask questions or complete tasks.  The Azure Communication Services team provides developers with the tools to integrate these conversational entities with communication channels.

In today's blog, we are building a conversational bot powered by Azure OpenAI and Azure Communication Services SMS. The bot will be called Anton and will act as a personal trainer at Contoso Gym. See the preview:

 

anton_demo.gif

 

To follow along you will need: 

 

You can find the finished code for this blog on GitHub.

 

This application will leverage Azure Event Grid to listen for incoming text messages to Azure Communication Services number and an Azure Function to process the event and respond with an Azure OpenAI generated response. 

 

To get started you must setup an Azure Function in your environment. For the purpose of this blog, you will need a JavaScript Azure Function configured to use Azure Event Grid as a trigger. You can learn how to do that in this QuickStart.

 

 

 

import { AzureFunction, Context } from "@azure/functions"

const eventGridTrigger: AzureFunction = async function (context: Context, eventGridEvent: any): Promise<void> {
    context.log(eventGridEvent);
    const to = eventGridEvent['data']['to'];
    const from = eventGridEvent['data']['from'];
    const message = eventGridEvent['data']['message'];

    // Insert Azure Open AI and SMS code here
};

export default eventGridTrigger;

 

 

 

Next, we must add a call to Azure OpenAI to ask the model to generate a response. We will use REST APIs to POST a request with our prompt. For the prompt, we will use a combination of the message sent by the user and a pre-designed text. In this example, we want the GPT-3 model to act like Anton, a personal trainer at Contoso Gym. We added some sample quotes for the model to draw inspiration from. These quotes help guide the model’s response and provide a more intuitive and conversational flow for the users. See the completed prompt below:

 

 

 

You're Anton Stanescu and you are a personal trainer at Contoso Gym. You are having a conversation with one of your students. You are trying to motivate them to work out.
Examples of the types of things that Anton says:
- The only bad workout is the one that didn't happen.
- Strive for progress, not perfection.
- You won't get the muscle without the dedication and the drive
- Train hard...so you can stop a train if you need to.
- Push up, push down, push up, push down
- Exercise should be regarded as tribute to the heart.
Anton: Welcome to the Contoso Gym! Big things await for you.
Student: I am ready to start working out!

 

 

 

We concatenate the prompts with the user’s message. We then send the full prompt to the Azure OpenAI service to generate a response. To communicate with the Azure OpenAI service, we use POST request. We get in return generated text by the Azure OpenAI model.

 

 

 

    // Azure OpenAI logic
    var url = '<Azure OpenAI Endpoint>/openai/deployments/<Deployment Name>/completions?api-version=2022-12-01'
    var instructions = 'You\'re Anton Stanescu and you are a personal trainer at Contoso Gym. You are having a conversation with one of your students. You are trying to motivate them to work out.\n' +
    'Examples of the types of things that Anton says:\n' +
    '- The only bad workout is the one that didn\'t happen.\n' +
    '- Strive for progress, not perfection.\n' +
    '- You won\'t get the muscle without the dedication and the drive\n' +
    '- Train hard...so you can stop a train if you need to.\n' +
    '- Push up, push down, push up, push down\n' +
    '- Exercise should be regarded as tribute to the heart.\n' +
    'Anton: Welcome to the Contoso Gym! Big things await for you.\n' +
    'Student: I am ready to start working out!\n'
    var prompt = instructions + message + '\n Anton:'

    fetch(url, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'api-key':'<Azure OpenAI key>'
        },
        body: JSON.stringify({ 
            prompt,
            stop: [
                'Student:',
                'Anton:'
            ],
            max_tokens: 100,
            temperature: 1
         })
    })
    .then(res => res.json())
    .then(async data => { console.log(data['choices'][0]['text'])})

 

 

 

Finally, you will configure the SMS Client to respond with the new response generated by Azure OpenAI. You will need Azure Communication Services connection string to initialize the SMS Client.  You can either paste the connection string directly in the code or place it inside your local.settings.json file in your Azure Function directory under values.  

 

 

 

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "node",
    "ACS_CONNECTION_STRING": "<<CONNECTION STRING>>"
  }
}

 

 

 

Then you will modify the function itself to add the SMS client logic.

 

 

 

import { AzureFunction, Context } from "@azure/functions"
import { SmsClient } from "@azure/communication-sms";

const connectionString = process.env.ACS_CONNECTION_STRING; //Replace with your connection string

const eventGridTrigger: AzureFunction = async function (context: Context, eventGridEvent: any): Promise<void> {
    context.log(eventGridEvent);
    const to = eventGridEvent['data']['to'];
    const from = eventGridEvent['data']['from'];
    const message = eventGridEvent['data']['message'];

    // Azure OpenAI logic
    var url = '<Azure OpenAI Endpoint>/openai/deployments/<Deployment Name>/completions?api-version=2022-12-01'
    var instructions = 'You\'re Anton Stanescu and you are a personal trainer at Contoso Gym. You are having a conversation with one of your students. You are trying to motivate them to work out.\n' +
    'Examples of the types of things that Anton says:\n' +
    '- The only bad workout is the one that didn\'t happen.\n' +
    '- Strive for progress, not perfection.\n' +
    '- You won\'t get the muscle without the dedication and the drive\n' +
    '- Train hard...so you can stop a train if you need to.\n' +
    '- Push up, push down, push up, push down\n' +
    '- Exercise should be regarded as tribute to the heart.\n' +
    'Anton: Welcome to the Contoso Gym! Big things await for you.\n' +
    'Student: I am ready to start working out!\n'
    var prompt = instructions + message + '\n Anton:'

    fetch(url, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'api-key':'<Azure OpenAI key>'
        },
        body: JSON.stringify({ 
            prompt,
            stop: [
                'Student:',
                'Anton:'
            ],
            max_tokens: 100,
            temperature: 1
         })
    })
    .then(res => res.json())
    .then(async data => {
        // SMS logic
        var generatedText = data['choices'][0]['text']
        const smsClient = new SmsClient(connectionString);
        const sendResults = await smsClient.send({
            from: to,
            to: [from],
            message: generatedText
        });
    })

};

export default eventGridTrigger;

 

 

 

To run the function locally, simply press F5 in Visual Studio Code. You will use ngrok  to hook the locally running Azure Function with Azure Event Grid. You will need to download ngrok  for your environment. Once the function is running, you will configure ngrok:

 

 

 

ngrok http 7071 

 

 

 

Copy the ngrok link provided where your function is running. 

 

Finally, you need to configure SMS events through Event Grid in your Azure Communication Services resource. You will do this using the Azure CLI . You will need the Azure Communication Services resource ID found in the Azure Portal.  (The resource ID will look something like:  /subscriptions/<<AZURE SUBSCRIPTION ID>>/resourceGroups/<<RESOURCE GROUP NAME>>/providers/Microsoft.Communication/CommunicationServices/<<RESOURCE NAME>>)

 

 

 

az eventgrid event-subscription create --name "<<EVENT_SUBSCRIPTION_NAME>>" --endpoint-type webhook --endpoint "<<NGROK URL/runtime/webhooks/EventGrid?functionName=FUNCTION NAME>> " --source-resource-id "<<RESOURCE_ID>>"  --included-event-types Microsoft.Communication.SMSReceived

 

 

 

Now that everything is connected, test the flow by sending an SMS from your phone to the phone number you acquired through the Azure Communication Services resource.

Congratulations! You have created a conversational SMS bot. Azure Communication Services enables you to connect with your customers across different channels like SMS, Chat, Email and Calling. Whether it is having Anton, the personal trainer bot talk to your customers or having a real person interact with them, Azure Communication Services can help. To learn more about other resources that Azure Communication Services offers see the links below:

 

3 Comments
Co-Authors
Version history
Last update:
‎Feb 16 2023 01:45 PM
Updated by: