---
# System prepended metadata

title: 20221215 Line-bot + GPT-3
tags: [AWS Learning Group]

---

###### tags: `AWS Learning Group`

# 20221215 Line-bot + GPT-3 

前言
> 12/1 OpenAI 釋出ChatGPT，短短一週就破百萬用戶，使用者可以在OpenAI提供的Web聊天室跟chatGPT聊天，可想而知，這樣的模式不太方便，所以本次Lab要延續LineBot，教大家將chatGPT串在一起，讓使用者可以透過Line跟chatGPT聊天。

> 12/7 開始各路的Bot的開發者，開始逆向工程ChatGPT的網頁版的聊天功能，讓他能串接在Line Discord。 

> 12/14 OpenAI 在ChatGPT伺服器前面掛了Cloudflare服務，造成第三方的方式去接觸ChatGPT後端會被擋下來。

本次目標
~~* LINE bot + OpenAI **chatGPT**~~
* LINE bot + OpenAI **GPT3**
* **Serverless Framework** = Cli for API Gateway + Lambda


<!-- ABOUT THE PROJECT -->
## System Overview
![](https://i.imgur.com/VV5dGFb.png)


<!-- GETTING STARTED -->
## Getting Started
準備以下
1. [Create a Line bot](https://developers.line.biz/en/) and save below information.
```
Channel_access_token=
Channel_secret=
```
3. [Create an OpenAI Account](https://openai.com/api/) and create a API Token.
```
openAI_API_token=
```
2. 點擊AWS右上角User / Security credentials / Access keys
```
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
```

## Development
1. Install **Serverless Framework**
```
export AWS_ACCESS_KEY_ID=**********
export AWS_SECRET_ACCESS_KEY=***********
npm install serverless -g
```
2. Use **Serverless Framework** to create a project based on AWS
```
serverless create --template aws-python --path aws-line-GPT3-bot
```
3. Go to repository

    in ```handler.py``` (Serverless Framework 主程式)
    ```
    import os
    import json
    import openai

    from linebot import (
        LineBotApi, WebhookHandler
    )
    from linebot.models import (
        MessageEvent, TextMessage, TextSendMessage,
    )
    '''
    Setting LINE
    '''
    line_bot_api = LineBotApi(os.environ['Channel_access_token'])
    handler = WebhookHandler(os.environ['Channel_secret'])

    '''
    Setting OpenAI
    '''
    openai.api_key = os.environ['openAI_API_token']

    def webhook(event, context):
        # Parse msg from LINE conversation request
        print('event: ', event)
        msg = json.loads(event['body'])
        print('msg: ', msg)

        # Parse texts we type from msg 
        gpt3_input = msg['events'][0]['message']['text']
        print('gpt3_input:', gpt3_input)

        # GPT3
        response = openai.Completion.create(
            engine="text-davinci-002",
            prompt=gpt3_input,
            temperature=0.7,
            max_tokens=709,
            top_p=1,
            frequency_penalty=0,
            presence_penalty=0
        )
        gpt3_response = response.choices[0].text
        print('gpt3_response: ', gpt3_response)

        # handle webhook body
        try:
            line_bot_api.reply_message(
            msg['events'][0]['replyToken'],
            TextSendMessage(text=gpt3_response)
            )
        except:
            return {
                'statusCode': 502,
                'body': json.dumps("Invalid signature. Please check your channel access token/channel secret.")
            }
        return {
            "statusCode": 200,
            "body": json.dumps({"message": 'ok'})
        }
    ```
    in ```requirements.txt```
    ```
    line-bot-sdk==1.12.1
    openai
    ```
    create ```setup.cfg```
    ```
    [install]
    prefix=
    ```
4. Install dependency locally
    ```
    pip install -r requirements
    ```   
6. Use **Serverless Framework** to pack python dependency
    ```
    sls plugin install -n serverless-python-requirements
    ```
6. Use **Serverless Framework** to deploy whole project to AWS
    in ```serverless.yml```
    ``` yaml    
    service: aws-line-GPT3-bot
    provider:
      name: aws
      stage: dev
      region: us-east-1
      runtime: python3.9

    functions:
      line_bot:
        handler: handler.webhook
        events:
          - http:
              path: /webhook
              method: POST

    plugins:
      - serverless-python-requirements
    ```
7. Final step: deploy to aws
```
sls deploy
```
## AWS Lambda
- Configuration -> General configuration -> timeout -> 1min
- Configuration -> environment variables
    - Channel_access_token
    - Channel_secret
    - openAI_API_token
## Debug technique
CloudWatch
- Utilized ```print()``` in your code and you'll see on CloudWatch.
## Reference
1. [上週 LineAPI + aws](https://hackmd.io/@hjQzzIhhQLq_F0m9RyygQA/BJElmrewj)
2. [Serverless Framework](https://www.serverless.com/)
3. [line+chatGPT](https://hackmd.io/@_ZfaCsoORM2ms6z9wV-aNw/rJkX5OO_j)