13
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ChatGPTとSlack上で会話するアプリを10分で構築しよう(前編:ローカル環境)

Last updated at Posted at 2023-04-16

使用するOSS

  • 言語:Python

ゴール

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

ローカルで動かす

まずはREADMEに従ってローカルで動かしてみましょう

Slack API

Slack APIで新しいAppを作成

image.png

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

  • Enter app manifest bellowを入力します

    • manifest-dev.ymlの内容をコピペします
      image.png

Appで必要な設定を行います

  • Install to Workspaceします
    image.png

  • App-Level Tokenをconnections:write scopeで作成します

    • Tokenを控えます
      image.png

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

  • Install AppからBot User OAuth Tokenを控えます
    FireShot Capture 014 - Slack API_ Applications - Sample Slack - api.slack.com.png

OpenAI API

image.png

Appを立ち上げます

  • READMEに記載の通り、実行します
    • 環境変数は前段階で控えたものに置き換えてください
    • OPENAI_MODELはgpt-3.5-turboとしてください
# Create an app-level token with connections:write scope
export SLACK_APP_TOKEN=xapp-1-...
# Install the app into your workspace to grab this token
export SLACK_BOT_TOKEN=xoxb-...
# Visit https://platform.openai.com/account/api-keys for this token
export OPENAI_API_KEY=sk-...

# Optional: gpt-3.5-turbo and gpt-4 are currently supported (default: gpt-3.5-turbo)
export OPENAI_MODEL=gpt-3.5-turbo
# Optional: You can adjust the timeout seconds for OpenAI calls (default: 30)
export OPENAI_TIMEOUT_SECONDS=60
# Optional: You can include priming instructions for ChatGPT to fine tune the bot purpose
export OPENAI_SYSTEM_TEXT="You proofread text. When you receive a message, you will check
for mistakes and make suggestion to improve the language of the given text"
# Optional: When the string is "true", this app translates ChatGPT prompts into a user's preferred language (default: true)
export USE_SLACK_LANGUAGE=true
# Optional: Adjust the app's logging level (default: DEBUG)
export SLACK_APP_LOG_LEVEL=INFO
# Optional: When the string is "true", translate between OpenAI markdown and Slack mrkdwn format (default: false)
export TRANSLATE_MARKDOWN=true

python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python main.py

Slackで会話してみましょう

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

後編

次回はこのアプリをAWS Lambdaに構築します

13
16
0

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
13
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?