chatGPT를 슬랙에서 사용하기

HGW XX/7·2023년 3월 1일
0
post-thumbnail

오늘 날짜로 chatGPT의 모델인 gpt-3.5-turbo를 API를 이용해서 사용할 수 있게 되었다.

기존에 회사에서 사용하던 코드에서 모델이랑 몇가지 코드만 변경하면 바로 사용이 가능하다.

SLACK_APP_TOKEN = "xapp-로 시작"
SLACK_BOT_TOKEN = "xoxb-로 시작
OPENAI_API_KEY = "sk-로 시작"

import os
import openai
from slack_bolt.adapter.socket_mode import SocketModeHandler
from slack import WebClient
from slack_bolt import App

# Event API & Web API
app = App(token=SLACK_BOT_TOKEN)
client = WebClient(SLACK_BOT_TOKEN)

# This gets activated when the bot is tagged in a channel
@app.event("app_mention")
def handle_message_events(body, logger):
    # Log message
    print(str(body["event"]["text"]).split(">")[1])

    # Create prompt for ChatGPT
    prompt = str(body["event"]["text"]).split(">")[1]

    # Let thre user know that we are busy with the request
    response = client.chat_postMessage(channel=body["event"]["channel"],
                                       thread_ts=body["event"]["event_ts"],
                                       text=f"Hello from chatGPT!")

    # Check ChatGPT
    openai.api_key = OPENAI_API_KEY
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
	messages=[
		{"role":"user","content":prompt}
	]
    ).choices[0].message['content']

    # Reply to thread
    response = client.chat_postMessage(channel=body["event"]["channel"],
                                       thread_ts=body["event"]["event_ts"],
                                       text=f"Here you go: {response}")

if __name__ == "__main__":
    SocketModeHandler(app, SLACK_APP_TOKEN).start()

0개의 댓글