LoginSignup
20
18

More than 1 year has passed since last update.

ChatGPTとSlack上で会話するアプリを10分で構築しよう(後編:AWS Lambda)

Last updated at Posted at 2023-04-28

使用するOSS

  • 言語:Python

ゴール

AWS LambdaでChatGPTと連続的な会話を実現するSlackアプリを構築する

AWS Lambdaで動かす

本記事は、以下の記事の後編です

ローカル環境で動作確認できたら、次はこれをLambdaにデプロイしてみましょう
(そこまでできれば、社内用ツールとして利用できるようになります)

Slack API

Slack APIで新しいAppを作成

最初に、SlackAppを作成します

image.png

  • From an app manifestを選択します
    image.png

  • Enter app manifest bellowを入力します

    • manifest-prod.ymlの内容をコピペします
    • request_url等はこの時点ではまだ確定していませんので、後で変更します
      image.png

Slack APIで必要な認証情報を控えます

  • Basic Information > App Credentialsから以下を控えます
    • Client ID
    • Client Secret
    • Signing Secret

FireShot Capture 015 - Slack API_ Applications - Sample Slack - api.slack.com.png

OpenAI API

image.png

AWS S3Bucketの準備

  • 以下の3つが必要になります
    • For SLACK_INSTALLATION
    • For SLACK_STATE
    • For OPENAI

ServerlessFrameworkの実行

  • 以下のコマンドを実行してください
export SLACK_CLIENT_ID=${YOUR SLACK_CLIENT_ID}
export SLACK_CLIENT_SECRET=${YOUR SLACK_CLIENT_SECRET}
export SLACK_SIGNING_SECRET=${YOUR SLACK_SIGNING_SECRET}
export SLACK_SCOPES=app_mentions:read,channels:history,groups:history,im:history,mpim:history,chat:write.public,chat:write,users:read
export SLACK_INSTALLATION_S3_BUCKET_NAME=${YOUR SLACK_INSTALLATION_S3_BUCKET_NAME}
export SLACK_STATE_S3_BUCKET_NAME=${YOUR SLACK_STATE_S3_BUCKET_NAME}
export OPENAI_S3_BUCKET_NAME=${YOUR OPENAI_S3_BUCKET_NAME}
npm install -g serverless
serverless plugin install -n serverless-python-requirements
serverless deploy
  • 質問にはそれぞれ以下のように答えてください
? Do you want to login/register to Serverless Dashboard? (Y/n)
# -> Y

? No AWS credentials found, what credentials do you want to use? (Use arrow keys)
# -> AWS Access Role (most secure)
  • CloudFormationのテンプレートを用意してくれるので、指示通り作成を進めます

Untitled.png

Untitled (1).png

Untitled (2).png

Slack APPにURLを登録

  • デプロイが完了すると、以下のような結果が出力されます
✔ Service deployed to stack slack-chat-gpt-bot-dev (48s)

dashboard: https://app.serverless.com/xxxxxxx/apps/slack-chat-gpt-bot/slack-chat-gpt-bot/dev/us-east-1                                                        
endpoints:
  POST - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/slack/events                                                                                         
  GET - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/slack/install
  GET - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/slack/oauth_redirect
functions:
  app: slack-chat-gpt-bot-dev-app (2.6 MB) 

Request URL

image.png

FireShot Capture 018 - Slack API_ Applications - Sample Slack - api.slack.com.png

Redirect URLs

Slack APP Installation & Configure

Installation

Configure

  • SlackのアプリのホームからConfigureを押します
  • OpenAI API keyを入力し、OpenAI Modelを選択してください
    image.png

Slackで会話してみましょう

  • 普通にWebで会話するように会話してみましょう
    image.png
20
18
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
18