---
# System prepended metadata

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

---

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

# 20221215 Line-bot + chatGPT

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


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

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

<!-- GETTING STARTED -->
## Getting Started
準備以下
1. [Create a Line bot](https://developers.line.biz/en/)
```
Channel_access_token=
Channel_secret=
```
3. [Create an OpenAI Account](https://chat.openai.com/auth/login) 
```
Email=
Password=
```
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-chatGPT-bot
```
3. Go to repository

    in ```handler.py``` (Serverless Framework 主程式)
    ```
    line_bot_api = LineBotApi(__Channel_access_token__)
    handler = WebhookHandler(__Channel_secret__)
    ```
    in ```config.json``` (放你的 OpenAI Email & Password)
    ```
    {
    "email": "",
    "password": ""
    }
    ```
    in ```requirement.txt```
    - line-bot dependency
    - tls client dependency
    
    in ```chatgpt.py```
    - python API client for chatGPT
    
    in ```error.py```
    - define some error msg
4. First time need to upload config.json to S3
    1. Go to IAM create a role for lambda to access s3
        - create a role: load-from-s3
        
        Add two official policy
        ```
        AWSXRayDaemonWriteAccess
        AWSLambdaBasicExecutionRole
        ```
        Create a custom inline permission : read-write-from-chatgpt-config 
        ```
        {
            "Version": "2012-10-17",
            "Statement": [
                {
                    "Sid": "ExampleStmt",
                    "Action": [
                        "s3:GetObject",
                        "s3:PutObject"
                    ],
                    "Effect": "Allow",
                    "Resource": [
                        "arn:aws:s3:::chatgpt-config/*"
                    ]
                }
            ]
        }
        ```
    3. Go to s3
        - create a bucket: chatgpt-config
        - upload ```config.json```
5. Use **Serverless Framework** to pack python dependency
```
pip install -r requirement
sls plugin install -n serverless-python-requirements
```
6. Use **Serverless Framework** to deploy whole project to AWS
    in ```severless.yml```
    ``` yaml    
    service: aws-line-chatGPT-bot
    provider:
      name: aws
      stage: dev
      region: us-east-1
      runtime: python3.9
      role: arn:aws:iam::XXXXXXXXXXXXXXXXX

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

    plugins:
      - serverless-python-requirements
    ```
```
serverless deploy
```
## AWS Lambda
- Go to Lambda to set timeout: 1min
## 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. [labteral/chatgpt-python](https://github.com/labteral/chatgpt-python)