overview

Here I bring you TATTOO, Terraform, AWS, Typescript, Telegram bot Of OpenAI ChatGPT.

In today’s world, we see more and more companies using chatbots to handle common customer inquiries. With advancements in Natural Language Processing (NLP), we can train chatbots to understand and learn from past interactions with users. This is where OpenAI’s GPT Model (Generative Pre-trained Transformer Model) comes into play. With it, we can generate automatic responses that are tailored to the context of the conversation.

In this tutorial, we will learn how to leverage Terraform to deploy a chatbot that is powered by OpenAI’s GPT Model on AWS. Specifically, we will focus on the creation of a Telegram bot using the Telegram Bot API, and its integration with OpenAI’s GPT API.

Creating a Telegram Bot

Telegram is a popular messaging application that provides an open API for developers to easily create bots. In order to create a Telegram bot:

  1. First, you will need to create a Telegram account or sign in to your existing one.
  2. Next, search for the “BotFather” and start a chat with it. botfather
  3. Once the chat is opened, type “/newbot” to create a new bot.
  4. BotFather will then ask you to choose a name and username for your bot, once chosen, it will provide you with an API token. Keep this token secure, as it is the unique identifier for your Telegram bot. newbot

Retrieving the OpenAI API keys

Once we have the Telegram Bot token, we can move on to retrieving the OpenAI API keys:

  1. First, sign up or sign in to your OpenAI account.
  2. Navigate to your dashboard and create a new API key.
  3. Once created, copy the key and keep it secure.

Deploying the Application to AWS using Terraform

Prerequisites

The tools you will need to complete the build:

  • AWS Console
  • AWS Command Line Interface
  • Terraform by HashiCorp
  • Node.js and NPM
  • tfenv (optional)

For AWS and Terraform setup, please read my other articles, this one or this one , and follow the build instructions. To install Node.js and npm, follow this instruction .

Instructions

  1. Clone the repository:
$ git clone git@github.com:blokaly/tattoo.git
  1. Navigate to the tattoo directory:
$ cd tattoo
  1. Edit the file called values.tfvars with the following contents:
1
2
3
4
telegram_token     = "<telegram_bot_token>"
openai_token       = "<openai_api_key>"
openai_max_token   = 1024  
openai_temperature = 0.6

Make sure to replace <telegram_bot_token> and <openai_api_key> with your own API tokens. And also can define the ChatGPT completions max toke and temperature values here.

  1. Initialize Terraform in the current directory:
$ terraform init
  1. Deploy the infrastructure to AWS:
$ terraform apply -var-file="values.tfvars" 
  1. Terraform will now create a new API Gateway, 2 Lambda functions, IAM policies and roles, and save the secret tokens/keys in the Parameter Store.

That’s it! Your chatbot is now deployed and ready to use. You can communicate with your bot through Telegram by simply searching for the username that you created earlier. tattoo-poem.png

Overview

  1. Here is the tattoo project structure 📂
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
.
├── babel.config.js
├── LICENSE
├── main.tf
├── package.json
├── README.md
├── src
│   ├── app.ts
│   ├── constants.ts
│   ├── logging.ts
│   ├── openai.ts
│   ├── prompts.json
│   ├── prompt.ts
│   ├── ssm.ts
│   └── telegram.ts
├── tsconfig.json
└── values.tfvars

main.tf and values.tfvars are for Terraform, other files are a typical typescript project setup.

  1. There is a JSON file, prompts.json, under the src folder. It contains a set of pre-defined ChatGPT prompts based on different languages, file format like below
1
2
3
4
5
{  
  "<language>": {  
    "<command>": "<prompt>"  
  }  
}

and such as this:

1
2
3
4
5
{  
  "en": {  
    "poet": "I want you to act as a poet. You will create poems that evoke emotions and have the power to stir people’s soul. Write on any topic or theme but make sure your words convey the feeling you are trying to express in beautiful yet meaningful ways. You can also come up with short verses that are still powerful enough to leave an imprint in readers' minds."  
  }  
}

