webexbot-openai

published

Webex bot working with openai's API

Overview

In this lab, we will create an interactive webex chatbot using openai's API for responses.

Chatbot logic resides in Lambda, an aws serverless service. We use aws Cloud9 as our development environment to upload Chatbot logic to Lambda more easily.
If there is input from the user, Chatbot logic accesses openai's API with that input as a prompt. Then send that response back to the user.

Requirements

  • python3.9
  • openai
  • webexteamssdk
  • Cisco webex account
  • OpenAI account
  • AWS account

To use OpenAI's API, we need to create an account and get an API key. This lab uses the $18.00 free trial allotted for the first time.

Each AWS resource is billed.

What you do is

  1. Create a Webex bot
  2. Create AWS Lambda function
  3. Create AWS API Gateway
  4. Create Cisco Webex webhook
  5. Create an AWS Cloud9 instance
  6. Upload Deployment package to Lambda

1. Create a Webex bot

Create a bot on Webex for Developers. You can go to the creation page from Start Building Apps.

Select Bots.

Enter Bot name , Bot username , select Icon and click Add Bot.

Enter a Description and click Add Bot.

Once the bot is created, copy the Bot access token to the editor.

2. Create AWS Lambda function

Create from Create function.
Select Author from scratch , enter WEBEXBOT_OPENAI for Function name, and select Python 3.9 for Runtime .

Leave the default settings and click Create function.

Configuration tab => General configuration => Edit

Increase the Timeout to 1 minute and click Save.

Configuration tab => Environment variables

Set the OpenAI API key and the Bot access token obtained when creating the bot in the environment variables. These values are referenced from the bot logic through Environment variables.

OPENAI_SECRET_KEY: { OpenAI API key }

WEBEX_TEAMS_ACCESS_TOKEN: { Bot access token }

3. Create AWS API Gateway

Click Build of REST API from API Gateway creation page.

Select REST for Choose the protocol, select New API for Create new API, enter any name for API name, select Regional for Endpoint Type, and click Create API.

From Actions, Create Resource.

Enter messages in Resource Name and click Create Resource.

From Actions, Create Method.

Select POST.

Set Integration Type to Lambda Function, enable Use Lambda Proxy Integration, select your region for Lambda Region, and set the name of the created Lambda function to Lambda Function.

From Actions, Deploy API.

Select a New Stage and enter an arbitrary stage name for Stage name. Click Deploy.

Copy the Invoke URL of the POST method to the editor.

4. Create Cisco Webex webhook

Log in to Webex for Developers and from the left pane of the Documentation page do the following:

APIs => Webex APIs => Messaging => Reference => Webhooks => POST Create a Webhook

Enter the parameters in the right pane as shown below.

Authorization:
Disable Use personal access token and paste the bot access token you copied when creating the bot.
name:
Enter any webhook name.
targetUrl:
Paste the Invoke URL of the POST method that you copied when creating API Gateway.
resources:
Enter messages
events:
Enter created

The following format is printed with 200/OK.

"*" will store your value.

{
  "id": "**********************************************************************************************",
  "name": "WEBEXBOT WITH OPENAI",
  "targetUrl": "https://**********.execute-api.ap-northeast-1.amazonaws.com/prod/messages",
  "resource": "messages",
  "event": "created",
  "orgId": "***************************************************************************************",
  "createdBy": "*******************************************************************************",
  "appId": "******************************************************************************************************************",
  "ownedBy": "creator",
  "status": "active",
  "created": "2023-02-14T02:58:58.914Z"
}

5. Create an AWS Cloud9 instance

Enter Name from the Cloud9 creation screen of the management console, and select New EC2 instance in Environment type.

Select t2.micro for Instance type and Ubuntu Server 18.04 LTS for Platform.

Click Create. Since it can be created, start the IDE from Open in Cloud9.

6. Upload Deployment package to Lambda

Upload the deployment package containing Bot logic and related libraries to Lambda in the Cloud9 terminal as follows.

