본문 바로가기
Programming/Etc

[chatGPT] openAI(chatGPT) API Slack 연동

by guru_k 2023. 2. 14.
728x90
반응형

openAPI 를 사용하여 chatGPT 이용하기 with nodejs.

1. openai API를 사용하기 위한 회원가입

https://openai.com/api/

 

OpenAI API

OpenAI is an AI research and deployment company. Our mission is to ensure that artificial general intelligence benefits all of humanity.

openai.com

위에 페이지에서 openAI 회원가입

 

2. API Key 생성하기

로그인 후 View API Keys 클릭

Create new secret key를 통해 new key 생성

 

3. openai package 설치

openai를 사용하기 위한 openai package 설치하기

https://www.npmjs.com/package/openai

 

openai

Node.js library for the OpenAI API. Latest version: 3.1.0, last published: 3 months ago. Start using openai in your project by running `npm i openai`. There are 136 other projects in the npm registry using openai.

www.npmjs.com

$ $ npm install openai

4. openai API 호출 함수 작성

const { Configuration, OpenAIApi } = require('openai');

const configuration = new Configuration({
  apiKey: 'sk-*****',
});
const openai = new OpenAIApi(configuration);

export class ChatGPTService {
  async chatGPT(reqText: string): Promise<string> {
    const response = await openai.createCompletion({
      model: 'text-davinci-003',
      prompt: `${reqText}`,
      temperature: 0.9,
      max_tokens: 4000,
    });
    const reply = response.data.choices[0].text;
    return reply;
  }
}

5. Slack web api 를 통한 message post

slack api package 설치

https://www.npmjs.com/package/@slack/web-api

 

@slack/web-api

Official library for using the Slack Platform's Web API. Latest version: 6.8.1, last published: 12 days ago. Start using @slack/web-api in your project by running `npm i @slack/web-api`. There are 424 other projects in the npm registry using @slack/web-api

www.npmjs.com

$ $ npm install @slack/web-api

slack으로 message post

import { WebClient } from '@slack/web-api';

export class EventService {
  private webBotClient: WebClient;
  constructor() {
    this.webBotClient = new WebClient(
      configService.get<string>('slack.tokens.botToken'),
    );
  }
  
  private async chatGPT(reqText: string, channelId, parentTs): Promise<void> {
    const reply = await this.chatGPTService.chatGPT(reqText);
    await this.webBotClient.chat.postMessage({
      text: '```' + reply + '```',
      channel: channelId,
      thread_ts: parentTs,
    });
  }
  
}

 

끝.

728x90
반응형

댓글