You can search for well written prompts and setup the ones you like. Here are two sources: awesome-chatgpt-prompts and best-chatgpt-prompts . After you added and deployed your own prompt commands, then the command can be used as /<command> in telegram bot.

  1. This project integrated the Telegram bot using Telegraf.js library. Telegraf is a library that makes it simple for you to develop your own Telegram bots using JavaScript or TypeScript. See its documentation for details.
  2. In order to make sure that the Telegram webhook was set by you and also prevent the AWS API from accessing by other unauthorised application, we can pass a secret token via the setWebhook call. Then the Telegram request will contain a header “X-Telegram-Bot-Api-Secret-Token” with the secret token as content, which subsequently can be verified by the the Lambda authoriser.
    📃 app.ts:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
export const authorise = async (  
    event: SetWebhookEvent | APIGatewayProxyEvent  
): Promise<AuthoriseResponse | void> => {  
  
    if ('setWebhook' in event && (<SetWebhookEvent>event).setWebhook) {  
        logger.info('setting bot webhook')  
        const {domain, path_key} = process.env  
        let params: any = {url: `${domain}/${path_key}/`}  
        const secretToken = await lookupSsm(process.env.secret_token)  
        if (secretToken) {  
            params = {...params, secret_token: secretToken}  
        }        await tgSetWebhook(params)  
    } else {  
        const secretToken = await lookupSsm(process.env.secret_token)  
        if ((<APIGatewayProxyEvent>event).headers[TELEGRAM_HEADER_SECRET_TOKEN] === secretToken) {  
            return {isAuthorized: true}  
        } else {  
            logger.info(`secret token does not match, reject event: ${JSON.stringify(event)}`)  
            return {isAuthorized: false}  
        }    }}

Line 5 to line 12, if a setWebhook event triggered, then we lookup the secret token from Parameter Store and set it for the Telegram webhook.
Line 14 to line 20, as the authoriser Lambda, we check if the request header contains the secret token, only accept if it does.
5. We configure the 2 Lambda functions in the mai.tf 📃

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
resource "aws_lambda_function" "auth_lambda" {  
  function_name = "tattoo-authoriser"  
  
  filename         = data.archive_file.lambda_zip.output_path  
  source_code_hash = data.archive_file.lambda_zip.output_base64sha256  
  environment {  
    variables = {  
      domain       = aws_apigatewayv2_api.api.api_endpoint  
      path_key     = random_id.random_path.hex  
      secret_token = aws_ssm_parameter.secret-token.name  
      bot_token    = aws_ssm_parameter.bot-token.name  
    }  
  }  
  timeout = 30  
  handler = "app.authorise"  
  runtime = "nodejs14.x"  
  role    = aws_iam_role.lambda_exec.arn  
}  
  
resource "aws_lambda_function" "app_lambda" {  
  function_name = "tattoo-app"  
  
  filename         = data.archive_file.lambda_zip.output_path  
  source_code_hash = data.archive_file.lambda_zip.output_base64sha256  
  environment {  
    variables = {  
      bot_token          = aws_ssm_parameter.bot-token.name  
      openai_token       = aws_ssm_parameter.openai-token.name  
      openai_max_token   = var.openai_max_token  
      openai_temperature = var.openai_temperature  
      log_level          = "info"  
    }  
  }  
  timeout = 30  
  handler = "app.handler"  
  runtime = "nodejs14.x"  
  role    = aws_iam_role.lambda_exec.arn  
}

These 2 Lambda functions are using the same compiled and zipped file. In line 10, we pass the secret token name as the environment variable to the Lambda, and the Lambda authorise function will use the name to lookup the Parameter Store, retrieving the encrypted secret token.

In conclusion, I hope this tutorial has given you a brief introduction to Terraform, AWS, Telegram bot creation, and OpenAI’s GPT Model. I encourage you to take this further and explore the many possibilities that these technologies provide. Happy coding!

References