0
1

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へご招待

Last updated at Posted at 2023-03-20

ラスクという可愛い猫キャラがいるのでそれをイメージして Slack にボットを入れてみました。

Fefyさんというユーチューバーがラスクのアバターつかって可愛い踊りを見せてくれています。ほんと癒やされます。MASOさんがビートセイバーの素敵な譜面を大量に作ってくれてます。この譜面ほんと可愛いな。最初に腕をブルンブルン回すのが可愛い。ぺろぺろ

3Dモデルはここにあるので買うと良いと思うます。

で、ボットです。
会話はこんな。スレッドの内容も把握してくれる感じにしてます。

2023-03-20_18h23_11.png

コードはこんなでメンションすると反応します。

import logging
import os
import openai
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError

# export OPENAI_API_KEY=""
# export SLACK_SIGNING_SECRET=""
# export SLACK_BOT_TOKEN="xoxb-"
# export SLACK_APP_TOKEN="xapp-"

openai.api_key = os.environ["OPENAI_API_KEY"]
logging.basicConfig(level=logging.DEBUG)

app = App(token=os.environ["SLACK_BOT_TOKEN"])
slack_client = WebClient(token=os.environ["SLACK_BOT_TOKEN"])

@app.event("app_mention")
def event_test(body, say, logger):
    try:
        #logger.info(body)
        mentioned_ts = body["event"]["ts"]
        channel = body["event"]["channel"]
        thread_ts = body["event"].get("thread_ts", None)
        messages = []

        if thread_ts:
            result = slack_client.conversations_replies(
                channel=channel,
                ts=thread_ts,
            )
            messages = result['messages']

        chat_logs = ""
        for message in messages:
            chat = "USER:{} TEXT:{}\n".format(message['user'], message['text']) 
            chat_logs += chat

        text = body["event"]["text"]
        #logger.info("say {}".format(say))
        prompt = """{}
==== ここからAIの性格 ====
あなたはとてもかわいい猫AIエージェントです。
名前はラスクです。
語尾は2割くらいで「にゃん」になります。
次の会話に答えてあげてください。
USER: TEXT:などは使わなくてよいです。
==== ここから質問 ====
{}
ラスクの答え:
""".format(chat_logs, text)
        prompt = prompt[-2000:]
        prompt = """==== ここからこれまでのチャットログ ====\n""" + prompt
        logger.info(prompt)
        completion = openai.ChatCompletion.create(model="gpt-3.5-turbo",  messages=[{"role": "user", "content": prompt}], temperature=0.7)
        answer = str(completion.choices[0].message.content).replace("\n", "")
        #logger.info(answer)
        say(answer, thread_ts=mentioned_ts)
    except SlackApiError as e:
        logger.info(f"Error: {e}")


if __name__ == "__main__":
    handler = SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"])
    handler.start()

Slack アプリを作るときは Socket Mode を有効にしてください。

2023-03-20_11h18_45.png

環境変数で各キーを設定します。

# export OPENAI_API_KEY=""
# export SLACK_SIGNING_SECRET=""
# export SLACK_BOT_TOKEN="xoxb-"
# export SLACK_APP_TOKEN="xapp-"

質問のしかたはこんな。

        prompt = """==== ここからこれまでのチャットログ ====
{}
==== ここからAIの性格 ====
あなたはとてもかわいい猫AIエージェントです。名前はラスクといいます。語尾は2割くらいで「にゃん」になります。次の質問に答えてあげてください。USER: TEXT:などは使わなくてよいです。
==== ここから質問 ====
{}
ラスクの答え:
""".format(chat_logs, text)

めっちゃ楽しいので会社のSlackに入れよう!

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?