Define default region and Lambda function name as environment variable

export DEFAULT_REGION={ Your Region }
export LAMBDA_FUNCTION_NAME={ Lambda Function name }

Clone the repository

git clone https://github.com/masanobu48154/webexbot-openai.git

Go into the webexbot-openai directory

cd webexbot-openai/

Run upload_package.sh.

bash upload_package.sh 
cloud9:~/environment $ export DEFAULT_REGION=ap-northeast-1
cloud9:~/environment $ git clone https://github.com/masanobu48154/webexbot-openai.git
Cloning into 'webexbot-openai'...
remote: Enumerating objects: 71, done.
remote: Counting objects: 100% (71/71), done.
remote: Compressing objects: 100% (59/59), done.
remote: Total 71 (delta 23), reused 57 (delta 12), pack-reused 0
Unpacking objects: 100% (71/71), done.
cloud9:~/environment $ cd webexbot-openai/
cloud9:~/environment/webexbot-openai (main) $ bash upload_package.sh 
Sending build context to Docker daemon  16.61MB
Step 1/2 : FROM python:3.9
3.9: Pulling from library/python
f2f58072e9ed: Pull complete 
5c8cfbf51e6e: Pull complete 
aa3a609d1579: Pull complete 
094e7d9bb04e: Pull complete 
2cbfd734f382: Pull complete 
aa86ac293d0f: Pull complete 
ea442e3d4174: Pull complete 
c662908b49d7: Pull complete 
f7e80cce9b62: Pull complete 
Digest: sha256:929da7c4e1285e844e70e901267059f63ea958778695a867111a77eaf09700ff
Status: Downloaded newer image for python:3.9
 ---> 7d357ce6a803
Step 2/2 : RUN mkdir /temp   && cd /temp   && mkdir package   && pip3 install --target ./package openai   && pip3 install --target ./package webexteamssdk
 ---> Running in df0d0e14bced
Collecting openai
  Downloading openai-0.25.0.tar.gz (44 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 44.9/44.9 KB 7.2 MB/s eta 0:00:00
  Installing build dependencies: started


(snip)


{
    "FunctionName": "WEBEXBOT_OPENAI",
    "FunctionArn": "arn:aws:lambda:ap-northeast-1:012345678901:function:WEBEXBOT_OPENAI",
    "Runtime": "python3.9",
    "Role": "arn:aws:iam::012345678901:role/service-role/WEBEXBOT_OPENAI-role-wseuyy5t",
    "Handler": "lambda_function.lambda_handler",
    "CodeSize": 43335723,
    "Description": "",
    "Timeout": 3,
    "MemorySize": 128,
    "LastModified": "2022-12-21T05:30:32.000+0000",
    "CodeSha256": "E4r+hTtnD8/qcXeqY57yMqhzr29nbeXzwkQFzqTPqk8=",
    "Version": "$LATEST",
    "TracingConfig": {
        "Mode": "PassThrough"
    },
    "RevisionId": "1f795f02-0db7-4fb2-8d34-012345678901",
    "State": "Active",
    "LastUpdateStatus": "InProgress",
    "LastUpdateStatusReason": "The function is being created.",
    "LastUpdateStatusReasonCode": "Creating",
    "PackageType": "Zip",
    "Architectures": [
        "x86_64"
    ],
    "EphemeralStorage": {
        "Size": 512
    }
}

Let's try it on webex.

View code on GitHub
  • Owner

  • Contributors

    +1Github contributor
  • Categories

  • Products

    Webex
  • Programming Languages

    Python
  • License

    BSD 3-Clause "New" or "Revised" License

Code Exchange Community

Get help, share code, and collaborate with other developers in the Code Exchange community.View Community
Disclaimer:
Please note that some of the repositories in Code Exchange may be enabled to interact with third-party Generative AI platforms outside of Cisco’s control, and users should review those third-party terms and privacy statements to understand how data is processed, stored or used, including input data.